From a48a574f7e890436363ca8ac71d866252b216052 Mon Sep 17 00:00:00 2001 From: Hao Nguyen <haon@google.com> Date: Fri, 5 Apr 2019 16:39:18 -0700 Subject: [PATCH] Down integrate to Github --- cmake/libprotobuf-lite.cmake | 2 +- cmake/tests.cmake | 2 +- conformance/text_format_conformance_suite.cc | 27 +- conformance/text_format_failure_list_java.txt | 2 + .../text_format_failure_list_python.txt | 2 + .../google/protobuf/AbstractMessageLite.java | 50 + .../com/google/protobuf/AllocatedBuffer.java | 263 + .../com/google/protobuf/ArrayDecoders.java | 1076 +++ .../com/google/protobuf/BinaryReader.java | 1729 +++++ .../com/google/protobuf/BinaryWriter.java | 3071 +++++++++ .../com/google/protobuf/BufferAllocator.java | 64 + .../java/com/google/protobuf/ByteString.java | 32 +- .../com/google/protobuf/CodedInputStream.java | 57 +- .../protobuf/CodedInputStreamReader.java | 1333 ++++ .../google/protobuf/CodedOutputStream.java | 113 + .../protobuf/CodedOutputStreamWriter.java | 691 ++ .../DescriptorMessageInfoFactory.java | 693 ++ .../protobuf/ExtensionRegistryLite.java | 13 +- .../com/google/protobuf/ExtensionSchema.java | 98 + .../google/protobuf/ExtensionSchemaFull.java | 547 ++ .../google/protobuf/ExtensionSchemaLite.java | 541 ++ .../com/google/protobuf/ExtensionSchemas.java | 56 + .../java/com/google/protobuf/FieldInfo.java | 577 ++ .../java/com/google/protobuf/FieldSet.java | 11 + .../java/com/google/protobuf/FieldType.java | 346 + .../protobuf/GeneratedMessageInfoFactory.java | 65 + .../google/protobuf/GeneratedMessageLite.java | 995 +-- .../google/protobuf/GeneratedMessageV3.java | 23 + .../java/com/google/protobuf/Internal.java | 18 + .../java/com/google/protobuf/JavaType.java | 76 + .../com/google/protobuf/LazyFieldLite.java | 12 + .../com/google/protobuf/ListFieldSchema.java | 190 + .../protobuf/ManifestSchemaFactory.java | 172 + .../com/google/protobuf/MapFieldSchema.java | 63 + .../google/protobuf/MapFieldSchemaFull.java | 112 + .../google/protobuf/MapFieldSchemaLite.java | 107 + .../com/google/protobuf/MapFieldSchemas.java | 53 + .../java/com/google/protobuf/MessageInfo.java | 43 + .../google/protobuf/MessageInfoFactory.java | 41 + .../com/google/protobuf/MessageSchema.java | 5886 +++++++++++++++++ .../com/google/protobuf/MessageSetSchema.java | 392 ++ .../google/protobuf/NewInstanceSchema.java | 36 + .../protobuf/NewInstanceSchemaFull.java | 39 + .../protobuf/NewInstanceSchemaLite.java | 39 + .../google/protobuf/NewInstanceSchemas.java | 53 + .../java/com/google/protobuf/OneofInfo.java | 66 + .../java/com/google/protobuf/ProtoSyntax.java | 38 + .../java/com/google/protobuf/Protobuf.java | 152 + .../com/google/protobuf/ProtobufLists.java | 94 + .../com/google/protobuf/RawMessageInfo.java | 226 + .../main/java/com/google/protobuf/Reader.java | 379 ++ .../com/google/protobuf/RopeByteString.java | 5 + .../main/java/com/google/protobuf/Schema.java | 85 + .../com/google/protobuf/SchemaFactory.java | 38 + .../java/com/google/protobuf/SchemaUtil.java | 991 +++ .../com/google/protobuf/SmallSortedMap.java | 65 + .../protobuf/StructuralMessageInfo.java | 167 + .../java/com/google/protobuf/TextFormat.java | 54 +- .../google/protobuf/UnknownFieldSchema.java | 133 + .../com/google/protobuf/UnknownFieldSet.java | 79 + .../google/protobuf/UnknownFieldSetLite.java | 66 + .../protobuf/UnknownFieldSetLiteSchema.java | 140 + .../protobuf/UnknownFieldSetSchema.java | 132 + .../main/java/com/google/protobuf/Writer.java | 219 + .../AbstractProto2LiteSchemaTest.java | 199 + .../protobuf/AbstractProto2SchemaTest.java | 224 + .../AbstractProto3LiteSchemaTest.java | 143 + .../protobuf/AbstractProto3SchemaTest.java | 151 + .../google/protobuf/AbstractSchemaTest.java | 157 + .../google/protobuf/ArrayDecodersTest.java | 236 + .../google/protobuf/BinaryProtocolTest.java | 90 + .../google/protobuf/CachedFieldSizeTest.java | 65 + .../com/google/protobuf/CodedAdapterTest.java | 110 + .../google/protobuf/CodedInputStreamTest.java | 56 + .../com/google/protobuf/DescriptorsTest.java | 21 + .../protobuf/ExperimentalMessageFactory.java | 40 + .../ExperimentalSerializationUtil.java | 113 + .../ExperimentalTestDataProvider.java | 189 + .../ExtensionRegistryFactoryTest.java | 43 +- .../java/com/google/protobuf/MapLiteTest.java | 894 +++ .../com/google/protobuf/PackedFieldTest.java | 232 + .../com/google/protobuf/ParserLiteTest.java | 191 + .../Proto2ExtensionLookupSchemaTest.java | 171 + .../google/protobuf/Proto2LiteSchemaTest.java | 49 + .../google/protobuf/Proto2MessageFactory.java | 555 ++ .../protobuf/Proto2MessageInfoFactory.java | 892 +++ .../protobuf/Proto2MessageLiteFactory.java | 558 ++ .../com/google/protobuf/Proto2SchemaTest.java | 49 + .../protobuf/Proto2UnknownEnumValueTest.java | 111 + .../google/protobuf/Proto3LiteSchemaTest.java | 49 + .../google/protobuf/Proto3MessageFactory.java | 450 ++ .../protobuf/Proto3MessageInfoFactory.java | 506 ++ .../protobuf/Proto3MessageLiteFactory.java | 452 ++ .../Proto3MessageLiteInfoFactory.java | 816 +++ .../com/google/protobuf/Proto3SchemaTest.java | 48 + .../java/com/google/protobuf/TestSchemas.java | 94 + .../com/google/protobuf/TestSchemasLite.java | 103 + .../com/google/protobuf/TextFormatTest.java | 21 + .../protobuf/UnknownFieldSetLiteTest.java | 601 -- .../java/com/google/protobuf/Utf8Test.java | 232 + .../java/com/google/protobuf/Utf8Utils.java | 193 + .../google/protobuf/WireFormatLiteTest.java | 1023 +++ .../protobuf/WrappersLiteOfMethodTest.java | 61 + .../google/protobuf/WrappersOfMethodTest.java | 61 + .../protobuf/cached_field_size_test.proto | 45 + .../message_lite_extension_util_test.proto | 47 + .../google/protobuf/nested_extension.proto | 4 +- .../protobuf/nested_extension_lite.proto | 4 +- .../google/protobuf/packed_field_test.proto | 76 + .../com/google/protobuf/proto2_message.proto | 429 ++ .../google/protobuf/proto2_message_lite.proto | 422 ++ .../com/google/protobuf/proto3_message.proto | 326 + .../google/protobuf/proto3_message_lite.proto | 319 + .../com/google/protobuf/wrappers_test.proto | 49 + .../java/com/google/protobuf/LiteTest.java | 22 + .../Proto2MessageLiteInfoFactory.java | 1327 ++++ .../google/protobuf/util/JsonFormatTest.java | 14 + js/message.js | 19 +- .../protobuf/internal/api_implementation.cc | 68 +- .../internal/descriptor_database_test.py | 3 +- .../protobuf/internal/descriptor_pool_test.py | 14 +- .../protobuf/internal/extension_dict.py | 21 + .../google/protobuf/internal/keywords_test.py | 105 + .../protobuf/internal/message_factory_test.py | 3 +- .../google/protobuf/internal/message_test.py | 36 +- .../protobuf/internal/more_extensions.proto | 4 +- .../protobuf/internal/more_messages.proto | 49 + .../protobuf/internal/python_protobuf.cc | 8 +- .../protobuf/internal/reflection_test.py | 77 +- .../protobuf/internal/testing_refleaks.py | 21 +- .../protobuf/internal/text_format_test.py | 6 + .../protobuf/internal/unknown_fields_test.py | 12 +- python/google/protobuf/message.py | 7 +- python/google/protobuf/pyext/descriptor.cc | 7 +- .../google/protobuf/pyext/extension_dict.cc | 247 +- python/google/protobuf/pyext/extension_dict.h | 1 + python/google/protobuf/pyext/map_container.cc | 192 +- python/google/protobuf/pyext/map_container.h | 42 +- python/google/protobuf/pyext/message.cc | 836 +-- python/google/protobuf/pyext/message.h | 122 +- .../pyext/repeated_composite_container.cc | 478 +- .../pyext/repeated_composite_container.h | 62 +- .../pyext/repeated_scalar_container.cc | 86 +- .../pyext/repeated_scalar_container.h | 36 +- .../protobuf/pyext/thread_unsafe_shared_ptr.h | 104 - .../google/protobuf/pyext/unknown_fields.cc | 2 + python/google/protobuf/text_format.py | 44 +- src/Makefile.am | 6 +- src/google/protobuf/any.cc | 20 +- src/google/protobuf/any.pb.cc | 24 +- src/google/protobuf/any_test.cc | 1 - src/google/protobuf/api.pb.cc | 116 +- src/google/protobuf/arena.cc | 13 +- src/google/protobuf/arena.h | 12 +- src/google/protobuf/arena_impl.h | 37 +- src/google/protobuf/arena_test_util.cc | 2 +- src/google/protobuf/arena_test_util.h | 5 +- src/google/protobuf/arena_unittest.cc | 108 +- src/google/protobuf/arenastring_unittest.cc | 3 +- .../protobuf/compiler/code_generator.cc | 5 +- src/google/protobuf/compiler/code_generator.h | 8 +- .../compiler/command_line_interface.cc | 391 +- .../compiler/command_line_interface.h | 32 +- .../command_line_interface_unittest.cc | 639 +- .../compiler/cpp/cpp_bootstrap_unittest.cc | 25 +- src/google/protobuf/compiler/cpp/cpp_enum.cc | 79 +- src/google/protobuf/compiler/cpp/cpp_enum.h | 6 +- .../protobuf/compiler/cpp/cpp_extension.cc | 3 +- src/google/protobuf/compiler/cpp/cpp_field.cc | 7 +- src/google/protobuf/compiler/cpp/cpp_file.cc | 98 +- src/google/protobuf/compiler/cpp/cpp_file.h | 4 +- .../protobuf/compiler/cpp/cpp_generator.cc | 18 +- .../protobuf/compiler/cpp/cpp_helpers.cc | 213 +- .../protobuf/compiler/cpp/cpp_helpers.h | 15 +- .../protobuf/compiler/cpp/cpp_map_field.cc | 42 +- .../protobuf/compiler/cpp/cpp_message.cc | 124 +- .../compiler/cpp/cpp_message_field.cc | 129 +- .../protobuf/compiler/cpp/cpp_options.h | 3 +- .../compiler/cpp/cpp_plugin_unittest.cc | 14 +- .../compiler/cpp/cpp_primitive_field.cc | 187 +- .../protobuf/compiler/cpp/cpp_string_field.cc | 146 +- .../protobuf/compiler/cpp/cpp_unittest.cc | 6 +- .../protobuf/compiler/cpp/metadata_test.cc | 3 +- src/google/protobuf/compiler/importer.cc | 58 +- src/google/protobuf/compiler/importer.h | 17 +- .../protobuf/compiler/importer_unittest.cc | 144 +- .../protobuf/compiler/java/java_context.cc | 3 +- .../protobuf/compiler/java/java_context.h | 20 +- .../compiler/java/java_doc_comment.cc | 43 +- .../protobuf/compiler/java/java_enum.cc | 198 +- src/google/protobuf/compiler/java/java_enum.h | 12 +- .../protobuf/compiler/java/java_enum_field.cc | 1081 +-- .../compiler/java/java_enum_field_lite.cc | 1082 ++- .../compiler/java/java_enum_field_lite.h | 34 +- .../protobuf/compiler/java/java_enum_lite.cc | 31 +- .../protobuf/compiler/java/java_enum_lite.h | 12 +- .../protobuf/compiler/java/java_extension.cc | 36 +- .../compiler/java/java_extension_lite.cc | 18 +- .../protobuf/compiler/java/java_field.cc | 48 +- .../protobuf/compiler/java/java_field.h | 38 +- .../protobuf/compiler/java/java_file.cc | 209 +- src/google/protobuf/compiler/java/java_file.h | 22 +- .../protobuf/compiler/java/java_generator.cc | 10 +- .../compiler/java/java_generator_factory.cc | 5 +- .../compiler/java/java_generator_factory.h | 20 +- .../protobuf/compiler/java/java_helpers.cc | 415 +- .../protobuf/compiler/java/java_helpers.h | 23 +- .../protobuf/compiler/java/java_map_field.cc | 504 +- .../protobuf/compiler/java/java_map_field.h | 6 +- .../compiler/java/java_map_field_lite.cc | 562 +- .../compiler/java/java_map_field_lite.h | 12 +- .../protobuf/compiler/java/java_message.cc | 994 ++- .../protobuf/compiler/java/java_message.h | 7 +- .../compiler/java/java_message_builder.cc | 636 +- .../compiler/java/java_message_builder.h | 2 +- .../java/java_message_builder_lite.cc | 84 +- .../compiler/java/java_message_builder_lite.h | 2 +- .../compiler/java/java_message_field.cc | 1466 ++-- .../compiler/java/java_message_field_lite.cc | 943 ++- .../compiler/java/java_message_field_lite.h | 30 +- .../compiler/java/java_message_lite.cc | 1029 +-- .../compiler/java/java_message_lite.h | 17 +- .../compiler/java/java_name_resolver.cc | 29 +- .../compiler/java/java_plugin_unittest.cc | 11 +- .../compiler/java/java_primitive_field.cc | 813 +-- .../java/java_primitive_field_lite.cc | 789 +-- .../compiler/java/java_primitive_field_lite.h | 36 +- .../protobuf/compiler/java/java_service.cc | 312 +- .../protobuf/compiler/java/java_service.h | 1 - .../java/java_shared_code_generator.cc | 34 +- .../java/java_shared_code_generator.h | 1 - .../compiler/java/java_string_field.cc | 1053 +-- .../compiler/java/java_string_field_lite.cc | 867 +-- .../compiler/java/java_string_field_lite.h | 22 +- .../protobuf/compiler/js/js_generator.cc | 650 +- .../protobuf/compiler/js/js_generator.h | 49 +- .../protobuf/compiler/mock_code_generator.cc | 36 +- .../compiler/objectivec/objectivec_helpers.cc | 4 +- src/google/protobuf/compiler/parser.cc | 397 +- src/google/protobuf/compiler/parser.h | 42 +- .../protobuf/compiler/parser_unittest.cc | 2986 +++++---- src/google/protobuf/compiler/plugin.cc | 27 +- src/google/protobuf/compiler/plugin.h | 2 +- src/google/protobuf/compiler/plugin.pb.cc | 179 +- .../compiler/python/python_generator.cc | 297 +- .../compiler/python/python_generator.h | 12 +- .../compiler/python/python_plugin_unittest.cc | 13 +- src/google/protobuf/compiler/subprocess.cc | 47 +- src/google/protobuf/compiler/subprocess.h | 6 +- src/google/protobuf/compiler/test_plugin.cc | 4 +- src/google/protobuf/compiler/zip_writer.cc | 157 +- src/google/protobuf/descriptor.cc | 1254 ++-- src/google/protobuf/descriptor.h | 196 +- src/google/protobuf/descriptor.pb.cc | 1564 +++-- src/google/protobuf/descriptor.pb.h | 96 +- src/google/protobuf/descriptor_database.cc | 61 +- src/google/protobuf/descriptor_database.h | 13 +- .../protobuf/descriptor_database_unittest.cc | 262 +- src/google/protobuf/descriptor_unittest.cc | 3965 +++++------ src/google/protobuf/duration.pb.cc | 28 +- src/google/protobuf/duration.proto | 1 - src/google/protobuf/dynamic_message.cc | 230 +- src/google/protobuf/dynamic_message.h | 18 +- .../protobuf/dynamic_message_unittest.cc | 76 +- src/google/protobuf/empty.pb.cc | 20 +- src/google/protobuf/extension_set.cc | 847 ++- src/google/protobuf/extension_set_heavy.cc | 199 +- src/google/protobuf/extension_set_unittest.cc | 521 +- src/google/protobuf/field_mask.pb.cc | 30 +- .../protobuf/generated_enum_reflection.h | 4 +- src/google/protobuf/generated_enum_util.h | 3 +- .../protobuf/generated_message_reflection.cc | 890 ++- .../protobuf/generated_message_reflection.h | 76 +- .../generated_message_reflection_unittest.cc | 250 +- .../generated_message_table_driven.cc | 26 +- .../protobuf/generated_message_table_driven.h | 3 +- .../generated_message_table_driven_lite.cc | 20 +- .../generated_message_table_driven_lite.h | 15 +- src/google/protobuf/generated_message_util.cc | 19 +- src/google/protobuf/generated_message_util.h | 14 +- src/google/protobuf/has_bits.h | 6 +- src/google/protobuf/io/coded_stream.cc | 72 +- src/google/protobuf/io/coded_stream.h | 179 +- src/google/protobuf/io/coded_stream_inl.h | 2 +- .../protobuf/io/coded_stream_unittest.cc | 294 +- src/google/protobuf/io/gzip_stream.cc | 47 +- src/google/protobuf/io/gzip_stream.h | 26 +- src/google/protobuf/{stubs => io}/io_win32.cc | 10 +- src/google/protobuf/{stubs => io}/io_win32.h | 18 +- .../{stubs => io}/io_win32_unittest.cc | 10 +- src/google/protobuf/io/printer.cc | 8 +- src/google/protobuf/io/printer.h | 4 +- src/google/protobuf/io/printer_unittest.cc | 79 +- src/google/protobuf/io/strtod.cc | 24 +- src/google/protobuf/io/tokenizer.cc | 227 +- src/google/protobuf/io/tokenizer.h | 36 +- src/google/protobuf/io/tokenizer_unittest.cc | 680 +- .../protobuf/io/zero_copy_stream_impl.cc | 123 +- .../protobuf/io/zero_copy_stream_impl.h | 4 +- .../protobuf/io/zero_copy_stream_impl_lite.cc | 86 +- .../protobuf/io/zero_copy_stream_impl_lite.h | 20 +- .../protobuf/io/zero_copy_stream_unittest.cc | 117 +- src/google/protobuf/lite_unittest.cc | 66 +- src/google/protobuf/map.h | 37 +- src/google/protobuf/map_entry_lite.h | 139 +- src/google/protobuf/map_field.cc | 94 +- src/google/protobuf/map_field.h | 150 +- src/google/protobuf/map_field_inl.h | 51 +- src/google/protobuf/map_field_lite.h | 2 +- src/google/protobuf/map_field_test.cc | 15 +- src/google/protobuf/map_test.cc | 301 +- src/google/protobuf/map_test_util.cc | 859 ++- src/google/protobuf/map_test_util.h | 3 +- src/google/protobuf/map_test_util_impl.h | 50 +- src/google/protobuf/map_type_handler.h | 313 +- src/google/protobuf/message.h | 300 +- src/google/protobuf/message_lite.cc | 18 +- src/google/protobuf/message_lite.h | 27 +- src/google/protobuf/message_unittest.inc | 7 +- src/google/protobuf/metadata.h | 10 +- src/google/protobuf/metadata_lite.h | 15 +- src/google/protobuf/no_field_presence_test.cc | 32 +- src/google/protobuf/parse_context.cc | 14 +- src/google/protobuf/parse_context.h | 6 + .../protobuf/preserve_unknown_enum_test.cc | 29 +- .../protobuf/proto3_arena_lite_unittest.cc | 14 +- src/google/protobuf/proto3_arena_unittest.cc | 18 +- src/google/protobuf/reflection.h | 216 +- src/google/protobuf/reflection_internal.h | 4 +- src/google/protobuf/reflection_ops.cc | 74 +- .../protobuf/reflection_ops_unittest.cc | 134 +- src/google/protobuf/repeated_field.cc | 7 +- src/google/protobuf/repeated_field.h | 415 +- .../repeated_field_reflection_unittest.cc | 149 +- .../protobuf/repeated_field_unittest.cc | 47 +- src/google/protobuf/service.cc | 1 - src/google/protobuf/service.h | 29 +- src/google/protobuf/source_context.pb.cc | 22 +- src/google/protobuf/struct.pb.cc | 74 +- src/google/protobuf/struct.pb.h | 14 +- src/google/protobuf/struct.proto | 1 - src/google/protobuf/test_util_lite.cc | 2736 ++++---- src/google/protobuf/testing/file.cc | 12 +- src/google/protobuf/testing/googletest.cc | 12 +- src/google/protobuf/text_format.h | 7 + src/google/protobuf/text_format_unittest.cc | 651 +- src/google/protobuf/timestamp.pb.cc | 28 +- src/google/protobuf/timestamp.proto | 11 +- src/google/protobuf/type.pb.cc | 192 +- src/google/protobuf/type.pb.h | 40 +- src/google/protobuf/type.proto | 40 +- src/google/protobuf/unknown_field_set.h | 35 +- .../protobuf/unknown_field_set_unittest.cc | 41 +- .../protobuf/util/delimited_message_util.cc | 36 +- .../protobuf/util/delimited_message_util.h | 30 + .../util/delimited_message_util_test.cc | 30 + src/google/protobuf/util/field_comparator.cc | 46 +- src/google/protobuf/util/field_comparator.h | 36 +- .../protobuf/util/field_comparator_test.cc | 262 +- src/google/protobuf/util/field_mask_util.cc | 12 +- src/google/protobuf/util/field_mask_util.h | 22 +- .../protobuf/util/field_mask_util_test.cc | 15 +- .../protobuf/util/internal/datapiece.cc | 2 +- .../internal/default_value_objectwriter.cc | 4 +- .../protobuf/util/internal/json_escaping.cc | 67 +- .../util/internal/json_stream_parser_test.cc | 5 +- .../protobuf/util/internal/proto_writer.cc | 8 +- .../util/internal/protostream_objectsource.cc | 2 +- .../util/internal/protostream_objectsource.h | 9 +- .../internal/protostream_objectsource_test.cc | 4 +- .../util/internal/protostream_objectwriter.cc | 14 +- .../internal/protostream_objectwriter_test.cc | 12 +- .../util/internal/type_info_test_helper.cc | 1 - src/google/protobuf/util/json_util.cc | 33 +- src/google/protobuf/util/json_util.h | 8 +- src/google/protobuf/util/json_util_test.cc | 20 +- .../protobuf/util/message_differencer.cc | 307 +- .../protobuf/util/message_differencer.h | 98 +- .../util/message_differencer_unittest.cc | 682 +- src/google/protobuf/util/time_util.cc | 2 +- src/google/protobuf/util/time_util.h | 6 +- src/google/protobuf/util/time_util_test.cc | 4 +- .../protobuf/util/type_resolver_util_test.cc | 275 +- src/google/protobuf/wire_format.cc | 403 +- src/google/protobuf/wire_format_lite.cc | 189 +- src/google/protobuf/wire_format_lite.h | 317 +- src/google/protobuf/wire_format_unittest.cc | 157 +- src/google/protobuf/wrappers.pb.cc | 160 +- 388 files changed, 58038 insertions(+), 25853 deletions(-) create mode 100755 java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java create mode 100755 java/core/src/main/java/com/google/protobuf/ArrayDecoders.java create mode 100755 java/core/src/main/java/com/google/protobuf/BinaryReader.java create mode 100755 java/core/src/main/java/com/google/protobuf/BinaryWriter.java create mode 100755 java/core/src/main/java/com/google/protobuf/BufferAllocator.java create mode 100755 java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java create mode 100755 java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java create mode 100755 java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java create mode 100755 java/core/src/main/java/com/google/protobuf/ExtensionSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java create mode 100755 java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java create mode 100755 java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java create mode 100755 java/core/src/main/java/com/google/protobuf/FieldInfo.java create mode 100755 java/core/src/main/java/com/google/protobuf/FieldType.java create mode 100755 java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java create mode 100755 java/core/src/main/java/com/google/protobuf/JavaType.java create mode 100755 java/core/src/main/java/com/google/protobuf/ListFieldSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java create mode 100755 java/core/src/main/java/com/google/protobuf/MapFieldSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java create mode 100755 java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java create mode 100755 java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java create mode 100755 java/core/src/main/java/com/google/protobuf/MessageInfo.java create mode 100755 java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java create mode 100755 java/core/src/main/java/com/google/protobuf/MessageSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/MessageSetSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java create mode 100755 java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java create mode 100755 java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java create mode 100755 java/core/src/main/java/com/google/protobuf/OneofInfo.java create mode 100755 java/core/src/main/java/com/google/protobuf/ProtoSyntax.java create mode 100755 java/core/src/main/java/com/google/protobuf/Protobuf.java create mode 100755 java/core/src/main/java/com/google/protobuf/ProtobufLists.java create mode 100755 java/core/src/main/java/com/google/protobuf/RawMessageInfo.java create mode 100755 java/core/src/main/java/com/google/protobuf/Reader.java create mode 100755 java/core/src/main/java/com/google/protobuf/Schema.java create mode 100755 java/core/src/main/java/com/google/protobuf/SchemaFactory.java create mode 100755 java/core/src/main/java/com/google/protobuf/SchemaUtil.java create mode 100755 java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java create mode 100755 java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java create mode 100755 java/core/src/main/java/com/google/protobuf/Writer.java create mode 100755 java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java create mode 100755 java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java create mode 100755 java/core/src/test/java/com/google/protobuf/MapLiteTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/PackedFieldTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/ParserLiteTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java create mode 100755 java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/TestSchemas.java create mode 100755 java/core/src/test/java/com/google/protobuf/TestSchemasLite.java delete mode 100644 java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/Utf8Test.java create mode 100755 java/core/src/test/java/com/google/protobuf/Utf8Utils.java create mode 100755 java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java create mode 100755 java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java create mode 100755 java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/packed_field_test.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/proto2_message.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/proto3_message.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto create mode 100755 java/core/src/test/proto/com/google/protobuf/wrappers_test.proto rename java/{core => lite}/src/test/java/com/google/protobuf/LiteTest.java (99%) mode change 100644 => 100755 create mode 100755 java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java create mode 100755 python/google/protobuf/internal/keywords_test.py delete mode 100644 python/google/protobuf/pyext/thread_unsafe_shared_ptr.h rename src/google/protobuf/{stubs => io}/io_win32.cc (98%) mode change 100644 => 100755 rename src/google/protobuf/{stubs => io}/io_win32.h (92%) mode change 100644 => 100755 rename src/google/protobuf/{stubs => io}/io_win32_unittest.cc (98%) mode change 100644 => 100755 diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index 890074f345..81a091fedb 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -7,6 +7,7 @@ set(libprotobuf_lite_files ${protobuf_source_dir}/src/google/protobuf/implicit_weak_message.cc ${protobuf_source_dir}/src/google/protobuf/parse_context.cc ${protobuf_source_dir}/src/google/protobuf/io/coded_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/io_win32.cc ${protobuf_source_dir}/src/google/protobuf/io/strtod.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl_lite.cc @@ -15,7 +16,6 @@ set(libprotobuf_lite_files ${protobuf_source_dir}/src/google/protobuf/stubs/bytestream.cc ${protobuf_source_dir}/src/google/protobuf/stubs/common.cc ${protobuf_source_dir}/src/google/protobuf/stubs/int128.cc - ${protobuf_source_dir}/src/google/protobuf/stubs/io_win32.cc ${protobuf_source_dir}/src/google/protobuf/stubs/status.cc ${protobuf_source_dir}/src/google/protobuf/stubs/statusor.cc ${protobuf_source_dir}/src/google/protobuf/stubs/stringpiece.cc diff --git a/cmake/tests.cmake b/cmake/tests.cmake index f4679d1a1e..72269617ef 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -156,6 +156,7 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/extension_set_unittest.cc ${protobuf_source_dir}/src/google/protobuf/generated_message_reflection_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/coded_stream_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/io_win32_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/printer_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/tokenizer_unittest.cc ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -175,7 +176,6 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/stubs/bytestream_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/common_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/int128_unittest.cc - ${protobuf_source_dir}/src/google/protobuf/stubs/io_win32_unittest.cc ${protobuf_source_dir}/src/google/protobuf/stubs/status_test.cc ${protobuf_source_dir}/src/google/protobuf/stubs/statusor_test.cc ${protobuf_source_dir}/src/google/protobuf/stubs/stringpiece_unittest.cc diff --git a/conformance/text_format_conformance_suite.cc b/conformance/text_format_conformance_suite.cc index 76f398c1a5..97b7b90727 100644 --- a/conformance/text_format_conformance_suite.cc +++ b/conformance/text_format_conformance_suite.cc @@ -236,7 +236,7 @@ void TextFormatConformanceTestSuite::RunSuiteImpl() { RunValidTextFormatTest("FloatFieldWithVeryPreciseNumber", REQUIRED, "optional_float: 3.123456789123456789"); RunValidTextFormatTest("FloatFieldMaxValue", REQUIRED, - "optional_float: 3.40282e+38"); + "optional_float: 3.4028235e+38"); RunValidTextFormatTest("FloatFieldMinValue", REQUIRED, "optional_float: 1.17549e-38"); RunValidTextFormatTest("FloatFieldNaNValue", REQUIRED, @@ -286,6 +286,31 @@ void TextFormatConformanceTestSuite::RunSuiteImpl() { message.add_repeated_int32(2); message.add_repeated_int32(3); RunValidUnknownTextFormatTest("RepeatedUnknownFields", message); + + // Any fields + RunValidTextFormatTest("AnyField", REQUIRED, + R"( + optional_any: { + [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] { + optional_int32: 12345 + } + } + )"); + RunValidTextFormatTest("AnyFieldWithRawBytes", REQUIRED, + R"( + optional_any: { + type_url: "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3" + value: "\b\271`" + } + )"); + ExpectParseFailure("AnyFieldWithInvalidType", REQUIRED, + R"( + optional_any: { + [type.googleapis.com/unknown] { + optional_int32: 12345 + } + } + )"); } } // namespace protobuf diff --git a/conformance/text_format_failure_list_java.txt b/conformance/text_format_failure_list_java.txt index 4902d461cd..81433b441a 100755 --- a/conformance/text_format_failure_list_java.txt +++ b/conformance/text_format_failure_list_java.txt @@ -2,3 +2,5 @@ Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput +Required.Proto3.TextFormatInput.AnyField.ProtobufOutput +Required.Proto3.TextFormatInput.AnyField.TextFormatOutput diff --git a/conformance/text_format_failure_list_python.txt b/conformance/text_format_failure_list_python.txt index 9a8709dcb1..b2db95e7de 100644 --- a/conformance/text_format_failure_list_python.txt +++ b/conformance/text_format_failure_list_python.txt @@ -1,3 +1,5 @@ # This is the list of text format conformance tests that are known to fail right # now. # TODO: These should be fixed. +Required.Proto3.TextFormatInput.FloatFieldMaxValue.ProtobufOutput +Required.Proto3.TextFormatInput.FloatFieldMaxValue.TextFormatOutput diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java index 1720575099..ba0138af53 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -106,6 +106,56 @@ public abstract class AbstractMessageLite< } + @ExperimentalApi + protected final boolean isInitializedInternal() { + return Protobuf.getInstance().schemaFor(this).isInitialized(this); + } + + @ExperimentalApi + protected final int getSerializedSizeInternal() { + return Protobuf.getInstance().schemaFor(this).getSerializedSize(this); + } + + int getSerializedSize(Schema schema) { + int memoizedSerializedSize = getMemoizedSerializedSize(); + if (memoizedSerializedSize == -1) { + memoizedSerializedSize = schema.getSerializedSize(this); + setMemoizedSerializedSize(memoizedSerializedSize); + } + return memoizedSerializedSize; + } + + @ExperimentalApi + protected final void writeToInternal(CodedOutputStream output) throws IOException { + Protobuf.getInstance() + .schemaFor(getClassInternal()) + .writeTo(this, CodedOutputStreamWriter.forCodedOutput(output)); + } + + @ExperimentalApi + protected void mergeFromInternal(CodedInputStream input, ExtensionRegistryLite extensionRegistry) + throws InvalidProtocolBufferException { + try { + Protobuf.getInstance() + .schemaFor(getClassInternal()) + .mergeFrom(this, CodedInputStreamReader.forCodedInput(input), extensionRegistry); + } catch (InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (IOException e) { + throw new InvalidProtocolBufferException(e).setUnfinishedMessage(this); + } + } + + @ExperimentalApi + protected void makeImmutableInternal() { + Protobuf.getInstance().schemaFor(getClassInternal()).makeImmutable(this); + } + + @SuppressWarnings("unchecked") + private Class<AbstractMessageLite<MessageType, BuilderType>> getClassInternal() { + return (Class<AbstractMessageLite<MessageType, BuilderType>>) getClass(); + } + /** Package private helper method for AbstractParser to create UninitializedMessageException. */ UninitializedMessageException newUninitializedMessageException() { return new UninitializedMessageException(this); diff --git a/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java new file mode 100755 index 0000000000..a01a6c1a8b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/AllocatedBuffer.java @@ -0,0 +1,263 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.nio.ByteBuffer; + +/** + * A buffer that was allocated by a {@link BufferAllocator}. For every buffer, it is guaranteed that + * at least one of {@link #hasArray()} or {@link #hasNioBuffer()} will be {@code true}. + */ +@ExperimentalApi +abstract class AllocatedBuffer { + /** + * Indicates whether this buffer contains a backing {@link ByteBuffer} (i.e. it is safe to call + * {@link #nioBuffer()}). + */ + public abstract boolean hasNioBuffer(); + + /** + * Indicates whether this buffer contains a backing array (i.e. it is safe to call {@link + * #array()}). + */ + public abstract boolean hasArray(); + + /** + * Returns the {@link ByteBuffer} that backs this buffer <i>(optional operation)</i>. + * + * <p>Call {@link #hasNioBuffer()} before invoking this method in order to ensure that this buffer + * has a backing {@link ByteBuffer}. + * + * @return The {@link ByteBuffer} that backs this buffer + * @throws UnsupportedOperationException If this buffer is not backed by a {@link ByteBuffer}. + */ + public abstract ByteBuffer nioBuffer(); + + /** + * Returns the byte array that backs this buffer <i>(optional operation)</i>. + * + * <p>Call {@link #hasArray()} before invoking this method in order to ensure that this buffer has + * an accessible backing array. + * + * @return The array that backs this buffer + * @throws java.nio.ReadOnlyBufferException If this buffer is backed by an array but is read-only + * @throws UnsupportedOperationException If this buffer is not backed by an accessible array + */ + public abstract byte[] array(); + + /** + * Returns the offset within this buffer's backing array of the first element of the buffer + * <i>(optional operation)</i>. + * + * <p>If this buffer is backed by an array then {@link #position()} corresponds to the array index + * {@link #position()} {@code +} {@link #arrayOffset()}. + * + * <p>Invoke the {@link #hasArray hasArray} method before invoking this method in order to ensure + * that this buffer has an accessible backing array. + * + * @return The offset within this buffer's array of the first element of the buffer + * @throws java.nio.ReadOnlyBufferException If this buffer is backed by an array but is read-only + * @throws UnsupportedOperationException If this buffer is not backed by an accessible array + */ + public abstract int arrayOffset(); + + /** + * Returns this buffer's position. + * + * @return The position of this buffer + */ + public abstract int position(); + + /** + * Sets this buffer's position. + * + * @param position The new position value; must be non-negative and no larger than the current + * limit + * @return This buffer + * @throws IllegalArgumentException If the preconditions on {@code position} do not hold + */ + public abstract AllocatedBuffer position(int position); + + /** + * Returns this buffer's limit. + * + * @return The limit of this buffer + */ + public abstract int limit(); + + /** + * Returns the number of elements between the current {@link #position()} and the {@link #limit()} + * . + * + * @return The number of elements remaining in this buffer + */ + public abstract int remaining(); + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given array. The returned buffer + * will have {@link #hasArray} == {@code true}, {@link #arrayOffset()} == {@code 0}, {@link + * #position()} == {@code 0} and {@link #limit()} equal to the length of {@code bytes}. + */ + public static AllocatedBuffer wrap(byte[] bytes) { + return wrapNoCheck(bytes, 0, bytes.length); + } + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given array. The returned buffer + * will have {@link #hasArray} == {@code true}, {@link #arrayOffset()} == {@code offset}, {@link + * #position()} == {@code 0} and {@link #limit()} == {@code length}. + */ + public static AllocatedBuffer wrap(final byte[] bytes, final int offset, final int length) { + if (offset < 0 || length < 0 || (offset + length) > bytes.length) { + throw new IndexOutOfBoundsException( + String.format("bytes.length=%d, offset=%d, length=%d", bytes.length, offset, length)); + } + + return wrapNoCheck(bytes, offset, length); + } + + /** + * Creates a new {@link AllocatedBuffer} that is backed by the given {@link ByteBuffer}. The + * returned buffer will have {@link #hasNioBuffer} == {@code true}. + */ + public static AllocatedBuffer wrap(final ByteBuffer buffer) { + checkNotNull(buffer, "buffer"); + + return new AllocatedBuffer() { + + @Override + public boolean hasNioBuffer() { + return true; + } + + @Override + public ByteBuffer nioBuffer() { + return buffer; + } + + @Override + public boolean hasArray() { + return buffer.hasArray(); + } + + @Override + public byte[] array() { + return buffer.array(); + } + + @Override + public int arrayOffset() { + return buffer.arrayOffset(); + } + + @Override + public int position() { + return buffer.position(); + } + + @Override + public AllocatedBuffer position(int position) { + buffer.position(position); + return this; + } + + @Override + public int limit() { + return buffer.limit(); + } + + @Override + public int remaining() { + return buffer.remaining(); + } + }; + } + + private static AllocatedBuffer wrapNoCheck( + final byte[] bytes, final int offset, final int length) { + return new AllocatedBuffer() { + // Relative to offset. + private int position; + + @Override + public boolean hasNioBuffer() { + return false; + } + + @Override + public ByteBuffer nioBuffer() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasArray() { + return true; + } + + @Override + public byte[] array() { + return bytes; + } + + @Override + public int arrayOffset() { + return offset; + } + + @Override + public int position() { + return position; + } + + @Override + public AllocatedBuffer position(int position) { + if (position < 0 || position > length) { + throw new IllegalArgumentException("Invalid position: " + position); + } + this.position = position; + return this; + } + + @Override + public int limit() { + // Relative to offset. + return length; + } + + @Override + public int remaining() { + return length - position; + } + }; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java new file mode 100755 index 0000000000..a969c04c5f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -0,0 +1,1076 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.MessageSchema.getMutableUnknownFields; + +import com.google.protobuf.Internal.ProtobufList; +import java.io.IOException; + +/** + * Helper functions to decode protobuf wire format from a byte array. + * + * <p>Note that these functions don't do boundary check on the byte array but instead rely on Java + * VM to check it. That means parsing rountines utilizing these functions must catch + * IndexOutOfBoundsException and convert it to protobuf's InvalidProtocolBufferException when + * crossing protobuf public API boundaries. + */ +final class ArrayDecoders { + /** + * A helper used to return multiple values in a Java function. Java doesn't natively support + * returning multiple values in a function. Creating a new Object to hold the return values will + * be too expensive. Instead, we pass a Registers instance to functions that want to return + * multiple values and let the function set the return value in this Registers instance instead. + * + * <p>TODO(xiaofeng): This could be merged into CodedInputStream or CodedInputStreamReader which + * is already being passed through all the parsing rountines. + */ + static final class Registers { + public int int1; + public long long1; + public Object object1; + public final ExtensionRegistryLite extensionRegistry; + + Registers() { + this.extensionRegistry = ExtensionRegistryLite.getEmptyRegistry(); + } + + Registers(ExtensionRegistryLite extensionRegistry) { + if (extensionRegistry == null) { + throw new NullPointerException(); + } + this.extensionRegistry = extensionRegistry; + } + } + + /** + * Decodes a varint. Returns the position after the varint. The decoded varint is stored in + * registers.int1. + */ + static int decodeVarint32(byte[] data, int position, Registers registers) { + int value = data[position++]; + if (value >= 0) { + registers.int1 = value; + return position; + } + return decodeVarint32(value, data, position, registers); + } + + /** Like decodeVarint32 except that the first byte is already read. */ + static int decodeVarint32(int firstByte, byte[] data, int position, Registers registers) { + int value = firstByte & 0x7F; + final byte b2 = data[position++]; + if (b2 >= 0) { + registers.int1 = value | ((int) b2 << 7); + return position; + } + value |= (b2 & 0x7F) << 7; + + final byte b3 = data[position++]; + if (b3 >= 0) { + registers.int1 = value | ((int) b3 << 14); + return position; + } + value |= (b3 & 0x7F) << 14; + + final byte b4 = data[position++]; + if (b4 >= 0) { + registers.int1 = value | ((int) b4 << 21); + return position; + } + value |= (b4 & 0x7F) << 21; + + final byte b5 = data[position++]; + if (b5 >= 0) { + registers.int1 = value | ((int) b5 << 28); + return position; + } + value |= (b5 & 0x7F) << 28; + + while (data[position++] < 0) {} + + registers.int1 = value; + return position; + } + + /** + * Decodes a varint. Returns the position after the varint. The decoded varint is stored in + * registers.long1. + */ + static int decodeVarint64(byte[] data, int position, Registers registers) { + long value = data[position++]; + if (value >= 0) { + registers.long1 = value; + return position; + } else { + return decodeVarint64(value, data, position, registers); + } + } + + /** Like decodeVarint64 except that the first byte is already read. */ + static int decodeVarint64(long firstByte, byte[] data, int position, Registers registers) { + long value = firstByte & 0x7F; + byte next = data[position++]; + int shift = 7; + value |= (long) (next & 0x7F) << 7; + while (next < 0) { + next = data[position++]; + shift += 7; + value |= (long) (next & 0x7F) << shift; + } + registers.long1 = value; + return position; + } + + /** Decodes and returns a fixed32 value. */ + static int decodeFixed32(byte[] data, int position) { + return (data[position] & 0xff) + | ((data[position + 1] & 0xff) << 8) + | ((data[position + 2] & 0xff) << 16) + | ((data[position + 3] & 0xff) << 24); + } + + /** Decodes and returns a fixed64 value. */ + static long decodeFixed64(byte[] data, int position) { + return (data[position] & 0xffL) + | ((data[position + 1] & 0xffL) << 8) + | ((data[position + 2] & 0xffL) << 16) + | ((data[position + 3] & 0xffL) << 24) + | ((data[position + 4] & 0xffL) << 32) + | ((data[position + 5] & 0xffL) << 40) + | ((data[position + 6] & 0xffL) << 48) + | ((data[position + 7] & 0xffL) << 56); + } + + /** Decodes and returns a double value. */ + static double decodeDouble(byte[] data, int position) { + return Double.longBitsToDouble(decodeFixed64(data, position)); + } + + /** Decodes and returns a float value. */ + static float decodeFloat(byte[] data, int position) { + return Float.intBitsToFloat(decodeFixed32(data, position)); + } + + /** Decodes a string value. */ + static int decodeString(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + registers.object1 = ""; + return position; + } else { + registers.object1 = new String(data, position, length, Internal.UTF_8); + return position + length; + } + } + + /** Decodes a string value with utf8 check. */ + static int decodeStringRequireUtf8(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + registers.object1 = ""; + return position; + } else { + registers.object1 = Utf8.decodeUtf8(data, position, length); + return position + length; + } + } + + /** Decodes a bytes value. */ + static int decodeBytes(byte[] data, int position, Registers registers) + throws InvalidProtocolBufferException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + registers.object1 = ByteString.EMPTY; + return position; + } else { + registers.object1 = ByteString.copyFrom(data, position, length); + return position + length; + } + } + + /** Decodes a message value. */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeMessageField( + Schema schema, byte[] data, int position, int limit, Registers registers) throws IOException { + int length = data[position++]; + if (length < 0) { + position = decodeVarint32(length, data, position, registers); + length = registers.int1; + } + if (length < 0 || length > limit - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + Object result = schema.newInstance(); + schema.mergeFrom(result, data, position, position + length, registers); + schema.makeImmutable(result); + registers.object1 = result; + return position + length; + } + + /** Decodes a group value. */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeGroupField( + Schema schema, byte[] data, int position, int limit, int endGroup, Registers registers) + throws IOException { + // A group field must has a MessageSchema (the only other subclass of Schema is MessageSetSchema + // and it can't be used in group fields). + final MessageSchema messageSchema = (MessageSchema) schema; + Object result = messageSchema.newInstance(); + // It's OK to directly use parseProto2Message since proto3 doesn't have group. + final int endPosition = + messageSchema.parseProto2Message(result, data, position, limit, endGroup, registers); + messageSchema.makeImmutable(result); + registers.object1 = result; + return endPosition; + } + + /** Decodes a repeated 32-bit varint field. Returns the position after all read values. */ + static int decodeVarint32List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + output.addInt(registers.int1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + output.addInt(registers.int1); + } + return position; + } + + /** Decodes a repeated 64-bit varint field. Returns the position after all read values. */ + static int decodeVarint64List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint64(data, position, registers); + output.addLong(registers.long1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addLong(registers.long1); + } + return position; + } + + /** Decodes a repeated fixed32 field. Returns the position after all read values. */ + static int decodeFixed32List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + output.addInt(decodeFixed32(data, position)); + position += 4; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addInt(decodeFixed32(data, nextPosition)); + position = nextPosition + 4; + } + return position; + } + + /** Decodes a repeated fixed64 field. Returns the position after all read values. */ + static int decodeFixed64List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + output.addLong(decodeFixed64(data, position)); + position += 8; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addLong(decodeFixed64(data, nextPosition)); + position = nextPosition + 8; + } + return position; + } + + /** Decodes a repeated float field. Returns the position after all read values. */ + static int decodeFloatList( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final FloatArrayList output = (FloatArrayList) list; + output.addFloat(decodeFloat(data, position)); + position += 4; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addFloat(decodeFloat(data, nextPosition)); + position = nextPosition + 4; + } + return position; + } + + /** Decodes a repeated double field. Returns the position after all read values. */ + static int decodeDoubleList( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final DoubleArrayList output = (DoubleArrayList) list; + output.addDouble(decodeDouble(data, position)); + position += 8; + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + output.addDouble(decodeDouble(data, nextPosition)); + position = nextPosition + 8; + } + return position; + } + + /** Decodes a repeated boolean field. Returns the position after all read values. */ + static int decodeBoolList( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final BooleanArrayList output = (BooleanArrayList) list; + position = decodeVarint64(data, position, registers); + output.addBoolean(registers.long1 != 0); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addBoolean(registers.long1 != 0); + } + return position; + } + + /** Decodes a repeated sint32 field. Returns the position after all read values. */ + static int decodeSInt32List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + } + return position; + } + + /** Decodes a repeated sint64 field. Returns the position after all read values. */ + static int decodeSInt64List( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint64(data, position, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint64(data, nextPosition, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + } + return position; + } + + /** Decodes a packed 32-bit varint field. Returns the position after all read values. */ + static int decodePackedVarint32List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint32(data, position, registers); + output.addInt(registers.int1); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed 64-bit varint field. Returns the position after all read values. */ + static int decodePackedVarint64List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addLong(registers.long1); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed fixed32 field. Returns the position after all read values. */ + static int decodePackedFixed32List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addInt(decodeFixed32(data, position)); + position += 4; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed fixed64 field. Returns the position after all read values. */ + static int decodePackedFixed64List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addLong(decodeFixed64(data, position)); + position += 8; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed float field. Returns the position after all read values. */ + static int decodePackedFloatList( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final FloatArrayList output = (FloatArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addFloat(decodeFloat(data, position)); + position += 4; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed double field. Returns the position after all read values. */ + static int decodePackedDoubleList( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final DoubleArrayList output = (DoubleArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + output.addDouble(decodeDouble(data, position)); + position += 8; + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed boolean field. Returns the position after all read values. */ + static int decodePackedBoolList( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final BooleanArrayList output = (BooleanArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addBoolean(registers.long1 != 0); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed sint32 field. Returns the position after all read values. */ + static int decodePackedSInt32List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final IntArrayList output = (IntArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint32(data, position, registers); + output.addInt(CodedInputStream.decodeZigZag32(registers.int1)); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a packed sint64 field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodePackedSInt64List( + byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException { + final LongArrayList output = (LongArrayList) list; + position = decodeVarint32(data, position, registers); + final int fieldLimit = position + registers.int1; + while (position < fieldLimit) { + position = decodeVarint64(data, position, registers); + output.addLong(CodedInputStream.decodeZigZag64(registers.long1)); + } + if (position != fieldLimit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return position; + } + + /** Decodes a repeated string field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodeStringList( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList<String> output = (ProtobufList<String>) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + output.add(""); + } else { + String value = new String(data, position, length, Internal.UTF_8); + output.add(value); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength == 0) { + output.add(""); + } else { + String value = new String(data, position, nextLength, Internal.UTF_8); + output.add(value); + position += nextLength; + } + } + return position; + } + + /** + * Decodes a repeated string field with utf8 check. Returns the position after all read values. + */ + @SuppressWarnings("unchecked") + static int decodeStringListRequireUtf8( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList<String> output = (ProtobufList<String>) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length == 0) { + output.add(""); + } else { + if (!Utf8.isValidUtf8(data, position, position + length)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String value = new String(data, position, length, Internal.UTF_8); + output.add(value); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength == 0) { + output.add(""); + } else { + if (!Utf8.isValidUtf8(data, position, position + nextLength)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String value = new String(data, position, nextLength, Internal.UTF_8); + output.add(value); + position += nextLength; + } + } + return position; + } + + /** Decodes a repeated bytes field. Returns the position after all read values. */ + @SuppressWarnings("unchecked") + static int decodeBytesList( + int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) + throws InvalidProtocolBufferException { + final ProtobufList<ByteString> output = (ProtobufList<ByteString>) list; + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + output.add(ByteString.EMPTY); + } else { + output.add(ByteString.copyFrom(data, position, length)); + position += length; + } + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeVarint32(data, nextPosition, registers); + final int nextLength = registers.int1; + if (nextLength < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (nextLength > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (nextLength == 0) { + output.add(ByteString.EMPTY); + } else { + output.add(ByteString.copyFrom(data, position, nextLength)); + position += nextLength; + } + } + return position; + } + + /** + * Decodes a repeated message field + * + * @return The position of after read all messages + */ + @SuppressWarnings({"unchecked"}) + static int decodeMessageList( + Schema<?> schema, + int tag, + byte[] data, + int position, + int limit, + ProtobufList<?> list, + Registers registers) + throws IOException { + final ProtobufList<Object> output = (ProtobufList<Object>) list; + position = decodeMessageField(schema, data, position, limit, registers); + output.add(registers.object1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeMessageField(schema, data, nextPosition, limit, registers); + output.add(registers.object1); + } + return position; + } + + /** + * Decodes a repeated group field + * + * @return The position of after read all groups + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + static int decodeGroupList( + Schema schema, + int tag, + byte[] data, + int position, + int limit, + ProtobufList<?> list, + Registers registers) + throws IOException { + final ProtobufList<Object> output = (ProtobufList<Object>) list; + final int endgroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + position = decodeGroupField(schema, data, position, limit, endgroup, registers); + output.add(registers.object1); + while (position < limit) { + int nextPosition = decodeVarint32(data, position, registers); + if (tag != registers.int1) { + break; + } + position = decodeGroupField(schema, data, nextPosition, limit, endgroup, registers); + output.add(registers.object1); + } + return position; + } + + static int decodeExtensionOrUnknownField( + int tag, byte[] data, int position, int limit, + Object message, + MessageLite defaultInstance, + UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite> unknownFieldSchema, + Registers registers) + throws IOException { + final int number = tag >>> 3; + GeneratedMessageLite.GeneratedExtension extension = + registers.extensionRegistry.findLiteExtensionByNumber(defaultInstance, number); + if (extension == null) { + return decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } else { + ((GeneratedMessageLite.ExtendableMessage<?, ?>) message).ensureExtensionsAreMutable(); + return decodeExtension( + tag, data, position, limit, (GeneratedMessageLite.ExtendableMessage) message, + extension, unknownFieldSchema, registers); + } + } + + static int decodeExtension( + int tag, + byte[] data, + int position, + int limit, + GeneratedMessageLite.ExtendableMessage<?, ?> message, + GeneratedMessageLite.GeneratedExtension<?, ?> extension, + UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite> unknownFieldSchema, + Registers registers) + throws IOException { + final FieldSet<GeneratedMessageLite.ExtensionDescriptor> extensions = message.extensions; + final int fieldNumber = tag >>> 3; + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + switch (extension.getLiteType()) { + case DOUBLE: + { + DoubleArrayList list = new DoubleArrayList(); + position = decodePackedDoubleList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FLOAT: + { + FloatArrayList list = new FloatArrayList(); + position = decodePackedFloatList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case INT64: + case UINT64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedVarint64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case INT32: + case UINT32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedVarint32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FIXED64: + case SFIXED64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedFixed64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case FIXED32: + case SFIXED32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedFixed32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case BOOL: + { + BooleanArrayList list = new BooleanArrayList(); + position = decodePackedBoolList(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case SINT32: + { + IntArrayList list = new IntArrayList(); + position = decodePackedSInt32List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case SINT64: + { + LongArrayList list = new LongArrayList(); + position = decodePackedSInt64List(data, position, list, registers); + extensions.setField(extension.descriptor, list); + break; + } + case ENUM: + { + IntArrayList list = new IntArrayList(); + position = decodePackedVarint32List(data, position, list, registers); + UnknownFieldSetLite unknownFields = message.unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = null; + } + unknownFields = + SchemaUtil.filterUnknownEnumList( + fieldNumber, + list, + extension.descriptor.getEnumType(), + unknownFields, + unknownFieldSchema); + if (unknownFields != null) { + message.unknownFields = unknownFields; + } + extensions.setField(extension.descriptor, list); + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + } else { + Object value = null; + // Enum is a special case becasue unknown enum values will be put into UnknownFieldSetLite. + if (extension.getLiteType() == WireFormat.FieldType.ENUM) { + position = decodeVarint32(data, position, registers); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(registers.int1); + if (enumValue == null) { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + SchemaUtil.storeUnknownEnum( + fieldNumber, registers.int1, unknownFields, unknownFieldSchema); + return position; + } + // Note, we store the integer value instead of the actual enum object in FieldSet. + // This is also different from full-runtime where we store EnumValueDescriptor. + value = registers.int1; + } else { + switch (extension.getLiteType()) { + case DOUBLE: + value = decodeDouble(data, position); + position += 8; + break; + case FLOAT: + value = decodeFloat(data, position); + position += 4; + break; + case INT64: + case UINT64: + position = decodeVarint64(data, position, registers); + value = registers.long1; + break; + case INT32: + case UINT32: + position = decodeVarint32(data, position, registers); + value = registers.int1; + break; + case FIXED64: + case SFIXED64: + value = decodeFixed64(data, position); + position += 8; + break; + case FIXED32: + case SFIXED32: + value = decodeFixed32(data, position); + position += 4; + break; + case BOOL: + position = decodeVarint64(data, position, registers); + value = (registers.long1 != 0); + break; + case BYTES: + position = decodeBytes(data, position, registers); + value = registers.object1; + break; + case SINT32: + position = decodeVarint32(data, position, registers); + value = CodedInputStream.decodeZigZag32(registers.int1); + break; + case SINT64: + position = decodeVarint64(data, position, registers); + value = CodedInputStream.decodeZigZag64(registers.long1); + break; + case STRING: + position = decodeString(data, position, registers); + value = registers.object1; + break; + case GROUP: + final int endTag = (fieldNumber << 3) | WireFormat.WIRETYPE_END_GROUP; + position = decodeGroupField( + Protobuf.getInstance().schemaFor(extension.getMessageDefaultInstance().getClass()), + data, position, limit, endTag, registers); + value = registers.object1; + break; + + case MESSAGE: + position = decodeMessageField( + Protobuf.getInstance().schemaFor(extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + value = registers.object1; + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return position; + } + + /** Decodes an unknown field. */ + static int decodeUnknownField( + int tag, + byte[] data, + int position, + int limit, + UnknownFieldSetLite unknownFields, + Registers registers) + throws InvalidProtocolBufferException { + if (WireFormat.getTagFieldNumber(tag) == 0) { + throw InvalidProtocolBufferException.invalidTag(); + } + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + position = decodeVarint64(data, position, registers); + unknownFields.storeField(tag, registers.long1); + return position; + case WireFormat.WIRETYPE_FIXED32: + unknownFields.storeField(tag, decodeFixed32(data, position)); + return position + 4; + case WireFormat.WIRETYPE_FIXED64: + unknownFields.storeField(tag, decodeFixed64(data, position)); + return position + 8; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0) { + throw InvalidProtocolBufferException.negativeSize(); + } else if (length > data.length - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } else if (length == 0) { + unknownFields.storeField(tag, ByteString.EMPTY); + } else { + unknownFields.storeField(tag, ByteString.copyFrom(data, position, length)); + } + return position + length; + case WireFormat.WIRETYPE_START_GROUP: + final UnknownFieldSetLite child = UnknownFieldSetLite.newInstance(); + final int endGroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + int lastTag = 0; + while (position < limit) { + position = decodeVarint32(data, position, registers); + lastTag = registers.int1; + if (lastTag == endGroup) { + break; + } + position = decodeUnknownField(lastTag, data, position, limit, child, registers); + } + if (position > limit || lastTag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + unknownFields.storeField(tag, child); + return position; + default: + throw InvalidProtocolBufferException.invalidTag(); + } + } + + /** Skips an unknown field. */ + static int skipField(int tag, byte[] data, int position, int limit, Registers registers) + throws InvalidProtocolBufferException { + if (WireFormat.getTagFieldNumber(tag) == 0) { + throw InvalidProtocolBufferException.invalidTag(); + } + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + position = decodeVarint64(data, position, registers); + return position; + case WireFormat.WIRETYPE_FIXED32: + return position + 4; + case WireFormat.WIRETYPE_FIXED64: + return position + 8; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + position = decodeVarint32(data, position, registers); + return position + registers.int1; + case WireFormat.WIRETYPE_START_GROUP: + final int endGroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + int lastTag = 0; + while (position < limit) { + position = decodeVarint32(data, position, registers); + lastTag = registers.int1; + if (lastTag == endGroup) { + break; + } + position = skipField(lastTag, data, position, limit, registers); + } + if (position > limit || lastTag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + return position; + default: + throw InvalidProtocolBufferException.invalidTag(); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BinaryReader.java b/java/core/src/main/java/com/google/protobuf/BinaryReader.java new file mode 100755 index 0000000000..d64574c2a5 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BinaryReader.java @@ -0,0 +1,1729 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +/** + * A {@link Reader} that reads from a buffer containing a message serialized with the binary + * protocol. + */ +@ExperimentalApi +abstract class BinaryReader implements Reader { + private static final int FIXED32_MULTIPLE_MASK = FIXED32_SIZE - 1; + private static final int FIXED64_MULTIPLE_MASK = FIXED64_SIZE - 1; + + /** + * Creates a new reader using the given {@code buffer} as input. + * + * @param buffer the input buffer. The buffer (including position, limit, etc.) will not be + * modified. To increment the buffer position after the read completes, use the value returned + * by {@link #getTotalBytesRead()}. + * @param bufferIsImmutable if {@code true} the reader assumes that the content of {@code buffer} + * will never change and any allocated {@link ByteString} instances will by directly wrap + * slices of {@code buffer}. + * @return the reader + */ + public static BinaryReader newInstance(ByteBuffer buffer, boolean bufferIsImmutable) { + if (buffer.hasArray()) { + // TODO(nathanmittler): Add support for unsafe operations. + return new SafeHeapReader(buffer, bufferIsImmutable); + } + // TODO(nathanmittler): Add support for direct buffers + throw new IllegalArgumentException("Direct buffers not yet supported"); + } + + /** Only allow subclassing for inner classes. */ + private BinaryReader() {} + + /** Returns the total number of bytes read so far from the input buffer. */ + public abstract int getTotalBytesRead(); + + @Override + public boolean shouldDiscardUnknownFields() { + return false; + } + + /** + * A {@link BinaryReader} implementation that operates on a heap {@link ByteBuffer}. Uses only + * safe operations on the underlying array. + */ + private static final class SafeHeapReader extends BinaryReader { + private final boolean bufferIsImmutable; + private final byte[] buffer; + private int pos; + private final int initialPos; + private int limit; + private int tag; + private int endGroupTag; + + public SafeHeapReader(ByteBuffer bytebuf, boolean bufferIsImmutable) { + this.bufferIsImmutable = bufferIsImmutable; + buffer = bytebuf.array(); + initialPos = pos = bytebuf.arrayOffset() + bytebuf.position(); + limit = bytebuf.arrayOffset() + bytebuf.limit(); + } + + private boolean isAtEnd() { + return pos == limit; + } + + @Override + public int getTotalBytesRead() { + return pos - initialPos; + } + + @Override + public int getFieldNumber() throws IOException { + if (isAtEnd()) { + return Reader.READ_DONE; + } + tag = readVarint32(); + if (tag == endGroupTag) { + return Reader.READ_DONE; + } + return WireFormat.getTagFieldNumber(tag); + } + + @Override + public int getTag() { + return tag; + } + + @Override + public boolean skipField() throws IOException { + if (isAtEnd() || tag == endGroupTag) { + return false; + } + + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_VARINT: + skipVarint(); + return true; + case WIRETYPE_FIXED64: + skipBytes(FIXED64_SIZE); + return true; + case WIRETYPE_LENGTH_DELIMITED: + skipBytes(readVarint32()); + return true; + case WIRETYPE_FIXED32: + skipBytes(FIXED32_SIZE); + return true; + case WIRETYPE_START_GROUP: + skipGroup(); + return true; + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + @Override + public double readDouble() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return Double.longBitsToDouble(readLittleEndian64()); + } + + @Override + public float readFloat() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return Float.intBitsToFloat(readLittleEndian32()); + } + + @Override + public long readUInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint64(); + } + + @Override + public long readInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint64(); + } + + @Override + public int readInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public long readFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return readLittleEndian64(); + } + + @Override + public int readFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return readLittleEndian32(); + } + + @Override + public boolean readBool() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32() != 0; + } + + @Override + public String readString() throws IOException { + return readStringInternal(false); + } + + @Override + public String readStringRequireUtf8() throws IOException { + return readStringInternal(true); + } + + public String readStringInternal(boolean requireUtf8) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + final int size = readVarint32(); + if (size == 0) { + return ""; + } + + requireBytes(size); + if (requireUtf8 && !Utf8.isValidUtf8(buffer, pos, pos + size)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + String result = new String(buffer, pos, size, Internal.UTF_8); + pos += size; + return result; + } + + @Override + public <T> T readMessage(Class<T> clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @Override + public <T> T readMessageBySchemaWithCheck( + Schema<T> schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(schema, extensionRegistry); + } + + private <T> T readMessage(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int size = readVarint32(); + requireBytes(size); + + // Update the limit. + int prevLimit = limit; + int newLimit = pos + size; + limit = newLimit; + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (pos != newLimit) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the limit. + limit = prevLimit; + } + } + + @Override + public <T> T readGroup(Class<T> clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @Override + public <T> T readGroupBySchemaWithCheck( + Schema<T> schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(schema, extensionRegistry); + } + + private <T> T readGroup(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the old end group tag. + endGroupTag = prevEndGroupTag; + } + } + + @Override + public ByteString readBytes() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = readVarint32(); + if (size == 0) { + return ByteString.EMPTY; + } + + requireBytes(size); + ByteString bytes = + bufferIsImmutable + ? ByteString.wrap(buffer, pos, size) + : ByteString.copyFrom(buffer, pos, size); + pos += size; + return bytes; + } + + @Override + public int readUInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public int readEnum() throws IOException { + requireWireType(WIRETYPE_VARINT); + return readVarint32(); + } + + @Override + public int readSFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return readLittleEndian32(); + } + + @Override + public long readSFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return readLittleEndian64(); + } + + @Override + public int readSInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return CodedInputStream.decodeZigZag32(readVarint32()); + } + + @Override + public long readSInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return CodedInputStream.decodeZigZag64(readVarint64()); + } + + @Override + public void readDoubleList(List<Double> target) throws IOException { + if (target instanceof DoubleArrayList) { + DoubleArrayList plist = (DoubleArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addDouble(Double.longBitsToDouble(readLittleEndian64_NoCheck())); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addDouble(readDouble()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(Double.longBitsToDouble(readLittleEndian64_NoCheck())); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readDouble()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFloatList(List<Float> target) throws IOException { + if (target instanceof FloatArrayList) { + FloatArrayList plist = (FloatArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addFloat(Float.intBitsToFloat(readLittleEndian32_NoCheck())); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addFloat(readFloat()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(Float.intBitsToFloat(readLittleEndian32_NoCheck())); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readFloat()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readUInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readUInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readUInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint64()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(readFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(readFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readBoolList(List<Boolean> target) throws IOException { + if (target instanceof BooleanArrayList) { + BooleanArrayList plist = (BooleanArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addBoolean(readVarint32() != 0); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addBoolean(readBool()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32() != 0); + } + requirePosition(fieldEndPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readBool()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readStringList(List<String> target) throws IOException { + readStringListInternal(target, false); + } + + @Override + public void readStringListRequireUtf8(List<String> target) throws IOException { + readStringListInternal(target, true); + } + + public void readStringListInternal(List<String> target, boolean requireUtf8) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + if (target instanceof LazyStringList && !requireUtf8) { + LazyStringList lazyList = (LazyStringList) target; + while (true) { + lazyList.add(readBytes()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } else { + while (true) { + target.add(readStringInternal(requireUtf8)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + } + + @Override + public <T> void readMessageList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType); + readMessageList(target, schema, extensionRegistry); + } + + @Override + public <T> void readMessageList( + List<T> target, Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readMessage(schema, extensionRegistry)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public <T> void readGroupList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType); + readGroupList(target, schema, extensionRegistry); + } + + @Override + public <T> void readGroupList( + List<T> target, Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_START_GROUP) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readGroup(schema, extensionRegistry)); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public void readBytesList(List<ByteString> target) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + while (true) { + target.add(readBytes()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + } + + @Override + public void readUInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readUInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readUInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readEnumList(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readEnum()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readVarint32()); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readEnum()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(readSFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed32Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian32_NoCheck()); + } + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(readSFixed32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(readSFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + verifyPackedFixed64Length(bytes); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(readLittleEndian64_NoCheck()); + } + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(readSFixed64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addInt(CodedInputStream.decodeZigZag32(readVarint32())); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(readSInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(CodedInputStream.decodeZigZag32(readVarint32())); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readSInt32()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + plist.addLong(CodedInputStream.decodeZigZag64(readVarint64())); + } + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(readSInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = readVarint32(); + final int fieldEndPos = pos + bytes; + while (pos < fieldEndPos) { + target.add(CodedInputStream.decodeZigZag64(readVarint64())); + } + break; + case WIRETYPE_VARINT: + while (true) { + target.add(readSInt64()); + + if (isAtEnd()) { + return; + } + int prevPos = pos; + int nextTag = readVarint32(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Rewind the buffer position to before + // the new tag. + pos = prevPos; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @SuppressWarnings("unchecked") + @Override + public <K, V> void readMap( + Map<K, V> target, + MapEntryLite.Metadata<K, V> metadata, + ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = readVarint32(); + requireBytes(size); + + // Update the limit. + int prevLimit = limit; + int newLimit = pos + size; + limit = newLimit; + + try { + K key = metadata.defaultKey; + V value = metadata.defaultValue; + while (true) { + int number = getFieldNumber(); + if (number == READ_DONE) { + break; + } + try { + switch (number) { + case 1: + key = (K) readField(metadata.keyType, null, null); + break; + case 2: + value = + (V) + readField( + metadata.valueType, + metadata.defaultValue.getClass(), + extensionRegistry); + break; + default: + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException ignore) { + // the type doesn't match, skip the field. + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + } + } + target.put(key, value); + } finally { + // Restore the limit. + limit = prevLimit; + } + } + + private Object readField( + WireFormat.FieldType fieldType, + Class<?> messageType, + ExtensionRegistryLite extensionRegistry) + throws IOException { + switch (fieldType) { + case BOOL: + return readBool(); + case BYTES: + return readBytes(); + case DOUBLE: + return readDouble(); + case ENUM: + return readEnum(); + case FIXED32: + return readFixed32(); + case FIXED64: + return readFixed64(); + case FLOAT: + return readFloat(); + case INT32: + return readInt32(); + case INT64: + return readInt64(); + case MESSAGE: + return readMessage(messageType, extensionRegistry); + case SFIXED32: + return readSFixed32(); + case SFIXED64: + return readSFixed64(); + case SINT32: + return readSInt32(); + case SINT64: + return readSInt64(); + case STRING: + return readStringRequireUtf8(); + case UINT32: + return readUInt32(); + case UINT64: + return readUInt64(); + default: + throw new RuntimeException("unsupported field type."); + } + } + + /** Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits. */ + private int readVarint32() throws IOException { + // See implementation notes for readRawVarint64 + int i = pos; + + if (limit == pos) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + + int x; + if ((x = buffer[i++]) >= 0) { + pos = i; + return x; + } else if (limit - i < 9) { + return (int) readVarint64SlowPath(); + } else if ((x ^= (buffer[i++] << 7)) < 0) { + x ^= (~0 << 7); + } else if ((x ^= (buffer[i++] << 14)) >= 0) { + x ^= (~0 << 7) ^ (~0 << 14); + } else if ((x ^= (buffer[i++] << 21)) < 0) { + x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21); + } else { + int y = buffer[i++]; + x ^= y << 28; + x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28); + if (y < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0 + && buffer[i++] < 0) { + throw InvalidProtocolBufferException.malformedVarint(); + } + } + pos = i; + return x; + } + + public long readVarint64() throws IOException { + // Implementation notes: + // + // Optimized for one-byte values, expected to be common. + // The particular code below was selected from various candidates + // empirically, by winning VarintBenchmark. + // + // Sign extension of (signed) Java bytes is usually a nuisance, but + // we exploit it here to more easily obtain the sign of bytes read. + // Instead of cleaning up the sign extension bits by masking eagerly, + // we delay until we find the final (positive) byte, when we clear all + // accumulated bits with one xor. We depend on javac to constant fold. + int i = pos; + + if (limit == i) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + + final byte[] buffer = this.buffer; + long x; + int y; + if ((y = buffer[i++]) >= 0) { + pos = i; + return y; + } else if (limit - i < 9) { + return readVarint64SlowPath(); + } else if ((y ^= (buffer[i++] << 7)) < 0) { + x = y ^ (~0 << 7); + } else if ((y ^= (buffer[i++] << 14)) >= 0) { + x = y ^ ((~0 << 7) ^ (~0 << 14)); + } else if ((y ^= (buffer[i++] << 21)) < 0) { + x = y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21)); + } else if ((x = y ^ ((long) buffer[i++] << 28)) >= 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28); + } else if ((x ^= ((long) buffer[i++] << 35)) < 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35); + } else if ((x ^= ((long) buffer[i++] << 42)) >= 0L) { + x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35) ^ (~0L << 42); + } else if ((x ^= ((long) buffer[i++] << 49)) < 0L) { + x ^= + (~0L << 7) + ^ (~0L << 14) + ^ (~0L << 21) + ^ (~0L << 28) + ^ (~0L << 35) + ^ (~0L << 42) + ^ (~0L << 49); + } else { + x ^= ((long) buffer[i++] << 56); + x ^= + (~0L << 7) + ^ (~0L << 14) + ^ (~0L << 21) + ^ (~0L << 28) + ^ (~0L << 35) + ^ (~0L << 42) + ^ (~0L << 49) + ^ (~0L << 56); + if (x < 0L) { + if (buffer[i++] < 0L) { + throw InvalidProtocolBufferException.malformedVarint(); + } + } + } + pos = i; + return x; + } + + private long readVarint64SlowPath() throws IOException { + long result = 0; + for (int shift = 0; shift < 64; shift += 7) { + final byte b = readByte(); + result |= (long) (b & 0x7F) << shift; + if ((b & 0x80) == 0) { + return result; + } + } + throw InvalidProtocolBufferException.malformedVarint(); + } + + private byte readByte() throws IOException { + if (pos == limit) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + return buffer[pos++]; + } + + private int readLittleEndian32() throws IOException { + requireBytes(FIXED32_SIZE); + return readLittleEndian32_NoCheck(); + } + + private long readLittleEndian64() throws IOException { + requireBytes(FIXED64_SIZE); + return readLittleEndian64_NoCheck(); + } + + private int readLittleEndian32_NoCheck() { + int p = pos; + final byte[] buffer = this.buffer; + pos = p + FIXED32_SIZE; + return (((buffer[p] & 0xff)) + | ((buffer[p + 1] & 0xff) << 8) + | ((buffer[p + 2] & 0xff) << 16) + | ((buffer[p + 3] & 0xff) << 24)); + } + + private long readLittleEndian64_NoCheck() { + int p = pos; + final byte[] buffer = this.buffer; + pos = p + FIXED64_SIZE; + return (((buffer[p] & 0xffL)) + | ((buffer[p + 1] & 0xffL) << 8) + | ((buffer[p + 2] & 0xffL) << 16) + | ((buffer[p + 3] & 0xffL) << 24) + | ((buffer[p + 4] & 0xffL) << 32) + | ((buffer[p + 5] & 0xffL) << 40) + | ((buffer[p + 6] & 0xffL) << 48) + | ((buffer[p + 7] & 0xffL) << 56)); + } + + private void skipVarint() throws IOException { + if (limit - pos >= 10) { + final byte[] buffer = this.buffer; + int p = pos; + for (int i = 0; i < 10; i++) { + if (buffer[p++] >= 0) { + pos = p; + return; + } + } + } + skipVarintSlowPath(); + } + + private void skipVarintSlowPath() throws IOException { + for (int i = 0; i < 10; i++) { + if (readByte() >= 0) { + return; + } + } + throw InvalidProtocolBufferException.malformedVarint(); + } + + private void skipBytes(final int size) throws IOException { + requireBytes(size); + + pos += size; + } + + private void skipGroup() throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + while (true) { + if (getFieldNumber() == READ_DONE || !skipField()) { + break; + } + } + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + endGroupTag = prevEndGroupTag; + } + + private void requireBytes(int size) throws IOException { + if (size < 0 || size > (limit - pos)) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } + + private void requireWireType(int requiredWireType) throws IOException { + if (WireFormat.getTagWireType(tag) != requiredWireType) { + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + private void verifyPackedFixed64Length(int bytes) throws IOException { + requireBytes(bytes); + if ((bytes & FIXED64_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 8. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void verifyPackedFixed32Length(int bytes) throws IOException { + requireBytes(bytes); + if ((bytes & FIXED32_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 4. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void requirePosition(int expectedPosition) throws IOException { + if (pos != expectedPosition) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BinaryWriter.java b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java new file mode 100755 index 0000000000..b1dbb8bf47 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BinaryWriter.java @@ -0,0 +1,3071 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.MAX_VARINT32_SIZE; +import static com.google.protobuf.WireFormat.MAX_VARINT64_SIZE; +import static com.google.protobuf.WireFormat.MESSAGE_SET_ITEM; +import static com.google.protobuf.WireFormat.MESSAGE_SET_MESSAGE; +import static com.google.protobuf.WireFormat.MESSAGE_SET_TYPE_ID; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayDeque; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +/** + * A protobuf writer that serializes messages in their binary form. Messages are serialized in + * reverse in order to avoid calculating the serialized size of each nested message. Since the + * message size is not known in advance, the writer employs a strategy of chunking and buffer + * chaining. Buffers are allocated as-needed by a provided {@link BufferAllocator}. Once writing is + * finished, the application can access the buffers in forward-writing order by calling {@link + * #complete()}. + * + * <p>Once {@link #complete()} has been called, the writer can not be reused for additional writes. + * The {@link #getTotalBytesWritten()} will continue to reflect the total of the write and will not + * be reset. + */ +@ExperimentalApi +abstract class BinaryWriter extends ByteOutput implements Writer { + public static final int DEFAULT_CHUNK_SIZE = 4096; + + private final BufferAllocator alloc; + private final int chunkSize; + + final ArrayDeque<AllocatedBuffer> buffers = new ArrayDeque<AllocatedBuffer>(4); + int totalDoneBytes; + + /** + * Creates a new {@link BinaryWriter} that will allocate heap buffers of {@link + * #DEFAULT_CHUNK_SIZE} as necessary. + */ + public static BinaryWriter newHeapInstance(BufferAllocator alloc) { + return newHeapInstance(alloc, DEFAULT_CHUNK_SIZE); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate heap buffers of {@code chunkSize} as + * necessary. + */ + public static BinaryWriter newHeapInstance(BufferAllocator alloc, int chunkSize) { + return isUnsafeHeapSupported() + ? newUnsafeHeapInstance(alloc, chunkSize) + : newSafeHeapInstance(alloc, chunkSize); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate direct (i.e. non-heap) buffers of {@link + * #DEFAULT_CHUNK_SIZE} as necessary. + */ + public static BinaryWriter newDirectInstance(BufferAllocator alloc) { + return newDirectInstance(alloc, DEFAULT_CHUNK_SIZE); + } + + /** + * Creates a new {@link BinaryWriter} that will allocate direct (i.e. non-heap) buffers of {@code + * chunkSize} as necessary. + */ + public static BinaryWriter newDirectInstance(BufferAllocator alloc, int chunkSize) { + return isUnsafeDirectSupported() + ? newUnsafeDirectInstance(alloc, chunkSize) + : newSafeDirectInstance(alloc, chunkSize); + } + + static boolean isUnsafeHeapSupported() { + return UnsafeHeapWriter.isSupported(); + } + + static boolean isUnsafeDirectSupported() { + return UnsafeDirectWriter.isSupported(); + } + + static BinaryWriter newSafeHeapInstance(BufferAllocator alloc, int chunkSize) { + return new SafeHeapWriter(alloc, chunkSize); + } + + static BinaryWriter newUnsafeHeapInstance(BufferAllocator alloc, int chunkSize) { + if (!isUnsafeHeapSupported()) { + throw new UnsupportedOperationException("Unsafe operations not supported"); + } + return new UnsafeHeapWriter(alloc, chunkSize); + } + + static BinaryWriter newSafeDirectInstance(BufferAllocator alloc, int chunkSize) { + return new SafeDirectWriter(alloc, chunkSize); + } + + static BinaryWriter newUnsafeDirectInstance(BufferAllocator alloc, int chunkSize) { + if (!isUnsafeDirectSupported()) { + throw new UnsupportedOperationException("Unsafe operations not supported"); + } + return new UnsafeDirectWriter(alloc, chunkSize); + } + + /** Only allow subclassing for inner classes. */ + private BinaryWriter(BufferAllocator alloc, int chunkSize) { + if (chunkSize <= 0) { + throw new IllegalArgumentException("chunkSize must be > 0"); + } + this.alloc = checkNotNull(alloc, "alloc"); + this.chunkSize = chunkSize; + } + + @Override + public final FieldOrder fieldOrder() { + return FieldOrder.DESCENDING; + } + + /** + * Completes the write operation and returns a queue of {@link AllocatedBuffer} objects in + * forward-writing order. This method should only be called once. + * + * <p>After calling this method, the writer can not be reused. Create a new writer for future + * writes. + */ + public final Queue<AllocatedBuffer> complete() { + finishCurrentBuffer(); + return buffers; + } + + @Override + public final void writeSFixed32(int fieldNumber, int value) throws IOException { + writeFixed32(fieldNumber, value); + } + + @Override + public final void writeInt64(int fieldNumber, long value) throws IOException { + writeUInt64(fieldNumber, value); + } + + @Override + public final void writeSFixed64(int fieldNumber, long value) throws IOException { + writeFixed64(fieldNumber, value); + } + + @Override + public final void writeFloat(int fieldNumber, float value) throws IOException { + writeFixed32(fieldNumber, Float.floatToRawIntBits(value)); + } + + @Override + public final void writeDouble(int fieldNumber, double value) throws IOException { + writeFixed64(fieldNumber, Double.doubleToRawLongBits(value)); + } + + @Override + public final void writeEnum(int fieldNumber, int value) throws IOException { + writeInt32(fieldNumber, value); + } + + @Override + public final void writeInt32List(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeFixed32List(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeFixed32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeFixed32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeFixed32List_Internal(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(fieldNumber, list.get(i)); + } + } + } + + private final void writeFixed32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeInt64List(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + writeUInt64List(fieldNumber, list, packed); + } + + @Override + public final void writeUInt64List(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeUInt64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeUInt64List_Internal(fieldNumber, list, packed); + } + } + + private final void writeUInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt64(fieldNumber, list.get(i)); + } + } + } + + private final void writeUInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeFixed64List(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeFixed64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeFixed64List_Internal(fieldNumber, list, packed); + } + } + + private final void writeFixed64List_Internal(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(fieldNumber, list.get(i)); + } + } + } + + private final void writeFixed64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeFloatList(int fieldNumber, List<Float> list, boolean packed) + throws IOException { + if (list instanceof FloatArrayList) { + writeFloatList_Internal(fieldNumber, (FloatArrayList) list, packed); + } else { + writeFloatList_Internal(fieldNumber, list, packed); + } + } + + private final void writeFloatList_Internal(int fieldNumber, List<Float> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(Float.floatToRawIntBits(list.get(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFloat(fieldNumber, list.get(i)); + } + } + } + + private final void writeFloatList_Internal(int fieldNumber, FloatArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed32(Float.floatToRawIntBits(list.getFloat(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeFloat(fieldNumber, list.getFloat(i)); + } + } + } + + @Override + public final void writeDoubleList(int fieldNumber, List<Double> list, boolean packed) + throws IOException { + if (list instanceof DoubleArrayList) { + writeDoubleList_Internal(fieldNumber, (DoubleArrayList) list, packed); + } else { + writeDoubleList_Internal(fieldNumber, list, packed); + } + } + + private final void writeDoubleList_Internal(int fieldNumber, List<Double> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(Double.doubleToRawLongBits(list.get(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeDouble(fieldNumber, list.get(i)); + } + } + } + + private final void writeDoubleList_Internal(int fieldNumber, DoubleArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * FIXED64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeFixed64(Double.doubleToRawLongBits(list.getDouble(i))); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeDouble(fieldNumber, list.getDouble(i)); + } + } + } + + @Override + public final void writeEnumList(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + writeInt32List(fieldNumber, list, packed); + } + + @Override + public final void writeBoolList(int fieldNumber, List<Boolean> list, boolean packed) + throws IOException { + if (list instanceof BooleanArrayList) { + writeBoolList_Internal(fieldNumber, (BooleanArrayList) list, packed); + } else { + writeBoolList_Internal(fieldNumber, list, packed); + } + } + + private final void writeBoolList_Internal(int fieldNumber, List<Boolean> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + list.size()); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(fieldNumber, list.get(i)); + } + } + } + + private final void writeBoolList_Internal(int fieldNumber, BooleanArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + list.size()); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(list.getBoolean(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeBool(fieldNumber, list.getBoolean(i)); + } + } + } + + @Override + public final void writeStringList(int fieldNumber, List<String> list) throws IOException { + if (list instanceof LazyStringList) { + final LazyStringList lazyList = (LazyStringList) list; + for (int i = list.size() - 1; i >= 0; i--) { + writeLazyString(fieldNumber, lazyList.getRaw(i)); + } + } else { + for (int i = list.size() - 1; i >= 0; i--) { + writeString(fieldNumber, list.get(i)); + } + } + } + + private void writeLazyString(int fieldNumber, Object value) throws IOException { + if (value instanceof String) { + writeString(fieldNumber, (String) value); + } else { + writeBytes(fieldNumber, (ByteString) value); + } + } + + @Override + public final void writeBytesList(int fieldNumber, List<ByteString> list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeBytes(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeUInt32List(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeUInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeUInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeUInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeUInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeVarint32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeUInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeSFixed32List(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + writeFixed32List(fieldNumber, list, packed); + } + + @Override + public final void writeSFixed64List(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + writeFixed64List(fieldNumber, list, packed); + } + + @Override + public final void writeSInt32List(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (list instanceof IntArrayList) { + writeSInt32List_Internal(fieldNumber, (IntArrayList) list, packed); + } else { + writeSInt32List_Internal(fieldNumber, list, packed); + } + } + + private final void writeSInt32List_Internal(int fieldNumber, List<Integer> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(fieldNumber, list.get(i)); + } + } + } + + private final void writeSInt32List_Internal(int fieldNumber, IntArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT32_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(list.getInt(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt32(fieldNumber, list.getInt(i)); + } + } + } + + @Override + public final void writeSInt64List(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (list instanceof LongArrayList) { + writeSInt64List_Internal(fieldNumber, (LongArrayList) list, packed); + } else { + writeSInt64List_Internal(fieldNumber, list, packed); + } + } + + private static final int MAP_KEY_NUMBER = 1; + private static final int MAP_VALUE_NUMBER = 2; + + @Override + public <K, V> void writeMap(int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, V> map) + throws IOException { + // TODO(liujisi): Reverse write those entries. + for (Map.Entry<K, V> entry : map.entrySet()) { + int prevBytes = getTotalBytesWritten(); + writeMapEntryField(this, MAP_VALUE_NUMBER, metadata.valueType, entry.getValue()); + writeMapEntryField(this, MAP_KEY_NUMBER, metadata.keyType, entry.getKey()); + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + } + + static final void writeMapEntryField( + Writer writer, int fieldNumber, WireFormat.FieldType fieldType, Object object) + throws IOException { + switch (fieldType) { + case BOOL: + writer.writeBool(fieldNumber, (Boolean) object); + break; + case FIXED32: + writer.writeFixed32(fieldNumber, (Integer) object); + break; + case FIXED64: + writer.writeFixed64(fieldNumber, (Long) object); + break; + case INT32: + writer.writeInt32(fieldNumber, (Integer) object); + break; + case INT64: + writer.writeInt64(fieldNumber, (Long) object); + break; + case SFIXED32: + writer.writeSFixed32(fieldNumber, (Integer) object); + break; + case SFIXED64: + writer.writeSFixed64(fieldNumber, (Long) object); + break; + case SINT32: + writer.writeSInt32(fieldNumber, (Integer) object); + break; + case SINT64: + writer.writeSInt64(fieldNumber, (Long) object); + break; + case STRING: + writer.writeString(fieldNumber, (String) object); + break; + case UINT32: + writer.writeUInt32(fieldNumber, (Integer) object); + break; + case UINT64: + writer.writeUInt64(fieldNumber, (Long) object); + break; + case FLOAT: + writer.writeFloat(fieldNumber, (Float) object); + break; + case DOUBLE: + writer.writeDouble(fieldNumber, (Double) object); + break; + case MESSAGE: + writer.writeMessage(fieldNumber, object); + break; + case BYTES: + writer.writeBytes(fieldNumber, (ByteString) object); + break; + case ENUM: + if (object instanceof Internal.EnumLite) { + writer.writeEnum(fieldNumber, ((Internal.EnumLite) object).getNumber()); + } else if (object instanceof Integer) { + writer.writeEnum(fieldNumber, (Integer) object); + } else { + throw new IllegalArgumentException("Unexpected type for enum in map."); + } + break; + default: + throw new IllegalArgumentException("Unsupported map value type for: " + fieldType); + } + } + + private final void writeSInt64List_Internal(int fieldNumber, List<Long> list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(list.get(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(fieldNumber, list.get(i)); + } + } + } + + private final void writeSInt64List_Internal(int fieldNumber, LongArrayList list, boolean packed) + throws IOException { + if (packed) { + requireSpace((MAX_VARINT32_SIZE * 2) + (list.size() * MAX_VARINT64_SIZE)); + int prevBytes = getTotalBytesWritten(); + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(list.getLong(i)); + } + int length = getTotalBytesWritten() - prevBytes; + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } else { + for (int i = list.size() - 1; i >= 0; --i) { + writeSInt64(fieldNumber, list.getLong(i)); + } + } + } + + @Override + public final void writeMessageList(int fieldNumber, List<?> list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeMessage(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeMessageList(int fieldNumber, List<?> list, Schema schema) + throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeMessage(fieldNumber, list.get(i), schema); + } + } + + @Override + public final void writeGroupList(int fieldNumber, List<?> list) throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeGroup(fieldNumber, list.get(i)); + } + } + + @Override + public final void writeGroupList(int fieldNumber, List<?> list, Schema schema) + throws IOException { + for (int i = list.size() - 1; i >= 0; i--) { + writeGroup(fieldNumber, list.get(i), schema); + } + } + + @Override + public final void writeMessageSetItem(int fieldNumber, Object value) throws IOException { + writeTag(MESSAGE_SET_ITEM, WIRETYPE_END_GROUP); + if (value instanceof ByteString) { + writeBytes(MESSAGE_SET_MESSAGE, (ByteString) value); + } else { + writeMessage(MESSAGE_SET_MESSAGE, value); + } + writeUInt32(MESSAGE_SET_TYPE_ID, fieldNumber); + writeTag(MESSAGE_SET_ITEM, WIRETYPE_START_GROUP); + } + + final AllocatedBuffer newHeapBuffer() { + return alloc.allocateHeapBuffer(chunkSize); + } + + final AllocatedBuffer newHeapBuffer(int capacity) { + return alloc.allocateHeapBuffer(Math.max(capacity, chunkSize)); + } + + final AllocatedBuffer newDirectBuffer() { + return alloc.allocateDirectBuffer(chunkSize); + } + + final AllocatedBuffer newDirectBuffer(int capacity) { + return alloc.allocateDirectBuffer(Math.max(capacity, chunkSize)); + } + + /** + * Gets the total number of bytes that have been written. This will not be reset by a call to + * {@link #complete()}. + */ + public abstract int getTotalBytesWritten(); + + abstract void requireSpace(int size); + + abstract void finishCurrentBuffer(); + + abstract void writeTag(int fieldNumber, int wireType); + + abstract void writeVarint32(int value); + + abstract void writeInt32(int value); + + abstract void writeSInt32(int value); + + abstract void writeFixed32(int value); + + abstract void writeVarint64(long value); + + abstract void writeSInt64(long value); + + abstract void writeFixed64(long value); + + abstract void writeBool(boolean value); + + abstract void writeString(String in); + + /** + * Not using the version in CodedOutputStream due to the fact that benchmarks have shown a + * performance improvement when returning a byte (rather than an int). + */ + private static byte computeUInt64SizeNoTag(long value) { + // handle two popular special cases up front ... + if ((value & (~0L << 7)) == 0L) { + // Byte 1 + return 1; + } + if (value < 0L) { + // Byte 10 + return 10; + } + // ... leaving us with 8 remaining, which we can divide and conquer + byte n = 2; + if ((value & (~0L << 35)) != 0L) { + // Byte 6-9 + n += 4; // + (value >>> 63); + value >>>= 28; + } + if ((value & (~0L << 21)) != 0L) { + // Byte 4-5 or 8-9 + n += 2; + value >>>= 14; + } + if ((value & (~0L << 14)) != 0L) { + // Byte 3 or 7 + n += 1; + } + return n; + } + + /** Writer that uses safe operations on target array. */ + private static final class SafeHeapWriter extends BinaryWriter { + private AllocatedBuffer allocatedBuffer; + private byte[] buffer; + private int offset; + private int limit; + private int offsetMinusOne; + private int limitMinusOne; + private int pos; + + SafeHeapWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + @Override + void finishCurrentBuffer() { + if (allocatedBuffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + allocatedBuffer.position((pos - allocatedBuffer.arrayOffset()) + 1); + allocatedBuffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private void nextBuffer() { + nextBuffer(newHeapBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newHeapBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasArray()) { + throw new RuntimeException("Allocator returned non-heap buffer"); + } + + finishCurrentBuffer(); + + buffers.addFirst(allocatedBuffer); + + this.allocatedBuffer = allocatedBuffer; + this.buffer = allocatedBuffer.array(); + int arrayOffset = allocatedBuffer.arrayOffset(); + this.limit = arrayOffset + allocatedBuffer.limit(); + this.offset = arrayOffset + allocatedBuffer.position(); + this.offsetMinusOne = offset - 1; + this.limitMinusOne = limit - 1; + this.pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + int bytesWrittenToCurrentBuffer() { + return limitMinusOne - pos; + } + + int spaceLeft() { + return pos - offsetMinusOne; + } + + @Override + public void writeUInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) throws IOException { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) throws IOException { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) throws IOException { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + buffer[pos--] = (byte) value; + } + + private void writeVarint32TwoBytes(int value) { + buffer[pos--] = (byte) (value >>> 7); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32ThreeBytes(int value) { + buffer[pos--] = (byte) (value >>> 14); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32FourBytes(int value) { + buffer[pos--] = (byte) (value >>> 21); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint32FiveBytes(int value) { + buffer[pos--] = (byte) (value >>> 28); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + buffer[pos--] = (byte) value; + } + + private void writeVarint64TwoBytes(long value) { + buffer[pos--] = (byte) (value >>> 7); + buffer[pos--] = (byte) (((int) value & 0x7F) | 0x80); + } + + private void writeVarint64ThreeBytes(long value) { + buffer[pos--] = (byte) (((int) value) >>> 14); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64FourBytes(long value) { + buffer[pos--] = (byte) (value >>> 21); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64FiveBytes(long value) { + buffer[pos--] = (byte) (value >>> 28); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64SixBytes(long value) { + buffer[pos--] = (byte) (value >>> 35); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64SevenBytes(long value) { + buffer[pos--] = (byte) (value >>> 42); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64EightBytes(long value) { + buffer[pos--] = (byte) (value >>> 49); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64NineBytes(long value) { + buffer[pos--] = (byte) (value >>> 56); + buffer[pos--] = (byte) (((value >>> 49) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + private void writeVarint64TenBytes(long value) { + buffer[pos--] = (byte) (value >>> 63); + buffer[pos--] = (byte) (((value >>> 56) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 49) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 42) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 35) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 28) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 21) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 14) & 0x7F) | 0x80); + buffer[pos--] = (byte) (((value >>> 7) & 0x7F) | 0x80); + buffer[pos--] = (byte) ((value & 0x7F) | 0x80); + } + + @Override + void writeFixed32(int value) { + buffer[pos--] = (byte) ((value >> 24) & 0xFF); + buffer[pos--] = (byte) ((value >> 16) & 0xFF); + buffer[pos--] = (byte) ((value >> 8) & 0xFF); + buffer[pos--] = (byte) (value & 0xFF); + } + + @Override + void writeFixed64(long value) { + buffer[pos--] = (byte) ((int) (value >> 56) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 48) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 40) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 32) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 24) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 16) & 0xFF); + buffer[pos--] = (byte) ((int) (value >> 8) & 0xFF); + buffer[pos--] = (byte) ((int) (value) & 0xFF); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Set pos to the start of the ASCII string. + pos -= i; + // Designed to take advantage of + // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + buffer[pos + i] = (byte) c; + } + if (i == -1) { + // Move pos past the String. + pos -= 1; + return; + } + pos += i; + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos > offsetMinusOne) { + buffer[pos--] = (byte) c; + } else if (c < 0x800 && pos > offset) { // 11 bits, two UTF-8 bytes + buffer[pos--] = (byte) (0x80 | (0x3F & c)); + buffer[pos--] = (byte) ((0xF << 6) | (c >>> 6)); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > (offset + 1)) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + buffer[pos--] = (byte) (0x80 | (0x3F & c)); + buffer[pos--] = (byte) (0x80 | (0x3F & (c >>> 6))); + buffer[pos--] = (byte) ((0xF << 5) | (c >>> 12)); + } else if (pos > (offset + 2)) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + char high = 0; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + buffer[pos--] = (byte) (0x80 | (0x3F & codePoint)); + buffer[pos--] = (byte) (0x80 | (0x3F & (codePoint >>> 6))); + buffer[pos--] = (byte) (0x80 | (0x3F & (codePoint >>> 12))); + buffer[pos--] = (byte) ((0xF << 4) | (codePoint >>> 18)); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + buffer[pos--] = value; + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + System.arraycopy(value, offset, buffer, pos + 1, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + System.arraycopy(value, offset, buffer, pos + 1, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + value.get(buffer, pos + 1, length); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + } + + pos -= length; + value.get(buffer, pos + 1, length); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses unsafe operations on a target array. */ + private static final class UnsafeHeapWriter extends BinaryWriter { + private AllocatedBuffer allocatedBuffer; + private byte[] buffer; + private long offset; + private long limit; + private long offsetMinusOne; + private long limitMinusOne; + private long pos; + + UnsafeHeapWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + /** Indicates whether the required unsafe operations are supported on this platform. */ + static boolean isSupported() { + return UnsafeUtil.hasUnsafeArrayOperations(); + } + + @Override + void finishCurrentBuffer() { + if (allocatedBuffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + allocatedBuffer.position((arrayPos() - allocatedBuffer.arrayOffset()) + 1); + allocatedBuffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private int arrayPos() { + return (int) pos; + } + + private void nextBuffer() { + nextBuffer(newHeapBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newHeapBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasArray()) { + throw new RuntimeException("Allocator returned non-heap buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + this.allocatedBuffer = allocatedBuffer; + this.buffer = allocatedBuffer.array(); + int arrayOffset = allocatedBuffer.arrayOffset(); + this.limit = arrayOffset + allocatedBuffer.limit(); + this.offset = arrayOffset + allocatedBuffer.position(); + this.offsetMinusOne = offset - 1; + this.limitMinusOne = limit - 1; + this.pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + int bytesWrittenToCurrentBuffer() { + return (int) (limitMinusOne - pos); + } + + int spaceLeft() { + return (int) (pos - offsetMinusOne); + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32ThreeBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 14)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FourBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) value); + } + + private void writeVarint64TwoBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((int) value & 0x7F) | 0x80)); + } + + private void writeVarint64ThreeBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (((int) value) >>> 14)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FourBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FiveBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SixBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 35)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SevenBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 42)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 49)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 56)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64TenBytes(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) (value >>> 63)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeFixed32(int value) { + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 24) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 16) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((value >> 8) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) (value & 0xFF)); + } + + @Override + void writeFixed64(long value) { + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 56) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 48) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 40) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 32) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 24) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 16) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value >> 8) & 0xFF)); + UnsafeUtil.putByte(buffer, pos--, (byte) ((int) (value) & 0xFF)); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Set pos to the start of the ASCII string. + // pos -= i; + // Designed to take advantage of + // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + UnsafeUtil.putByte(buffer, pos--, (byte) c); + } + if (i == -1) { + // Move pos past the String. + return; + } + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos > offsetMinusOne) { + UnsafeUtil.putByte(buffer, pos--, (byte) c); + } else if (c < 0x800 && pos > offset) { // 11 bits, two UTF-8 bytes + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > offset + 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > offset + 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + final char high; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & codePoint))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + UnsafeUtil.putByte(buffer, pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + UnsafeUtil.putByte(buffer, pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + UnsafeUtil.putByte(buffer, pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (offset < 0 || offset + length > value.length) { + throw new ArrayIndexOutOfBoundsException( + String.format("value.length=%d, offset=%d, length=%d", value.length, offset, length)); + } + requireSpace(length); + + pos -= length; + System.arraycopy(value, offset, buffer, arrayPos() + 1, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (offset < 0 || offset + length > value.length) { + throw new ArrayIndexOutOfBoundsException( + String.format("value.length=%d, offset=%d, length=%d", value.length, offset, length)); + } + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + System.arraycopy(value, offset, buffer, arrayPos() + 1, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + requireSpace(length); + + pos -= length; + value.get(buffer, arrayPos() + 1, length); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + } + + pos -= length; + value.get(buffer, arrayPos() + 1, length); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses safe operations on a target {@link ByteBuffer}. */ + private static final class SafeDirectWriter extends BinaryWriter { + private ByteBuffer buffer; + private int limitMinusOne; + private int pos; + + SafeDirectWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + private void nextBuffer() { + nextBuffer(newDirectBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newDirectBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasNioBuffer()) { + throw new RuntimeException("Allocated buffer does not have NIO buffer"); + } + ByteBuffer nioBuffer = allocatedBuffer.nioBuffer(); + if (!nioBuffer.isDirect()) { + throw new RuntimeException("Allocator returned non-direct buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + buffer = nioBuffer; + buffer.limit(buffer.capacity()); + buffer.position(0); + // Set byte order to little endian for fast writing of fixed 32/64. + buffer.order(ByteOrder.LITTLE_ENDIAN); + + limitMinusOne = buffer.limit() - 1; + pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + private int bytesWrittenToCurrentBuffer() { + return limitMinusOne - pos; + } + + private int spaceLeft() { + return pos + 1; + } + + @Override + void finishCurrentBuffer() { + if (buffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + // Update the indices on the netty buffer. + buffer.position(pos + 1); + buffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + buffer.put(pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + // Byte order is little-endian. + pos -= 2; + buffer.putShort(pos + 1, (short) (((value & (0x7F << 7)) << 1) | ((value & 0x7F) | 0x80))); + } + + private void writeVarint32ThreeBytes(int value) { + // Byte order is little-endian. + pos -= 3; + buffer.putInt( + pos, + ((value & (0x7F << 14)) << 10) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 9) + | ((value & 0x7F) | 0x80) << 8); + } + + private void writeVarint32FourBytes(int value) { + // Byte order is little-endian. + pos -= 4; + buffer.putInt( + pos + 1, + ((value & (0x7F << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + // Byte order is little-endian. + buffer.put(pos--, (byte) (value >>> 28)); + pos -= 4; + buffer.putInt( + pos + 1, + ((((value >>> 21) & 0x7F) | 0x80) << 24) + | ((((value >>> 14) & 0x7F) | 0x80) << 16) + | ((((value >>> 7) & 0x7F) | 0x80) << 8) + | ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + writeVarint32OneByte((int) value); + } + + private void writeVarint64TwoBytes(long value) { + writeVarint32TwoBytes((int) value); + } + + private void writeVarint64ThreeBytes(long value) { + writeVarint32ThreeBytes((int) value); + } + + private void writeVarint64FourBytes(long value) { + writeVarint32FourBytes((int) value); + } + + private void writeVarint64FiveBytes(long value) { + // Byte order is little-endian. + pos -= 5; + buffer.putLong( + pos - 2, + ((value & (0x7FL << 28)) << 28) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 27) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 26) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 25) + | (((value & 0x7F) | 0x80)) << 24); + } + + private void writeVarint64SixBytes(long value) { + // Byte order is little-endian. + pos -= 6; + buffer.putLong( + pos - 1, + ((value & (0x7FL << 35)) << 21) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 20) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 19) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 18) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 17) + | (((value & 0x7F) | 0x80)) << 16); + } + + private void writeVarint64SevenBytes(long value) { + // Byte order is little-endian. + pos -= 7; + buffer.putLong( + pos, + ((value & (0x7FL << 42)) << 14) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 13) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 12) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 11) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 10) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 9) + | (((value & 0x7F) | 0x80)) << 8); + } + + private void writeVarint64EightBytes(long value) { + // Byte order is little-endian. + pos -= 8; + buffer.putLong( + pos + 1, + ((value & (0x7FL << 49)) << 7) + | (((value & (0x7FL << 42)) | (0x80L << 42)) << 6) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 5) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 4) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytesWithSign(long value) { + // Byte order is little-endian. + pos -= 8; + buffer.putLong( + pos + 1, + (((value & (0x7FL << 49)) | (0x80L << 49)) << 7) + | (((value & (0x7FL << 42)) | (0x80L << 42)) << 6) + | (((value & (0x7FL << 35)) | (0x80L << 35)) << 5) + | (((value & (0x7FL << 28)) | (0x80L << 28)) << 4) + | (((value & (0x7F << 21)) | (0x80 << 21)) << 3) + | (((value & (0x7F << 14)) | (0x80 << 14)) << 2) + | (((value & (0x7F << 7)) | (0x80 << 7)) << 1) + | ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + buffer.put(pos--, (byte) (value >>> 56)); + writeVarint64EightBytesWithSign(value & 0xFFFFFFFFFFFFFFL); + } + + private void writeVarint64TenBytes(long value) { + buffer.put(pos--, (byte) (value >>> 63)); + buffer.put(pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + writeVarint64EightBytesWithSign(value & 0xFFFFFFFFFFFFFFL); + } + + @Override + void writeFixed32(int value) { + pos -= 4; + buffer.putInt(pos + 1, value); + } + + @Override + void writeFixed64(long value) { + pos -= 8; + buffer.putLong(pos + 1, value); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + pos -= i; + // Designed to take advantage of + // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + buffer.put(pos + i, (byte) c); + } + if (i == -1) { + // Move the position past the ASCII string. + pos -= 1; + return; + } + pos += i; + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos >= 0) { + buffer.put(pos--, (byte) c); + } else if (c < 0x800 && pos > 0) { // 11 bits, two UTF-8 bytes + buffer.put(pos--, (byte) (0x80 | (0x3F & c))); + buffer.put(pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) && pos > 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + buffer.put(pos--, (byte) (0x80 | (0x3F & c))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + buffer.put(pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + char high = 0; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + buffer.put(pos--, (byte) (0x80 | (0x3F & codePoint))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + buffer.put(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + buffer.put(pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + buffer.put(pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value, offset, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value, offset, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(pos + 1); + buffer.put(value); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } + + /** Writer that uses unsafe operations on a target {@link ByteBuffer}. */ + private static final class UnsafeDirectWriter extends BinaryWriter { + private ByteBuffer buffer; + private long bufferOffset; + private long limitMinusOne; + private long pos; + + UnsafeDirectWriter(BufferAllocator alloc, int chunkSize) { + super(alloc, chunkSize); + nextBuffer(); + } + + /** Indicates whether the required unsafe operations are supported on this platform. */ + private static boolean isSupported() { + return UnsafeUtil.hasUnsafeByteBufferOperations(); + } + + private void nextBuffer() { + nextBuffer(newDirectBuffer()); + } + + private void nextBuffer(int capacity) { + nextBuffer(newDirectBuffer(capacity)); + } + + private void nextBuffer(AllocatedBuffer allocatedBuffer) { + if (!allocatedBuffer.hasNioBuffer()) { + throw new RuntimeException("Allocated buffer does not have NIO buffer"); + } + ByteBuffer nioBuffer = allocatedBuffer.nioBuffer(); + if (!nioBuffer.isDirect()) { + throw new RuntimeException("Allocator returned non-direct buffer"); + } + + finishCurrentBuffer(); + buffers.addFirst(allocatedBuffer); + + buffer = nioBuffer; + buffer.limit(buffer.capacity()); + buffer.position(0); + + bufferOffset = UnsafeUtil.addressOffset(buffer); + limitMinusOne = bufferOffset + (buffer.limit() - 1); + pos = limitMinusOne; + } + + @Override + public int getTotalBytesWritten() { + return totalDoneBytes + bytesWrittenToCurrentBuffer(); + } + + private int bytesWrittenToCurrentBuffer() { + return (int) (limitMinusOne - pos); + } + + private int spaceLeft() { + return bufferPos() + 1; + } + + @Override + void finishCurrentBuffer() { + if (buffer != null) { + totalDoneBytes += bytesWrittenToCurrentBuffer(); + // Update the indices on the netty buffer. + buffer.position(bufferPos() + 1); + buffer = null; + pos = 0; + limitMinusOne = 0; + } + } + + private int bufferPos() { + return (int) (pos - bufferOffset); + } + + @Override + public void writeUInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE * 2); + writeSInt32(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed32(int fieldNumber, int value) { + requireSpace(MAX_VARINT32_SIZE + FIXED32_SIZE); + writeFixed32(value); + writeTag(fieldNumber, WIRETYPE_FIXED32); + } + + @Override + public void writeUInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeVarint64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeSInt64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + MAX_VARINT64_SIZE); + writeSInt64(value); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeFixed64(int fieldNumber, long value) { + requireSpace(MAX_VARINT32_SIZE + FIXED64_SIZE); + writeFixed64(value); + writeTag(fieldNumber, WIRETYPE_FIXED64); + } + + @Override + public void writeBool(int fieldNumber, boolean value) { + requireSpace(MAX_VARINT32_SIZE + 1); + write((byte) (value ? 1 : 0)); + writeTag(fieldNumber, WIRETYPE_VARINT); + } + + @Override + public void writeString(int fieldNumber, String value) { + int prevBytes = getTotalBytesWritten(); + writeString(value); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(2 * MAX_VARINT32_SIZE); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) { + try { + value.writeToReverse(this); + } catch (IOException e) { + // Should never happen since the writer does not throw. + throw new RuntimeException(e); + } + + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(value.size()); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + int prevBytes = getTotalBytesWritten(); + Protobuf.getInstance().writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + int prevBytes = getTotalBytesWritten(); + schema.writeTo(value, this); + int length = getTotalBytesWritten() - prevBytes; + requireSpace(MAX_VARINT32_SIZE * 2); + writeVarint32(length); + writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + Protobuf.getInstance().writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + schema.writeTo(value, this); + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeStartGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) { + writeTag(fieldNumber, WIRETYPE_END_GROUP); + } + + @Override + void writeInt32(int value) { + if (value >= 0) { + writeVarint32(value); + } else { + writeVarint64(value); + } + } + + @Override + void writeSInt32(int value) { + writeVarint32(CodedOutputStream.encodeZigZag32(value)); + } + + @Override + void writeSInt64(long value) { + writeVarint64(CodedOutputStream.encodeZigZag64(value)); + } + + @Override + void writeBool(boolean value) { + write((byte) (value ? 1 : 0)); + } + + @Override + void writeTag(int fieldNumber, int wireType) { + writeVarint32(WireFormat.makeTag(fieldNumber, wireType)); + } + + @Override + void writeVarint32(int value) { + if ((value & (~0 << 7)) == 0) { + writeVarint32OneByte(value); + } else if ((value & (~0 << 14)) == 0) { + writeVarint32TwoBytes(value); + } else if ((value & (~0 << 21)) == 0) { + writeVarint32ThreeBytes(value); + } else if ((value & (~0 << 28)) == 0) { + writeVarint32FourBytes(value); + } else { + writeVarint32FiveBytes(value); + } + } + + private void writeVarint32OneByte(int value) { + UnsafeUtil.putByte(pos--, (byte) value); + } + + private void writeVarint32TwoBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32ThreeBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 14)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FourBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint32FiveBytes(int value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeVarint64(long value) { + switch (computeUInt64SizeNoTag(value)) { + case 1: + writeVarint64OneByte(value); + break; + case 2: + writeVarint64TwoBytes(value); + break; + case 3: + writeVarint64ThreeBytes(value); + break; + case 4: + writeVarint64FourBytes(value); + break; + case 5: + writeVarint64FiveBytes(value); + break; + case 6: + writeVarint64SixBytes(value); + break; + case 7: + writeVarint64SevenBytes(value); + break; + case 8: + writeVarint64EightBytes(value); + break; + case 9: + writeVarint64NineBytes(value); + break; + case 10: + writeVarint64TenBytes(value); + break; + } + } + + private void writeVarint64OneByte(long value) { + UnsafeUtil.putByte(pos--, (byte) value); + } + + private void writeVarint64TwoBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 7)); + UnsafeUtil.putByte(pos--, (byte) (((int) value & 0x7F) | 0x80)); + } + + private void writeVarint64ThreeBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (((int) value) >>> 14)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FourBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 21)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64FiveBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 28)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SixBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 35)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64SevenBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 42)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64EightBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 49)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64NineBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 56)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + private void writeVarint64TenBytes(long value) { + UnsafeUtil.putByte(pos--, (byte) (value >>> 63)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 56) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 49) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 42) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 35) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 28) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 21) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 14) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) (((value >>> 7) & 0x7F) | 0x80)); + UnsafeUtil.putByte(pos--, (byte) ((value & 0x7F) | 0x80)); + } + + @Override + void writeFixed32(int value) { + UnsafeUtil.putByte(pos--, (byte) ((value >> 24) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((value >> 16) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((value >> 8) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) (value & 0xFF)); + } + + @Override + void writeFixed64(long value) { + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 56) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 48) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 40) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 32) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 24) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 16) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value >> 8) & 0xFF)); + UnsafeUtil.putByte(pos--, (byte) ((int) (value) & 0xFF)); + } + + @Override + void writeString(String in) { + // Request enough space to write the ASCII string. + requireSpace(in.length()); + + // We know the buffer is big enough... + int i = in.length() - 1; + // Designed to take advantage of + // https://wikis.oracle.com/display/HotSpotInternals/RangeCheckElimination + for (char c; i >= 0 && (c = in.charAt(i)) < 0x80; i--) { + UnsafeUtil.putByte(pos--, (byte) c); + } + if (i == -1) { + // ASCII. + return; + } + for (char c; i >= 0; i--) { + c = in.charAt(i); + if (c < 0x80 && pos >= bufferOffset) { + UnsafeUtil.putByte(pos--, (byte) c); + } else if (c < 0x800 && pos > bufferOffset) { // 11 bits, two UTF-8 bytes + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 6) | (c >>> 6))); + } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c) + && pos > bufferOffset + 1) { + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & c))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (c >>> 6)))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 5) | (c >>> 12))); + } else if (pos > bufferOffset + 2) { + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, + // four UTF-8 bytes + final char high; + if (i == 0 || !Character.isSurrogatePair(high = in.charAt(i - 1), c)) { + throw new Utf8.UnpairedSurrogateException(i - 1, i); + } + i--; + int codePoint = Character.toCodePoint(high, c); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & codePoint))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 6)))); + UnsafeUtil.putByte(pos--, (byte) (0x80 | (0x3F & (codePoint >>> 12)))); + UnsafeUtil.putByte(pos--, (byte) ((0xF << 4) | (codePoint >>> 18))); + } else { + // Buffer is full - allocate a new one and revisit the current character. + requireSpace(i); + i++; + } + } + } + + @Override + public void write(byte value) { + UnsafeUtil.putByte(pos--, value); + } + + @Override + public void write(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value, offset, length); + } + + @Override + public void writeLazy(byte[] value, int offset, int length) { + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value, offset, length)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value, offset, length); + } + + @Override + public void write(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + nextBuffer(length); + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value); + } + + @Override + public void writeLazy(ByteBuffer value) { + int length = value.remaining(); + if (spaceLeft() < length) { + // We consider the value to be immutable (likely the internals of a ByteString). Just + // wrap it in a Netty buffer and add it to the output buffer. + totalDoneBytes += length; + buffers.addFirst(AllocatedBuffer.wrap(value)); + + // Advance the writer to the next buffer. + // TODO(nathanmittler): Consider slicing if space available above some threshold. + nextBuffer(); + return; + } + + pos -= length; + buffer.position(bufferPos() + 1); + buffer.put(value); + } + + @Override + void requireSpace(int size) { + if (spaceLeft() < size) { + nextBuffer(size); + } + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/BufferAllocator.java b/java/core/src/main/java/com/google/protobuf/BufferAllocator.java new file mode 100755 index 0000000000..bfd9c7237c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/BufferAllocator.java @@ -0,0 +1,64 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.nio.ByteBuffer; + +/** + * An object responsible for allocation of buffers. This is an extension point to enable buffer + * pooling within an application. + */ +@ExperimentalApi +abstract class BufferAllocator { + private static final BufferAllocator UNPOOLED = + new BufferAllocator() { + @Override + public AllocatedBuffer allocateHeapBuffer(int capacity) { + return AllocatedBuffer.wrap(new byte[capacity]); + } + + @Override + public AllocatedBuffer allocateDirectBuffer(int capacity) { + return AllocatedBuffer.wrap(ByteBuffer.allocateDirect(capacity)); + } + }; + + /** Returns an unpooled buffer allocator, which will create a new buffer for each request. */ + public static BufferAllocator unpooled() { + return UNPOOLED; + } + + /** Allocates a buffer with the given capacity that is backed by an array on the heap. */ + public abstract AllocatedBuffer allocateHeapBuffer(int capacity); + + /** Allocates a direct (i.e. non-heap) buffer with the given capacity. */ + public abstract AllocatedBuffer allocateDirectBuffer(int capacity); +} diff --git a/java/core/src/main/java/com/google/protobuf/ByteString.java b/java/core/src/main/java/com/google/protobuf/ByteString.java index 648991dc25..e5454a63d3 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteString.java +++ b/java/core/src/main/java/com/google/protobuf/ByteString.java @@ -732,6 +732,16 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { */ abstract void writeTo(ByteOutput byteOutput) throws IOException; + /** + * This method behaves exactly the same as {@link #writeTo(ByteOutput)} unless the {@link + * ByteString} is a rope. For ropes, the leaf nodes are written in reverse order to the {@code + * byteOutput}. + * + * @param byteOutput the output target to receive the bytes + * @throws IOException if an I/O error occurs + * @see UnsafeByteOperations#unsafeWriteToReverse(ByteString, ByteOutput) + */ + abstract void writeToReverse(ByteOutput byteOutput) throws IOException; /** * Constructs a read-only {@code java.nio.ByteBuffer} whose content is equal to the contents of @@ -862,6 +872,10 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { return true; } + @Override + void writeToReverse(ByteOutput byteOutput) throws IOException { + writeTo(byteOutput); + } /** * Check equality of the substring of given length of this object starting at zero with another @@ -1438,14 +1452,16 @@ public abstract class ByteString implements Iterable<Byte>, Serializable { LiteralByteString lbsOther = (LiteralByteString) other; byte[] thisBytes = bytes; byte[] otherBytes = lbsOther.bytes; - - return UnsafeUtil.mismatch( - thisBytes, - getOffsetIntoBytes(), - otherBytes, - lbsOther.getOffsetIntoBytes() + offset, - length) - == -1; + int thisLimit = getOffsetIntoBytes() + length; + for (int thisIndex = getOffsetIntoBytes(), + otherIndex = lbsOther.getOffsetIntoBytes() + offset; + (thisIndex < thisLimit); + ++thisIndex, ++otherIndex) { + if (thisBytes[thisIndex] != otherBytes[otherIndex]) { + return false; + } + } + return true; } return other.substring(offset, offset + length).equals(substring(0, length)); diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java index 6cece058e2..87c683725f 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -72,6 +72,9 @@ public abstract class CodedInputStream { /** Visible for subclasses. See setSizeLimit() */ int sizeLimit = DEFAULT_SIZE_LIMIT; + /** Used to adapt to the experimental {@link Reader} interface. */ + CodedInputStreamReader wrapper; + /** Create a new CodedInputStream wrapping the given InputStream. */ public static CodedInputStream newInstance(final InputStream input) { return newInstance(input, DEFAULT_BUFFER_SIZE); @@ -2263,7 +2266,7 @@ public abstract class CodedInputStream { return result; } // Slow path: Build a byte array first then copy it. - return new String(readRawBytesSlowPath(size), UTF_8); + return new String(readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false), UTF_8); } @Override @@ -2287,7 +2290,7 @@ public abstract class CodedInputStream { pos = tempPos + size; } else { // Slow path: Build a byte array first then copy it. - bytes = readRawBytesSlowPath(size); + bytes = readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); tempPos = 0; } return Utf8.decodeUtf8(bytes, tempPos, size); @@ -2392,7 +2395,8 @@ public abstract class CodedInputStream { return result; } else { // Slow path: Build a byte array first then copy it. - return readRawBytesSlowPath(size); + // TODO(dweis): Do we want to protect from malicious input streams here? + return readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); } } @@ -2409,7 +2413,10 @@ public abstract class CodedInputStream { return Internal.EMPTY_BYTE_BUFFER; } // Slow path: Build a byte array first then copy it. - return ByteBuffer.wrap(readRawBytesSlowPath(size)); + + // We must copy as the byte array was handed off to the InputStream and a malicious + // implementation could retain a reference. + return ByteBuffer.wrap(readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ true)); } @Override @@ -2812,19 +2819,24 @@ public abstract class CodedInputStream { pos = tempPos + size; return Arrays.copyOfRange(buffer, tempPos, tempPos + size); } else { - return readRawBytesSlowPath(size); + // TODO(dweis): Do we want to protect from malicious input streams here? + return readRawBytesSlowPath(size, /* ensureNoLeakedReferences= */ false); } } /** * Exactly like readRawBytes, but caller must have already checked the fast path: (size <= * (bufferSize - pos) && size > 0) + * + * If ensureNoLeakedReferences is true, the value is guaranteed to have not escaped to + * untrusted code. */ - private byte[] readRawBytesSlowPath(final int size) throws IOException { + private byte[] readRawBytesSlowPath( + final int size, boolean ensureNoLeakedReferences) throws IOException { // Attempt to read the data in one byte array when it's safe to do. byte[] result = readRawBytesSlowPathOneChunk(size); if (result != null) { - return result; + return ensureNoLeakedReferences ? result.clone() : result; } final int originalBufferPos = pos; @@ -2862,6 +2874,8 @@ public abstract class CodedInputStream { /** * Attempts to read the data in one byte array when it's safe to do. Returns null if the size to * read is too large and needs to be allocated in smaller chunks for security reasons. + * + * Returns a byte[] that may have escaped to user code via InputStream APIs. */ private byte[] readRawBytesSlowPathOneChunk(final int size) throws IOException { if (size == 0) { @@ -2916,7 +2930,11 @@ public abstract class CodedInputStream { return null; } - /** Reads the remaining data in small chunks from the input stream. */ + /** + * Reads the remaining data in small chunks from the input stream. + * + * Returns a byte[] that may have escaped to user code via InputStream APIs. + */ private List<byte[]> readRawBytesSlowPathRemainingChunks(int sizeLeft) throws IOException { // The size is very large. For security reasons, we can't allocate the // entire byte array yet. The size comes directly from the input, so a @@ -2953,7 +2971,9 @@ public abstract class CodedInputStream { private ByteString readBytesSlowPath(final int size) throws IOException { final byte[] result = readRawBytesSlowPathOneChunk(size); if (result != null) { - return ByteString.wrap(result); + // We must copy as the byte array was handed off to the InputStream and a malicious + // implementation could retain a reference. + return ByteString.copyFrom(result); } final int originalBufferPos = pos; @@ -2971,13 +2991,20 @@ public abstract class CodedInputStream { // chunks. List<byte[]> chunks = readRawBytesSlowPathRemainingChunks(sizeLeft); - // Wrap the byte arrays into a single ByteString. - List<ByteString> byteStrings = new ArrayList<ByteString>(1 + chunks.size()); - byteStrings.add(ByteString.copyFrom(buffer, originalBufferPos, bufferedBytes)); - for (byte[] chunk : chunks) { - byteStrings.add(ByteString.wrap(chunk)); + // OK, got everything. Now concatenate it all into one buffer. + final byte[] bytes = new byte[size]; + + // Start by copying the leftover bytes from this.buffer. + System.arraycopy(buffer, originalBufferPos, bytes, 0, bufferedBytes); + + // And now all the chunks. + int tempPos = bufferedBytes; + for (final byte[] chunk : chunks) { + System.arraycopy(chunk, 0, bytes, tempPos, chunk.length); + tempPos += chunk.length; } - return ByteString.copyFrom(byteStrings); + + return ByteString.wrap(bytes); } @Override diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java new file mode 100755 index 0000000000..7658f629d3 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStreamReader.java @@ -0,0 +1,1333 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.WireFormat.FIXED32_SIZE; +import static com.google.protobuf.WireFormat.FIXED64_SIZE; +import static com.google.protobuf.WireFormat.WIRETYPE_END_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED32; +import static com.google.protobuf.WireFormat.WIRETYPE_FIXED64; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; +import static com.google.protobuf.WireFormat.WIRETYPE_START_GROUP; +import static com.google.protobuf.WireFormat.WIRETYPE_VARINT; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** An adapter between the {@link Reader} interface and {@link CodedInputStream}. */ +@ExperimentalApi +final class CodedInputStreamReader implements Reader { + private static final int FIXED32_MULTIPLE_MASK = FIXED32_SIZE - 1; + private static final int FIXED64_MULTIPLE_MASK = FIXED64_SIZE - 1; + private static final int NEXT_TAG_UNSET = 0; + + private final CodedInputStream input; + private int tag; + private int endGroupTag; + private int nextTag = NEXT_TAG_UNSET; + + public static CodedInputStreamReader forCodedInput(CodedInputStream input) { + if (input.wrapper != null) { + return input.wrapper; + } + return new CodedInputStreamReader(input); + } + + private CodedInputStreamReader(CodedInputStream input) { + this.input = Internal.checkNotNull(input, "input"); + this.input.wrapper = this; + } + + @Override + public boolean shouldDiscardUnknownFields() { + return input.shouldDiscardUnknownFields(); + } + + @Override + public int getFieldNumber() throws IOException { + if (nextTag != NEXT_TAG_UNSET) { + tag = nextTag; + nextTag = NEXT_TAG_UNSET; + } else { + tag = input.readTag(); + } + if (tag == 0 || tag == endGroupTag) { + return Reader.READ_DONE; + } + return WireFormat.getTagFieldNumber(tag); + } + + @Override + public int getTag() { + return tag; + } + + @Override + public boolean skipField() throws IOException { + if (input.isAtEnd() || tag == endGroupTag) { + return false; + } + return input.skipField(tag); + } + + private void requireWireType(int requiredWireType) throws IOException { + if (WireFormat.getTagWireType(tag) != requiredWireType) { + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + @Override + public double readDouble() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readDouble(); + } + + @Override + public float readFloat() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readFloat(); + } + + @Override + public long readUInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readUInt64(); + } + + @Override + public long readInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readInt64(); + } + + @Override + public int readInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readInt32(); + } + + @Override + public long readFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readFixed64(); + } + + @Override + public int readFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readFixed32(); + } + + @Override + public boolean readBool() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readBool(); + } + + @Override + public String readString() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readString(); + } + + @Override + public String readStringRequireUtf8() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readStringRequireUtf8(); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T readMessage(Class<T> clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T readMessageBySchemaWithCheck( + Schema<T> schema, ExtensionRegistryLite extensionRegistry) throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return readMessage(schema, extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T readGroup(Class<T> clazz, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(Protobuf.getInstance().schemaFor(clazz), extensionRegistry); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T readGroupBySchemaWithCheck(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_START_GROUP); + return readGroup(schema, extensionRegistry); + } + + // Should have the same semantics of CodedInputStream#readMessage() + private <T> T readMessage(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int size = input.readUInt32(); + if (input.recursionDepth >= input.recursionLimit) { + throw InvalidProtocolBufferException.recursionLimitExceeded(); + } + + // Push the new limit. + final int prevLimit = input.pushLimit(size); + // Allocate and read the message. + T message = schema.newInstance(); + ++input.recursionDepth; + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + input.checkLastTagWas(0); + --input.recursionDepth; + // Restore the previous limit. + input.popLimit(prevLimit); + return message; + } + + private <T> T readGroup(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + int prevEndGroupTag = endGroupTag; + endGroupTag = WireFormat.makeTag(WireFormat.getTagFieldNumber(tag), WIRETYPE_END_GROUP); + + try { + // Allocate and read the message. + T message = schema.newInstance(); + schema.mergeFrom(message, this, extensionRegistry); + schema.makeImmutable(message); + + if (tag != endGroupTag) { + throw InvalidProtocolBufferException.parseFailure(); + } + return message; + } finally { + // Restore the old end group tag. + endGroupTag = prevEndGroupTag; + } + } + + @Override + public ByteString readBytes() throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + return input.readBytes(); + } + + @Override + public int readUInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readUInt32(); + } + + @Override + public int readEnum() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readEnum(); + } + + @Override + public int readSFixed32() throws IOException { + requireWireType(WIRETYPE_FIXED32); + return input.readSFixed32(); + } + + @Override + public long readSFixed64() throws IOException { + requireWireType(WIRETYPE_FIXED64); + return input.readSFixed64(); + } + + @Override + public int readSInt32() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readSInt32(); + } + + @Override + public long readSInt64() throws IOException { + requireWireType(WIRETYPE_VARINT); + return input.readSInt64(); + } + + @Override + public void readDoubleList(List<Double> target) throws IOException { + if (target instanceof DoubleArrayList) { + DoubleArrayList plist = (DoubleArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addDouble(input.readDouble()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addDouble(input.readDouble()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readDouble()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readDouble()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFloatList(List<Float> target) throws IOException { + if (target instanceof FloatArrayList) { + FloatArrayList plist = (FloatArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addFloat(input.readFloat()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addFloat(input.readFloat()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFloat()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readFloat()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readUInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readUInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readUInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readUInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readUInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(input.readFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readFixed32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(input.readFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readBoolList(List<Boolean> target) throws IOException { + if (target instanceof BooleanArrayList) { + BooleanArrayList plist = (BooleanArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addBoolean(input.readBool()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addBoolean(input.readBool()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readBool()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readBool()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readStringList(List<String> target) throws IOException { + readStringListInternal(target, false); + } + + @Override + public void readStringListRequireUtf8(List<String> target) throws IOException { + readStringListInternal(target, true); + } + + public void readStringListInternal(List<String> target, boolean requireUtf8) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + if (target instanceof LazyStringList && !requireUtf8) { + LazyStringList lazyList = (LazyStringList) target; + while (true) { + lazyList.add(readBytes()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } else { + while (true) { + target.add(requireUtf8 ? readStringRequireUtf8() : readString()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + } + + @Override + public <T> void readMessageList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType); + readMessageList(target, schema, extensionRegistry); + } + + @Override + public <T> void readMessageList( + List<T> target, Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readMessage(schema, extensionRegistry)); + if (input.isAtEnd() || nextTag != NEXT_TAG_UNSET) { + return; + } + int nextTag = input.readTag(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public <T> void readGroupList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException { + final Schema<T> schema = Protobuf.getInstance().schemaFor(targetType); + readGroupList(target, schema, extensionRegistry); + } + + @Override + public <T> void readGroupList( + List<T> target, Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_START_GROUP) { + throw InvalidProtocolBufferException.invalidWireType(); + } + final int listTag = tag; + while (true) { + target.add(readGroup(schema, extensionRegistry)); + if (input.isAtEnd() || nextTag != NEXT_TAG_UNSET) { + return; + } + int nextTag = input.readTag(); + if (nextTag != listTag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public void readBytesList(List<ByteString> target) throws IOException { + if (WireFormat.getTagWireType(tag) != WIRETYPE_LENGTH_DELIMITED) { + throw InvalidProtocolBufferException.invalidWireType(); + } + + while (true) { + target.add(readBytes()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + } + + @Override + public void readUInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readUInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readUInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readUInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readUInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readEnumList(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readEnum()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readEnum()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readEnum()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readEnum()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readSFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + plist.addInt(input.readSFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed32Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSFixed32()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED32: + while (true) { + target.add(input.readSFixed32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSFixed64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readSFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + plist.addLong(input.readSFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + verifyPackedFixed64Length(bytes); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSFixed64()); + } while (input.getTotalBytesRead() < endPos); + break; + case WIRETYPE_FIXED64: + while (true) { + target.add(input.readSFixed64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt32List(List<Integer> target) throws IOException { + if (target instanceof IntArrayList) { + IntArrayList plist = (IntArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addInt(input.readSInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addInt(input.readSInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSInt32()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readSInt32()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + @Override + public void readSInt64List(List<Long> target) throws IOException { + if (target instanceof LongArrayList) { + LongArrayList plist = (LongArrayList) target; + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + plist.addLong(input.readSInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + plist.addLong(input.readSInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } else { + switch (WireFormat.getTagWireType(tag)) { + case WIRETYPE_LENGTH_DELIMITED: + final int bytes = input.readUInt32(); + int endPos = input.getTotalBytesRead() + bytes; + do { + target.add(input.readSInt64()); + } while (input.getTotalBytesRead() < endPos); + requirePosition(endPos); + break; + case WIRETYPE_VARINT: + while (true) { + target.add(input.readSInt64()); + if (input.isAtEnd()) { + return; + } + int nextTag = input.readTag(); + if (nextTag != tag) { + // We've reached the end of the repeated field. Save the next tag value. + this.nextTag = nextTag; + return; + } + } + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + } + + private void verifyPackedFixed64Length(int bytes) throws IOException { + if ((bytes & FIXED64_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 8. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + @SuppressWarnings("unchecked") + @Override + public <K, V> void readMap( + Map<K, V> target, + MapEntryLite.Metadata<K, V> metadata, + ExtensionRegistryLite extensionRegistry) + throws IOException { + requireWireType(WIRETYPE_LENGTH_DELIMITED); + int size = input.readUInt32(); + final int prevLimit = input.pushLimit(size); + K key = metadata.defaultKey; + V value = metadata.defaultValue; + try { + while (true) { + int number = getFieldNumber(); + if (number == READ_DONE || input.isAtEnd()) { + break; + } + try { + switch (number) { + case 1: + key = (K) readField(metadata.keyType, null, null); + break; + case 2: + value = + (V) + readField( + metadata.valueType, metadata.defaultValue.getClass(), extensionRegistry); + break; + default: + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException ignore) { + // the type doesn't match, skip the field. + if (!skipField()) { + throw new InvalidProtocolBufferException("Unable to parse map entry."); + } + } + } + target.put(key, value); + } finally { + // Restore the previous limit. + input.popLimit(prevLimit); + } + } + + private Object readField( + WireFormat.FieldType fieldType, Class<?> messageType, ExtensionRegistryLite extensionRegistry) + throws IOException { + switch (fieldType) { + case BOOL: + return readBool(); + case BYTES: + return readBytes(); + case DOUBLE: + return readDouble(); + case ENUM: + return readEnum(); + case FIXED32: + return readFixed32(); + case FIXED64: + return readFixed64(); + case FLOAT: + return readFloat(); + case INT32: + return readInt32(); + case INT64: + return readInt64(); + case MESSAGE: + return readMessage(messageType, extensionRegistry); + case SFIXED32: + return readSFixed32(); + case SFIXED64: + return readSFixed64(); + case SINT32: + return readSInt32(); + case SINT64: + return readSInt64(); + case STRING: + return readStringRequireUtf8(); + case UINT32: + return readUInt32(); + case UINT64: + return readUInt64(); + default: + throw new RuntimeException("unsupported field type."); + } + } + + private void verifyPackedFixed32Length(int bytes) throws IOException { + if ((bytes & FIXED32_MULTIPLE_MASK) != 0) { + // Require that the number of bytes be a multiple of 4. + throw InvalidProtocolBufferException.parseFailure(); + } + } + + private void requirePosition(int expectedPosition) throws IOException { + if (input.getTotalBytesRead() != expectedPosition) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 3823f81fc7..a9192d33e4 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -60,6 +60,9 @@ public abstract class CodedOutputStream extends ByteOutput { private static final Logger logger = Logger.getLogger(CodedOutputStream.class.getName()); private static final boolean HAS_UNSAFE_ARRAY_OPERATIONS = UnsafeUtil.hasUnsafeArrayOperations(); + /** Used to adapt to the experimental {@link Writer} interface. */ + CodedOutputStreamWriter wrapper; + /** @deprecated Use {@link #computeFixed32SizeNoTag(int)} instead. */ @Deprecated public static final int LITTLE_ENDIAN_32_SIZE = FIXED32_SIZE; @@ -361,6 +364,10 @@ public abstract class CodedOutputStream extends ByteOutput { public abstract void writeMessage(final int fieldNumber, final MessageLite value) throws IOException; + /** Write an embedded message field, including tag, to the stream. */ + // Abstract to avoid overhead of additional virtual method calls. + abstract void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException; /** * Write a MessageSet extension field to the stream. For historical reasons, the wire format @@ -466,6 +473,9 @@ public abstract class CodedOutputStream extends ByteOutput { // Abstract to avoid overhead of additional virtual method calls. public abstract void writeMessageNoTag(final MessageLite value) throws IOException; + /** Write an embedded message field to the stream. */ + // Abstract to avoid overhead of additional virtual method calls. + abstract void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException; // ================================================================= @@ -651,6 +661,14 @@ public abstract class CodedOutputStream extends ByteOutput { return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value); } + /** + * Compute the number of bytes that would be needed to encode an embedded message field, including + * tag. + */ + static int computeMessageSize( + final int fieldNumber, final MessageLite value, final Schema schema) { + return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value, schema); + } /** * Compute the number of bytes that would be needed to encode a MessageSet extension to the @@ -859,6 +877,10 @@ public abstract class CodedOutputStream extends ByteOutput { return computeLengthDelimitedFieldSize(value.getSerializedSize()); } + /** Compute the number of bytes that would be needed to encode an embedded message field. */ + static int computeMessageSizeNoTag(final MessageLite value, final Schema schema) { + return computeLengthDelimitedFieldSize(((AbstractMessageLite) value).getSerializedSize(schema)); + } static int computeLengthDelimitedFieldSize(int fieldLength) { return computeUInt32SizeNoTag(fieldLength) + fieldLength; @@ -993,6 +1015,18 @@ public abstract class CodedOutputStream extends ByteOutput { writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); } + /** + * Write a {@code group} field, including tag, to the stream. + * + * @deprecated groups are deprecated. + */ + @Deprecated + final void writeGroup(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP); + writeGroupNoTag(value, schema); + writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + } /** * Write a {@code group} field to the stream. @@ -1004,6 +1038,15 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + /** + * Write a {@code group} field to the stream. + * + * @deprecated groups are deprecated. + */ + @Deprecated + final void writeGroupNoTag(final MessageLite value, Schema schema) throws IOException { + schema.writeTo(value, wrapper); + } /** * Compute the number of bytes that would be needed to encode a {@code group} field, including @@ -1016,6 +1059,16 @@ public abstract class CodedOutputStream extends ByteOutput { return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value); } + /** + * Compute the number of bytes that would be needed to encode a {@code group} field, including + * tag. + * + * @deprecated groups are deprecated. + */ + @Deprecated + static int computeGroupSize(final int fieldNumber, final MessageLite value, Schema schema) { + return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value, schema); + } /** Compute the number of bytes that would be needed to encode a {@code group} field. */ @Deprecated @@ -1023,6 +1076,11 @@ public abstract class CodedOutputStream extends ByteOutput { return value.getSerializedSize(); } + /** Compute the number of bytes that would be needed to encode a {@code group} field. */ + @Deprecated + static int computeGroupSizeNoTag(final MessageLite value, Schema schema) { + return ((AbstractMessageLite) value).getSerializedSize(schema); + } /** * Encode and write a varint. {@code value} is treated as unsigned, so it won't be sign-extended @@ -1216,6 +1274,13 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + final void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public final void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -1241,6 +1306,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + final void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public final void write(byte value) throws IOException { @@ -1571,6 +1641,12 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -1596,6 +1672,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -1893,6 +1974,11 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + void writeMessage(int fieldNumber, MessageLite value, Schema schema) throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(int fieldNumber, MessageLite value) throws IOException { @@ -1916,6 +2002,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -2419,6 +2510,12 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -2444,6 +2541,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { @@ -2722,6 +2824,12 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override + void writeMessage(final int fieldNumber, final MessageLite value, Schema schema) + throws IOException { + writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); + writeMessageNoTag(value, schema); + } @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) @@ -2747,6 +2855,11 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override + void writeMessageNoTag(final MessageLite value, Schema schema) throws IOException { + writeUInt32NoTag(((AbstractMessageLite) value).getSerializedSize(schema)); + schema.writeTo(value, wrapper); + } @Override public void write(byte value) throws IOException { diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java new file mode 100755 index 0000000000..0d1983cb07 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java @@ -0,0 +1,691 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; +import static com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** An adapter between the {@link Writer} interface and {@link CodedOutputStream}. */ +@ExperimentalApi +final class CodedOutputStreamWriter implements Writer { + private final CodedOutputStream output; + + public static CodedOutputStreamWriter forCodedOutput(CodedOutputStream output) { + if (output.wrapper != null) { + return output.wrapper; + } + return new CodedOutputStreamWriter(output); + } + + private CodedOutputStreamWriter(CodedOutputStream output) { + this.output = checkNotNull(output, "output"); + this.output.wrapper = this; + } + + @Override + public FieldOrder fieldOrder() { + return FieldOrder.ASCENDING; + } + + public int getTotalBytesWritten() { + return output.getTotalBytesWritten(); + } + + @Override + public void writeSFixed32(int fieldNumber, int value) throws IOException { + output.writeSFixed32(fieldNumber, value); + } + + @Override + public void writeInt64(int fieldNumber, long value) throws IOException { + output.writeInt64(fieldNumber, value); + } + + @Override + public void writeSFixed64(int fieldNumber, long value) throws IOException { + output.writeSFixed64(fieldNumber, value); + } + + @Override + public void writeFloat(int fieldNumber, float value) throws IOException { + output.writeFloat(fieldNumber, value); + } + + @Override + public void writeDouble(int fieldNumber, double value) throws IOException { + output.writeDouble(fieldNumber, value); + } + + @Override + public void writeEnum(int fieldNumber, int value) throws IOException { + output.writeEnum(fieldNumber, value); + } + + @Override + public void writeUInt64(int fieldNumber, long value) throws IOException { + output.writeUInt64(fieldNumber, value); + } + + @Override + public void writeInt32(int fieldNumber, int value) throws IOException { + output.writeInt32(fieldNumber, value); + } + + @Override + public void writeFixed64(int fieldNumber, long value) throws IOException { + output.writeFixed64(fieldNumber, value); + } + + @Override + public void writeFixed32(int fieldNumber, int value) throws IOException { + output.writeFixed32(fieldNumber, value); + } + + @Override + public void writeBool(int fieldNumber, boolean value) throws IOException { + output.writeBool(fieldNumber, value); + } + + @Override + public void writeString(int fieldNumber, String value) throws IOException { + output.writeString(fieldNumber, value); + } + + @Override + public void writeBytes(int fieldNumber, ByteString value) throws IOException { + output.writeBytes(fieldNumber, value); + } + + @Override + public void writeUInt32(int fieldNumber, int value) throws IOException { + output.writeUInt32(fieldNumber, value); + } + + @Override + public void writeSInt32(int fieldNumber, int value) throws IOException { + output.writeSInt32(fieldNumber, value); + } + + @Override + public void writeSInt64(int fieldNumber, long value) throws IOException { + output.writeSInt64(fieldNumber, value); + } + + @Override + public void writeMessage(int fieldNumber, Object value) throws IOException { + output.writeMessage(fieldNumber, (MessageLite) value); + } + + @Override + public void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException { + output.writeMessage(fieldNumber, (MessageLite) value, schema); + } + + @Override + public void writeGroup(int fieldNumber, Object value) throws IOException { + output.writeGroup(fieldNumber, (MessageLite) value); + } + + @Override + public void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException { + output.writeGroup(fieldNumber, (MessageLite) value, schema); + } + + @Override + public void writeStartGroup(int fieldNumber) throws IOException { + output.writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP); + } + + @Override + public void writeEndGroup(int fieldNumber) throws IOException { + output.writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + } + + @Override + public final void writeMessageSetItem(int fieldNumber, Object value) throws IOException { + if (value instanceof ByteString) { + output.writeRawMessageSetExtension(fieldNumber, (ByteString) value); + } else { + output.writeMessageSetExtension(fieldNumber, (MessageLite) value); + } + } + + @Override + public void writeInt32List(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFixed32List(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFixed32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFixed32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFixed32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeInt64List(int fieldNumber, List<Long> value, boolean packed) throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeUInt64List(int fieldNumber, List<Long> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeUInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeUInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeUInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFixed64List(int fieldNumber, List<Long> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFixed64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFixed64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFixed64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeFloatList(int fieldNumber, List<Float> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeFloatSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeFloatNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeFloat(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeDoubleList(int fieldNumber, List<Double> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeDoubleSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeDoubleNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeDouble(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeEnumList(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeEnumSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeEnumNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeEnum(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeBoolList(int fieldNumber, List<Boolean> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeBoolSizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeBoolNoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeBool(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeStringList(int fieldNumber, List<String> value) throws IOException { + if (value instanceof LazyStringList) { + final LazyStringList lazyList = (LazyStringList) value; + for (int i = 0; i < value.size(); ++i) { + writeLazyString(fieldNumber, lazyList.getRaw(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeString(fieldNumber, value.get(i)); + } + } + } + + private void writeLazyString(int fieldNumber, Object value) throws IOException { + if (value instanceof String) { + output.writeString(fieldNumber, (String) value); + } else { + output.writeBytes(fieldNumber, (ByteString) value); + } + } + + @Override + public void writeBytesList(int fieldNumber, List<ByteString> value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + output.writeBytes(fieldNumber, value.get(i)); + } + } + + @Override + public void writeUInt32List(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeUInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeUInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeUInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSFixed32List(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSFixed32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSFixed64List(int fieldNumber, List<Long> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSFixed64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSFixed64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSInt32List(int fieldNumber, List<Integer> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSInt32SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSInt32NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSInt32(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeSInt64List(int fieldNumber, List<Long> value, boolean packed) + throws IOException { + if (packed) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + + // Compute and write the length of the data. + int dataSize = 0; + for (int i = 0; i < value.size(); ++i) { + dataSize += CodedOutputStream.computeSInt64SizeNoTag(value.get(i)); + } + output.writeUInt32NoTag(dataSize); + + // Write the data itself, without any tags. + for (int i = 0; i < value.size(); ++i) { + output.writeSInt64NoTag(value.get(i)); + } + } else { + for (int i = 0; i < value.size(); ++i) { + output.writeSInt64(fieldNumber, value.get(i)); + } + } + } + + @Override + public void writeMessageList(int fieldNumber, List<?> value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeMessage(fieldNumber, value.get(i)); + } + } + + @Override + public void writeMessageList(int fieldNumber, List<?> value, Schema schema) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeMessage(fieldNumber, value.get(i), schema); + } + } + + @Override + public void writeGroupList(int fieldNumber, List<?> value) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeGroup(fieldNumber, value.get(i)); + } + } + + @Override + public void writeGroupList(int fieldNumber, List<?> value, Schema schema) throws IOException { + for (int i = 0; i < value.size(); ++i) { + writeGroup(fieldNumber, value.get(i), schema); + } + } + + @Override + public <K, V> void writeMap(int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, V> map) + throws IOException { + if (output.isSerializationDeterministic()) { + writeDeterministicMap(fieldNumber, metadata, map); + return; + } + for (Map.Entry<K, V> entry : map.entrySet()) { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag( + MapEntryLite.computeSerializedSize(metadata, entry.getKey(), entry.getValue())); + MapEntryLite.writeTo(output, metadata, entry.getKey(), entry.getValue()); + } + } + + @SuppressWarnings("unchecked") + private <K, V> void writeDeterministicMap( + int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, V> map) throws IOException { + switch (metadata.keyType) { + case BOOL: + V value; + if ((value = map.get(Boolean.FALSE)) != null) { + writeDeterministicBooleanMapEntry( + fieldNumber, /* key= */ false, value, (MapEntryLite.Metadata<Boolean, V>) metadata); + } + if ((value = map.get(Boolean.TRUE)) != null) { + writeDeterministicBooleanMapEntry( + fieldNumber, /* key= */ true, value, (MapEntryLite.Metadata<Boolean, V>) metadata); + } + break; + case FIXED32: + case INT32: + case SFIXED32: + case SINT32: + case UINT32: + writeDeterministicIntegerMap( + fieldNumber, (MapEntryLite.Metadata<Integer, V>) metadata, (Map<Integer, V>) map); + break; + case FIXED64: + case INT64: + case SFIXED64: + case SINT64: + case UINT64: + writeDeterministicLongMap( + fieldNumber, (MapEntryLite.Metadata<Long, V>) metadata, (Map<Long, V>) map); + break; + case STRING: + writeDeterministicStringMap( + fieldNumber, (MapEntryLite.Metadata<String, V>) metadata, (Map<String, V>) map); + break; + default: + throw new IllegalArgumentException("does not support key type: " + metadata.keyType); + } + } + + private <V> void writeDeterministicBooleanMapEntry( + int fieldNumber, boolean key, V value, MapEntryLite.Metadata<Boolean, V> metadata) + throws IOException { + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + + private <V> void writeDeterministicIntegerMap( + int fieldNumber, MapEntryLite.Metadata<Integer, V> metadata, Map<Integer, V> map) + throws IOException { + int[] keys = new int[map.size()]; + int index = 0; + for (int k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (int key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } + + private <V> void writeDeterministicLongMap( + int fieldNumber, MapEntryLite.Metadata<Long, V> metadata, Map<Long, V> map) + throws IOException { + long[] keys = new long[map.size()]; + int index = 0; + for (long k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (long key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } + + private <V> void writeDeterministicStringMap( + int fieldNumber, MapEntryLite.Metadata<String, V> metadata, Map<String, V> map) + throws IOException { + String[] keys = new String[map.size()]; + int index = 0; + for (String k : map.keySet()) { + keys[index++] = k; + } + Arrays.sort(keys); + for (String key : keys) { + V value = map.get(key); + output.writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED); + output.writeUInt32NoTag(MapEntryLite.computeSerializedSize(metadata, key, value)); + MapEntryLite.writeTo(output, metadata, key, value); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java new file mode 100755 index 0000000000..cd29b7beb0 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/DescriptorMessageInfoFactory.java @@ -0,0 +1,693 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forPackedField; +import static com.google.protobuf.FieldInfo.forPackedFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.Descriptors.Descriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor.Type; +import com.google.protobuf.Descriptors.OneofDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; +import java.util.concurrent.ConcurrentHashMap; + +/** A factory for message info based on protobuf descriptors for a {@link GeneratedMessageV3}. */ +@ExperimentalApi +final class DescriptorMessageInfoFactory implements MessageInfoFactory { + private static final String GET_DEFAULT_INSTANCE_METHOD_NAME = "getDefaultInstance"; + private static final DescriptorMessageInfoFactory instance = new DescriptorMessageInfoFactory(); + private static final Set<String> specialFieldNames = + new HashSet<>(Arrays.asList("cached_size", "serialized_size", "class")); + + // Disallow construction - it's a singleton. + private DescriptorMessageInfoFactory() {} + + public static DescriptorMessageInfoFactory getInstance() { + return instance; + } + + @Override + public boolean isSupported(Class<?> messageType) { + return GeneratedMessageV3.class.isAssignableFrom(messageType); + } + + @Override + public MessageInfo messageInfoFor(Class<?> messageType) { + if (!GeneratedMessageV3.class.isAssignableFrom(messageType)) { + throw new IllegalArgumentException("Unsupported message type: " + messageType.getName()); + } + + return convert(messageType, descriptorForType(messageType)); + } + + private static Message getDefaultInstance(Class<?> messageType) { + try { + Method method = messageType.getDeclaredMethod(GET_DEFAULT_INSTANCE_METHOD_NAME); + return (Message) method.invoke(null); + } catch (Exception e) { + throw new IllegalArgumentException( + "Unable to get default instance for message class " + messageType.getName(), e); + } + } + + private static Descriptor descriptorForType(Class<?> messageType) { + return getDefaultInstance(messageType).getDescriptorForType(); + } + + private static MessageInfo convert(Class<?> messageType, Descriptor messageDescriptor) { + switch (messageDescriptor.getFile().getSyntax()) { + case PROTO2: + return convertProto2(messageType, messageDescriptor); + case PROTO3: + return convertProto3(messageType, messageDescriptor); + default: + throw new IllegalArgumentException( + "Unsupported syntax: " + messageDescriptor.getFile().getSyntax()); + } + } + + /** + * A helper class to determine whether a message type needs to implement {@code isInitialized()}. + * + * <p>If a message type doesn't have any required fields or extensions (directly and + * transitively), it doesn't need to implement isInitialized() and can always return true there. + * It's a bit tricky to determine whether a type has transitive required fields because protobuf + * allows cycle references within the same .proto file (e.g., message Foo has a Bar field, and + * message Bar has a Foo field). For that we use Tarjan's strongly connected components algorithm + * to classify messages into strongly connected groups. Messages in the same group are + * transitively including each other, so they should either all have transitive required fields + * (or extensions), or none have. + * + * <p>The code is adapted from the C++ implementation: + * https://cs.corp.google.com/piper///depot/google3/net/proto2/compiler/cpp/internal/helpers.h?type=cs&rcl=161303268&l=346 + * + * <p>This class is thread-safe. + */ + static class IsInitializedCheckAnalyzer { + + private final Map<Descriptor, Boolean> resultCache = + new ConcurrentHashMap<Descriptor, Boolean>(); + + // The following data members are part of Tarjan's SCC algorithm. See: + // https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm + private int index = 0; + private final Stack<Node> stack = new Stack<Node>(); + private final Map<Descriptor, Node> nodeCache = new HashMap<Descriptor, Node>(); + + public boolean needsIsInitializedCheck(Descriptor descriptor) { + Boolean cachedValue = resultCache.get(descriptor); + if (cachedValue != null) { + return cachedValue; + } + synchronized (this) { + // Double-check the cache because some other thread may have updated it while we + // were acquiring the lock. + cachedValue = resultCache.get(descriptor); + if (cachedValue != null) { + return cachedValue; + } + return dfs(descriptor).component.needsIsInitializedCheck; + } + } + + private static class Node { + final Descriptor descriptor; + final int index; + int lowLink; + StronglyConnectedComponent component; // null if the node is still on stack. + + Node(Descriptor descriptor, int index) { + this.descriptor = descriptor; + this.index = index; + this.lowLink = index; + this.component = null; + } + } + + private static class StronglyConnectedComponent { + final List<Descriptor> messages = new ArrayList<Descriptor>(); + boolean needsIsInitializedCheck = false; + } + + private Node dfs(Descriptor descriptor) { + Node result = new Node(descriptor, index++); + stack.push(result); + nodeCache.put(descriptor, result); + + // Recurse the fields / nodes in graph + for (FieldDescriptor field : descriptor.getFields()) { + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + Node child = nodeCache.get(field.getMessageType()); + if (child == null) { + // Unexplored node + child = dfs(field.getMessageType()); + result.lowLink = Math.min(result.lowLink, child.lowLink); + } else { + if (child.component == null) { + // Still in the stack so we found a back edge. + result.lowLink = Math.min(result.lowLink, child.lowLink); + } + } + } + } + + if (result.index == result.lowLink) { + // This is the root of a strongly connected component. + StronglyConnectedComponent component = new StronglyConnectedComponent(); + while (true) { + Node node = stack.pop(); + node.component = component; + component.messages.add(node.descriptor); + if (node == result) { + break; + } + } + + analyze(component); + } + + return result; + } + + // Determine whether messages in this SCC needs isInitialized check. + private void analyze(StronglyConnectedComponent component) { + boolean needsIsInitializedCheck = false; + loop: + for (Descriptor descriptor : component.messages) { + if (descriptor.isExtendable()) { + needsIsInitializedCheck = true; + break; + } + + for (FieldDescriptor field : descriptor.getFields()) { + if (field.isRequired()) { + needsIsInitializedCheck = true; + break loop; + } + + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + // Since we are analyzing the graph bottom-up, all referenced fields should either be + // in this same component or in a different already-analyzed component. + Node node = nodeCache.get(field.getMessageType()); + if (node.component != component) { + if (node.component.needsIsInitializedCheck) { + needsIsInitializedCheck = true; + break loop; + } + } + } + } + } + + component.needsIsInitializedCheck = needsIsInitializedCheck; + + for (Descriptor descriptor : component.messages) { + resultCache.put(descriptor, component.needsIsInitializedCheck); + } + } + } + + private static IsInitializedCheckAnalyzer isInitializedCheckAnalyzer = + new IsInitializedCheckAnalyzer(); + + private static boolean needsIsInitializedCheck(Descriptor descriptor) { + return isInitializedCheckAnalyzer.needsIsInitializedCheck(descriptor); + } + + private static StructuralMessageInfo convertProto2( + Class<?> messageType, Descriptor messageDescriptor) { + List<FieldDescriptor> fieldDescriptors = messageDescriptor.getFields(); + StructuralMessageInfo.Builder builder = + StructuralMessageInfo.newBuilder(fieldDescriptors.size()); + builder.withDefaultInstance(getDefaultInstance(messageType)); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withMessageSetWireFormat(messageDescriptor.getOptions().getMessageSetWireFormat()); + + OneofState oneofState = new OneofState(); + int bitFieldIndex = 0; + int presenceMask = 1; + Field bitField = null; + + // Fields in the descriptor are ordered by the index position in which they appear in the + // proto file. This is the same order used to determine the presence mask used in the + // bitFields. So to determine the appropriate presence mask to be used for a field, we simply + // need to shift the presence mask whenever a presence-checked field is encountered. + for (int i = 0; i < fieldDescriptors.size(); ++i) { + final FieldDescriptor fd = fieldDescriptors.get(i); + boolean enforceUtf8 = fd.getFile().getOptions().getJavaStringCheckUtf8(); + Internal.EnumVerifier enumVerifier = null; + if (fd.getJavaType() == Descriptors.FieldDescriptor.JavaType.ENUM) { + enumVerifier = + new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return fd.getEnumType().findValueByNumber(number) != null; + } + }; + } + if (fd.getContainingOneof() != null) { + // Build a oneof member field. + builder.withField(buildOneofMember(messageType, fd, oneofState, enforceUtf8, enumVerifier)); + } else { + Field field = field(messageType, fd); + int number = fd.getNumber(); + FieldType type = getFieldType(fd); + + if (fd.isMapField()) { + // Map field points to an auto-generated message entry type with the definition: + // message MapEntry { + // K key = 1; + // V value = 2; + // } + final FieldDescriptor valueField = fd.getMessageType().findFieldByNumber(2); + if (valueField.getJavaType() == Descriptors.FieldDescriptor.JavaType.ENUM) { + enumVerifier = + new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return valueField.getEnumType().findValueByNumber(number) != null; + } + }; + } + builder.withField( + forMapField( + field, + number, + SchemaUtil.getMapDefaultEntry(messageType, fd.getName()), + enumVerifier)); + continue; + } + + if (fd.isRepeated()) { + // Repeated fields are not presence-checked. + if (enumVerifier != null) { + if (fd.isPacked()) { + builder.withField( + forPackedFieldWithEnumVerifier( + field, number, type, enumVerifier, cachedSizeField(messageType, fd))); + } else { + builder.withField(forFieldWithEnumVerifier(field, number, type, enumVerifier)); + } + } else if (fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + builder.withField( + forRepeatedMessageField( + field, number, type, getTypeForRepeatedMessageField(messageType, fd))); + } else { + if (fd.isPacked()) { + builder.withField( + forPackedField(field, number, type, cachedSizeField(messageType, fd))); + } else { + builder.withField(forField(field, number, type, enforceUtf8)); + } + } + continue; + } + + if (bitField == null) { + // Lazy-create the next bitfield since we know it must exist. + bitField = bitField(messageType, bitFieldIndex); + } + + // It's a presence-checked field. + if (fd.isRequired()) { + builder.withField( + forProto2RequiredField( + field, number, type, bitField, presenceMask, enforceUtf8, enumVerifier)); + } else { + builder.withField( + forProto2OptionalField( + field, number, type, bitField, presenceMask, enforceUtf8, enumVerifier)); + } + } + + // Update the presence mask for the next iteration. If the shift clears out the mask, we will + // go to the next bitField. + presenceMask <<= 1; + if (presenceMask == 0) { + bitField = null; + presenceMask = 1; + bitFieldIndex++; + } + } + + List<Integer> fieldsToCheckIsInitialized = new ArrayList<Integer>(); + for (int i = 0; i < fieldDescriptors.size(); ++i) { + FieldDescriptor fd = fieldDescriptors.get(i); + if (fd.isRequired() + || (fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE + && needsIsInitializedCheck(fd.getMessageType()))) { + fieldsToCheckIsInitialized.add(fd.getNumber()); + } + } + int[] numbers = new int[fieldsToCheckIsInitialized.size()]; + for (int i = 0; i < fieldsToCheckIsInitialized.size(); i++) { + numbers[i] = fieldsToCheckIsInitialized.get(i); + } + builder.withCheckInitialized(numbers); + + return builder.build(); + } + + private static StructuralMessageInfo convertProto3( + Class<?> messageType, Descriptor messageDescriptor) { + List<FieldDescriptor> fieldDescriptors = messageDescriptor.getFields(); + StructuralMessageInfo.Builder builder = + StructuralMessageInfo.newBuilder(fieldDescriptors.size()); + builder.withDefaultInstance(getDefaultInstance(messageType)); + builder.withSyntax(ProtoSyntax.PROTO3); + + OneofState oneofState = new OneofState(); + boolean enforceUtf8 = true; + for (int i = 0; i < fieldDescriptors.size(); ++i) { + FieldDescriptor fd = fieldDescriptors.get(i); + if (fd.getContainingOneof() != null) { + // Build a oneof member field. + builder.withField(buildOneofMember(messageType, fd, oneofState, enforceUtf8, null)); + continue; + } + if (fd.isMapField()) { + builder.withField( + forMapField( + field(messageType, fd), + fd.getNumber(), + SchemaUtil.getMapDefaultEntry(messageType, fd.getName()), + null)); + continue; + } + if (fd.isRepeated() && fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + builder.withField( + forRepeatedMessageField( + field(messageType, fd), + fd.getNumber(), + getFieldType(fd), + getTypeForRepeatedMessageField(messageType, fd))); + continue; + } + if (fd.isPacked()) { + builder.withField( + forPackedField( + field(messageType, fd), + fd.getNumber(), + getFieldType(fd), + cachedSizeField(messageType, fd))); + } else { + builder.withField( + forField(field(messageType, fd), fd.getNumber(), getFieldType(fd), enforceUtf8)); + } + } + + return builder.build(); + } + + /** Builds info for a oneof member field. */ + private static FieldInfo buildOneofMember( + Class<?> messageType, + FieldDescriptor fd, + OneofState oneofState, + boolean enforceUtf8, + Internal.EnumVerifier enumVerifier) { + OneofInfo oneof = oneofState.getOneof(messageType, fd.getContainingOneof()); + FieldType type = getFieldType(fd); + Class<?> oneofStoredType = getOneofStoredType(messageType, fd, type); + return forOneofMemberField( + fd.getNumber(), type, oneof, oneofStoredType, enforceUtf8, enumVerifier); + } + + private static Class<?> getOneofStoredType( + Class<?> messageType, FieldDescriptor fd, FieldType type) { + switch (type.getJavaType()) { + case BOOLEAN: + return Boolean.class; + case BYTE_STRING: + return ByteString.class; + case DOUBLE: + return Double.class; + case FLOAT: + return Float.class; + case ENUM: + case INT: + return Integer.class; + case LONG: + return Long.class; + case STRING: + return String.class; + case MESSAGE: + return getOneofStoredTypeForMessage(messageType, fd); + default: + throw new IllegalArgumentException("Invalid type for oneof: " + type); + } + } + + private static FieldType getFieldType(FieldDescriptor fd) { + switch (fd.getType()) { + case BOOL: + if (!fd.isRepeated()) { + return FieldType.BOOL; + } + return fd.isPacked() ? FieldType.BOOL_LIST_PACKED : FieldType.BOOL_LIST; + case BYTES: + return fd.isRepeated() ? FieldType.BYTES_LIST : FieldType.BYTES; + case DOUBLE: + if (!fd.isRepeated()) { + return FieldType.DOUBLE; + } + return fd.isPacked() ? FieldType.DOUBLE_LIST_PACKED : FieldType.DOUBLE_LIST; + case ENUM: + if (!fd.isRepeated()) { + return FieldType.ENUM; + } + return fd.isPacked() ? FieldType.ENUM_LIST_PACKED : FieldType.ENUM_LIST; + case FIXED32: + if (!fd.isRepeated()) { + return FieldType.FIXED32; + } + return fd.isPacked() ? FieldType.FIXED32_LIST_PACKED : FieldType.FIXED32_LIST; + case FIXED64: + if (!fd.isRepeated()) { + return FieldType.FIXED64; + } + return fd.isPacked() ? FieldType.FIXED64_LIST_PACKED : FieldType.FIXED64_LIST; + case FLOAT: + if (!fd.isRepeated()) { + return FieldType.FLOAT; + } + return fd.isPacked() ? FieldType.FLOAT_LIST_PACKED : FieldType.FLOAT_LIST; + case GROUP: + return fd.isRepeated() ? FieldType.GROUP_LIST : FieldType.GROUP; + case INT32: + if (!fd.isRepeated()) { + return FieldType.INT32; + } + return fd.isPacked() ? FieldType.INT32_LIST_PACKED : FieldType.INT32_LIST; + case INT64: + if (!fd.isRepeated()) { + return FieldType.INT64; + } + return fd.isPacked() ? FieldType.INT64_LIST_PACKED : FieldType.INT64_LIST; + case MESSAGE: + if (fd.isMapField()) { + return FieldType.MAP; + } + return fd.isRepeated() ? FieldType.MESSAGE_LIST : FieldType.MESSAGE; + case SFIXED32: + if (!fd.isRepeated()) { + return FieldType.SFIXED32; + } + return fd.isPacked() ? FieldType.SFIXED32_LIST_PACKED : FieldType.SFIXED32_LIST; + case SFIXED64: + if (!fd.isRepeated()) { + return FieldType.SFIXED64; + } + return fd.isPacked() ? FieldType.SFIXED64_LIST_PACKED : FieldType.SFIXED64_LIST; + case SINT32: + if (!fd.isRepeated()) { + return FieldType.SINT32; + } + return fd.isPacked() ? FieldType.SINT32_LIST_PACKED : FieldType.SINT32_LIST; + case SINT64: + if (!fd.isRepeated()) { + return FieldType.SINT64; + } + return fd.isPacked() ? FieldType.SINT64_LIST_PACKED : FieldType.SINT64_LIST; + case STRING: + return fd.isRepeated() ? FieldType.STRING_LIST : FieldType.STRING; + case UINT32: + if (!fd.isRepeated()) { + return FieldType.UINT32; + } + return fd.isPacked() ? FieldType.UINT32_LIST_PACKED : FieldType.UINT32_LIST; + case UINT64: + if (!fd.isRepeated()) { + return FieldType.UINT64; + } + return fd.isPacked() ? FieldType.UINT64_LIST_PACKED : FieldType.UINT64_LIST; + default: + throw new IllegalArgumentException("Unsupported field type: " + fd.getType()); + } + } + + private static Field bitField(Class<?> messageType, int index) { + return field(messageType, "bitField" + index + "_"); + } + + private static Field field(Class<?> messageType, FieldDescriptor fd) { + return field(messageType, getFieldName(fd)); + } + + private static Field cachedSizeField(Class<?> messageType, FieldDescriptor fd) { + return field(messageType, getCachedSizeFieldName(fd)); + } + + private static Field field(Class<?> messageType, String fieldName) { + try { + return messageType.getDeclaredField(fieldName); + } catch (Exception e) { + throw new IllegalArgumentException( + "Unable to find field " + fieldName + " in message class " + messageType.getName()); + } + } + + static String getFieldName(FieldDescriptor fd) { + String name = (fd.getType() == FieldDescriptor.Type.GROUP) + ? fd.getMessageType().getName() + : fd.getName(); + String suffix = specialFieldNames.contains(name) ? "__" : "_"; + return snakeCaseToCamelCase(name) + suffix; + } + + private static String getCachedSizeFieldName(FieldDescriptor fd) { + return snakeCaseToCamelCase(fd.getName()) + "MemoizedSerializedSize"; + } + + /** + * This method must match exactly with the corresponding function in protocol compiler. See: + * https://github.com/google/protobuf/blob/v3.0.0/src/google/protobuf/compiler/java/java_helpers.cc#L153 + */ + private static String snakeCaseToCamelCase(String snakeCase) { + StringBuilder sb = new StringBuilder(snakeCase.length() + 1); + boolean capNext = false; + for (int ctr = 0; ctr < snakeCase.length(); ctr++) { + char next = snakeCase.charAt(ctr); + if (next == '_') { + capNext = true; + } else if (Character.isDigit(next)) { + sb.append(next); + capNext = true; + } else if (capNext) { + sb.append(Character.toUpperCase(next)); + capNext = false; + } else if (ctr == 0) { + sb.append(Character.toLowerCase(next)); + } else { + sb.append(next); + } + } + return sb.toString(); + } + + /** + * Inspects the message to identify the stored type for a message field that is part of a oneof. + */ + private static Class<?> getOneofStoredTypeForMessage(Class<?> messageType, FieldDescriptor fd) { + try { + String name = fd.getType() == Type.GROUP ? fd.getMessageType().getName() : fd.getName(); + Method getter = messageType.getDeclaredMethod(getterForField(name)); + return getter.getReturnType(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** Inspects the message to identify the message type of a repeated message field. */ + private static Class<?> getTypeForRepeatedMessageField(Class<?> messageType, FieldDescriptor fd) { + try { + String name = fd.getType() == Type.GROUP ? fd.getMessageType().getName() : fd.getName(); + Method getter = messageType.getDeclaredMethod(getterForField(name), int.class); + return getter.getReturnType(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** Constructs the name of the get method for the given field in the proto. */ + private static String getterForField(String snakeCase) { + String camelCase = snakeCaseToCamelCase(snakeCase); + StringBuilder builder = new StringBuilder("get"); + // Capitalize the first character in the field name. + builder.append(Character.toUpperCase(camelCase.charAt(0))); + builder.append(camelCase.substring(1, camelCase.length())); + return builder.toString(); + } + + private static final class OneofState { + private OneofInfo[] oneofs = new OneofInfo[2]; + + OneofInfo getOneof(Class<?> messageType, OneofDescriptor desc) { + int index = desc.getIndex(); + if (index >= oneofs.length) { + // Grow the array. + oneofs = Arrays.copyOf(oneofs, index * 2); + } + OneofInfo info = oneofs[index]; + if (info == null) { + info = newInfo(messageType, desc); + oneofs[index] = info; + } + return info; + } + + private static OneofInfo newInfo(Class<?> messageType, OneofDescriptor desc) { + String camelCase = snakeCaseToCamelCase(desc.getName()); + String valueFieldName = camelCase + "_"; + String caseFieldName = camelCase + "Case_"; + + return new OneofInfo( + desc.getIndex(), field(messageType, caseFieldName), field(messageType, valueFieldName)); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java index 0ce5f5493f..11843b6c39 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java @@ -110,12 +110,23 @@ public class ExtensionRegistryLite { return ExtensionRegistryFactory.create(); } + private static volatile ExtensionRegistryLite emptyRegistry; + /** * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or {@code * ExtensionRegistry} (if the full (non-Lite) proto libraries are available). */ public static ExtensionRegistryLite getEmptyRegistry() { - return ExtensionRegistryFactory.createEmpty(); + ExtensionRegistryLite result = emptyRegistry; + if (result == null) { + synchronized (ExtensionRegistryLite.class) { + result = emptyRegistry; + if (result == null) { + result = emptyRegistry = ExtensionRegistryFactory.createEmpty(); + } + } + } + return result; } diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java new file mode 100755 index 0000000000..2eae22d26a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchema.java @@ -0,0 +1,98 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.Map; + +abstract class ExtensionSchema<T extends FieldSet.FieldDescriptorLite<T>> { + + /** Returns true for messages that support extensions. */ + abstract boolean hasExtensions(MessageLite prototype); + + /** Returns the extension {@link FieldSet} for the message instance. */ + abstract FieldSet<T> getExtensions(Object message); + + /** Replaces the extension {@link FieldSet} for the message instance. */ + abstract void setExtensions(Object message, FieldSet<T> extensions); + + /** Returns the extension {@link FieldSet} and ensures it's mutable. */ + abstract FieldSet<T> getMutableExtensions(Object message); + + /** Marks the extension {@link FieldSet} as immutable. */ + abstract void makeImmutable(Object message); + + /** + * Parses an extension. Returns the passed-in unknownFields parameter if no unknown enum value is + * found or a modified unknownFields (a new instance if the passed-in unknownFields is null) + * containing unknown enum values found while parsing. + * + * @param <UT> The type used to store unknown fields. It's either UnknownFieldSet in full runtime + * or UnknownFieldSetLite in lite runtime. + */ + abstract <UT, UB> UB parseExtension( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet<T> extensions, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) + throws IOException; + + /** Gets the field number of an extension entry. */ + abstract int extensionNumber(Map.Entry<?, ?> extension); + + /** Serializes one extension entry. */ + abstract void serializeExtension(Writer writer, Map.Entry<?, ?> extension) throws IOException; + + /** Finds an extension by field number. */ + abstract Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number); + + /** Parses a length-prefixed MessageSet item from the reader. */ + abstract void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet<T> extensions) + throws IOException; + + /** + * Parses the entire content of a {@link ByteString} as one MessageSet item. Unlike {@link + * #parseLengthPrefixedMessageSetItem}, there isn't a length-prefix. + */ + abstract void parseMessageSetItem( + ByteString data, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet<T> extensions) + throws IOException; +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java new file mode 100755 index 0000000000..90558518b9 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaFull.java @@ -0,0 +1,547 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.Descriptors.EnumValueDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +final class ExtensionSchemaFull extends ExtensionSchema<FieldDescriptor> { + + private static final long EXTENSION_FIELD_OFFSET = getExtensionsFieldOffset(); + + private static <T> long getExtensionsFieldOffset() { + try { + Field field = GeneratedMessageV3.ExtendableMessage.class.getDeclaredField("extensions"); + return UnsafeUtil.objectFieldOffset(field); + } catch (Throwable e) { + throw new IllegalStateException("Unable to lookup extension field offset"); + } + } + + @Override + boolean hasExtensions(MessageLite prototype) { + return prototype instanceof GeneratedMessageV3.ExtendableMessage; + } + + @Override + public FieldSet<FieldDescriptor> getExtensions(Object message) { + return (FieldSet<FieldDescriptor>) UnsafeUtil.getObject(message, EXTENSION_FIELD_OFFSET); + } + + @Override + void setExtensions(Object message, FieldSet<FieldDescriptor> extensions) { + UnsafeUtil.putObject(message, EXTENSION_FIELD_OFFSET, extensions); + } + + @Override + FieldSet<FieldDescriptor> getMutableExtensions(Object message) { + FieldSet<FieldDescriptor> extensions = getExtensions(message); + if (extensions.isImmutable()) { + extensions = extensions.clone(); + setExtensions(message, extensions); + } + return extensions; + } + + @Override + void makeImmutable(Object message) { + getExtensions(message).makeImmutable(); + } + + @Override + <UT, UB> UB parseExtension( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet<FieldDescriptor> extensions, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) + throws IOException { + ExtensionRegistry.ExtensionInfo extension = (ExtensionRegistry.ExtensionInfo) extensionObject; + int fieldNumber = extension.descriptor.getNumber(); + + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + Object value = null; + switch (extension.descriptor.getLiteType()) { + case DOUBLE: + { + List<Double> list = new ArrayList<Double>(); + reader.readDoubleList(list); + value = list; + break; + } + case FLOAT: + { + List<Float> list = new ArrayList<Float>(); + reader.readFloatList(list); + value = list; + break; + } + case INT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readInt64List(list); + value = list; + break; + } + case UINT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readUInt64List(list); + value = list; + break; + } + case INT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readInt32List(list); + value = list; + break; + } + case FIXED64: + { + List<Long> list = new ArrayList<Long>(); + reader.readFixed64List(list); + value = list; + break; + } + case FIXED32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readFixed32List(list); + value = list; + break; + } + case BOOL: + { + List<Boolean> list = new ArrayList<Boolean>(); + reader.readBoolList(list); + value = list; + break; + } + case UINT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readUInt32List(list); + value = list; + break; + } + case SFIXED32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readSFixed32List(list); + value = list; + break; + } + case SFIXED64: + { + List<Long> list = new ArrayList<Long>(); + reader.readSFixed64List(list); + value = list; + break; + } + case SINT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readSInt32List(list); + value = list; + break; + } + case SINT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readSInt64List(list); + value = list; + break; + } + case ENUM: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readEnumList(list); + List<EnumValueDescriptor> enumList = new ArrayList<EnumValueDescriptor>(); + for (int number : list) { + EnumValueDescriptor enumDescriptor = + extension.descriptor.getEnumType().findValueByNumber(number); + if (enumDescriptor != null) { + enumList.add(enumDescriptor); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + } + value = enumList; + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + extensions.setField(extension.descriptor, value); + } else { + Object value = null; + // Enum is a special case because unknown enum values will be put into UnknownFieldSetLite. + if (extension.descriptor.getLiteType() == WireFormat.FieldType.ENUM) { + int number = reader.readInt32(); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(number); + if (enumValue == null) { + return SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + value = enumValue; + } else { + switch (extension.descriptor.getLiteType()) { + case DOUBLE: + value = reader.readDouble(); + break; + case FLOAT: + value = reader.readFloat(); + break; + case INT64: + value = reader.readInt64(); + break; + case UINT64: + value = reader.readUInt64(); + break; + case INT32: + value = reader.readInt32(); + break; + case FIXED64: + value = reader.readFixed64(); + break; + case FIXED32: + value = reader.readFixed32(); + break; + case BOOL: + value = reader.readBool(); + break; + case BYTES: + value = reader.readBytes(); + break; + case UINT32: + value = reader.readUInt32(); + break; + case SFIXED32: + value = reader.readSFixed32(); + break; + case SFIXED64: + value = reader.readSFixed64(); + break; + case SINT32: + value = reader.readSInt32(); + break; + case SINT64: + value = reader.readSInt64(); + break; + + case STRING: + value = reader.readString(); + break; + case GROUP: + value = reader.readGroup(extension.defaultInstance.getClass(), extensionRegistry); + break; + + case MESSAGE: + value = reader.readMessage(extension.defaultInstance.getClass(), extensionRegistry); + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.descriptor.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.descriptor.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return unknownFields; + } + + @Override + int extensionNumber(Map.Entry<?, ?> extension) { + FieldDescriptor descriptor = (FieldDescriptor) extension.getKey(); + return descriptor.getNumber(); + } + + @Override + void serializeExtension(Writer writer, Map.Entry<?, ?> extension) throws IOException { + FieldDescriptor descriptor = (FieldDescriptor) extension.getKey(); + if (descriptor.isRepeated()) { + switch (descriptor.getLiteType()) { + case DOUBLE: + SchemaUtil.writeDoubleList( + descriptor.getNumber(), + (List<Double>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FLOAT: + SchemaUtil.writeFloatList( + descriptor.getNumber(), + (List<Float>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT64: + SchemaUtil.writeInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case UINT64: + SchemaUtil.writeUInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT32: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED64: + SchemaUtil.writeFixed64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED32: + SchemaUtil.writeFixed32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BOOL: + SchemaUtil.writeBoolList( + descriptor.getNumber(), + (List<Boolean>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BYTES: + SchemaUtil.writeBytesList( + descriptor.getNumber(), (List<ByteString>) extension.getValue(), writer); + break; + case UINT32: + SchemaUtil.writeUInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED32: + SchemaUtil.writeSFixed32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED64: + SchemaUtil.writeSFixed64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT32: + SchemaUtil.writeSInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT64: + SchemaUtil.writeSInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case ENUM: + { + List<EnumValueDescriptor> enumList = (List<EnumValueDescriptor>) extension.getValue(); + List<Integer> list = new ArrayList<Integer>(); + for (EnumValueDescriptor d : enumList) { + list.add(d.getNumber()); + } + SchemaUtil.writeInt32List(descriptor.getNumber(), list, writer, descriptor.isPacked()); + break; + } + case STRING: + SchemaUtil.writeStringList( + descriptor.getNumber(), (List<String>) extension.getValue(), writer); + break; + case GROUP: + SchemaUtil.writeGroupList(descriptor.getNumber(), (List<?>) extension.getValue(), writer); + break; + case MESSAGE: + SchemaUtil.writeMessageList( + descriptor.getNumber(), (List<?>) extension.getValue(), writer); + break; + } + } else { + switch (descriptor.getLiteType()) { + case DOUBLE: + writer.writeDouble(descriptor.getNumber(), (Double) extension.getValue()); + break; + case FLOAT: + writer.writeFloat(descriptor.getNumber(), (Float) extension.getValue()); + break; + case INT64: + writer.writeInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case UINT64: + writer.writeUInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case INT32: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case FIXED64: + writer.writeFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case FIXED32: + writer.writeFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case BOOL: + writer.writeBool(descriptor.getNumber(), (Boolean) extension.getValue()); + break; + case BYTES: + writer.writeBytes(descriptor.getNumber(), (ByteString) extension.getValue()); + break; + case UINT32: + writer.writeUInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED32: + writer.writeSFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED64: + writer.writeSFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case SINT32: + writer.writeSInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SINT64: + writer.writeSInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case ENUM: + writer.writeInt32( + descriptor.getNumber(), ((EnumValueDescriptor) extension.getValue()).getNumber()); + break; + case STRING: + writer.writeString(descriptor.getNumber(), (String) extension.getValue()); + break; + case GROUP: + writer.writeGroup(descriptor.getNumber(), extension.getValue()); + break; + case MESSAGE: + writer.writeMessage(descriptor.getNumber(), extension.getValue()); + break; + } + } + } + + @Override + Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number) { + return ((ExtensionRegistry) extensionRegistry) + .findExtensionByNumber(((Message) defaultInstance).getDescriptorForType(), number); + } + + @Override + void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet<FieldDescriptor> extensions) + throws IOException { + ExtensionRegistry.ExtensionInfo extensionInfo = (ExtensionRegistry.ExtensionInfo) extension; + + if (ExtensionRegistryLite.isEagerlyParseMessageSets()) { + Object value = + reader.readMessage(extensionInfo.defaultInstance.getClass(), extensionRegistry); + extensions.setField(extensionInfo.descriptor, value); + } else { + extensions.setField( + extensionInfo.descriptor, + new LazyField(extensionInfo.defaultInstance, extensionRegistry, reader.readBytes())); + } + } + + @Override + void parseMessageSetItem( + ByteString data, + Object extension, + ExtensionRegistryLite extensionRegistry, + FieldSet<FieldDescriptor> extensions) + throws IOException { + ExtensionRegistry.ExtensionInfo extensionInfo = (ExtensionRegistry.ExtensionInfo) extension; + Object value = extensionInfo.defaultInstance.newBuilderForType().buildPartial(); + + if (ExtensionRegistryLite.isEagerlyParseMessageSets()) { + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(data.toByteArray()), true); + Protobuf.getInstance().mergeFrom(value, reader, extensionRegistry); + extensions.setField(extensionInfo.descriptor, value); + + if (reader.getFieldNumber() != Reader.READ_DONE) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + } else { + extensions.setField( + extensionInfo.descriptor, + new LazyField(extensionInfo.defaultInstance, extensionRegistry, data)); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java new file mode 100755 index 0000000000..7f6846eb0a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemaLite.java @@ -0,0 +1,541 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.GeneratedMessageLite.ExtensionDescriptor; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +final class ExtensionSchemaLite extends ExtensionSchema<ExtensionDescriptor> { + + @Override + boolean hasExtensions(MessageLite prototype) { + return prototype instanceof GeneratedMessageLite.ExtendableMessage; + } + + @Override + FieldSet<ExtensionDescriptor> getExtensions(Object message) { + return ((GeneratedMessageLite.ExtendableMessage<?, ?>) message).extensions; + } + + @Override + void setExtensions(Object message, FieldSet<ExtensionDescriptor> extensions) { + ((GeneratedMessageLite.ExtendableMessage<?, ?>) message).extensions = extensions; + } + + @Override + FieldSet<ExtensionDescriptor> getMutableExtensions(Object message) { + return ((GeneratedMessageLite.ExtendableMessage<?, ?>) message).ensureExtensionsAreMutable(); + } + + @Override + void makeImmutable(Object message) { + getExtensions(message).makeImmutable(); + } + + @Override + <UT, UB> UB parseExtension( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet<ExtensionDescriptor> extensions, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) + throws IOException { + GeneratedMessageLite.GeneratedExtension<?, ?> extension = + (GeneratedMessageLite.GeneratedExtension<?, ?>) extensionObject; + int fieldNumber = extension.getNumber(); + + if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) { + Object value = null; + switch (extension.getLiteType()) { + case DOUBLE: + { + List<Double> list = new ArrayList<Double>(); + reader.readDoubleList(list); + value = list; + break; + } + case FLOAT: + { + List<Float> list = new ArrayList<Float>(); + reader.readFloatList(list); + value = list; + break; + } + case INT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readInt64List(list); + value = list; + break; + } + case UINT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readUInt64List(list); + value = list; + break; + } + case INT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readInt32List(list); + value = list; + break; + } + case FIXED64: + { + List<Long> list = new ArrayList<Long>(); + reader.readFixed64List(list); + value = list; + break; + } + case FIXED32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readFixed32List(list); + value = list; + break; + } + case BOOL: + { + List<Boolean> list = new ArrayList<Boolean>(); + reader.readBoolList(list); + value = list; + break; + } + case UINT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readUInt32List(list); + value = list; + break; + } + case SFIXED32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readSFixed32List(list); + value = list; + break; + } + case SFIXED64: + { + List<Long> list = new ArrayList<Long>(); + reader.readSFixed64List(list); + value = list; + break; + } + case SINT32: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readSInt32List(list); + value = list; + break; + } + case SINT64: + { + List<Long> list = new ArrayList<Long>(); + reader.readSInt64List(list); + value = list; + break; + } + case ENUM: + { + List<Integer> list = new ArrayList<Integer>(); + reader.readEnumList(list); + unknownFields = + SchemaUtil.filterUnknownEnumList( + fieldNumber, + list, + extension.descriptor.getEnumType(), + unknownFields, + unknownFieldSchema); + value = list; + break; + } + default: + throw new IllegalStateException( + "Type cannot be packed: " + extension.descriptor.getLiteType()); + } + extensions.setField(extension.descriptor, value); + } else { + Object value = null; + // Enum is a special case becasue unknown enum values will be put into UnknownFieldSetLite. + if (extension.getLiteType() == WireFormat.FieldType.ENUM) { + int number = reader.readInt32(); + Object enumValue = extension.descriptor.getEnumType().findValueByNumber(number); + if (enumValue == null) { + return SchemaUtil.storeUnknownEnum( + fieldNumber, number, unknownFields, unknownFieldSchema); + } + // Note, we store the integer value instead of the actual enum object in FieldSet. + // This is also different from full-runtime where we store EnumValueDescriptor. + value = number; + } else { + switch (extension.getLiteType()) { + case DOUBLE: + value = reader.readDouble(); + break; + case FLOAT: + value = reader.readFloat(); + break; + case INT64: + value = reader.readInt64(); + break; + case UINT64: + value = reader.readUInt64(); + break; + case INT32: + value = reader.readInt32(); + break; + case FIXED64: + value = reader.readFixed64(); + break; + case FIXED32: + value = reader.readFixed32(); + break; + case BOOL: + value = reader.readBool(); + break; + case BYTES: + value = reader.readBytes(); + break; + case UINT32: + value = reader.readUInt32(); + break; + case SFIXED32: + value = reader.readSFixed32(); + break; + case SFIXED64: + value = reader.readSFixed64(); + break; + case SINT32: + value = reader.readSInt32(); + break; + case SINT64: + value = reader.readSInt64(); + break; + + case STRING: + value = reader.readString(); + break; + case GROUP: + value = + reader.readGroup( + extension.getMessageDefaultInstance().getClass(), extensionRegistry); + break; + + case MESSAGE: + value = + reader.readMessage( + extension.getMessageDefaultInstance().getClass(), extensionRegistry); + break; + + case ENUM: + throw new IllegalStateException("Shouldn't reach here."); + } + } + if (extension.isRepeated()) { + extensions.addRepeatedField(extension.descriptor, value); + } else { + switch (extension.getLiteType()) { + case MESSAGE: + case GROUP: + Object oldValue = extensions.getField(extension.descriptor); + if (oldValue != null) { + value = Internal.mergeMessage(oldValue, value); + } + break; + default: + break; + } + extensions.setField(extension.descriptor, value); + } + } + return unknownFields; + } + + @Override + int extensionNumber(Map.Entry<?, ?> extension) { + GeneratedMessageLite.ExtensionDescriptor descriptor = + (GeneratedMessageLite.ExtensionDescriptor) extension.getKey(); + return descriptor.getNumber(); + } + + @Override + void serializeExtension(Writer writer, Map.Entry<?, ?> extension) throws IOException { + GeneratedMessageLite.ExtensionDescriptor descriptor = + (GeneratedMessageLite.ExtensionDescriptor) extension.getKey(); + if (descriptor.isRepeated()) { + switch (descriptor.getLiteType()) { + case DOUBLE: + SchemaUtil.writeDoubleList( + descriptor.getNumber(), + (List<Double>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FLOAT: + SchemaUtil.writeFloatList( + descriptor.getNumber(), + (List<Float>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT64: + SchemaUtil.writeInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case UINT64: + SchemaUtil.writeUInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case INT32: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED64: + SchemaUtil.writeFixed64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case FIXED32: + SchemaUtil.writeFixed32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BOOL: + SchemaUtil.writeBoolList( + descriptor.getNumber(), + (List<Boolean>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case BYTES: + SchemaUtil.writeBytesList( + descriptor.getNumber(), (List<ByteString>) extension.getValue(), writer); + break; + case UINT32: + SchemaUtil.writeUInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED32: + SchemaUtil.writeSFixed32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SFIXED64: + SchemaUtil.writeSFixed64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT32: + SchemaUtil.writeSInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case SINT64: + SchemaUtil.writeSInt64List( + descriptor.getNumber(), + (List<Long>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case ENUM: + SchemaUtil.writeInt32List( + descriptor.getNumber(), + (List<Integer>) extension.getValue(), + writer, + descriptor.isPacked()); + break; + case STRING: + SchemaUtil.writeStringList( + descriptor.getNumber(), (List<String>) extension.getValue(), writer); + break; + case GROUP: + { + List<?> data = (List<?>) extension.getValue(); + if (data != null && !data.isEmpty()) { + SchemaUtil.writeGroupList( + descriptor.getNumber(), + (List<?>) extension.getValue(), + writer, + Protobuf.getInstance().schemaFor(data.get(0).getClass())); + } + } + break; + case MESSAGE: + { + List<?> data = (List<?>) extension.getValue(); + if (data != null && !data.isEmpty()) { + SchemaUtil.writeMessageList( + descriptor.getNumber(), + (List<?>) extension.getValue(), + writer, + Protobuf.getInstance().schemaFor(data.get(0).getClass())); + } + } + break; + } + } else { + switch (descriptor.getLiteType()) { + case DOUBLE: + writer.writeDouble(descriptor.getNumber(), (Double) extension.getValue()); + break; + case FLOAT: + writer.writeFloat(descriptor.getNumber(), (Float) extension.getValue()); + break; + case INT64: + writer.writeInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case UINT64: + writer.writeUInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case INT32: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case FIXED64: + writer.writeFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case FIXED32: + writer.writeFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case BOOL: + writer.writeBool(descriptor.getNumber(), (Boolean) extension.getValue()); + break; + case BYTES: + writer.writeBytes(descriptor.getNumber(), (ByteString) extension.getValue()); + break; + case UINT32: + writer.writeUInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED32: + writer.writeSFixed32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SFIXED64: + writer.writeSFixed64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case SINT32: + writer.writeSInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case SINT64: + writer.writeSInt64(descriptor.getNumber(), (Long) extension.getValue()); + break; + case ENUM: + writer.writeInt32(descriptor.getNumber(), (Integer) extension.getValue()); + break; + case STRING: + writer.writeString(descriptor.getNumber(), (String) extension.getValue()); + break; + case GROUP: + writer.writeGroup( + descriptor.getNumber(), + extension.getValue(), + Protobuf.getInstance().schemaFor(extension.getValue().getClass())); + break; + case MESSAGE: + writer.writeMessage( + descriptor.getNumber(), + extension.getValue(), + Protobuf.getInstance().schemaFor(extension.getValue().getClass())); + break; + } + } + } + + @Override + Object findExtensionByNumber( + ExtensionRegistryLite extensionRegistry, MessageLite defaultInstance, int number) { + return extensionRegistry.findLiteExtensionByNumber(defaultInstance, number); + } + + @Override + void parseLengthPrefixedMessageSetItem( + Reader reader, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet<ExtensionDescriptor> extensions) + throws IOException { + GeneratedMessageLite.GeneratedExtension<?, ?> extension = + (GeneratedMessageLite.GeneratedExtension<?, ?>) extensionObject; + Object value = + reader.readMessage(extension.getMessageDefaultInstance().getClass(), extensionRegistry); + extensions.setField(extension.descriptor, value); + } + + @Override + void parseMessageSetItem( + ByteString data, + Object extensionObject, + ExtensionRegistryLite extensionRegistry, + FieldSet<ExtensionDescriptor> extensions) + throws IOException { + GeneratedMessageLite.GeneratedExtension<?, ?> extension = + (GeneratedMessageLite.GeneratedExtension<?, ?>) extensionObject; + Object value = extension.getMessageDefaultInstance().newBuilderForType().buildPartial(); + + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(data.toByteArray()), true); + + Protobuf.getInstance().mergeFrom(value, reader, extensionRegistry); + extensions.setField(extension.descriptor, value); + + if (reader.getFieldNumber() != Reader.READ_DONE) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java b/java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java new file mode 100755 index 0000000000..46ce327d13 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ExtensionSchemas.java @@ -0,0 +1,56 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +final class ExtensionSchemas { + private static final ExtensionSchema<?> LITE_SCHEMA = new ExtensionSchemaLite(); + private static final ExtensionSchema<?> FULL_SCHEMA = loadSchemaForFullRuntime(); + + private static ExtensionSchema<?> loadSchemaForFullRuntime() { + try { + Class<?> clazz = Class.forName("com.google.protobuf.ExtensionSchemaFull"); + return (ExtensionSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } + + static ExtensionSchema<?> lite() { + return LITE_SCHEMA; + } + + static ExtensionSchema<?> full() { + if (FULL_SCHEMA == null) { + throw new IllegalStateException("Protobuf runtime is not correctly loaded."); + } + return FULL_SCHEMA; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/FieldInfo.java b/java/core/src/main/java/com/google/protobuf/FieldInfo.java new file mode 100755 index 0000000000..71a307a895 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/FieldInfo.java @@ -0,0 +1,577 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import com.google.protobuf.Internal.EnumVerifier; +import java.lang.reflect.Field; + +/** Information for a single field in a protobuf message class. */ +@ExperimentalApi +final class FieldInfo implements Comparable<FieldInfo> { + private final Field field; + private final FieldType type; + private final Class<?> messageClass; // The message type for repeated message fields. + private final int fieldNumber; + private final Field presenceField; + private final int presenceMask; + private final boolean required; + private final boolean enforceUtf8; + private final OneofInfo oneof; + private final Field cachedSizeField; + /** + * The actual type stored in the oneof value for this field. Since the oneof value is an {@link + * Object}, primitives will store their boxed type. Only valid in conjunction with {@link #oneof} + * (both must be either null or non-null. + */ + private final Class<?> oneofStoredType; + + // TODO(liujisi): make map default entry lazy? + private final Object mapDefaultEntry; + + private final EnumVerifier enumVerifier; + + /** Constructs a new descriptor for a field. */ + public static FieldInfo forField( + Field field, int fieldNumber, FieldType fieldType, boolean enforceUtf8) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { + throw new IllegalStateException("Shouldn't be called for repeated message fields."); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + /* cachedSizeField= */ null); + } + + /** Constructs a new descriptor for a packed field. */ + public static FieldInfo forPackedField( + Field field, int fieldNumber, FieldType fieldType, Field cachedSizeField) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + if (fieldType == FieldType.MESSAGE_LIST || fieldType == FieldType.GROUP_LIST) { + throw new IllegalStateException("Shouldn't be called for repeated message fields."); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + cachedSizeField); + } + + /** Constructs a new descriptor for a repeated message field. */ + public static FieldInfo forRepeatedMessageField( + Field field, int fieldNumber, FieldType fieldType, Class<?> messageClass) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(messageClass, "messageClass"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + messageClass, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ null, + /* cachedSizeField= */ null); + } + + public static FieldInfo forFieldWithEnumVerifier( + Field field, int fieldNumber, FieldType fieldType, EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + public static FieldInfo forPackedFieldWithEnumVerifier( + Field field, + int fieldNumber, + FieldType fieldType, + EnumVerifier enumVerifier, + Field cachedSizeField) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ false, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + cachedSizeField); + } + + /** Constructor for a proto2 optional field. */ + public static FieldInfo forProto2OptionalField( + Field field, + int fieldNumber, + FieldType fieldType, + Field presenceField, + int presenceMask, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); + if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { + throw new IllegalArgumentException( + "presenceMask must have exactly one bit set: " + presenceMask); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + presenceField, + presenceMask, + /* required= */ false, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + /** + * Constructor for a field that is part of a oneof. + * + * @param fieldNumber the unique field number for this field within the message. + * @param fieldType the type of the field (must be non-null). + * @param oneof the oneof for which this field is associated (must be non-null). + * @param oneofStoredType the actual type stored in the oneof value for this field. Since the + * oneof value is an {@link Object}, primitives will store their boxed type. Must be non-null. + * @param enforceUtf8 Only used for string fields. If {@code true}, will enforce UTF-8 on a string + * field. + * @return the {@link FieldInfo} describing this field. + */ + public static FieldInfo forOneofMemberField( + int fieldNumber, + FieldType fieldType, + OneofInfo oneof, + Class<?> oneofStoredType, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(fieldType, "fieldType"); + checkNotNull(oneof, "oneof"); + checkNotNull(oneofStoredType, "oneofStoredType"); + if (!fieldType.isScalar()) { + throw new IllegalArgumentException( + "Oneof is only supported for scalar fields. Field " + + fieldNumber + + " is of type " + + fieldType); + } + return new FieldInfo( + /* field= */ null, + fieldNumber, + fieldType, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + enforceUtf8, + oneof, + oneofStoredType, + /* mapDefaultEntry= */ null, + enumVerifier, + /* cachedSizeField= */ null); + } + + private static void checkFieldNumber(int fieldNumber) { + if (fieldNumber <= 0) { + throw new IllegalArgumentException("fieldNumber must be positive: " + fieldNumber); + } + } + + /** Constructor for a proto2 required field. */ + public static FieldInfo forProto2RequiredField( + Field field, + int fieldNumber, + FieldType fieldType, + Field presenceField, + int presenceMask, + boolean enforceUtf8, + EnumVerifier enumVerifier) { + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(presenceField, "presenceField"); + if (presenceField != null && !isExactlyOneBitSet(presenceMask)) { + throw new IllegalArgumentException( + "presenceMask must have exactly one bit set: " + presenceMask); + } + return new FieldInfo( + field, + fieldNumber, + fieldType, + /* messageClass= */ null, + presenceField, + presenceMask, + /* required= */ true, + enforceUtf8, + /* oneof= */ null, + /* oneofStoredType= */ null, + /* mapDefaultEntry= */ null, + /* enumVerifier= */ enumVerifier, + /* cachedSizeField= */ null); + } + + public static FieldInfo forMapField( + Field field, int fieldNumber, Object mapDefaultEntry, EnumVerifier enumVerifier) { + checkNotNull(mapDefaultEntry, "mapDefaultEntry"); + checkFieldNumber(fieldNumber); + checkNotNull(field, "field"); + return new FieldInfo( + field, + fieldNumber, + FieldType.MAP, + /* messageClass= */ null, + /* presenceField= */ null, + /* presenceMask= */ 0, + /* required= */ false, + /* enforceUtf8= */ true, + /* oneof= */ null, + /* oneofStoredType= */ null, + mapDefaultEntry, + enumVerifier, + /* cachedSizeField= */ null); + } + + private FieldInfo( + Field field, + int fieldNumber, + FieldType type, + Class<?> messageClass, + Field presenceField, + int presenceMask, + boolean required, + boolean enforceUtf8, + OneofInfo oneof, + Class<?> oneofStoredType, + Object mapDefaultEntry, + EnumVerifier enumVerifier, + Field cachedSizeField) { + this.field = field; + this.type = type; + this.messageClass = messageClass; + this.fieldNumber = fieldNumber; + this.presenceField = presenceField; + this.presenceMask = presenceMask; + this.required = required; + this.enforceUtf8 = enforceUtf8; + this.oneof = oneof; + this.oneofStoredType = oneofStoredType; + this.mapDefaultEntry = mapDefaultEntry; + this.enumVerifier = enumVerifier; + this.cachedSizeField = cachedSizeField; + } + + /** Gets the field number for the field. */ + public int getFieldNumber() { + return fieldNumber; + } + + /** Gets the subject {@link Field} of this descriptor. */ + public Field getField() { + return field; + } + + /** Gets the type information for the field. */ + public FieldType getType() { + return type; + } + + /** Gets the oneof for which this field is a member, or {@code null} if not part of a oneof. */ + public OneofInfo getOneof() { + return oneof; + } + + /** + * Gets the actual type stored in the oneof value by this field. Since the oneof value is an + * {@link Object}, primitives will store their boxed type. For non-oneof fields, this will always + * be {@code null}. + */ + public Class<?> getOneofStoredType() { + return oneofStoredType; + } + + /** Gets the {@code EnumVerifier} if the field is an enum field. */ + public EnumVerifier getEnumVerifier() { + return enumVerifier; + } + + @Override + public int compareTo(FieldInfo o) { + return fieldNumber - o.fieldNumber; + } + + /** + * For repeated message fields, returns the message type of the field. For other fields, returns + * {@code null}. + */ + public Class<?> getListElementType() { + return messageClass; + } + + /** Gets the presence bit field. Only valid for unary fields. For lists, returns {@code null}. */ + public Field getPresenceField() { + return presenceField; + } + + public Object getMapDefaultEntry() { + return mapDefaultEntry; + } + + /** + * If {@link #getPresenceField()} is non-{@code null}, returns the mask used to identify the + * presence bit for this field in the message. + */ + public int getPresenceMask() { + return presenceMask; + } + + /** Whether this is a required field. */ + public boolean isRequired() { + return required; + } + + /** + * Whether a UTF-8 should be enforced on string fields. Only applies to strings and string lists. + */ + public boolean isEnforceUtf8() { + return enforceUtf8; + } + + public Field getCachedSizeField() { + return cachedSizeField; + } + + /** + * For singular or repeated message fields, returns the message type. For other fields, returns + * {@code null}. + */ + public Class<?> getMessageFieldClass() { + switch (type) { + case MESSAGE: + case GROUP: + return field != null ? field.getType() : oneofStoredType; + case MESSAGE_LIST: + case GROUP_LIST: + return messageClass; + default: + return null; + } + } + + public static Builder newBuilder() { + return new Builder(); + } + + /** A builder for {@link FieldInfo} instances. */ + public static final class Builder { + private Field field; + private FieldType type; + private int fieldNumber; + private Field presenceField; + private int presenceMask; + private boolean required; + private boolean enforceUtf8; + private OneofInfo oneof; + private Class<?> oneofStoredType; + private Object mapDefaultEntry; + private EnumVerifier enumVerifier; + private Field cachedSizeField; + + private Builder() {} + + /** + * Specifies the actual field on the message represented by this field. This should not be + * called for oneof member fields. + */ + public Builder withField(Field field) { + if (oneof != null) { + throw new IllegalStateException("Cannot set field when building a oneof."); + } + this.field = field; + return this; + } + + /** Specifies the type of this field. */ + public Builder withType(FieldType type) { + this.type = type; + return this; + } + + /** Specifies the unique field number for this field within the message. */ + public Builder withFieldNumber(int fieldNumber) { + this.fieldNumber = fieldNumber; + return this; + } + + /** Specifies proto2 presence information. This should not be called for oneof fields. */ + public Builder withPresence(Field presenceField, int presenceMask) { + this.presenceField = checkNotNull(presenceField, "presenceField"); + this.presenceMask = presenceMask; + return this; + } + + /** + * Sets the information for building a oneof member field. This is incompatible with {@link + * #withField(Field)} and {@link #withPresence(Field, int)}. + * + * @param oneof the oneof for which this field is associated. + * @param oneofStoredType the actual type stored in the oneof value for this field. Since the + * oneof value is an {@link Object}, primitives will store their boxed type. + */ + public Builder withOneof(OneofInfo oneof, Class<?> oneofStoredType) { + if (field != null || presenceField != null) { + throw new IllegalStateException( + "Cannot set oneof when field or presenceField have been provided"); + } + this.oneof = oneof; + this.oneofStoredType = oneofStoredType; + return this; + } + + public Builder withRequired(boolean required) { + this.required = required; + return this; + } + + public Builder withMapDefaultEntry(Object mapDefaultEntry) { + this.mapDefaultEntry = mapDefaultEntry; + return this; + } + + public Builder withEnforceUtf8(boolean enforceUtf8) { + this.enforceUtf8 = enforceUtf8; + return this; + } + + public Builder withEnumVerifier(EnumVerifier enumVerifier) { + this.enumVerifier = enumVerifier; + return this; + } + + public Builder withCachedSizeField(Field cachedSizeField) { + this.cachedSizeField = cachedSizeField; + return this; + } + + public FieldInfo build() { + if (oneof != null) { + return forOneofMemberField( + fieldNumber, type, oneof, oneofStoredType, enforceUtf8, enumVerifier); + } + if (mapDefaultEntry != null) { + return forMapField(field, fieldNumber, mapDefaultEntry, enumVerifier); + } + if (presenceField != null) { + if (required) { + return forProto2RequiredField( + field, fieldNumber, type, presenceField, presenceMask, enforceUtf8, enumVerifier); + } else { + return forProto2OptionalField( + field, fieldNumber, type, presenceField, presenceMask, enforceUtf8, enumVerifier); + } + } + if (enumVerifier != null) { + if (cachedSizeField == null) { + return forFieldWithEnumVerifier(field, fieldNumber, type, enumVerifier); + } else { + return forPackedFieldWithEnumVerifier( + field, fieldNumber, type, enumVerifier, cachedSizeField); + } + } else { + if (cachedSizeField == null) { + return forField(field, fieldNumber, type, enforceUtf8); + } else { + return forPackedField(field, fieldNumber, type, cachedSizeField); + } + } + } + } + + private static boolean isExactlyOneBitSet(int value) { + return value != 0 && (value & (value - 1)) == 0; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/FieldSet.java b/java/core/src/main/java/com/google/protobuf/FieldSet.java index 40a37625ac..ddfd0e5601 100644 --- a/java/core/src/main/java/com/google/protobuf/FieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java @@ -218,6 +218,17 @@ final class FieldSet< return fields.entrySet().iterator(); } + /** + * Get an iterator over the fields in the map in descending (i.e. reverse) order. This iterator + * should not be leaked out of the protobuf library as it is not protected from mutation when + * fields is not immutable. + */ + Iterator<Map.Entry<FieldDescriptorType, Object>> descendingIterator() { + if (hasLazyField) { + return new LazyIterator<FieldDescriptorType>(fields.descendingEntrySet().iterator()); + } + return fields.descendingEntrySet().iterator(); + } /** Useful for implementing {@link Message#hasField(Descriptors.FieldDescriptor)}. */ public boolean hasField(final FieldDescriptorType descriptor) { diff --git a/java/core/src/main/java/com/google/protobuf/FieldType.java b/java/core/src/main/java/com/google/protobuf/FieldType.java new file mode 100755 index 0000000000..1b8f9e5da2 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/FieldType.java @@ -0,0 +1,346 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.util.List; + +/** Enumeration identifying all relevant type information for a protobuf field. */ +@ExperimentalApi +public enum FieldType { + DOUBLE(0, Collection.SCALAR, JavaType.DOUBLE), + FLOAT(1, Collection.SCALAR, JavaType.FLOAT), + INT64(2, Collection.SCALAR, JavaType.LONG), + UINT64(3, Collection.SCALAR, JavaType.LONG), + INT32(4, Collection.SCALAR, JavaType.INT), + FIXED64(5, Collection.SCALAR, JavaType.LONG), + FIXED32(6, Collection.SCALAR, JavaType.INT), + BOOL(7, Collection.SCALAR, JavaType.BOOLEAN), + STRING(8, Collection.SCALAR, JavaType.STRING), + MESSAGE(9, Collection.SCALAR, JavaType.MESSAGE), + BYTES(10, Collection.SCALAR, JavaType.BYTE_STRING), + UINT32(11, Collection.SCALAR, JavaType.INT), + ENUM(12, Collection.SCALAR, JavaType.ENUM), + SFIXED32(13, Collection.SCALAR, JavaType.INT), + SFIXED64(14, Collection.SCALAR, JavaType.LONG), + SINT32(15, Collection.SCALAR, JavaType.INT), + SINT64(16, Collection.SCALAR, JavaType.LONG), + GROUP(17, Collection.SCALAR, JavaType.MESSAGE), + DOUBLE_LIST(18, Collection.VECTOR, JavaType.DOUBLE), + FLOAT_LIST(19, Collection.VECTOR, JavaType.FLOAT), + INT64_LIST(20, Collection.VECTOR, JavaType.LONG), + UINT64_LIST(21, Collection.VECTOR, JavaType.LONG), + INT32_LIST(22, Collection.VECTOR, JavaType.INT), + FIXED64_LIST(23, Collection.VECTOR, JavaType.LONG), + FIXED32_LIST(24, Collection.VECTOR, JavaType.INT), + BOOL_LIST(25, Collection.VECTOR, JavaType.BOOLEAN), + STRING_LIST(26, Collection.VECTOR, JavaType.STRING), + MESSAGE_LIST(27, Collection.VECTOR, JavaType.MESSAGE), + BYTES_LIST(28, Collection.VECTOR, JavaType.BYTE_STRING), + UINT32_LIST(29, Collection.VECTOR, JavaType.INT), + ENUM_LIST(30, Collection.VECTOR, JavaType.ENUM), + SFIXED32_LIST(31, Collection.VECTOR, JavaType.INT), + SFIXED64_LIST(32, Collection.VECTOR, JavaType.LONG), + SINT32_LIST(33, Collection.VECTOR, JavaType.INT), + SINT64_LIST(34, Collection.VECTOR, JavaType.LONG), + DOUBLE_LIST_PACKED(35, Collection.PACKED_VECTOR, JavaType.DOUBLE), + FLOAT_LIST_PACKED(36, Collection.PACKED_VECTOR, JavaType.FLOAT), + INT64_LIST_PACKED(37, Collection.PACKED_VECTOR, JavaType.LONG), + UINT64_LIST_PACKED(38, Collection.PACKED_VECTOR, JavaType.LONG), + INT32_LIST_PACKED(39, Collection.PACKED_VECTOR, JavaType.INT), + FIXED64_LIST_PACKED(40, Collection.PACKED_VECTOR, JavaType.LONG), + FIXED32_LIST_PACKED(41, Collection.PACKED_VECTOR, JavaType.INT), + BOOL_LIST_PACKED(42, Collection.PACKED_VECTOR, JavaType.BOOLEAN), + UINT32_LIST_PACKED(43, Collection.PACKED_VECTOR, JavaType.INT), + ENUM_LIST_PACKED(44, Collection.PACKED_VECTOR, JavaType.ENUM), + SFIXED32_LIST_PACKED(45, Collection.PACKED_VECTOR, JavaType.INT), + SFIXED64_LIST_PACKED(46, Collection.PACKED_VECTOR, JavaType.LONG), + SINT32_LIST_PACKED(47, Collection.PACKED_VECTOR, JavaType.INT), + SINT64_LIST_PACKED(48, Collection.PACKED_VECTOR, JavaType.LONG), + GROUP_LIST(49, Collection.VECTOR, JavaType.MESSAGE), + MAP(50, Collection.MAP, JavaType.VOID); + + private final JavaType javaType; + private final int id; + private final Collection collection; + private final Class<?> elementType; + private final boolean primitiveScalar; + + FieldType(int id, Collection collection, JavaType javaType) { + this.id = id; + this.collection = collection; + this.javaType = javaType; + + switch (collection) { + case MAP: + elementType = javaType.getBoxedType(); + break; + case VECTOR: + elementType = javaType.getBoxedType(); + break; + case SCALAR: + default: + elementType = null; + break; + } + + boolean primitiveScalar = false; + if (collection == Collection.SCALAR) { + switch (javaType) { + case BYTE_STRING: + case MESSAGE: + case STRING: + break; + default: + primitiveScalar = true; + break; + } + } + this.primitiveScalar = primitiveScalar; + } + + /** A reliable unique identifier for this type. */ + public int id() { + return id; + } + + /** + * Gets the {@link JavaType} for this field. For lists, this identifies the type of the elements + * contained within the list. + */ + public JavaType getJavaType() { + return javaType; + } + + /** Indicates whether a list field should be represented on the wire in packed form. */ + public boolean isPacked() { + return Collection.PACKED_VECTOR.equals(collection); + } + + /** + * Indicates whether this field type represents a primitive scalar value. If this is {@code true}, + * then {@link #isScalar()} will also be {@code true}. + */ + public boolean isPrimitiveScalar() { + return primitiveScalar; + } + + /** Indicates whether this field type represents a scalar value. */ + public boolean isScalar() { + return collection == Collection.SCALAR; + } + + /** Indicates whether this field represents a list of values. */ + public boolean isList() { + return collection.isList(); + } + + /** Indicates whether this field represents a map. */ + public boolean isMap() { + return collection == Collection.MAP; + } + + /** Indicates whether or not this {@link FieldType} can be applied to the given {@link Field}. */ + public boolean isValidForField(Field field) { + if (Collection.VECTOR.equals(collection)) { + return isValidForList(field); + } else { + return javaType.getType().isAssignableFrom(field.getType()); + } + } + + private boolean isValidForList(Field field) { + Class<?> clazz = field.getType(); + if (!javaType.getType().isAssignableFrom(clazz)) { + // The field isn't a List type. + return false; + } + Type[] types = EMPTY_TYPES; + Type genericType = field.getGenericType(); + if (genericType instanceof ParameterizedType) { + types = ((ParameterizedType) field.getGenericType()).getActualTypeArguments(); + } + Type listParameter = getListParameter(clazz, types); + if (!(listParameter instanceof Class)) { + // It's a wildcard, we should allow anything in the list. + return true; + } + return elementType.isAssignableFrom((Class<?>) listParameter); + } + + /** + * Looks up the appropriate {@link FieldType} by it's identifier. + * + * @return the {@link FieldType} or {@code null} if not found. + */ + /* @Nullable */ + public static FieldType forId(int id) { + if (id < 0 || id >= VALUES.length) { + return null; + } + return VALUES[id]; + } + + private static final FieldType[] VALUES; + private static final Type[] EMPTY_TYPES = new Type[0]; + + static { + FieldType[] values = values(); + VALUES = new FieldType[values.length]; + for (FieldType type : values) { + VALUES[type.id] = type; + } + } + + /** + * Given a class, finds a generic super class or interface that extends {@link List}. + * + * @return the generic super class/interface, or {@code null} if not found. + */ + /* @Nullable */ + private static Type getGenericSuperList(Class<?> clazz) { + // First look at interfaces. + Type[] genericInterfaces = clazz.getGenericInterfaces(); + for (Type genericInterface : genericInterfaces) { + if (genericInterface instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericInterface; + Class<?> rawType = (Class<?>) parameterizedType.getRawType(); + if (List.class.isAssignableFrom(rawType)) { + return genericInterface; + } + } + } + + // Try the subclass + Type type = clazz.getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + Class<?> rawType = (Class<?>) parameterizedType.getRawType(); + if (List.class.isAssignableFrom(rawType)) { + return type; + } + } + + // No super class/interface extends List. + return null; + } + + /** + * Inspects the inheritance hierarchy for the given class and finds the generic type parameter for + * {@link List}. + * + * @param clazz the class to begin the search. + * @param realTypes the array of actual type parameters for {@code clazz}. These will be used to + * substitute generic parameters up the inheritance hierarchy. If {@code clazz} does not have + * any generic parameters, this list should be empty. + * @return the {@link List} parameter. + */ + private static Type getListParameter(Class<?> clazz, Type[] realTypes) { + top: + while (clazz != List.class) { + // First look at generic subclass and interfaces. + Type genericType = getGenericSuperList(clazz); + if (genericType instanceof ParameterizedType) { + // Replace any generic parameters with the real values. + ParameterizedType parameterizedType = (ParameterizedType) genericType; + Type[] superArgs = parameterizedType.getActualTypeArguments(); + for (int i = 0; i < superArgs.length; ++i) { + Type superArg = superArgs[i]; + if (superArg instanceof TypeVariable) { + // Get the type variables for this class so that we can match them to the variables + // used on the super class. + TypeVariable<?>[] clazzParams = clazz.getTypeParameters(); + if (realTypes.length != clazzParams.length) { + throw new RuntimeException("Type array mismatch"); + } + + // Replace the variable parameter with the real type. + boolean foundReplacement = false; + for (int j = 0; j < clazzParams.length; ++j) { + if (superArg == clazzParams[j]) { + Type realType = realTypes[j]; + superArgs[i] = realType; + foundReplacement = true; + break; + } + } + if (!foundReplacement) { + throw new RuntimeException("Unable to find replacement for " + superArg); + } + } + } + + Class<?> parent = (Class<?>) parameterizedType.getRawType(); + + realTypes = superArgs; + clazz = parent; + continue; + } + + // None of the parameterized types inherit List. Just continue up the inheritance hierarchy + // toward the List interface until we can identify the parameters. + realTypes = EMPTY_TYPES; + for (Class<?> iface : clazz.getInterfaces()) { + if (List.class.isAssignableFrom(iface)) { + clazz = iface; + continue top; + } + } + clazz = clazz.getSuperclass(); + } + + if (realTypes.length != 1) { + throw new RuntimeException("Unable to identify parameter type for List<T>"); + } + return realTypes[0]; + } + + enum Collection { + SCALAR(false), + VECTOR(true), + PACKED_VECTOR(true), + MAP(false); + + private final boolean isList; + + Collection(boolean isList) { + this.isList = isList; + } + + /** @return the isList */ + public boolean isList() { + return isList; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java new file mode 100755 index 0000000000..3e3efb929b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageInfoFactory.java @@ -0,0 +1,65 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** A factory for message info that is generated into the message itself. */ +@ExperimentalApi +class GeneratedMessageInfoFactory implements MessageInfoFactory { + + private static final GeneratedMessageInfoFactory instance = new GeneratedMessageInfoFactory(); + + // Disallow construction - it's a singleton. + private GeneratedMessageInfoFactory() {} + + public static GeneratedMessageInfoFactory getInstance() { + return instance; + } + + @Override + public boolean isSupported(Class<?> messageType) { + return GeneratedMessageLite.class.isAssignableFrom(messageType); + } + + @Override + public MessageInfo messageInfoFor(Class<?> messageType) { + if (!GeneratedMessageLite.class.isAssignableFrom(messageType)) { + throw new IllegalArgumentException("Unsupported message type: " + messageType.getName()); + } + + try { + return (MessageInfo) GeneratedMessageLite.getDefaultInstance( + messageType.asSubclass(GeneratedMessageLite.class)) + .buildMessageInfo(); + } catch (Exception e) { + throw new RuntimeException("Unable to get message info for " + messageType.getName(), e); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java index 619ef0999f..61cbed60a6 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java @@ -61,12 +61,6 @@ public abstract class GeneratedMessageLite< MessageType extends GeneratedMessageLite<MessageType, BuilderType>, BuilderType extends GeneratedMessageLite.Builder<MessageType, BuilderType>> extends AbstractMessageLite<MessageType, BuilderType> { - // BEGIN REGULAR - static final boolean ENABLE_EXPERIMENTAL_RUNTIME_AT_BUILD_TIME = false; - // END REGULAR - // BEGIN EXPERIMENTAL - // static final boolean ENABLE_EXPERIMENTAL_RUNTIME_AT_BUILD_TIME = true; - // END EXPERIMENTAL /** For use by generated code only. Lazily initialized to reduce allocations. */ protected UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance(); @@ -115,32 +109,10 @@ public abstract class GeneratedMessageLite< if (memoizedHashCode != 0) { return memoizedHashCode; } - // BEGIN EXPERIMENTAL - // memoizedHashCode = Protobuf.getInstance().schemaFor(this).hashCode(this); - // return memoizedHashCode; - // END EXPERIMENTAL - // BEGIN REGULAR - HashCodeVisitor visitor = new HashCodeVisitor(); - visit(visitor, (MessageType) this); - memoizedHashCode = visitor.hashCode; + memoizedHashCode = Protobuf.getInstance().schemaFor(this).hashCode(this); return memoizedHashCode; - // END REGULAR } - // BEGIN REGULAR - @SuppressWarnings("unchecked") // Guaranteed by runtime - int hashCode(HashCodeVisitor visitor) { - if (memoizedHashCode == 0) { - int inProgressHashCode = visitor.hashCode; - visitor.hashCode = 0; - visit(visitor, (MessageType) this); - memoizedHashCode = visitor.hashCode; - visitor.hashCode = inProgressHashCode; - } - return memoizedHashCode; - } - // END REGULAR - @SuppressWarnings("unchecked") // Guaranteed by isInstance + runtime @Override public boolean equals(Object other) { @@ -152,36 +124,8 @@ public abstract class GeneratedMessageLite< return false; } - // BEGIN EXPERIMENTAL - // return Protobuf.getInstance().schemaFor(this).equals(this, (MessageType) other); - // END EXPERIMENTAL - // BEGIN REGULAR - - try { - visit(EqualsVisitor.INSTANCE, (MessageType) other); - } catch (EqualsVisitor.NotEqualsException e) { - return false; - } - return true; - // END REGULAR - } - - // BEGIN REGULAR - /** Same as {@link #equals(Object)} but throws {@code NotEqualsException}. */ - @SuppressWarnings("unchecked") // Guaranteed by isInstance + runtime - boolean equals(EqualsVisitor visitor, MessageLite other) { - if (this == other) { - return true; - } - - if (!getDefaultInstanceForType().getClass().isInstance(other)) { - return false; - } - - visit(visitor, (MessageType) other); - return true; + return Protobuf.getInstance().schemaFor(this).equals(this, (MessageType) other); } - // END REGULAR // The general strategy for unknown fields is to use an UnknownFieldSetLite that is treated as // mutable during the parsing constructor and immutable after. This allows us to avoid @@ -223,13 +167,7 @@ public abstract class GeneratedMessageLite< /** Called by subclasses to complete parsing. For use by generated code only. */ protected void makeImmutable() { - // BEGIN REGULAR - dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE); - unknownFields.makeImmutable(); - // END REGULAR - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(this).makeImmutable(this); - // END EXPERIMENTAL + Protobuf.getInstance().schemaFor(this).makeImmutable(this); } protected final < @@ -266,17 +204,12 @@ public abstract class GeneratedMessageLite< * <p>For use by generated code only. */ public static enum MethodToInvoke { - // BEGIN REGULAR - IS_INITIALIZED, - VISIT, - MERGE_FROM_STREAM, - MAKE_IMMUTABLE, - // END REGULAR // Rely on/modify instance state GET_MEMOIZED_IS_INITIALIZED, SET_MEMOIZED_IS_INITIALIZED, // Rely on static state + BUILD_MESSAGE_INFO, NEW_MUTABLE_INSTANCE, NEW_BUILDER, GET_DEFAULT_INSTANCE, @@ -290,10 +223,6 @@ public abstract class GeneratedMessageLite< * count. * * <ul> - * <li>{@code MERGE_FROM_STREAM} is parameterized with an {@link CodedInputStream} and {@link - * ExtensionRegistryLite}. It consumes the input stream, parsing the contents into the - * returned protocol buffer. If parsing throws an {@link InvalidProtocolBufferException}, - * the implementation wraps it in a RuntimeException. * <li>{@code NEW_INSTANCE} returns a new instance of the protocol buffer that has not yet been * made immutable. See {@code MAKE_IMMUTABLE}. * <li>{@code IS_INITIALIZED} returns {@code null} for false and the default instance for true. @@ -303,9 +232,6 @@ public abstract class GeneratedMessageLite< * <li>{@code SET_MEMOIZED_IS_INITIALIZED} sets the memoized {@code isInitilaized} byte value to * 1 if the first parameter is not null, or to 0 if the first parameter is null. * <li>{@code NEW_BUILDER} returns a {@code BuilderType} instance. - * <li>{@code VISIT} is parameterized with a {@code Visitor} and a {@code MessageType} and - * recursively iterates through the fields side by side between this and the instance. - * <li>{@code MAKE_IMMUTABLE} sets all internal fields to an immutable state. * </ul> * * This method, plus the implementation of the Builder, enables the Builder class to be proguarded @@ -325,13 +251,6 @@ public abstract class GeneratedMessageLite< return dynamicMethod(method, null, null); } - // BEGIN REGULAR - void visit(Visitor visitor, MessageType other) { - dynamicMethod(MethodToInvoke.VISIT, visitor, other); - unknownFields = visitor.visitUnknownFields(unknownFields, other.unknownFields); - } - // END REGULAR - @Override int getMemoizedSerializedSize() { return memoizedSerializedSize; @@ -342,7 +261,60 @@ public abstract class GeneratedMessageLite< memoizedSerializedSize = size; } + public void writeTo(CodedOutputStream output) throws IOException { + writeToInternal(output); + } + + public int getSerializedSize() { + if (memoizedSerializedSize == -1) { + memoizedSerializedSize = getSerializedSizeInternal(); + } + return memoizedSerializedSize; + } + + /** Constructs a {@link MessageInfo} for this message type. */ + Object buildMessageInfo() throws Exception { + return dynamicMethod(MethodToInvoke.BUILD_MESSAGE_INFO); + } + + private static Map<Object, GeneratedMessageLite<?, ?>> defaultInstanceMap = + new ConcurrentHashMap<Object, GeneratedMessageLite<?, ?>>(); + + @SuppressWarnings("unchecked") + static <T extends GeneratedMessageLite<?, ?>> T getDefaultInstance(Class<T> clazz) { + T result = (T) defaultInstanceMap.get(clazz); + if (result == null) { + // Foo.class does not initialize the class so we need to force the initialization in order to + // get the default instance registered. + try { + Class.forName(clazz.getName(), true, clazz.getClassLoader()); + } catch (ClassNotFoundException e) { + throw new IllegalStateException("Class initialization cannot fail.", e); + } + result = (T) defaultInstanceMap.get(clazz); + } + if (result == null) { + // On some Samsung devices, this still doesn't return a valid value for some reason. We add a + // reflective fallback to keep the device running. See b/114675342. + result = (T) UnsafeUtil.allocateInstance(clazz).getDefaultInstanceForType(); + // A sanity check to ensure that <clinit> was actually invoked. + if (result == null) { + throw new IllegalStateException(); + } + defaultInstanceMap.put(clazz, result); + } + return result; + } + + protected static <T extends GeneratedMessageLite<?, ?>> void registerDefaultInstance( + Class<T> clazz, T defaultInstance) { + defaultInstanceMap.put(clazz, defaultInstance); + } + protected static Object newMessageInfo( + MessageLite defaultInstance, String info, Object[] objects) { + return new RawMessageInfo(defaultInstance, info, objects); + } /** * Merge some unknown fields into the {@link UnknownFieldSetLite} for this message. @@ -437,12 +409,7 @@ public abstract class GeneratedMessageLite< } private void mergeFromInstance(MessageType dest, MessageType src) { - // BEGIN EXPERIMENTAL - // Protobuf.getInstance().schemaFor(dest).mergeFrom(dest, src); - // END EXPERIMENTAL - // BEGIN REGULAR - dest.visit(MergeFromVisitor.INSTANCE, src); - // END REGULAR + Protobuf.getInstance().schemaFor(dest).mergeFrom(dest, src); } @Override @@ -454,36 +421,26 @@ public abstract class GeneratedMessageLite< public BuilderType mergeFrom( byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - // BEGIN REGULAR - return super.mergeFrom(input, offset, length, extensionRegistry); - // END REGULAR - // BEGIN EXPERIMENTAL - // copyOnWrite(); - // try { - // Protobuf.getInstance().schemaFor(instance).mergeFrom( - // instance, input, offset, offset + length, - // new ArrayDecoders.Registers(extensionRegistry)); - // } catch (InvalidProtocolBufferException e) { - // throw e; - // } catch (IndexOutOfBoundsException e) { - // throw InvalidProtocolBufferException.truncatedMessage(); - // } catch (IOException e) { - // throw new RuntimeException("Reading from byte array should not throw IOException.", e); - // } - // return (BuilderType) this; - // END EXPERIMENTAL + copyOnWrite(); + try { + Protobuf.getInstance().schemaFor(instance).mergeFrom( + instance, input, offset, offset + length, + new ArrayDecoders.Registers(extensionRegistry)); + } catch (InvalidProtocolBufferException e) { + throw e; + } catch (IndexOutOfBoundsException e) { + throw InvalidProtocolBufferException.truncatedMessage(); + } catch (IOException e) { + throw new RuntimeException("Reading from byte array should not throw IOException.", e); + } + return (BuilderType) this; } @Override public BuilderType mergeFrom( byte[] input, int offset, int length) throws InvalidProtocolBufferException { - // BEGIN REGULAR - return super.mergeFrom(input, offset, length); - // END REGULAR - // BEGIN EXPERIMENTAL - // return mergeFrom(input, offset, length, ExtensionRegistryLite.getEmptyRegistry()); - // END EXPERIMENTAL + return mergeFrom(input, offset, length, ExtensionRegistryLite.getEmptyRegistry()); } @Override @@ -493,15 +450,10 @@ public abstract class GeneratedMessageLite< throws IOException { copyOnWrite(); try { - // BEGIN REGULAR - instance.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); - // END REGULAR - // BEGIN EXPERIMENTAL - // // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use - // // fast path. - // Protobuf.getInstance().schemaFor(instance).mergeFrom( - // instance, CodedInputStreamReader.forCodedInput(input), extensionRegistry); - // END EXPERIMENTAL + // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use + // fast path. + Protobuf.getInstance().schemaFor(instance).mergeFrom( + instance, CodedInputStreamReader.forCodedInput(input), extensionRegistry); } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); @@ -553,14 +505,6 @@ public abstract class GeneratedMessageLite< extensions.mergeFrom(((ExtendableMessage) other).extensions); } - // BEGIN REGULAR - @Override - final void visit(Visitor visitor, MessageType other) { - super.visit(visitor, other); - extensions = visitor.visitExtensions(extensions, other.extensions); - } - // END REGULAR - /** * Parse an unknown field or an extension. For use by generated code only. * @@ -898,14 +842,6 @@ public abstract class GeneratedMessageLite< return extensions.isInitialized(); } - @Override - protected final void makeImmutable() { - super.makeImmutable(); - // BEGIN REGULAR - extensions.makeImmutable(); - // END REGULAR - } - /** * Used by subclasses to serialize extensions. Extension ranges may be interleaved with field * numbers, but we must write them in canonical (sorted by field number) order. ExtensionWriter @@ -1479,13 +1415,7 @@ public abstract class GeneratedMessageLite< if (memoizedIsInitialized == 0) { return false; } - // BEGIN EXPERIMENTAL - // boolean isInitialized = Protobuf.getInstance().schemaFor(message).isInitialized(message); - // END EXPERIMENTAL - // BEGIN REGULAR - boolean isInitialized = - message.dynamicMethod(MethodToInvoke.IS_INITIALIZED, Boolean.FALSE) != null; - // END REGULAR + boolean isInitialized = Protobuf.getInstance().schemaFor(message).isInitialized(message); if (shouldMemoize) { message.dynamicMethod( MethodToInvoke.SET_MEMOIZED_IS_INITIALIZED, isInitialized ? message : null); @@ -1593,23 +1523,16 @@ public abstract class GeneratedMessageLite< @SuppressWarnings("unchecked") // Guaranteed by protoc T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); try { - // BEGIN REGULAR - result.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); - // END REGULAR - // BEGIN EXPERIMENTAL - // // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use - // // fast path. - // Protobuf.getInstance().schemaFor(result).mergeFrom( - // result, CodedInputStreamReader.forCodedInput(input), extensionRegistry); - // END EXPERIMENTAL + // TODO(yilunchong): Try to make input with type CodedInpuStream.ArrayDecoder use + // fast path. + Protobuf.getInstance().schemaFor(result).mergeFrom( + result, CodedInputStreamReader.forCodedInput(input), extensionRegistry); result.makeImmutable(); - // BEGIN EXPERIMENTAL - // } catch (IOException e) { - // if (e.getCause() instanceof InvalidProtocolBufferException) { - // throw (InvalidProtocolBufferException) e.getCause(); - // } - // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); - // END EXPERIMENTAL + } catch (IOException e) { + if (e.getCause() instanceof InvalidProtocolBufferException) { + throw (InvalidProtocolBufferException) e.getCause(); + } + throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); } catch (RuntimeException e) { if (e.getCause() instanceof InvalidProtocolBufferException) { throw (InvalidProtocolBufferException) e.getCause(); @@ -1623,42 +1546,25 @@ public abstract class GeneratedMessageLite< static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom( T instance, byte[] input, int offset, int length, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { - // BEGIN REGULAR - T message; + @SuppressWarnings("unchecked") // Guaranteed by protoc + T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); try { - CodedInputStream cis = CodedInputStream.newInstance(input, offset, length); - message = parsePartialFrom(instance, cis, extensionRegistry); - try { - cis.checkLastTagWas(0); - } catch (InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(message); + Protobuf.getInstance().schemaFor(result).mergeFrom( + result, input, offset, offset + length, + new ArrayDecoders.Registers(extensionRegistry)); + result.makeImmutable(); + if (result.memoizedHashCode != 0) { + throw new RuntimeException(); } - return message; - } catch (InvalidProtocolBufferException e) { - throw e; + } catch (IOException e) { + if (e.getCause() instanceof InvalidProtocolBufferException) { + throw (InvalidProtocolBufferException) e.getCause(); + } + throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); + } catch (IndexOutOfBoundsException e) { + throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result); } - // END REGULAR - // BEGIN EXPERIMENTAL - // @SuppressWarnings("unchecked") // Guaranteed by protoc - // T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); - // try { - // Protobuf.getInstance().schemaFor(result).mergeFrom( - // result, input, offset, offset + length, - // new ArrayDecoders.Registers(extensionRegistry)); - // result.makeImmutable(); - // if (result.memoizedHashCode != 0) { - // throw new RuntimeException(); - // } - // } catch (IOException e) { - // if (e.getCause() instanceof InvalidProtocolBufferException) { - // throw (InvalidProtocolBufferException) e.getCause(); - // } - // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); - // } catch (IndexOutOfBoundsException e) { - // throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result); - // } - // return result; - // END EXPERIMENTAL + return result; } protected static <T extends GeneratedMessageLite<T, ?>> T parsePartialFrom( @@ -1825,701 +1731,4 @@ public abstract class GeneratedMessageLite< } return message; } - - // BEGIN REGULAR - /** - * An abstract visitor that the generated code calls into that we use to implement various - * features. Fields that are not members of oneofs are always visited. Members of a oneof are only - * visited when they are the set oneof case value on the "other" proto. The visitOneofNotSet - * method is invoked if other's oneof case is not set. - */ - protected interface Visitor { - boolean visitBoolean(boolean minePresent, boolean mine, boolean otherPresent, boolean other); - - int visitInt(boolean minePresent, int mine, boolean otherPresent, int other); - - double visitDouble(boolean minePresent, double mine, boolean otherPresent, double other); - - float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other); - - long visitLong(boolean minePresent, long mine, boolean otherPresent, long other); - - String visitString(boolean minePresent, String mine, boolean otherPresent, String other); - - ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other); - - Object visitOneofBoolean(boolean minePresent, Object mine, Object other); - - Object visitOneofInt(boolean minePresent, Object mine, Object other); - - Object visitOneofDouble(boolean minePresent, Object mine, Object other); - - Object visitOneofFloat(boolean minePresent, Object mine, Object other); - - Object visitOneofLong(boolean minePresent, Object mine, Object other); - - Object visitOneofString(boolean minePresent, Object mine, Object other); - - Object visitOneofByteString(boolean minePresent, Object mine, Object other); - - Object visitOneofMessage(boolean minePresent, Object mine, Object other); - - void visitOneofNotSet(boolean minePresent); - - /** Message fields use null sentinals. */ - <T extends MessageLite> T visitMessage(T mine, T other); - - <T> ProtobufList<T> visitList(ProtobufList<T> mine, ProtobufList<T> other); - - BooleanList visitBooleanList(BooleanList mine, BooleanList other); - - IntList visitIntList(IntList mine, IntList other); - - DoubleList visitDoubleList(DoubleList mine, DoubleList other); - - FloatList visitFloatList(FloatList mine, FloatList other); - - LongList visitLongList(LongList mine, LongList other); - - FieldSet<ExtensionDescriptor> visitExtensions( - FieldSet<ExtensionDescriptor> mine, FieldSet<ExtensionDescriptor> other); - - UnknownFieldSetLite visitUnknownFields(UnknownFieldSetLite mine, UnknownFieldSetLite other); - - <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other); - } - - /** Implements equals. Throws a {@link NotEqualsException} when not equal. */ - static class EqualsVisitor implements Visitor { - - static final class NotEqualsException extends RuntimeException {} - - static final EqualsVisitor INSTANCE = new EqualsVisitor(); - - static final NotEqualsException NOT_EQUALS = new NotEqualsException(); - - private EqualsVisitor() {} - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - if (minePresent != otherPresent || mine != other) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - if (minePresent != otherPresent || !mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - if (minePresent != otherPresent || !mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - if (minePresent && mine.equals(other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - if (minePresent && ((GeneratedMessageLite<?, ?>) mine).equals(this, (MessageLite) other)) { - return mine; - } - throw NOT_EQUALS; - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - if (minePresent) { - throw NOT_EQUALS; - } - } - - @Override - public <T extends MessageLite> T visitMessage(T mine, T other) { - if (mine == null && other == null) { - return null; - } - - if (mine == null || other == null) { - throw NOT_EQUALS; - } - - ((GeneratedMessageLite<?, ?>) mine).equals(this, other); - - return mine; - } - - @Override - public <T> ProtobufList<T> visitList(ProtobufList<T> mine, ProtobufList<T> other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public FieldSet<ExtensionDescriptor> visitExtensions( - FieldSet<ExtensionDescriptor> mine, FieldSet<ExtensionDescriptor> other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, UnknownFieldSetLite other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - - @Override - public <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other) { - if (!mine.equals(other)) { - throw NOT_EQUALS; - } - return mine; - } - } - - /** Implements hashCode by accumulating state. */ - static class HashCodeVisitor implements Visitor { - - // The caller must ensure that the visitor is invoked parameterized with this and this such that - // other is this. This is required due to how oneof cases are handled. See the class comment - // on Visitor for more information. - - int hashCode = 0; - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - hashCode = (53 * hashCode) + Internal.hashBoolean(mine); - return mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - hashCode = (53 * hashCode) + mine; - return mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - hashCode = (53 * hashCode) + Internal.hashLong(Double.doubleToLongBits(mine)); - return mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - hashCode = (53 * hashCode) + Float.floatToIntBits(mine); - return mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - hashCode = (53 * hashCode) + Internal.hashLong(mine); - return mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashBoolean(((Boolean) mine)); - return mine; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + (Integer) mine; - return mine; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashLong(Double.doubleToLongBits((Double) mine)); - return mine; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Float.floatToIntBits((Float) mine); - return mine; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + Internal.hashLong((Long) mine); - return mine; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - return visitMessage((MessageLite) mine, (MessageLite) other); - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - if (minePresent) { - throw new IllegalStateException(); // Can't happen if other == this. - } - } - - @Override - public <T extends MessageLite> T visitMessage(T mine, T other) { - final int protoHash; - if (mine != null) { - if (mine instanceof GeneratedMessageLite) { - protoHash = ((GeneratedMessageLite) mine).hashCode(this); - } else { - protoHash = mine.hashCode(); - } - } else { - protoHash = 37; - } - hashCode = (53 * hashCode) + protoHash; - return mine; - } - - @Override - public <T> ProtobufList<T> visitList(ProtobufList<T> mine, ProtobufList<T> other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public FieldSet<ExtensionDescriptor> visitExtensions( - FieldSet<ExtensionDescriptor> mine, FieldSet<ExtensionDescriptor> other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, UnknownFieldSetLite other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - - @Override - public <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other) { - hashCode = (53 * hashCode) + mine.hashCode(); - return mine; - } - } - - /** Implements field merging semantics over the visitor interface. */ - protected static class MergeFromVisitor implements Visitor { - - public static final MergeFromVisitor INSTANCE = new MergeFromVisitor(); - - private MergeFromVisitor() {} - - @Override - public boolean visitBoolean( - boolean minePresent, boolean mine, boolean otherPresent, boolean other) { - return otherPresent ? other : mine; - } - - @Override - public int visitInt(boolean minePresent, int mine, boolean otherPresent, int other) { - return otherPresent ? other : mine; - } - - @Override - public double visitDouble( - boolean minePresent, double mine, boolean otherPresent, double other) { - return otherPresent ? other : mine; - } - - @Override - public float visitFloat(boolean minePresent, float mine, boolean otherPresent, float other) { - return otherPresent ? other : mine; - } - - @Override - public long visitLong(boolean minePresent, long mine, boolean otherPresent, long other) { - return otherPresent ? other : mine; - } - - @Override - public String visitString( - boolean minePresent, String mine, boolean otherPresent, String other) { - return otherPresent ? other : mine; - } - - @Override - public ByteString visitByteString( - boolean minePresent, ByteString mine, boolean otherPresent, ByteString other) { - return otherPresent ? other : mine; - } - - @Override - public Object visitOneofBoolean(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofInt(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofDouble(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofFloat(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofLong(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofString(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofByteString(boolean minePresent, Object mine, Object other) { - return other; - } - - @Override - public Object visitOneofMessage(boolean minePresent, Object mine, Object other) { - if (minePresent) { - return visitMessage((MessageLite) mine, (MessageLite) other); - } - return other; - } - - @Override - public void visitOneofNotSet(boolean minePresent) { - return; - } - - @SuppressWarnings("unchecked") // Guaranteed by runtime. - @Override - public <T extends MessageLite> T visitMessage(T mine, T other) { - if (mine != null && other != null) { - return (T) mine.toBuilder().mergeFrom(other).build(); - } - - return mine != null ? mine : other; - } - - @Override - public <T> ProtobufList<T> visitList(ProtobufList<T> mine, ProtobufList<T> other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public BooleanList visitBooleanList(BooleanList mine, BooleanList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public IntList visitIntList(IntList mine, IntList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public DoubleList visitDoubleList(DoubleList mine, DoubleList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public FloatList visitFloatList(FloatList mine, FloatList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public LongList visitLongList(LongList mine, LongList other) { - int size = mine.size(); - int otherSize = other.size(); - if (size > 0 && otherSize > 0) { - if (!mine.isModifiable()) { - mine = mine.mutableCopyWithCapacity(size + otherSize); - } - mine.addAll(other); - } - - return size > 0 ? mine : other; - } - - @Override - public FieldSet<ExtensionDescriptor> visitExtensions( - FieldSet<ExtensionDescriptor> mine, FieldSet<ExtensionDescriptor> other) { - if (mine.isImmutable()) { - mine = mine.clone(); - } - mine.mergeFrom(other); - return mine; - } - - @Override - public UnknownFieldSetLite visitUnknownFields( - UnknownFieldSetLite mine, UnknownFieldSetLite other) { - return other == UnknownFieldSetLite.getDefaultInstance() - ? mine - : UnknownFieldSetLite.mutableCopyOf(mine, other); - } - - @Override - public <K, V> MapFieldLite<K, V> visitMap(MapFieldLite<K, V> mine, MapFieldLite<K, V> other) { - if (!other.isEmpty()) { - if (!mine.isMutable()) { - mine = mine.mutableCopy(); - } - mine.mergeFrom(other); - } - return mine; - } - } - // END REGULAR } diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index e9eb34f2f4..0ed724384f 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java @@ -458,6 +458,29 @@ public abstract class GeneratedMessageV3 extends AbstractMessage + /** + * This class is used to make a generated protected method inaccessible from user's code (e.g., + * the {@link #newInstance} method below). When this class is used as a parameter's type in a + * generated protected method, the method is visible to user's code in the same package, but + * since the constructor of this class is private to protobuf runtime, user's code can't obtain + * an instance of this class and as such can't actually make a method call on the protected + * method. + */ + protected static final class UnusedPrivateParameter { + static final UnusedPrivateParameter INSTANCE = new UnusedPrivateParameter(); + + private UnusedPrivateParameter() { + } + } + + /** + * Creates a new instance of this message type. Overridden in the generated code. + */ + @SuppressWarnings({"unused"}) + protected Object newInstance(UnusedPrivateParameter unused) { + throw new UnsupportedOperationException("This method must be overridden by the subclass."); + } + /** * Used by parsing constructors in generated classes. */ diff --git a/java/core/src/main/java/com/google/protobuf/Internal.java b/java/core/src/main/java/com/google/protobuf/Internal.java index 713ccb583c..15deb37c49 100644 --- a/java/core/src/main/java/com/google/protobuf/Internal.java +++ b/java/core/src/main/java/com/google/protobuf/Internal.java @@ -540,6 +540,24 @@ public final class Internal { } return valueConverter.doForward(oldValue); } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof Map.Entry)) { + return false; + } + @SuppressWarnings("unchecked") + Map.Entry<?, ?> other = (Map.Entry<?, ?>) o; + return getKey().equals(other.getKey()) && getValue().equals(getValue()); + } + + @Override + public int hashCode() { + return realEntry.hashCode(); + } } } diff --git a/java/core/src/main/java/com/google/protobuf/JavaType.java b/java/core/src/main/java/com/google/protobuf/JavaType.java new file mode 100755 index 0000000000..770aa9ef1f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/JavaType.java @@ -0,0 +1,76 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** Enum that identifies the Java types required to store protobuf fields. */ +@ExperimentalApi +public enum JavaType { + VOID(Void.class, Void.class, null), + INT(int.class, Integer.class, 0), + LONG(long.class, Long.class, 0L), + FLOAT(float.class, Float.class, 0F), + DOUBLE(double.class, Double.class, 0D), + BOOLEAN(boolean.class, Boolean.class, false), + STRING(String.class, String.class, ""), + BYTE_STRING(ByteString.class, ByteString.class, ByteString.EMPTY), + ENUM(int.class, Integer.class, null), + MESSAGE(Object.class, Object.class, null); + + private final Class<?> type; + private final Class<?> boxedType; + private final Object defaultDefault; + + JavaType(Class<?> type, Class<?> boxedType, Object defaultDefault) { + this.type = type; + this.boxedType = boxedType; + this.defaultDefault = defaultDefault; + } + + /** The default default value for fields of this type, if it's a primitive type. */ + public Object getDefaultDefault() { + return defaultDefault; + } + + /** Gets the required type for a field that would hold a value of this type. */ + public Class<?> getType() { + return type; + } + + /** @return the boxedType */ + public Class<?> getBoxedType() { + return boxedType; + } + + /** Indicates whether or not this {@link JavaType} can be applied to a field of the given type. */ + public boolean isValidType(Class<?> t) { + return type.isAssignableFrom(t); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java index 5651e131b3..d6b594d384 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/LazyFieldLite.java @@ -388,6 +388,18 @@ public class LazyFieldLite { } } + /** Writes this lazy field into a {@link Writer}. */ + void writeTo(Writer writer, int fieldNumber) throws IOException { + if (memoizedBytes != null) { + writer.writeBytes(fieldNumber, memoizedBytes); + } else if (delayedBytes != null) { + writer.writeBytes(fieldNumber, delayedBytes); + } else if (value != null) { + writer.writeMessage(fieldNumber, value); + } else { + writer.writeBytes(fieldNumber, ByteString.EMPTY); + } + } /** Might lazily parse the bytes that were previously passed in. Is thread-safe. */ protected void ensureInitialized(MessageLite defaultInstance) { diff --git a/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java b/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java new file mode 100755 index 0000000000..ebc8561a72 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ListFieldSchema.java @@ -0,0 +1,190 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.Internal.ProtobufList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Utility class that aids in properly manipulating list fields for either the lite or full runtime. + */ +abstract class ListFieldSchema { + // Disallow construction. + private ListFieldSchema() {} + + private static final ListFieldSchema FULL_INSTANCE = new ListFieldSchemaFull(); + private static final ListFieldSchema LITE_INSTANCE = new ListFieldSchemaLite(); + + abstract <L> List<L> mutableListAt(Object msg, long offset); + + abstract void makeImmutableListAt(Object msg, long offset); + + abstract <L> void mergeListsAt(Object msg, Object otherMsg, long offset); + + static ListFieldSchema full() { + return FULL_INSTANCE; + } + + static ListFieldSchema lite() { + return LITE_INSTANCE; + } + + /** Implementation for the full runtime. */ + private static final class ListFieldSchemaFull extends ListFieldSchema { + + private static final Class<?> UNMODIFIABLE_LIST_CLASS = + Collections.unmodifiableList(Collections.emptyList()).getClass(); + + @Override + <L> List<L> mutableListAt(Object message, long offset) { + return mutableListAt(message, offset, AbstractProtobufList.DEFAULT_CAPACITY); + } + + @Override + void makeImmutableListAt(Object message, long offset) { + List<?> list = (List<?>) UnsafeUtil.getObject(message, offset); + Object immutable = null; + if (list instanceof LazyStringList) { + immutable = ((LazyStringList) list).getUnmodifiableView(); + } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) { + // already immutable + return; + } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) { + if (((ProtobufList<?>) list).isModifiable()) { + ((ProtobufList<?>) list).makeImmutable(); + } + return; + } else { + immutable = Collections.unmodifiableList((List<?>) list); + } + UnsafeUtil.putObject(message, offset, immutable); + } + + @SuppressWarnings("unchecked") + private static <L> List<L> mutableListAt(Object message, long offset, int additionalCapacity) { + List<L> list = getList(message, offset); + if (list.isEmpty()) { + if (list instanceof LazyStringList) { + list = (List<L>) new LazyStringArrayList(additionalCapacity); + } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) { + list = ((ProtobufList<L>) list).mutableCopyWithCapacity(additionalCapacity); + } else { + list = new ArrayList<L>(additionalCapacity); + } + UnsafeUtil.putObject(message, offset, list); + } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) { + ArrayList<L> newList = new ArrayList<L>(list.size() + additionalCapacity); + newList.addAll(list); + list = newList; + UnsafeUtil.putObject(message, offset, list); + } else if (list instanceof UnmodifiableLazyStringList) { + LazyStringArrayList newList = new LazyStringArrayList(list.size() + additionalCapacity); + newList.addAll((UnmodifiableLazyStringList) list); + list = (List<L>) newList; + UnsafeUtil.putObject(message, offset, list); + } else if (list instanceof PrimitiveNonBoxingCollection + && list instanceof ProtobufList + && !((ProtobufList<L>) list).isModifiable()) { + list = ((ProtobufList<L>) list).mutableCopyWithCapacity(list.size() + additionalCapacity); + UnsafeUtil.putObject(message, offset, list); + } + return list; + } + + @Override + <E> void mergeListsAt(Object msg, Object otherMsg, long offset) { + List<E> other = getList(otherMsg, offset); + List<E> mine = mutableListAt(msg, offset, other.size()); + + int size = mine.size(); + int otherSize = other.size(); + if (size > 0 && otherSize > 0) { + mine.addAll(other); + } + + List<E> merged = size > 0 ? mine : other; + UnsafeUtil.putObject(msg, offset, merged); + } + + @SuppressWarnings("unchecked") + static <E> List<E> getList(Object message, long offset) { + return (List<E>) UnsafeUtil.getObject(message, offset); + } + } + + /** Implementation for the lite runtime. */ + private static final class ListFieldSchemaLite extends ListFieldSchema { + + @Override + <L> List<L> mutableListAt(Object message, long offset) { + ProtobufList<L> list = getProtobufList(message, offset); + if (!list.isModifiable()) { + int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + UnsafeUtil.putObject(message, offset, list); + } + return list; + } + + @Override + void makeImmutableListAt(Object message, long offset) { + ProtobufList<?> list = getProtobufList(message, offset); + list.makeImmutable(); + } + + @Override + <E> void mergeListsAt(Object msg, Object otherMsg, long offset) { + ProtobufList<E> mine = getProtobufList(msg, offset); + ProtobufList<E> other = getProtobufList(otherMsg, offset); + + int size = mine.size(); + int otherSize = other.size(); + if (size > 0 && otherSize > 0) { + if (!mine.isModifiable()) { + mine = mine.mutableCopyWithCapacity(size + otherSize); + } + mine.addAll(other); + } + + ProtobufList<E> merged = size > 0 ? mine : other; + UnsafeUtil.putObject(msg, offset, merged); + } + + @SuppressWarnings("unchecked") + static <E> ProtobufList<E> getProtobufList(Object message, long offset) { + return (ProtobufList<E>) UnsafeUtil.getObject(message, offset); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java new file mode 100755 index 0000000000..84ca9ae0f7 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ManifestSchemaFactory.java @@ -0,0 +1,172 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +/** + * Dynamically generates a manifest-based (i.e. table-based) schema for a given protobuf message. + */ +@ExperimentalApi +final class ManifestSchemaFactory implements SchemaFactory { + + private final MessageInfoFactory messageInfoFactory; + + public ManifestSchemaFactory() { + this(getDefaultMessageInfoFactory()); + } + + private ManifestSchemaFactory(MessageInfoFactory messageInfoFactory) { + this.messageInfoFactory = checkNotNull(messageInfoFactory, "messageInfoFactory"); + } + + @Override + public <T> Schema<T> createSchema(Class<T> messageType) { + SchemaUtil.requireGeneratedMessage(messageType); + + MessageInfo messageInfo = messageInfoFactory.messageInfoFor(messageType); + + // MessageSet has a special schema. + if (messageInfo.isMessageSetWireFormat()) { + if (GeneratedMessageLite.class.isAssignableFrom(messageType)) { + return MessageSetSchema.newSchema( + SchemaUtil.unknownFieldSetLiteSchema(), + ExtensionSchemas.lite(), + messageInfo.getDefaultInstance()); + } + return MessageSetSchema.newSchema( + SchemaUtil.proto2UnknownFieldSetSchema(), + ExtensionSchemas.full(), + messageInfo.getDefaultInstance()); + } + + return newSchema(messageType, messageInfo); + } + + private static <T> Schema<T> newSchema(Class<T> messageType, MessageInfo messageInfo) { + if (GeneratedMessageLite.class.isAssignableFrom(messageType)) { + return isProto2(messageInfo) + ? MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.lite(), + ListFieldSchema.lite(), + SchemaUtil.unknownFieldSetLiteSchema(), + ExtensionSchemas.lite(), + MapFieldSchemas.lite()) + : MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.lite(), + ListFieldSchema.lite(), + SchemaUtil.unknownFieldSetLiteSchema(), + /* extensionSchema= */ null, + MapFieldSchemas.lite()); + } + return isProto2(messageInfo) + ? MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.full(), + ListFieldSchema.full(), + SchemaUtil.proto2UnknownFieldSetSchema(), + ExtensionSchemas.full(), + MapFieldSchemas.full()) + : MessageSchema.newSchema( + messageType, + messageInfo, + NewInstanceSchemas.full(), + ListFieldSchema.full(), + SchemaUtil.proto3UnknownFieldSetSchema(), + /* extensionSchema= */ null, + MapFieldSchemas.full()); + } + + private static boolean isProto2(MessageInfo messageInfo) { + return messageInfo.getSyntax() == ProtoSyntax.PROTO2; + } + + private static MessageInfoFactory getDefaultMessageInfoFactory() { + return new CompositeMessageInfoFactory( + GeneratedMessageInfoFactory.getInstance(), getDescriptorMessageInfoFactory()); + } + + private static class CompositeMessageInfoFactory implements MessageInfoFactory { + private MessageInfoFactory[] factories; + + CompositeMessageInfoFactory(MessageInfoFactory... factories) { + this.factories = factories; + } + + @Override + public boolean isSupported(Class<?> clazz) { + for (MessageInfoFactory factory : factories) { + if (factory.isSupported(clazz)) { + return true; + } + } + return false; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + for (MessageInfoFactory factory : factories) { + if (factory.isSupported(clazz)) { + return factory.messageInfoFor(clazz); + } + } + throw new UnsupportedOperationException( + "No factory is available for message type: " + clazz.getName()); + } + } + + private static final MessageInfoFactory EMPTY_FACTORY = + new MessageInfoFactory() { + @Override + public boolean isSupported(Class<?> clazz) { + return false; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + throw new IllegalStateException("This should never be called."); + } + }; + + private static MessageInfoFactory getDescriptorMessageInfoFactory() { + try { + Class<?> clazz = Class.forName("com.google.protobuf.DescriptorMessageInfoFactory"); + return (MessageInfoFactory) clazz.getDeclaredMethod("getInstance").invoke(null); + } catch (Exception e) { + return EMPTY_FACTORY; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java new file mode 100755 index 0000000000..195126e514 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchema.java @@ -0,0 +1,63 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.util.Map; + +interface MapFieldSchema { + /** Returns the map data for mutation. */ + Map<?, ?> forMutableMapData(Object mapField); + + /** Returns the map data for read. */ + Map<?, ?> forMapData(Object mapField); + + /** Whether toImmutable() has been called on this map field. */ + boolean isImmutable(Object mapField); + + /** + * Returns an immutable instance of the map field. It may make the parameter immutable and return + * the parameter, or create an immutable copy. The status of the parameter after the call is + * undefined. + */ + Object toImmutable(Object mapField); + + /** Returns a new instance of the map field given a map default entry. */ + Object newMapField(Object mapDefaultEntry); + + /** Returns the metadata from a default entry. */ + MapEntryLite.Metadata<?, ?> forMapMetadata(Object mapDefaultEntry); + + /** Merges {@code srcMapField} into {@code destMapField}, and returns the merged instance. */ + Object mergeFrom(Object destMapField, Object srcMapField); + + /** Compute the serialized size for the map with a given field number. */ + int getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry); +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java new file mode 100755 index 0000000000..1efaab3bce --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaFull.java @@ -0,0 +1,112 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.MapEntryLite.Metadata; +import java.util.Map; + +class MapFieldSchemaFull implements MapFieldSchema { + @Override + public Map<?, ?> forMutableMapData(Object mapField) { + return ((MapField<?, ?>) mapField).getMutableMap(); + } + + @Override + public Map<?, ?> forMapData(Object mapField) { + return ((MapField<?, ?>) mapField).getMap(); + } + + @Override + public boolean isImmutable(Object mapField) { + return !((MapField<?, ?>) mapField).isMutable(); + } + + @Override + public Object toImmutable(Object mapField) { + ((MapField<?, ?>) mapField).makeImmutable(); + return mapField; + } + + @Override + public Object newMapField(Object mapDefaultEntry) { + return MapField.newMapField((MapEntry<?, ?>) mapDefaultEntry); + } + + @Override + public Metadata<?, ?> forMapMetadata(Object mapDefaultEntry) { + return ((MapEntry<?, ?>) mapDefaultEntry).getMetadata(); + } + + @Override + public Object mergeFrom(Object destMapField, Object srcMapField) { + return mergeFromFull(destMapField, srcMapField); + } + + @SuppressWarnings("unchecked") + private static <K, V> Object mergeFromFull(Object destMapField, Object srcMapField) { + MapField<K, V> mine = (MapField<K, V>) destMapField; + MapField<K, V> other = (MapField<K, V>) srcMapField; + if (!mine.isMutable()) { + mine.copy(); + } + mine.mergeFrom(other); + return mine; + } + + @Override + public int getSerializedSize(int number, Object mapField, Object mapDefaultEntry) { + return getSerializedSizeFull(number, mapField, mapDefaultEntry); + } + + @SuppressWarnings("unchecked") + private static <K, V> int getSerializedSizeFull( + int number, Object mapField, Object defaultEntryObject) { + // Full runtime allocates map fields lazily. + if (mapField == null) { + return 0; + } + + Map<K, V> map = ((MapField<K, V>) mapField).getMap(); + MapEntry<K, V> defaultEntry = (MapEntry<K, V>) defaultEntryObject; + if (map.isEmpty()) { + return 0; + } + int size = 0; + for (Map.Entry<K, V> entry : map.entrySet()) { + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeLengthDelimitedFieldSize( + MapEntryLite.computeSerializedSize( + defaultEntry.getMetadata(), entry.getKey(), entry.getValue())); + } + return size; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java new file mode 100755 index 0000000000..8a8c78de8a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemaLite.java @@ -0,0 +1,107 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.MapEntryLite.Metadata; +import java.util.Map; + +class MapFieldSchemaLite implements MapFieldSchema { + + @Override + public Map<?, ?> forMutableMapData(Object mapField) { + return (MapFieldLite<?, ?>) mapField; + } + + @Override + public Metadata<?, ?> forMapMetadata(Object mapDefaultEntry) { + return ((MapEntryLite<?, ?>) mapDefaultEntry).getMetadata(); + } + + @Override + public Map<?, ?> forMapData(Object mapField) { + return (MapFieldLite<?, ?>) mapField; + } + + @Override + public boolean isImmutable(Object mapField) { + return !((MapFieldLite<?, ?>) mapField).isMutable(); + } + + @Override + public Object toImmutable(Object mapField) { + ((MapFieldLite<?, ?>) mapField).makeImmutable(); + return mapField; + } + + @Override + public Object newMapField(Object unused) { + return MapFieldLite.emptyMapField().mutableCopy(); + } + + @Override + public Object mergeFrom(Object destMapField, Object srcMapField) { + return mergeFromLite(destMapField, srcMapField); + } + + @SuppressWarnings("unchecked") + private static <K, V> MapFieldLite<K, V> mergeFromLite(Object destMapField, Object srcMapField) { + MapFieldLite<K, V> mine = (MapFieldLite<K, V>) destMapField; + MapFieldLite<K, V> other = (MapFieldLite<K, V>) srcMapField; + if (!other.isEmpty()) { + if (!mine.isMutable()) { + mine = mine.mutableCopy(); + } + mine.mergeFrom(other); + } + return mine; + } + + @Override + public int getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry) { + return getSerializedSizeLite(fieldNumber, mapField, mapDefaultEntry); + } + + @SuppressWarnings("unchecked") + private static <K, V> int getSerializedSizeLite( + int fieldNumber, Object mapField, Object defaultEntry) { + MapFieldLite<K, V> mapFieldLite = (MapFieldLite<K, V>) mapField; + MapEntryLite<K, V> defaultEntryLite = (MapEntryLite<K, V>) defaultEntry; + + if (mapFieldLite.isEmpty()) { + return 0; + } + int size = 0; + for (Map.Entry<K, V> entry : mapFieldLite.entrySet()) { + size += defaultEntryLite.computeMessageSize(fieldNumber, entry.getKey(), entry.getValue()); + } + return size; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java b/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java new file mode 100755 index 0000000000..b398c61022 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MapFieldSchemas.java @@ -0,0 +1,53 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +final class MapFieldSchemas { + private static final MapFieldSchema FULL_SCHEMA = loadSchemaForFullRuntime(); + private static final MapFieldSchema LITE_SCHEMA = new MapFieldSchemaLite(); + + static MapFieldSchema full() { + return FULL_SCHEMA; + } + + static MapFieldSchema lite() { + return LITE_SCHEMA; + } + + private static MapFieldSchema loadSchemaForFullRuntime() { + try { + Class<?> clazz = Class.forName("com.google.protobuf.MapFieldSchemaFull"); + return (MapFieldSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageInfo.java b/java/core/src/main/java/com/google/protobuf/MessageInfo.java new file mode 100755 index 0000000000..69e318696a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageInfo.java @@ -0,0 +1,43 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** A MesageInfo object describes a proto message type. */ +interface MessageInfo { + /** Gets syntax for this type. */ + ProtoSyntax getSyntax(); + + /** Whether this type is MessageSet. */ + boolean isMessageSetWireFormat(); + + /** Gets the default instance of this type. */ + MessageLite getDefaultInstance(); +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java b/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java new file mode 100755 index 0000000000..005c26d05f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageInfoFactory.java @@ -0,0 +1,41 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** A factory that creates {@link MessageInfo} instances for message types. */ +@ExperimentalApi +interface MessageInfoFactory { + /** Whether the message class is supported by this factory. */ + boolean isSupported(Class<?> clazz); + + /** Returns a information of the message class. */ + MessageInfo messageInfoFor(Class<?> clazz); +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java new file mode 100755 index 0000000000..67584ed413 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -0,0 +1,5886 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.ArrayDecoders.decodeBoolList; +import static com.google.protobuf.ArrayDecoders.decodeBytes; +import static com.google.protobuf.ArrayDecoders.decodeBytesList; +import static com.google.protobuf.ArrayDecoders.decodeDouble; +import static com.google.protobuf.ArrayDecoders.decodeDoubleList; +import static com.google.protobuf.ArrayDecoders.decodeExtensionOrUnknownField; +import static com.google.protobuf.ArrayDecoders.decodeFixed32; +import static com.google.protobuf.ArrayDecoders.decodeFixed32List; +import static com.google.protobuf.ArrayDecoders.decodeFixed64; +import static com.google.protobuf.ArrayDecoders.decodeFixed64List; +import static com.google.protobuf.ArrayDecoders.decodeFloat; +import static com.google.protobuf.ArrayDecoders.decodeFloatList; +import static com.google.protobuf.ArrayDecoders.decodeGroupField; +import static com.google.protobuf.ArrayDecoders.decodeGroupList; +import static com.google.protobuf.ArrayDecoders.decodeMessageField; +import static com.google.protobuf.ArrayDecoders.decodeMessageList; +import static com.google.protobuf.ArrayDecoders.decodePackedBoolList; +import static com.google.protobuf.ArrayDecoders.decodePackedDoubleList; +import static com.google.protobuf.ArrayDecoders.decodePackedFixed32List; +import static com.google.protobuf.ArrayDecoders.decodePackedFixed64List; +import static com.google.protobuf.ArrayDecoders.decodePackedFloatList; +import static com.google.protobuf.ArrayDecoders.decodePackedSInt32List; +import static com.google.protobuf.ArrayDecoders.decodePackedSInt64List; +import static com.google.protobuf.ArrayDecoders.decodePackedVarint32List; +import static com.google.protobuf.ArrayDecoders.decodePackedVarint64List; +import static com.google.protobuf.ArrayDecoders.decodeSInt32List; +import static com.google.protobuf.ArrayDecoders.decodeSInt64List; +import static com.google.protobuf.ArrayDecoders.decodeString; +import static com.google.protobuf.ArrayDecoders.decodeStringList; +import static com.google.protobuf.ArrayDecoders.decodeStringListRequireUtf8; +import static com.google.protobuf.ArrayDecoders.decodeStringRequireUtf8; +import static com.google.protobuf.ArrayDecoders.decodeUnknownField; +import static com.google.protobuf.ArrayDecoders.decodeVarint32; +import static com.google.protobuf.ArrayDecoders.decodeVarint32List; +import static com.google.protobuf.ArrayDecoders.decodeVarint64; +import static com.google.protobuf.ArrayDecoders.decodeVarint64List; +import static com.google.protobuf.ArrayDecoders.skipField; + +import com.google.protobuf.ArrayDecoders.Registers; +import com.google.protobuf.ByteString.CodedBuilder; +import com.google.protobuf.FieldSet.FieldDescriptorLite; +import com.google.protobuf.Internal.EnumVerifier; +import com.google.protobuf.Internal.ProtobufList; +import com.google.protobuf.MapEntryLite.Metadata; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** Schema used for standard messages. */ +final class MessageSchema<T> implements Schema<T> { + private static final int INTS_PER_FIELD = 3; + private static final int OFFSET_BITS = 20; + private static final int OFFSET_MASK = 0XFFFFF; + private static final int FIELD_TYPE_MASK = 0x0FF00000; + private static final int REQUIRED_MASK = 0x10000000; + private static final int ENFORCE_UTF8_MASK = 0x20000000; + private static final int[] EMPTY_INT_ARRAY = new int[0]; + + /** An offset applied to the field type ID for scalar fields that are a member of a oneof. */ + static final int ONEOF_TYPE_OFFSET = 51 /* FieldType.MAP + 1 */; + + /** + * Keep a direct reference to the unsafe object so we don't need to go through the UnsafeUtil + * wrapper for every call. + */ + private static final sun.misc.Unsafe UNSAFE = UnsafeUtil.getUnsafe(); + + /** + * Holds all information for accessing the message fields. The layout is as follows (field + * positions are relative to the offset of the start of the field in the buffer): + * + * <p> + * + * <pre> + * [ 0 - 3] unused + * [ 4 - 31] field number + * [32 - 33] unused + * [34 - 34] whether UTF-8 enforcement should be applied to a string field. + * [35 - 35] whether the field is required + * [36 - 43] field type / for oneof: field type + {@link #ONEOF_TYPE_OFFSET} + * [44 - 63] field offset / oneof value field offset + * [64 - 69] unused + * [70 - 75] field presence mask shift (unused for oneof/repeated fields) + * [76 - 95] presence field offset / oneof case field offset / cached size field offset + * </pre> + */ + private final int[] buffer; + + /** + * Holds object references for fields. For each field entry in {@code buffer}, there are two + * corresponding entries in this array. The content is different from different field types: + * + * <pre> + * Map fields: + * objects[pos] = map default entry instance + * objects[pos + 1] = EnumVerifier if map value is enum, or message class reference if map + * value is message. + * Message fields: + * objects[pos] = null or cached message schema + * objects[pos + 1] = message class reference + * Enum fields: + * objects[pos] = null + * objects[pos + 1] = EnumVerifier + * </pre> + */ + private final Object[] objects; + + private final int minFieldNumber; + private final int maxFieldNumber; + + private final MessageLite defaultInstance; + private final boolean hasExtensions; + private final boolean lite; + private final boolean proto3; + // TODO(xiaofeng): Make both full-runtime and lite-runtime support cached field size. + private final boolean useCachedSizeField; + + /** Represents [checkInitialized positions, map field positions, repeated field offsets]. */ + private final int[] intArray; + + /** + * Values at indices 0 -> checkInitializedCount in intArray are positions to check for + * initialization. + */ + private final int checkInitializedCount; + + /** + * Values at indices checkInitializedCount -> repeatedFieldOffsetStart are map positions. + * Everything after that are repeated field offsets. + */ + private final int repeatedFieldOffsetStart; + + private final NewInstanceSchema newInstanceSchema; + private final ListFieldSchema listFieldSchema; + private final UnknownFieldSchema<?, ?> unknownFieldSchema; + private final ExtensionSchema<?> extensionSchema; + private final MapFieldSchema mapFieldSchema; + + private MessageSchema( + int[] buffer, + Object[] objects, + int minFieldNumber, + int maxFieldNumber, + MessageLite defaultInstance, + boolean proto3, + boolean useCachedSizeField, + int[] intArray, + int checkInitialized, + int mapFieldPositions, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MapFieldSchema mapFieldSchema) { + this.buffer = buffer; + this.objects = objects; + this.minFieldNumber = minFieldNumber; + this.maxFieldNumber = maxFieldNumber; + + this.lite = defaultInstance instanceof GeneratedMessageLite; + this.proto3 = proto3; + this.hasExtensions = extensionSchema != null && extensionSchema.hasExtensions(defaultInstance); + this.useCachedSizeField = useCachedSizeField; + + this.intArray = intArray; + this.checkInitializedCount = checkInitialized; + this.repeatedFieldOffsetStart = mapFieldPositions; + + this.newInstanceSchema = newInstanceSchema; + this.listFieldSchema = listFieldSchema; + this.unknownFieldSchema = unknownFieldSchema; + this.extensionSchema = extensionSchema; + this.defaultInstance = defaultInstance; + this.mapFieldSchema = mapFieldSchema; + } + + static <T> MessageSchema<T> newSchema( + Class<T> messageClass, + MessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MapFieldSchema mapFieldSchema) { + if (messageInfo instanceof RawMessageInfo) { + return newSchemaForRawMessageInfo( + (RawMessageInfo) messageInfo, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + + } else { + return newSchemaForMessageInfo( + (StructuralMessageInfo) messageInfo, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + } + + static <T> MessageSchema<T> newSchemaForRawMessageInfo( + RawMessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MapFieldSchema mapFieldSchema) { + final boolean isProto3 = messageInfo.getSyntax() == ProtoSyntax.PROTO3; + + String info = messageInfo.getStringInfo(); + final int length = info.length(); + int i = 0; + + int next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + final int flags = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + final int fieldCount = next; + + final int oneofCount; + final int hasBitsCount; + final int minFieldNumber; + final int maxFieldNumber; + final int numEntries; + final int mapFieldCount; + final int repeatedFieldCount; + final int checkInitialized; + final int[] intArray; + int objectsPosition; + if (fieldCount == 0) { + oneofCount = 0; + hasBitsCount = 0; + minFieldNumber = 0; + maxFieldNumber = 0; + numEntries = 0; + mapFieldCount = 0; + repeatedFieldCount = 0; + checkInitialized = 0; + intArray = EMPTY_INT_ARRAY; + objectsPosition = 0; + } else { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + oneofCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + hasBitsCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + minFieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + maxFieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + numEntries = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + mapFieldCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + repeatedFieldCount = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + checkInitialized = next; + intArray = new int[checkInitialized + mapFieldCount + repeatedFieldCount]; + // Field objects are after a list of (oneof, oneofCase) pairs + a list of hasbits fields. + objectsPosition = oneofCount * 2 + hasBitsCount; + } + + final sun.misc.Unsafe unsafe = UNSAFE; + final Object[] messageInfoObjects = messageInfo.getObjects(); + int checkInitializedPosition = 0; + final Class<?> messageClass = messageInfo.getDefaultInstance().getClass(); + int[] buffer = new int[numEntries * INTS_PER_FIELD]; + Object[] objects = new Object[numEntries * 2]; + + int mapFieldIndex = checkInitialized; + int repeatedFieldIndex = checkInitialized + mapFieldCount; + + int bufferIndex = 0; + while (i < length) { + final int fieldNumber; + final int fieldTypeWithExtraBits; + final int fieldType; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + fieldNumber = next; + + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + fieldTypeWithExtraBits = next; + fieldType = fieldTypeWithExtraBits & 0xFF; + + if ((fieldTypeWithExtraBits & 0x400) != 0) { + intArray[checkInitializedPosition++] = bufferIndex; + } + + final int fieldOffset; + final int presenceMaskShift; + final int presenceFieldOffset; + + // Oneof + if (fieldType >= ONEOF_TYPE_OFFSET) { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + int oneofIndex = next; + + final int oneofFieldType = fieldType - ONEOF_TYPE_OFFSET; + if (oneofFieldType == 9 /* FieldType.MESSAGE */ + || oneofFieldType == 17 /* FieldType.GROUP */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } else if (oneofFieldType == 12 /* FieldType.ENUM */) { + // proto2 + if ((flags & 0x1) == 0x1) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } + + final java.lang.reflect.Field oneofField; + int index = oneofIndex * 2; + Object o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + oneofField = (java.lang.reflect.Field) o; + } else { + oneofField = reflectField(messageClass, (String) o); + // Memoize java.lang.reflect.Field instances for oneof/hasbits fields, since they're + // potentially used for many Protobuf fields. Since there's a 1-1 mapping from the + // Protobuf field to the Java Field for non-oneofs, there's no benefit for memoizing + // those. + messageInfoObjects[index] = oneofField; + } + + fieldOffset = (int) unsafe.objectFieldOffset(oneofField); + + final java.lang.reflect.Field oneofCaseField; + index++; + o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + oneofCaseField = (java.lang.reflect.Field) o; + } else { + oneofCaseField = reflectField(messageClass, (String) o); + messageInfoObjects[index] = oneofCaseField; + } + + presenceFieldOffset = (int) unsafe.objectFieldOffset(oneofCaseField); + presenceMaskShift = 0; + } else { + Field field = reflectField(messageClass, (String) messageInfoObjects[objectsPosition++]); + if (fieldType == 9 /* FieldType.MESSAGE */ || fieldType == 17 /* FieldType.GROUP */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = field.getType(); + } else if (fieldType == 27 /* FieldType.MESSAGE_LIST */ + || fieldType == 49 /* FieldType.GROUP_LIST */) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } else if (fieldType == 12 /* FieldType.ENUM */ + || fieldType == 30 /* FieldType.ENUM_LIST */ + || fieldType == 44 /* FieldType.ENUM_LIST_PACKED */) { + if ((flags & 0x1) == 0x1) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } else if (fieldType == 50 /* FieldType.MAP */) { + intArray[mapFieldIndex++] = bufferIndex; + objects[bufferIndex / INTS_PER_FIELD * 2] = messageInfoObjects[objectsPosition++]; + if ((fieldTypeWithExtraBits & 0x800) != 0) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageInfoObjects[objectsPosition++]; + } + } + + fieldOffset = (int) unsafe.objectFieldOffset(field); + if ((flags & 0x1) == 0x1 && fieldType <= 17 /* FieldType.GROUP */) { + next = info.charAt(i++); + if (next >= 0xD800) { + int result = next & 0x1FFF; + int shift = 13; + while ((next = info.charAt(i++)) >= 0xD800) { + result |= (next & 0x1FFF) << shift; + shift += 13; + } + next = result | (next << shift); + } + int hasBitsIndex = next; + + final java.lang.reflect.Field hasBitsField; + int index = oneofCount * 2 + hasBitsIndex / 32; + Object o = messageInfoObjects[index]; + if (o instanceof java.lang.reflect.Field) { + hasBitsField = (java.lang.reflect.Field) o; + } else { + hasBitsField = reflectField(messageClass, (String) o); + messageInfoObjects[index] = hasBitsField; + } + + presenceFieldOffset = (int) unsafe.objectFieldOffset(hasBitsField); + presenceMaskShift = hasBitsIndex % 32; + } else { + presenceFieldOffset = 0; + presenceMaskShift = 0; + } + + if (fieldType >= 18 && fieldType <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + intArray[repeatedFieldIndex++] = fieldOffset; + } + } + + buffer[bufferIndex++] = fieldNumber; + buffer[bufferIndex++] = + ((fieldTypeWithExtraBits & 0x200) != 0 ? ENFORCE_UTF8_MASK : 0) + | ((fieldTypeWithExtraBits & 0x100) != 0 ? REQUIRED_MASK : 0) + | (fieldType << OFFSET_BITS) + | fieldOffset; + buffer[bufferIndex++] = (presenceMaskShift << OFFSET_BITS) | presenceFieldOffset; + } + + return new MessageSchema<T>( + buffer, + objects, + minFieldNumber, + maxFieldNumber, + messageInfo.getDefaultInstance(), + isProto3, + /* useCachedSizeField= */ false, + intArray, + checkInitialized, + checkInitialized + mapFieldCount, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + + private static java.lang.reflect.Field reflectField(Class<?> messageClass, String fieldName) { + try { + return messageClass.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + // Some Samsung devices lie about what fields are present via the getDeclaredField API so + // we do the for loop properly that they seem to have messed up... + java.lang.reflect.Field[] fields = messageClass.getDeclaredFields(); + for (java.lang.reflect.Field field : fields) { + if (fieldName.equals(field.getName())) { + return field; + } + } + + // If we make it here, the runtime still lies about what we know to be true at compile + // time. We throw to alert server monitoring for further remediation. + throw new RuntimeException( + "Field " + + fieldName + + " for " + + messageClass.getName() + + " not found. Known fields are " + + Arrays.toString(fields)); + } + } + + static <T> MessageSchema<T> newSchemaForMessageInfo( + StructuralMessageInfo messageInfo, + NewInstanceSchema newInstanceSchema, + ListFieldSchema listFieldSchema, + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MapFieldSchema mapFieldSchema) { + final boolean isProto3 = messageInfo.getSyntax() == ProtoSyntax.PROTO3; + FieldInfo[] fis = messageInfo.getFields(); + final int minFieldNumber; + final int maxFieldNumber; + if (fis.length == 0) { + minFieldNumber = 0; + maxFieldNumber = 0; + } else { + minFieldNumber = fis[0].getFieldNumber(); + maxFieldNumber = fis[fis.length - 1].getFieldNumber(); + } + + final int numEntries = fis.length; + + int[] buffer = new int[numEntries * INTS_PER_FIELD]; + Object[] objects = new Object[numEntries * 2]; + + int mapFieldCount = 0; + int repeatedFieldCount = 0; + for (FieldInfo fi : fis) { + if (fi.getType() == FieldType.MAP) { + mapFieldCount++; + } else if (fi.getType().id() >= 18 && fi.getType().id() <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + repeatedFieldCount++; + } + } + int[] mapFieldPositions = mapFieldCount > 0 ? new int[mapFieldCount] : null; + int[] repeatedFieldOffsets = repeatedFieldCount > 0 ? new int[repeatedFieldCount] : null; + mapFieldCount = 0; + repeatedFieldCount = 0; + + int[] checkInitialized = messageInfo.getCheckInitialized(); + if (checkInitialized == null) { + checkInitialized = EMPTY_INT_ARRAY; + } + int checkInitializedIndex = 0; + // Fill in the manifest data from the descriptors. + int fieldIndex = 0; + for (int bufferIndex = 0; fieldIndex < fis.length; bufferIndex += INTS_PER_FIELD) { + final FieldInfo fi = fis[fieldIndex]; + final int fieldNumber = fi.getFieldNumber(); + + // We found the entry for the next field. Store the entry in the manifest for + // this field and increment the field index. + storeFieldData(fi, buffer, bufferIndex, isProto3, objects); + + // Convert field number to index + if (checkInitializedIndex < checkInitialized.length + && checkInitialized[checkInitializedIndex] == fieldNumber) { + checkInitialized[checkInitializedIndex++] = bufferIndex; + } + + if (fi.getType() == FieldType.MAP) { + mapFieldPositions[mapFieldCount++] = bufferIndex; + } else if (fi.getType().id() >= 18 && fi.getType().id() <= 49) { + // Field types of repeated fields are in a consecutive range from 18 (DOUBLE_LIST) to + // 49 (GROUP_LIST). + repeatedFieldOffsets[repeatedFieldCount++] = + (int) UnsafeUtil.objectFieldOffset(fi.getField()); + } + + fieldIndex++; + } + + if (mapFieldPositions == null) { + mapFieldPositions = EMPTY_INT_ARRAY; + } + if (repeatedFieldOffsets == null) { + repeatedFieldOffsets = EMPTY_INT_ARRAY; + } + int[] combined = + new int[checkInitialized.length + mapFieldPositions.length + repeatedFieldOffsets.length]; + System.arraycopy(checkInitialized, 0, combined, 0, checkInitialized.length); + System.arraycopy( + mapFieldPositions, 0, combined, checkInitialized.length, mapFieldPositions.length); + System.arraycopy( + repeatedFieldOffsets, + 0, + combined, + checkInitialized.length + mapFieldPositions.length, + repeatedFieldOffsets.length); + + return new MessageSchema<T>( + buffer, + objects, + minFieldNumber, + maxFieldNumber, + messageInfo.getDefaultInstance(), + isProto3, + /* useCachedSizeField= */ true, + combined, + checkInitialized.length, + checkInitialized.length + mapFieldPositions.length, + newInstanceSchema, + listFieldSchema, + unknownFieldSchema, + extensionSchema, + mapFieldSchema); + } + + private static void storeFieldData( + FieldInfo fi, int[] buffer, int bufferIndex, boolean proto3, Object[] objects) { + final int fieldOffset; + final int typeId; + final int presenceMaskShift; + final int presenceFieldOffset; + + OneofInfo oneof = fi.getOneof(); + if (oneof != null) { + typeId = fi.getType().id() + ONEOF_TYPE_OFFSET; + fieldOffset = (int) UnsafeUtil.objectFieldOffset(oneof.getValueField()); + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(oneof.getCaseField()); + presenceMaskShift = 0; + } else { + FieldType type = fi.getType(); + fieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getField()); + typeId = type.id(); + if (!proto3 && !type.isList() && !type.isMap()) { + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getPresenceField()); + presenceMaskShift = Integer.numberOfTrailingZeros(fi.getPresenceMask()); + } else { + if (fi.getCachedSizeField() == null) { + presenceFieldOffset = 0; + presenceMaskShift = 0; + } else { + presenceFieldOffset = (int) UnsafeUtil.objectFieldOffset(fi.getCachedSizeField()); + presenceMaskShift = 0; + } + } + } + + buffer[bufferIndex] = fi.getFieldNumber(); + buffer[bufferIndex + 1] = + (fi.isEnforceUtf8() ? ENFORCE_UTF8_MASK : 0) + | (fi.isRequired() ? REQUIRED_MASK : 0) + | (typeId << OFFSET_BITS) + | fieldOffset; + buffer[bufferIndex + 2] = (presenceMaskShift << OFFSET_BITS) | presenceFieldOffset; + + Object messageFieldClass = fi.getMessageFieldClass(); + if (fi.getMapDefaultEntry() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2] = fi.getMapDefaultEntry(); + if (messageFieldClass != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageFieldClass; + } else if (fi.getEnumVerifier() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = fi.getEnumVerifier(); + } + } else { + if (messageFieldClass != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = messageFieldClass; + } else if (fi.getEnumVerifier() != null) { + objects[bufferIndex / INTS_PER_FIELD * 2 + 1] = fi.getEnumVerifier(); + } + } + } + + @SuppressWarnings("unchecked") + @Override + public T newInstance() { + return (T) newInstanceSchema.newInstance(defaultInstance); + } + + @Override + public boolean equals(T message, T other) { + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + if (!equals(message, other, pos)) { + return false; + } + } + + Object messageUnknown = unknownFieldSchema.getFromMessage(message); + Object otherUnknown = unknownFieldSchema.getFromMessage(other); + if (!messageUnknown.equals(otherUnknown)) { + return false; + } + + if (hasExtensions) { + FieldSet<?> messageExtensions = extensionSchema.getExtensions(message); + FieldSet<?> otherExtensions = extensionSchema.getExtensions(other); + return messageExtensions.equals(otherExtensions); + } + return true; + } + + private boolean equals(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + return arePresentForEquals(message, other, pos) + && Double.doubleToLongBits(UnsafeUtil.getDouble(message, offset)) + == Double.doubleToLongBits(UnsafeUtil.getDouble(other, offset)); + case 1: // FLOAT: + return arePresentForEquals(message, other, pos) + && Float.floatToIntBits(UnsafeUtil.getFloat(message, offset)) + == Float.floatToIntBits(UnsafeUtil.getFloat(other, offset)); + case 2: // INT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 3: // UINT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 4: // INT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 5: // FIXED64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 6: // FIXED32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 7: // BOOL: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getBoolean(message, offset) == UnsafeUtil.getBoolean(other, offset); + case 8: // STRING: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 9: // MESSAGE: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 10: // BYTES: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 11: // UINT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 12: // ENUM: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 13: // SFIXED32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 14: // SFIXED64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 15: // SINT32: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getInt(message, offset) == UnsafeUtil.getInt(other, offset); + case 16: // SINT64: + return arePresentForEquals(message, other, pos) + && UnsafeUtil.getLong(message, offset) == UnsafeUtil.getLong(other, offset); + case 17: // GROUP: + return arePresentForEquals(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + return SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 50: // MAP: + return SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + case 51: // ONEOF_DOUBLE: + case 52: // ONEOF_FLOAT: + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + case 55: // ONEOF_INT32: + case 56: // ONEOF_FIXED64: + case 57: // ONEOF_FIXED32: + case 58: // ONEOF_BOOL: + case 59: // ONEOF_STRING: + case 60: // ONEOF_MESSAGE: + case 61: // ONEOF_BYTES: + case 62: // ONEOF_UINT32: + case 63: // ONEOF_ENUM: + case 64: // ONEOF_SFIXED32: + case 65: // ONEOF_SFIXED64: + case 66: // ONEOF_SINT32: + case 67: // ONEOF_SINT64: + case 68: // ONEOF_GROUP: + return isOneofCaseEqual(message, other, pos) + && SchemaUtil.safeEquals( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(other, offset)); + default: + // Assume it's an empty entry - just go to the next entry. + return true; + } + } + + @Override + public int hashCode(T message) { + int hashCode = 0; + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int entryNumber = numberAt(pos); + + final long offset = offset(typeAndOffset); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + hashCode = + (hashCode * 53) + + Internal.hashLong( + Double.doubleToLongBits(UnsafeUtil.getDouble(message, offset))); + break; + case 1: // FLOAT: + hashCode = (hashCode * 53) + Float.floatToIntBits(UnsafeUtil.getFloat(message, offset)); + break; + case 2: // INT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 3: // UINT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 4: // INT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 5: // FIXED64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 6: // FIXED32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 7: // BOOL: + hashCode = (hashCode * 53) + Internal.hashBoolean(UnsafeUtil.getBoolean(message, offset)); + break; + case 8: // STRING: + hashCode = (hashCode * 53) + ((String) UnsafeUtil.getObject(message, offset)).hashCode(); + break; + case 9: // MESSAGE: + { + int protoHash = 37; + Object submessage = UnsafeUtil.getObject(message, offset); + if (submessage != null) { + protoHash = submessage.hashCode(); + } + hashCode = (53 * hashCode) + protoHash; + break; + } + case 10: // BYTES: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 11: // UINT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 12: // ENUM: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 13: // SFIXED32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 14: // SFIXED64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + case 15: // SINT32: + hashCode = (hashCode * 53) + (UnsafeUtil.getInt(message, offset)); + break; + case 16: // SINT64: + hashCode = (hashCode * 53) + Internal.hashLong(UnsafeUtil.getLong(message, offset)); + break; + + case 17: // GROUP: + { + int protoHash = 37; + Object submessage = UnsafeUtil.getObject(message, offset); + if (submessage != null) { + protoHash = submessage.hashCode(); + } + hashCode = (53 * hashCode) + protoHash; + break; + } + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 50: // MAP: + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = + (hashCode * 53) + + Internal.hashLong(Double.doubleToLongBits(oneofDoubleAt(message, offset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Float.floatToIntBits(oneofFloatAt(message, offset)); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashBoolean(oneofBooleanAt(message, offset)); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = + (hashCode * 53) + ((String) UnsafeUtil.getObject(message, offset)).hashCode(); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, entryNumber, pos)) { + Object submessage = UnsafeUtil.getObject(message, offset); + hashCode = (53 * hashCode) + submessage.hashCode(); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + UnsafeUtil.getObject(message, offset).hashCode(); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + (oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, entryNumber, pos)) { + hashCode = (hashCode * 53) + Internal.hashLong(oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, entryNumber, pos)) { + Object submessage = UnsafeUtil.getObject(message, offset); + hashCode = (53 * hashCode) + submessage.hashCode(); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + + hashCode = (hashCode * 53) + unknownFieldSchema.getFromMessage(message).hashCode(); + + if (hasExtensions) { + hashCode = (hashCode * 53) + extensionSchema.getExtensions(message).hashCode(); + } + + return hashCode; + } + + @Override + public void mergeFrom(T message, T other) { + if (other == null) { + throw new NullPointerException(); + } + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + // A separate method allows for better JIT optimizations + mergeSingleField(message, other, i); + } + + if (!proto3) { + SchemaUtil.mergeUnknownFields(unknownFieldSchema, message, other); + + if (hasExtensions) { + SchemaUtil.mergeExtensions(extensionSchema, message, other); + } + } + } + + private void mergeSingleField(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + final int number = numberAt(pos); + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putDouble(message, offset, UnsafeUtil.getDouble(other, offset)); + setFieldPresent(message, pos); + } + break; + case 1: // FLOAT: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putFloat(message, offset, UnsafeUtil.getFloat(other, offset)); + setFieldPresent(message, pos); + } + break; + case 2: // INT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 3: // UINT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 4: // INT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 5: // FIXED64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 6: // FIXED32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 7: // BOOL: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putBoolean(message, offset, UnsafeUtil.getBoolean(other, offset)); + setFieldPresent(message, pos); + } + break; + case 8: // STRING: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setFieldPresent(message, pos); + } + break; + case 9: // MESSAGE: + mergeMessage(message, other, pos); + break; + case 10: // BYTES: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setFieldPresent(message, pos); + } + break; + case 11: // UINT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 12: // ENUM: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 15: // SINT32: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putInt(message, offset, UnsafeUtil.getInt(other, offset)); + setFieldPresent(message, pos); + } + break; + case 16: // SINT64: + if (isFieldPresent(other, pos)) { + UnsafeUtil.putLong(message, offset, UnsafeUtil.getLong(other, offset)); + setFieldPresent(message, pos); + } + break; + case 17: // GROUP: + mergeMessage(message, other, pos); + break; + case 18: // DOUBLE_LIST: + case 19: // FLOAT_LIST: + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 22: // INT32_LIST: + case 23: // FIXED64_LIST: + case 24: // FIXED32_LIST: + case 25: // BOOL_LIST: + case 26: // STRING_LIST: + case 27: // MESSAGE_LIST: + case 28: // BYTES_LIST: + case 29: // UINT32_LIST: + case 30: // ENUM_LIST: + case 31: // SFIXED32_LIST: + case 32: // SFIXED64_LIST: + case 33: // SINT32_LIST: + case 34: // SINT64_LIST: + case 35: // DOUBLE_LIST_PACKED: + case 36: // FLOAT_LIST_PACKED: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + case 39: // INT32_LIST_PACKED: + case 40: // FIXED64_LIST_PACKED: + case 41: // FIXED32_LIST_PACKED: + case 42: // BOOL_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + case 44: // ENUM_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + case 47: // SINT32_LIST_PACKED: + case 48: // SINT64_LIST_PACKED: + case 49: // GROUP_LIST: + listFieldSchema.mergeListsAt(message, other, offset); + break; + case 50: // MAP: + SchemaUtil.mergeMap(mapFieldSchema, message, other, offset); + break; + case 51: // ONEOF_DOUBLE: + case 52: // ONEOF_FLOAT: + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + case 55: // ONEOF_INT32: + case 56: // ONEOF_FIXED64: + case 57: // ONEOF_FIXED32: + case 58: // ONEOF_BOOL: + case 59: // ONEOF_STRING: + if (isOneofPresent(other, number, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setOneofPresent(message, number, pos); + } + break; + + case 60: // ONEOF_MESSAGE: + mergeOneofMessage(message, other, pos); + break; + case 61: // ONEOF_BYTES: + case 62: // ONEOF_UINT32: + case 63: // ONEOF_ENUM: + case 64: // ONEOF_SFIXED32: + case 65: // ONEOF_SFIXED64: + case 66: // ONEOF_SINT32: + case 67: // ONEOF_SINT64: + if (isOneofPresent(other, number, pos)) { + UnsafeUtil.putObject(message, offset, UnsafeUtil.getObject(other, offset)); + setOneofPresent(message, number, pos); + } + break; + case 68: // ONEOF_GROUP: + mergeOneofMessage(message, other, pos); + break; + default: + break; + } + } + + private void mergeMessage(T message, T other, int pos) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + + if (!isFieldPresent(other, pos)) { + return; + } + + Object mine = UnsafeUtil.getObject(message, offset); + Object theirs = UnsafeUtil.getObject(other, offset); + if (mine != null && theirs != null) { + Object merged = Internal.mergeMessage(mine, theirs); + UnsafeUtil.putObject(message, offset, merged); + setFieldPresent(message, pos); + } else if (theirs != null) { + UnsafeUtil.putObject(message, offset, theirs); + setFieldPresent(message, pos); + } + } + + private void mergeOneofMessage(T message, T other, int pos) { + int typeAndOffset = typeAndOffsetAt(pos); + int number = numberAt(pos); + long offset = offset(typeAndOffset); + + if (!isOneofPresent(other, number, pos)) { + return; + } + + Object mine = UnsafeUtil.getObject(message, offset); + Object theirs = UnsafeUtil.getObject(other, offset); + if (mine != null && theirs != null) { + Object merged = Internal.mergeMessage(mine, theirs); + UnsafeUtil.putObject(message, offset, merged); + setOneofPresent(message, number, pos); + } else if (theirs != null) { + UnsafeUtil.putObject(message, offset, theirs); + setOneofPresent(message, number, pos); + } + } + + @Override + public int getSerializedSize(T message) { + return proto3 ? getSerializedSizeProto3(message) : getSerializedSizeProto2(message); + } + + @SuppressWarnings("unchecked") + private int getSerializedSizeProto2(T message) { + int size = 0; + + final sun.misc.Unsafe unsafe = UNSAFE; + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(i); + final int number = numberAt(i); + + int fieldType = type(typeAndOffset); + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (fieldType <= 17) { + presenceMaskAndOffset = buffer[i + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + } else if (useCachedSizeField + && fieldType >= FieldType.DOUBLE_LIST_PACKED.id() + && fieldType <= FieldType.SINT64_LIST_PACKED.id()) { + presenceMaskAndOffset = buffer[i + 2] & OFFSET_MASK; + } + + final long offset = offset(typeAndOffset); + + switch (fieldType) { + case 0: // DOUBLE: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 1: // FLOAT: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 2: // INT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 3: // UINT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeUInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 4: // INT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 6: // FIXED32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 7: // BOOL: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 8: // STRING: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 9: // MESSAGE: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 10: // BYTES: + if ((currentPresenceField & presenceMask) != 0) { + ByteString value = (ByteString) unsafe.getObject(message, offset); + size += CodedOutputStream.computeBytesSize(number, value); + } + break; + case 11: // UINT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeUInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 12: // ENUM: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeEnumSize(number, unsafe.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 14: // SFIXED64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 15: // SINT32: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSInt32Size(number, unsafe.getInt(message, offset)); + } + break; + case 16: // SINT64: + if ((currentPresenceField & presenceMask) != 0) { + size += CodedOutputStream.computeSInt64Size(number, unsafe.getLong(message, offset)); + } + break; + case 17: // GROUP: + if ((currentPresenceField & presenceMask) != 0) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + case 18: // DOUBLE_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List<?>) unsafe.getObject(message, offset), false); + break; + case 19: // FLOAT_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List<?>) unsafe.getObject(message, offset), false); + break; + case 20: // INT64_LIST: + size += + SchemaUtil.computeSizeInt64List( + number, (List<Long>) unsafe.getObject(message, offset), false); + break; + case 21: // UINT64_LIST: + size += + SchemaUtil.computeSizeUInt64List( + number, (List<Long>) unsafe.getObject(message, offset), false); + break; + case 22: // INT32_LIST: + size += + SchemaUtil.computeSizeInt32List( + number, (List<Integer>) unsafe.getObject(message, offset), false); + break; + case 23: // FIXED64_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List<?>) unsafe.getObject(message, offset), false); + break; + case 24: // FIXED32_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List<?>) unsafe.getObject(message, offset), false); + break; + case 25: // BOOL_LIST: + size += + SchemaUtil.computeSizeBoolList( + number, (List<?>) unsafe.getObject(message, offset), false); + break; + case 26: // STRING_LIST: + size += + SchemaUtil.computeSizeStringList(number, (List<?>) unsafe.getObject(message, offset)); + break; + case 27: // MESSAGE_LIST: + size += + SchemaUtil.computeSizeMessageList( + number, (List<?>) unsafe.getObject(message, offset), getMessageFieldSchema(i)); + break; + case 28: // BYTES_LIST: + size += + SchemaUtil.computeSizeByteStringList( + number, (List<ByteString>) unsafe.getObject(message, offset)); + break; + case 29: // UINT32_LIST: + size += + SchemaUtil.computeSizeUInt32List( + number, (List<Integer>) unsafe.getObject(message, offset), false); + break; + case 30: // ENUM_LIST: + size += + SchemaUtil.computeSizeEnumList( + number, (List<Integer>) unsafe.getObject(message, offset), false); + break; + case 31: // SFIXED32_LIST: + size += + SchemaUtil.computeSizeFixed32List( + number, (List<Integer>) unsafe.getObject(message, offset), false); + break; + case 32: // SFIXED64_LIST: + size += + SchemaUtil.computeSizeFixed64List( + number, (List<Long>) unsafe.getObject(message, offset), false); + break; + case 33: // SINT32_LIST: + size += + SchemaUtil.computeSizeSInt32List( + number, (List<Integer>) unsafe.getObject(message, offset), false); + break; + case 34: // SINT64_LIST: + size += + SchemaUtil.computeSizeSInt64List( + number, (List<Long>) unsafe.getObject(message, offset), false); + break; + case 35: + { // DOUBLE_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Double>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 36: + { // FLOAT_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Float>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 37: + { // INT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 38: + { // UINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 39: + { // INT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 40: + { // FIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 41: + { // FIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 42: + { // BOOL_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeBoolListNoTag( + (List<Boolean>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 43: + { // UINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 44: + { // ENUM_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeEnumListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 45: + { // SFIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 46: + { // SFIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 47: + { // SINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 48: + { // SINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) presenceMaskAndOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 49: // GROUP_LIST: + size += + SchemaUtil.computeSizeGroupList( + number, + (List<MessageLite>) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + size += + mapFieldSchema.getSerializedSize( + number, unsafe.getObject(message, offset), getMapFieldDefaultEntry(i)); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, i)) { + Object value = unsafe.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, i)) { + Object value = unsafe.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeBytesSize( + number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeEnumSize(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) unsafe.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + default: + // Assume it's an empty entry. + } + } + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + if (hasExtensions) { + size += extensionSchema.getExtensions(message).getSerializedSize(); + } + + return size; + } + + private int getSerializedSizeProto3(T message) { + final sun.misc.Unsafe unsafe = UNSAFE; + int size = 0; + for (int i = 0; i < buffer.length; i += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(i); + final int fieldType = type(typeAndOffset); + final int number = numberAt(i); + + final long offset = offset(typeAndOffset); + final int cachedSizeOffset = + fieldType >= FieldType.DOUBLE_LIST_PACKED.id() + && fieldType <= FieldType.SINT64_LIST_PACKED.id() + ? buffer[i + 2] & OFFSET_MASK + : 0; + + switch (fieldType) { + case 0: // DOUBLE: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 2: // INT64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeUInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 4: // INT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 8: // STRING: + if (isFieldPresent(message, i)) { + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, i)) { + Object value = UnsafeUtil.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, i)) { + ByteString value = (ByteString) UnsafeUtil.getObject(message, offset); + size += CodedOutputStream.computeBytesSize(number, value); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeUInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeEnumSize(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, i)) { + size += CodedOutputStream.computeSInt32Size(number, UnsafeUtil.getInt(message, offset)); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeSInt64Size(number, UnsafeUtil.getLong(message, offset)); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) UnsafeUtil.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + case 18: // DOUBLE_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 19: // FLOAT_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 20: // INT64_LIST: + size += + SchemaUtil.computeSizeInt64List(number, (List<Long>) listAt(message, offset), false); + break; + case 21: // UINT64_LIST: + size += + SchemaUtil.computeSizeUInt64List(number, (List<Long>) listAt(message, offset), false); + break; + case 22: // INT32_LIST: + size += + SchemaUtil.computeSizeInt32List( + number, (List<Integer>) listAt(message, offset), false); + break; + case 23: // FIXED64_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 24: // FIXED32_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 25: // BOOL_LIST: + size += SchemaUtil.computeSizeBoolList(number, listAt(message, offset), false); + break; + case 26: // STRING_LIST: + size += SchemaUtil.computeSizeStringList(number, listAt(message, offset)); + break; + case 27: // MESSAGE_LIST: + size += + SchemaUtil.computeSizeMessageList( + number, listAt(message, offset), getMessageFieldSchema(i)); + break; + case 28: // BYTES_LIST: + size += + SchemaUtil.computeSizeByteStringList( + number, (List<ByteString>) listAt(message, offset)); + break; + case 29: // UINT32_LIST: + size += + SchemaUtil.computeSizeUInt32List( + number, (List<Integer>) listAt(message, offset), false); + break; + case 30: // ENUM_LIST: + size += + SchemaUtil.computeSizeEnumList( + number, (List<Integer>) listAt(message, offset), false); + break; + case 31: // SFIXED32_LIST: + size += SchemaUtil.computeSizeFixed32List(number, listAt(message, offset), false); + break; + case 32: // SFIXED64_LIST: + size += SchemaUtil.computeSizeFixed64List(number, listAt(message, offset), false); + break; + case 33: // SINT32_LIST: + size += + SchemaUtil.computeSizeSInt32List( + number, (List<Integer>) listAt(message, offset), false); + break; + case 34: // SINT64_LIST: + size += + SchemaUtil.computeSizeSInt64List(number, (List<Long>) listAt(message, offset), false); + break; + case 35: + { // DOUBLE_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Double>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 36: + { // FLOAT_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Float>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 37: + { // INT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 38: + { // UINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 39: + { // INT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 40: + { // FIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 41: + { // FIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 42: + { // BOOL_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeBoolListNoTag( + (List<Boolean>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 43: + { // UINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeUInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 44: + { // ENUM_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeEnumListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 45: + { // SFIXED32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 46: + { // SFIXED64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeFixed64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 47: + { // SINT32_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt32ListNoTag( + (List<Integer>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 48: + { // SINT64_LIST_PACKED: + int fieldSize = + SchemaUtil.computeSizeSInt64ListNoTag( + (List<Long>) unsafe.getObject(message, offset)); + if (fieldSize > 0) { + if (useCachedSizeField) { + unsafe.putInt(message, (long) cachedSizeOffset, fieldSize); + } + size += + CodedOutputStream.computeTagSize(number) + + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + + fieldSize; + } + break; + } + case 49: // GROUP_LIST: + size += + SchemaUtil.computeSizeGroupList( + number, (List<MessageLite>) listAt(message, offset), getMessageFieldSchema(i)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + size += + mapFieldSchema.getSerializedSize( + number, UnsafeUtil.getObject(message, offset), getMapFieldDefaultEntry(i)); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeDoubleSize(number, 0); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFloatSize(number, 0); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed64Size(number, 0); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeFixed32Size(number, 0); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeBoolSize(number, true); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, i)) { + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSize(number, (ByteString) value); + } else { + size += CodedOutputStream.computeStringSize(number, (String) value); + } + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, i)) { + Object value = UnsafeUtil.getObject(message, offset); + size += SchemaUtil.computeSizeMessage(number, value, getMessageFieldSchema(i)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeBytesSize( + number, (ByteString) UnsafeUtil.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeUInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeEnumSize(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed32Size(number, 0); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSFixed64Size(number, 0); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt32Size(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, i)) { + size += CodedOutputStream.computeSInt64Size(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, i)) { + size += + CodedOutputStream.computeGroupSize( + number, + (MessageLite) UnsafeUtil.getObject(message, offset), + getMessageFieldSchema(i)); + } + break; + default: + // Assume it's an empty entry. + } + } + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + return size; + } + + private <UT, UB> int getUnknownFieldsSerializedSize( + UnknownFieldSchema<UT, UB> schema, T message) { + UT unknowns = schema.getFromMessage(message); + return schema.getSerializedSize(unknowns); + } + + private static List<?> listAt(Object message, long offset) { + return (List<?>) UnsafeUtil.getObject(message, offset); + } + + @SuppressWarnings("unchecked") + @Override + // TODO(nathanmittler): Consider serializing oneof fields last so that only one entry per + // oneof is actually serialized. This would mean that we would violate the serialization order + // contract. It should also be noted that Go currently does this. + public void writeTo(T message, Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + writeFieldsInDescendingOrder(message, writer); + } else { + if (proto3) { + writeFieldsInAscendingOrderProto3(message, writer); + } else { + writeFieldsInAscendingOrderProto2(message, writer); + } + } + } + + @SuppressWarnings("unchecked") + private void writeFieldsInAscendingOrderProto2(T message, Writer writer) throws IOException { + Iterator<? extends Map.Entry<?, ?>> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.iterator(); + nextExtension = extensionIterator.next(); + } + } + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + final int bufferLength = buffer.length; + final sun.misc.Unsafe unsafe = UNSAFE; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + final int fieldType = type(typeAndOffset); + + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (!proto3 && fieldType <= 17) { + presenceMaskAndOffset = buffer[pos + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + } + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) <= number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + final long offset = offset(typeAndOffset); + + switch (fieldType) { + case 0: // DOUBLE: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeDouble(number, doubleAt(message, offset)); + } + break; + case 1: // FLOAT: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFloat(number, floatAt(message, offset)); + } + break; + case 2: // INT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeInt64(number, unsafe.getLong(message, offset)); + } + break; + case 3: // UINT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeUInt64(number, unsafe.getLong(message, offset)); + } + break; + case 4: // INT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeInt32(number, unsafe.getInt(message, offset)); + } + break; + case 5: // FIXED64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFixed64(number, unsafe.getLong(message, offset)); + } + break; + case 6: // FIXED32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeFixed32(number, unsafe.getInt(message, offset)); + } + break; + case 7: // BOOL: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeBool(number, booleanAt(message, offset)); + } + break; + case 8: // STRING: + if ((currentPresenceField & presenceMask) != 0) { + writeString(number, unsafe.getObject(message, offset), writer); + } + break; + case 9: // MESSAGE: + if ((currentPresenceField & presenceMask) != 0) { + Object value = unsafe.getObject(message, offset); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeBytes(number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 11: // UINT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeUInt32(number, unsafe.getInt(message, offset)); + } + break; + case 12: // ENUM: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeEnum(number, unsafe.getInt(message, offset)); + } + break; + case 13: // SFIXED32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSFixed32(number, unsafe.getInt(message, offset)); + } + break; + case 14: // SFIXED64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSFixed64(number, unsafe.getLong(message, offset)); + } + break; + case 15: // SINT32: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSInt32(number, unsafe.getInt(message, offset)); + } + break; + case 16: // SINT64: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeSInt64(number, unsafe.getLong(message, offset)); + } + break; + case 17: // GROUP: + if ((currentPresenceField & presenceMask) != 0) { + writer.writeGroup( + number, unsafe.getObject(message, offset), getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), (List<Double>) unsafe.getObject(message, offset), writer, false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), (List<Float>) unsafe.getObject(message, offset), writer, false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), (List<Boolean>) unsafe.getObject(message, offset), writer, false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), (List<String>) unsafe.getObject(message, offset), writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List<?>) unsafe.getObject(message, offset), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), (List<ByteString>) unsafe.getObject(message, offset), writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, false); + break; + case 35: // DOUBLE_LIST_PACKED: + // TODO(xiaofeng): Make use of cached field size to speed up serialization. + SchemaUtil.writeDoubleList( + numberAt(pos), (List<Double>) unsafe.getObject(message, offset), writer, true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), (List<Float>) unsafe.getObject(message, offset), writer, true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), (List<Boolean>) unsafe.getObject(message, offset), writer, true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), (List<Integer>) unsafe.getObject(message, offset), writer, true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), (List<Long>) unsafe.getObject(message, offset), writer, true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List<?>) unsafe.getObject(message, offset), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, unsafe.getObject(message, offset), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset)); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset)); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset)); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset)); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset)); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset)); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset)); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset)); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, unsafe.getObject(message, offset), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = unsafe.getObject(message, offset); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes(number, (ByteString) unsafe.getObject(message, offset)); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset)); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset)); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset)); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset)); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset)); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset)); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, unsafe.getObject(message, offset), getMessageFieldSchema(pos)); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + } + + @SuppressWarnings("unchecked") + private void writeFieldsInAscendingOrderProto3(T message, Writer writer) throws IOException { + Iterator<? extends Map.Entry<?, ?>> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.iterator(); + nextExtension = extensionIterator.next(); + } + } + + final int bufferLength = buffer.length; + for (int pos = 0; pos < bufferLength; pos += INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) <= number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(message, pos)) { + writer.writeDouble(number, doubleAt(message, offset(typeAndOffset))); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, pos)) { + writer.writeFloat(number, floatAt(message, offset(typeAndOffset))); + } + break; + case 2: // INT64: + if (isFieldPresent(message, pos)) { + writer.writeInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, pos)) { + writer.writeUInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 4: // INT32: + if (isFieldPresent(message, pos)) { + writer.writeInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, pos)) { + writer.writeFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, pos)) { + writer.writeFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, pos)) { + writer.writeBool(number, booleanAt(message, offset(typeAndOffset))); + } + break; + case 8: // STRING: + if (isFieldPresent(message, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, pos)) { + writer.writeUInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, pos)) { + writer.writeEnum(number, intAt(message, offset(typeAndOffset))); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, pos)) { + writer.writeSFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, pos)) { + writer.writeSFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, pos)) { + writer.writeSInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, pos)) { + writer.writeSInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List<Double>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), + (List<Float>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), + (List<Boolean>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), + (List<String>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List<?>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), + (List<ByteString>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 35: // DOUBLE_LIST_PACKED: + // TODO(xiaofeng): Make use of cached field size to speed up serialization. + SchemaUtil.writeDoubleList( + numberAt(pos), + (List<Double>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), + (List<Float>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), + (List<Boolean>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List<?>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, UnsafeUtil.getObject(message, offset(typeAndOffset)), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset(typeAndOffset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset(typeAndOffset))); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset(typeAndOffset))); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + default: + // Assume it's an empty entry - just go to the next entry. + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + } + + @SuppressWarnings("unchecked") + private void writeFieldsInDescendingOrder(T message, Writer writer) throws IOException { + writeUnknownInMessageTo(unknownFieldSchema, message, writer); + + Iterator<? extends Map.Entry<?, ?>> extensionIterator = null; + Map.Entry nextExtension = null; + if (hasExtensions) { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + if (!extensions.isEmpty()) { + extensionIterator = extensions.descendingIterator(); + nextExtension = extensionIterator.next(); + } + } + + for (int pos = buffer.length - INTS_PER_FIELD; pos >= 0; pos -= INTS_PER_FIELD) { + final int typeAndOffset = typeAndOffsetAt(pos); + final int number = numberAt(pos); + + // Write any extensions that need to be written before the current field. + while (nextExtension != null && extensionSchema.extensionNumber(nextExtension) > number) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + if (isFieldPresent(message, pos)) { + writer.writeDouble(number, doubleAt(message, offset(typeAndOffset))); + } + break; + case 1: // FLOAT: + if (isFieldPresent(message, pos)) { + writer.writeFloat(number, floatAt(message, offset(typeAndOffset))); + } + break; + case 2: // INT64: + if (isFieldPresent(message, pos)) { + writer.writeInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 3: // UINT64: + if (isFieldPresent(message, pos)) { + writer.writeUInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 4: // INT32: + if (isFieldPresent(message, pos)) { + writer.writeInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 5: // FIXED64: + if (isFieldPresent(message, pos)) { + writer.writeFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 6: // FIXED32: + if (isFieldPresent(message, pos)) { + writer.writeFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 7: // BOOL: + if (isFieldPresent(message, pos)) { + writer.writeBool(number, booleanAt(message, offset(typeAndOffset))); + } + break; + case 8: // STRING: + if (isFieldPresent(message, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 9: // MESSAGE: + if (isFieldPresent(message, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 10: // BYTES: + if (isFieldPresent(message, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 11: // UINT32: + if (isFieldPresent(message, pos)) { + writer.writeUInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 12: // ENUM: + if (isFieldPresent(message, pos)) { + writer.writeEnum(number, intAt(message, offset(typeAndOffset))); + } + break; + case 13: // SFIXED32: + if (isFieldPresent(message, pos)) { + writer.writeSFixed32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 14: // SFIXED64: + if (isFieldPresent(message, pos)) { + writer.writeSFixed64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 15: // SINT32: + if (isFieldPresent(message, pos)) { + writer.writeSInt32(number, intAt(message, offset(typeAndOffset))); + } + break; + case 16: // SINT64: + if (isFieldPresent(message, pos)) { + writer.writeSInt64(number, longAt(message, offset(typeAndOffset))); + } + break; + case 17: // GROUP: + if (isFieldPresent(message, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + case 18: // DOUBLE_LIST: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List<Double>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 19: // FLOAT_LIST: + SchemaUtil.writeFloatList( + numberAt(pos), + (List<Float>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 20: // INT64_LIST: + SchemaUtil.writeInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 21: // UINT64_LIST: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 22: // INT32_LIST: + SchemaUtil.writeInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 23: // FIXED64_LIST: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 24: // FIXED32_LIST: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 25: // BOOL_LIST: + SchemaUtil.writeBoolList( + numberAt(pos), + (List<Boolean>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 26: // STRING_LIST: + SchemaUtil.writeStringList( + numberAt(pos), + (List<String>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 27: // MESSAGE_LIST: + SchemaUtil.writeMessageList( + numberAt(pos), + (List<?>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 28: // BYTES_LIST: + SchemaUtil.writeBytesList( + numberAt(pos), + (List<ByteString>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer); + break; + case 29: // UINT32_LIST: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 30: // ENUM_LIST: + SchemaUtil.writeEnumList( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 31: // SFIXED32_LIST: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 32: // SFIXED64_LIST: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 33: // SINT32_LIST: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 34: // SINT64_LIST: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + false); + break; + case 35: // DOUBLE_LIST_PACKED: + SchemaUtil.writeDoubleList( + numberAt(pos), + (List<Double>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 36: // FLOAT_LIST_PACKED: + SchemaUtil.writeFloatList( + numberAt(pos), + (List<Float>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 37: // INT64_LIST_PACKED: + SchemaUtil.writeInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 38: // UINT64_LIST_PACKED: + SchemaUtil.writeUInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 39: // INT32_LIST_PACKED: + SchemaUtil.writeInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 40: // FIXED64_LIST_PACKED: + SchemaUtil.writeFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 41: // FIXED32_LIST_PACKED: + SchemaUtil.writeFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + + break; + case 42: // BOOL_LIST_PACKED: + SchemaUtil.writeBoolList( + numberAt(pos), + (List<Boolean>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 43: // UINT32_LIST_PACKED: + SchemaUtil.writeUInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 44: // ENUM_LIST_PACKED: + SchemaUtil.writeEnumList( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 45: // SFIXED32_LIST_PACKED: + SchemaUtil.writeSFixed32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 46: // SFIXED64_LIST_PACKED: + SchemaUtil.writeSFixed64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 47: // SINT32_LIST_PACKED: + SchemaUtil.writeSInt32List( + numberAt(pos), + (List<Integer>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 48: // SINT64_LIST_PACKED: + SchemaUtil.writeSInt64List( + numberAt(pos), + (List<Long>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + true); + break; + case 49: // GROUP_LIST: + SchemaUtil.writeGroupList( + numberAt(pos), + (List<?>) UnsafeUtil.getObject(message, offset(typeAndOffset)), + writer, + getMessageFieldSchema(pos)); + break; + case 50: // MAP: + // TODO(dweis): Use schema cache. + writeMapHelper(writer, number, UnsafeUtil.getObject(message, offset(typeAndOffset)), pos); + break; + case 51: // ONEOF_DOUBLE: + if (isOneofPresent(message, number, pos)) { + writer.writeDouble(number, oneofDoubleAt(message, offset(typeAndOffset))); + } + break; + case 52: // ONEOF_FLOAT: + if (isOneofPresent(message, number, pos)) { + writer.writeFloat(number, oneofFloatAt(message, offset(typeAndOffset))); + } + break; + case 53: // ONEOF_INT64: + if (isOneofPresent(message, number, pos)) { + writer.writeInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 54: // ONEOF_UINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 55: // ONEOF_INT32: + if (isOneofPresent(message, number, pos)) { + writer.writeInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 56: // ONEOF_FIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 57: // ONEOF_FIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 58: // ONEOF_BOOL: + if (isOneofPresent(message, number, pos)) { + writer.writeBool(number, oneofBooleanAt(message, offset(typeAndOffset))); + } + break; + case 59: // ONEOF_STRING: + if (isOneofPresent(message, number, pos)) { + writeString(number, UnsafeUtil.getObject(message, offset(typeAndOffset)), writer); + } + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object value = UnsafeUtil.getObject(message, offset(typeAndOffset)); + writer.writeMessage(number, value, getMessageFieldSchema(pos)); + } + break; + case 61: // ONEOF_BYTES: + if (isOneofPresent(message, number, pos)) { + writer.writeBytes( + number, (ByteString) UnsafeUtil.getObject(message, offset(typeAndOffset))); + } + break; + case 62: // ONEOF_UINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeUInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 63: // ONEOF_ENUM: + if (isOneofPresent(message, number, pos)) { + writer.writeEnum(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 64: // ONEOF_SFIXED32: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 65: // ONEOF_SFIXED64: + if (isOneofPresent(message, number, pos)) { + writer.writeSFixed64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 66: // ONEOF_SINT32: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt32(number, oneofIntAt(message, offset(typeAndOffset))); + } + break; + case 67: // ONEOF_SINT64: + if (isOneofPresent(message, number, pos)) { + writer.writeSInt64(number, oneofLongAt(message, offset(typeAndOffset))); + } + break; + case 68: // ONEOF_GROUP: + if (isOneofPresent(message, number, pos)) { + writer.writeGroup( + number, + UnsafeUtil.getObject(message, offset(typeAndOffset)), + getMessageFieldSchema(pos)); + } + break; + default: + break; + } + } + while (nextExtension != null) { + extensionSchema.serializeExtension(writer, nextExtension); + nextExtension = extensionIterator.hasNext() ? extensionIterator.next() : null; + } + } + + @SuppressWarnings("unchecked") + private <K, V> void writeMapHelper(Writer writer, int number, Object mapField, int pos) + throws IOException { + if (mapField != null) { + writer.writeMap( + number, + (MapEntryLite.Metadata<K, V>) mapFieldSchema.forMapMetadata(getMapFieldDefaultEntry(pos)), + (Map<K, V>) mapFieldSchema.forMapData(mapField)); + } + } + + private <UT, UB> void writeUnknownInMessageTo( + UnknownFieldSchema<UT, UB> schema, T message, Writer writer) throws IOException { + schema.writeTo(schema.getFromMessage(message), writer); + } + + @Override + public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + if (extensionRegistry == null) { + throw new NullPointerException(); + } + mergeFromHelper(unknownFieldSchema, extensionSchema, message, reader, extensionRegistry); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + private <UT, UB, ET extends FieldDescriptorLite<ET>> void mergeFromHelper( + UnknownFieldSchema<UT, UB> unknownFieldSchema, + ExtensionSchema<ET> extensionSchema, + T message, + Reader reader, + ExtensionRegistryLite extensionRegistry) + throws IOException { + UB unknownFields = null; + FieldSet<ET> extensions = null; + try { + while (true) { + final int number = reader.getFieldNumber(); + final int pos = positionForFieldNumber(number); + if (pos < 0) { + if (number == Reader.READ_DONE) { + return; + } + // Check if it's an extension. + Object extension = + !hasExtensions + ? null + : extensionSchema.findExtensionByNumber( + extensionRegistry, defaultInstance, number); + if (extension != null) { + if (extensions == null) { + extensions = extensionSchema.getMutableExtensions(message); + } + unknownFields = + extensionSchema.parseExtension( + reader, + extension, + extensionRegistry, + extensions, + unknownFields, + unknownFieldSchema); + continue; + } + if (unknownFieldSchema.shouldDiscardUnknownFields(reader)) { + if (reader.skipField()) { + continue; + } + } else { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + } + // Unknown field. + if (unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + continue; + } + } + // Done reading. + return; + } + final int typeAndOffset = typeAndOffsetAt(pos); + + try { + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + UnsafeUtil.putDouble(message, offset(typeAndOffset), reader.readDouble()); + setFieldPresent(message, pos); + break; + case 1: // FLOAT: + UnsafeUtil.putFloat(message, offset(typeAndOffset), reader.readFloat()); + setFieldPresent(message, pos); + break; + case 2: // INT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readInt64()); + setFieldPresent(message, pos); + break; + case 3: // UINT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readUInt64()); + setFieldPresent(message, pos); + break; + case 4: // INT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readInt32()); + setFieldPresent(message, pos); + break; + case 5: // FIXED64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readFixed64()); + setFieldPresent(message, pos); + break; + case 6: // FIXED32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readFixed32()); + setFieldPresent(message, pos); + break; + case 7: // BOOL: + UnsafeUtil.putBoolean(message, offset(typeAndOffset), reader.readBool()); + setFieldPresent(message, pos); + break; + case 8: // STRING: + readString(message, typeAndOffset, reader); + setFieldPresent(message, pos); + break; + case 9: + { // MESSAGE: + if (isFieldPresent(message, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readMessageBySchemaWithCheck( + (Schema<T>) getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readMessageBySchemaWithCheck( + (Schema<T>) getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + break; + } + case 10: // BYTES: + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + setFieldPresent(message, pos); + break; + case 11: // UINT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readUInt32()); + setFieldPresent(message, pos); + break; + case 12: // ENUM: + { + int enumValue = reader.readEnum(); + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + UnsafeUtil.putInt(message, offset(typeAndOffset), enumValue); + setFieldPresent(message, pos); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + number, enumValue, unknownFields, unknownFieldSchema); + } + break; + } + case 13: // SFIXED32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readSFixed32()); + setFieldPresent(message, pos); + break; + case 14: // SFIXED64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readSFixed64()); + setFieldPresent(message, pos); + break; + case 15: // SINT32: + UnsafeUtil.putInt(message, offset(typeAndOffset), reader.readSInt32()); + setFieldPresent(message, pos); + break; + case 16: // SINT64: + UnsafeUtil.putLong(message, offset(typeAndOffset), reader.readSInt64()); + setFieldPresent(message, pos); + break; + case 17: + { // GROUP: + if (isFieldPresent(message, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readGroupBySchemaWithCheck( + (Schema<T>) getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readGroupBySchemaWithCheck( + (Schema<T>) getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + break; + } + case 18: // DOUBLE_LIST: + reader.readDoubleList( + listFieldSchema.<Double>mutableListAt(message, offset(typeAndOffset))); + break; + case 19: // FLOAT_LIST: + reader.readFloatList( + listFieldSchema.<Float>mutableListAt(message, offset(typeAndOffset))); + break; + case 20: // INT64_LIST: + reader.readInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 21: // UINT64_LIST: + reader.readUInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 22: // INT32_LIST: + reader.readInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 23: // FIXED64_LIST: + reader.readFixed64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 24: // FIXED32_LIST: + reader.readFixed32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 25: // BOOL_LIST: + reader.readBoolList( + listFieldSchema.<Boolean>mutableListAt(message, offset(typeAndOffset))); + break; + case 26: // STRING_LIST: + readStringList(message, typeAndOffset, reader); + break; + case 27: + { // MESSAGE_LIST: + readMessageList( + message, + typeAndOffset, + reader, + (Schema<T>) getMessageFieldSchema(pos), + extensionRegistry); + break; + } + case 28: // BYTES_LIST: + reader.readBytesList( + listFieldSchema.<ByteString>mutableListAt(message, offset(typeAndOffset))); + break; + case 29: // UINT32_LIST: + reader.readUInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 30: // ENUM_LIST: + { + List<Integer> enumList = + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset)); + reader.readEnumList(enumList); + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + enumList, + getEnumFieldVerifier(pos), + unknownFields, + unknownFieldSchema); + break; + } + case 31: // SFIXED32_LIST: + reader.readSFixed32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 32: // SFIXED64_LIST: + reader.readSFixed64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 33: // SINT32_LIST: + reader.readSInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 34: // SINT64_LIST: + reader.readSInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 35: // DOUBLE_LIST_PACKED: + reader.readDoubleList( + listFieldSchema.<Double>mutableListAt(message, offset(typeAndOffset))); + break; + case 36: // FLOAT_LIST_PACKED: + reader.readFloatList( + listFieldSchema.<Float>mutableListAt(message, offset(typeAndOffset))); + break; + case 37: // INT64_LIST_PACKED: + reader.readInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 38: // UINT64_LIST_PACKED: + reader.readUInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 39: // INT32_LIST_PACKED: + reader.readInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 40: // FIXED64_LIST_PACKED: + reader.readFixed64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 41: // FIXED32_LIST_PACKED: + reader.readFixed32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 42: // BOOL_LIST_PACKED: + reader.readBoolList( + listFieldSchema.<Boolean>mutableListAt(message, offset(typeAndOffset))); + break; + case 43: // UINT32_LIST_PACKED: + reader.readUInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 44: // ENUM_LIST_PACKED: + { + List<Integer> enumList = + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset)); + reader.readEnumList(enumList); + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + enumList, + getEnumFieldVerifier(pos), + unknownFields, + unknownFieldSchema); + break; + } + case 45: // SFIXED32_LIST_PACKED: + reader.readSFixed32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 46: // SFIXED64_LIST_PACKED: + reader.readSFixed64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 47: // SINT32_LIST_PACKED: + reader.readSInt32List( + listFieldSchema.<Integer>mutableListAt(message, offset(typeAndOffset))); + break; + case 48: // SINT64_LIST_PACKED: + reader.readSInt64List( + listFieldSchema.<Long>mutableListAt(message, offset(typeAndOffset))); + break; + case 49: + { // GROUP_LIST: + readGroupList( + message, + offset(typeAndOffset), + reader, + (Schema<T>) getMessageFieldSchema(pos), + extensionRegistry); + break; + } + case 50: // MAP: + mergeMap(message, pos, getMapFieldDefaultEntry(pos), extensionRegistry, reader); + break; + case 51: // ONEOF_DOUBLE: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Double.valueOf(reader.readDouble())); + setOneofPresent(message, number, pos); + break; + case 52: // ONEOF_FLOAT: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Float.valueOf(reader.readFloat())); + setOneofPresent(message, number, pos); + break; + case 53: // ONEOF_INT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readInt64())); + setOneofPresent(message, number, pos); + break; + case 54: // ONEOF_UINT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readUInt64())); + setOneofPresent(message, number, pos); + break; + case 55: // ONEOF_INT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readInt32())); + setOneofPresent(message, number, pos); + break; + case 56: // ONEOF_FIXED64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readFixed64())); + setOneofPresent(message, number, pos); + break; + case 57: // ONEOF_FIXED32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readFixed32())); + setOneofPresent(message, number, pos); + break; + case 58: // ONEOF_BOOL: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Boolean.valueOf(reader.readBool())); + setOneofPresent(message, number, pos); + break; + case 59: // ONEOF_STRING: + readString(message, typeAndOffset, reader); + setOneofPresent(message, number, pos); + break; + case 60: // ONEOF_MESSAGE: + if (isOneofPresent(message, number, pos)) { + Object mergedResult = + Internal.mergeMessage( + UnsafeUtil.getObject(message, offset(typeAndOffset)), + reader.readMessageBySchemaWithCheck( + getMessageFieldSchema(pos), extensionRegistry)); + UnsafeUtil.putObject(message, offset(typeAndOffset), mergedResult); + } else { + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readMessageBySchemaWithCheck( + getMessageFieldSchema(pos), extensionRegistry)); + setFieldPresent(message, pos); + } + setOneofPresent(message, number, pos); + break; + case 61: // ONEOF_BYTES: + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + setOneofPresent(message, number, pos); + break; + case 62: // ONEOF_UINT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readUInt32())); + setOneofPresent(message, number, pos); + break; + case 63: // ONEOF_ENUM: + { + int enumValue = reader.readEnum(); + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + UnsafeUtil.putObject(message, offset(typeAndOffset), enumValue); + setOneofPresent(message, number, pos); + } else { + unknownFields = + SchemaUtil.storeUnknownEnum( + number, enumValue, unknownFields, unknownFieldSchema); + } + break; + } + case 64: // ONEOF_SFIXED32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readSFixed32())); + setOneofPresent(message, number, pos); + break; + case 65: // ONEOF_SFIXED64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readSFixed64())); + setOneofPresent(message, number, pos); + break; + case 66: // ONEOF_SINT32: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Integer.valueOf(reader.readSInt32())); + setOneofPresent(message, number, pos); + break; + case 67: // ONEOF_SINT64: + UnsafeUtil.putObject( + message, offset(typeAndOffset), Long.valueOf(reader.readSInt64())); + setOneofPresent(message, number, pos); + break; + case 68: // ONEOF_GROUP: + UnsafeUtil.putObject( + message, + offset(typeAndOffset), + reader.readGroupBySchemaWithCheck(getMessageFieldSchema(pos), extensionRegistry)); + setOneofPresent(message, number, pos); + break; + default: + // Assume we've landed on an empty entry. Treat it as an unknown field. + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + if (!unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + return; + } + break; + } + } catch (InvalidProtocolBufferException.InvalidWireTypeException e) { + // Treat fields with an invalid wire type as unknown fields + // (i.e. same as the default case). + if (unknownFieldSchema.shouldDiscardUnknownFields(reader)) { + if (!reader.skipField()) { + return; + } + } else { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + } + if (!unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader)) { + return; + } + } + } + } + } finally { + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + unknownFields = + filterMapUnknownEnumValues(message, intArray[i], unknownFields, unknownFieldSchema); + } + if (unknownFields != null) { + unknownFieldSchema.setBuilderToMessage(message, unknownFields); + } + } + } + + @SuppressWarnings("ReferenceEquality") + static UnknownFieldSetLite getMutableUnknownFields(Object message) { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + return unknownFields; + } + + /** Decodes a map entry key or value. Stores result in registers.object1. */ + private int decodeMapEntryValue( + byte[] data, + int position, + int limit, + WireFormat.FieldType fieldType, + Class<?> messageType, + Registers registers) + throws IOException { + switch (fieldType) { + case BOOL: + position = decodeVarint64(data, position, registers); + registers.object1 = registers.long1 != 0; + break; + case BYTES: + position = decodeBytes(data, position, registers); + break; + case DOUBLE: + registers.object1 = decodeDouble(data, position); + position += 8; + break; + case FIXED32: + case SFIXED32: + registers.object1 = decodeFixed32(data, position); + position += 4; + break; + case FIXED64: + case SFIXED64: + registers.object1 = decodeFixed64(data, position); + position += 8; + break; + case FLOAT: + registers.object1 = decodeFloat(data, position); + position += 4; + break; + case ENUM: + case INT32: + case UINT32: + position = decodeVarint32(data, position, registers); + registers.object1 = registers.int1; + break; + case INT64: + case UINT64: + position = decodeVarint64(data, position, registers); + registers.object1 = registers.long1; + break; + case MESSAGE: + position = + decodeMessageField( + Protobuf.getInstance().schemaFor(messageType), data, position, limit, registers); + break; + case SINT32: + position = decodeVarint32(data, position, registers); + registers.object1 = CodedInputStream.decodeZigZag32(registers.int1); + break; + case SINT64: + position = decodeVarint64(data, position, registers); + registers.object1 = CodedInputStream.decodeZigZag64(registers.long1); + break; + case STRING: + position = decodeStringRequireUtf8(data, position, registers); + break; + default: + throw new RuntimeException("unsupported field type."); + } + return position; + } + + /** Decodes a map entry. */ + private <K, V> int decodeMapEntry( + byte[] data, + int position, + int limit, + MapEntryLite.Metadata<K, V> metadata, + Map<K, V> target, + Registers registers) + throws IOException { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length < 0 || length > limit - position) { + throw InvalidProtocolBufferException.truncatedMessage(); + } + final int end = position + length; + K key = metadata.defaultKey; + V value = metadata.defaultValue; + while (position < end) { + int tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int fieldNumber = tag >>> 3; + final int wireType = tag & 0x7; + switch (fieldNumber) { + case 1: + if (wireType == metadata.keyType.getWireType()) { + position = + decodeMapEntryValue(data, position, limit, metadata.keyType, null, registers); + key = (K) registers.object1; + continue; + } + break; + case 2: + if (wireType == metadata.valueType.getWireType()) { + position = + decodeMapEntryValue( + data, + position, + limit, + metadata.valueType, + metadata.defaultValue.getClass(), + registers); + value = (V) registers.object1; + continue; + } + break; + default: + break; + } + position = skipField(tag, data, position, limit, registers); + } + if (position != end) { + throw InvalidProtocolBufferException.parseFailure(); + } + target.put(key, value); + return end; + } + + @SuppressWarnings("ReferenceEquality") + private int parseRepeatedField( + T message, + byte[] data, + int position, + int limit, + int tag, + int number, + int wireType, + int bufferPosition, + long typeAndOffset, + int fieldType, + long fieldOffset, + Registers registers) + throws IOException { + ProtobufList<?> list = (ProtobufList<?>) UNSAFE.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + UNSAFE.putObject(message, fieldOffset, list); + } + switch (fieldType) { + case 18: // DOUBLE_LIST: + case 35: // DOUBLE_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedDoubleList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED64) { + position = decodeDoubleList(tag, data, position, limit, list, registers); + } + break; + case 19: // FLOAT_LIST: + case 36: // FLOAT_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFloatList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED32) { + position = decodeFloatList(tag, data, position, limit, list, registers); + } + break; + case 20: // INT64_LIST: + case 21: // UINT64_LIST: + case 37: // INT64_LIST_PACKED: + case 38: // UINT64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64List(tag, data, position, limit, list, registers); + } + break; + case 22: // INT32_LIST: + case 29: // UINT32_LIST: + case 39: // INT32_LIST_PACKED: + case 43: // UINT32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32List(tag, data, position, limit, list, registers); + } + break; + case 23: // FIXED64_LIST: + case 32: // SFIXED64_LIST: + case 40: // FIXED64_LIST_PACKED: + case 46: // SFIXED64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFixed64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED64) { + position = decodeFixed64List(tag, data, position, limit, list, registers); + } + break; + case 24: // FIXED32_LIST: + case 31: // SFIXED32_LIST: + case 41: // FIXED32_LIST_PACKED: + case 45: // SFIXED32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedFixed32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_FIXED32) { + position = decodeFixed32List(tag, data, position, limit, list, registers); + } + break; + case 25: // BOOL_LIST: + case 42: // BOOL_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedBoolList(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeBoolList(tag, data, position, limit, list, registers); + } + break; + case 26: // STRING_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeStringList(tag, data, position, limit, list, registers); + } else { + position = decodeStringListRequireUtf8(tag, data, position, limit, list, registers); + } + } + break; + case 27: // MESSAGE_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageList( + getMessageFieldSchema(bufferPosition), + tag, + data, + position, + limit, + list, + registers); + } + break; + case 28: // BYTES_LIST: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytesList(tag, data, position, limit, list, registers); + } + break; + case 30: // ENUM_LIST: + case 44: // ENUM_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedVarint32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32List(tag, data, position, limit, list, registers); + } else { + break; + } + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + // filterUnknownEnumList() expects the unknownFields parameter to be mutable or null. + // Since we don't know yet whether there exist unknown enum values, we'd better pass + // null to it instead of allocating a mutable instance. This is also needed to be + // consistent with the behavior of generated parser/builder. + unknownFields = null; + } + unknownFields = + SchemaUtil.filterUnknownEnumList( + number, + (ProtobufList<Integer>) list, + getEnumFieldVerifier(bufferPosition), + unknownFields, + (UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite>) unknownFieldSchema); + if (unknownFields != null) { + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + break; + case 33: // SINT32_LIST: + case 47: // SINT32_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedSInt32List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeSInt32List(tag, data, position, limit, list, registers); + } + break; + case 34: // SINT64_LIST: + case 48: // SINT64_LIST_PACKED: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodePackedSInt64List(data, position, list, registers); + } else if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeSInt64List(tag, data, position, limit, list, registers); + } + break; + case 49: // GROUP_LIST: + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + position = + decodeGroupList( + getMessageFieldSchema(bufferPosition), + tag, + data, + position, + limit, + list, + registers); + } + break; + default: + break; + } + return position; + } + + private <K, V> int parseMapField( + T message, + byte[] data, + int position, + int limit, + int bufferPosition, + long fieldOffset, + Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + Object mapDefaultEntry = getMapFieldDefaultEntry(bufferPosition); + Object mapField = unsafe.getObject(message, fieldOffset); + if (mapFieldSchema.isImmutable(mapField)) { + Object oldMapField = mapField; + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + mapFieldSchema.mergeFrom(mapField, oldMapField); + unsafe.putObject(message, fieldOffset, mapField); + } + return decodeMapEntry( + data, + position, + limit, + (Metadata<K, V>) mapFieldSchema.forMapMetadata(mapDefaultEntry), + (Map<K, V>) mapFieldSchema.forMutableMapData(mapField), + registers); + } + + private int parseOneofField( + T message, + byte[] data, + int position, + int limit, + int tag, + int number, + int wireType, + int typeAndOffset, + int fieldType, + long fieldOffset, + int bufferPosition, + Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + final long oneofCaseOffset = buffer[bufferPosition + 2] & OFFSET_MASK; + switch (fieldType) { + case 51: // ONEOF_DOUBLE: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putObject(message, fieldOffset, decodeDouble(data, position)); + position += 8; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 52: // ONEOF_FLOAT: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putObject(message, fieldOffset, decodeFloat(data, position)); + position += 4; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 53: // ONEOF_INT64: + case 54: // ONEOF_UINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.long1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 55: // ONEOF_INT32: + case 62: // ONEOF_UINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.int1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 56: // ONEOF_FIXED64: + case 65: // ONEOF_SFIXED64: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putObject(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 57: // ONEOF_FIXED32: + case 64: // ONEOF_SFIXED32: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putObject(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 58: // ONEOF_BOOL: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.long1 != 0); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 59: // ONEOF_STRING: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeVarint32(data, position, registers); + final int length = registers.int1; + if (length == 0) { + unsafe.putObject(message, fieldOffset, ""); + } else { + if ((typeAndOffset & ENFORCE_UTF8_MASK) != 0 + && !Utf8.isValidUtf8(data, position, position + length)) { + throw InvalidProtocolBufferException.invalidUtf8(); + } + final String value = new String(data, position, length, Internal.UTF_8); + unsafe.putObject(message, fieldOffset, value); + position += length; + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 60: // ONEOF_MESSAGE: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(bufferPosition), data, position, limit, registers); + final Object oldValue = + unsafe.getInt(message, oneofCaseOffset) == number + ? unsafe.getObject(message, fieldOffset) + : null; + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 61: // ONEOF_BYTES: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 63: // ONEOF_ENUM: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + final int enumValue = registers.int1; + EnumVerifier enumVerifier = getEnumFieldVerifier(bufferPosition); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + unsafe.putObject(message, fieldOffset, enumValue); + unsafe.putInt(message, oneofCaseOffset, number); + } else { + // UnknownFieldSetLite requires varint to be represented as Long: + // https://cs.corp.google.com/piper///depot/google3/java/com/google/protobuf/UnknownFieldSetLite.java?l=202&rcl=167074986 + getMutableUnknownFields(message).storeField(tag, (long) enumValue); + } + } + break; + case 66: // ONEOF_SINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putObject(message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 67: // ONEOF_SINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putObject(message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + case 68: // ONEOF_GROUP: + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + final int endTag = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; + position = + decodeGroupField( + getMessageFieldSchema(bufferPosition), data, position, limit, endTag, registers); + final Object oldValue = + unsafe.getInt(message, oneofCaseOffset) == number + ? unsafe.getObject(message, fieldOffset) + : null; + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + unsafe.putInt(message, oneofCaseOffset, number); + } + break; + default: + break; + } + return position; + } + + private Schema getMessageFieldSchema(int pos) { + final int index = pos / INTS_PER_FIELD * 2; + Schema schema = (Schema) objects[index]; + if (schema != null) { + return schema; + } + schema = Protobuf.getInstance().schemaFor((Class) objects[index + 1]); + objects[index] = schema; + return schema; + } + + private Object getMapFieldDefaultEntry(int pos) { + return objects[pos / INTS_PER_FIELD * 2]; + } + + private EnumVerifier getEnumFieldVerifier(int pos) { + return (EnumVerifier) objects[pos / INTS_PER_FIELD * 2 + 1]; + } + + /** + * Parses a proto2 message or group and returns the position after the message/group. If it's + * parsing a message (endGroup == 0), returns limit if parsing is successful; It it's parsing a + * group (endGroup != 0), parsing ends when a tag == endGroup is encountered and the position + * after that tag is returned. + */ + int parseProto2Message( + T message, byte[] data, int position, int limit, int endGroup, Registers registers) + throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + int tag = 0; + int oldNumber = -1; + int pos = 0; + while (position < limit) { + tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int number = tag >>> 3; + final int wireType = tag & 0x7; + if (number > oldNumber) { + pos = positionForFieldNumber(number, pos / INTS_PER_FIELD); + } else { + pos = positionForFieldNumber(number); + } + oldNumber = number; + if (pos == -1) { + // need to reset + pos = 0; + } else { + final int typeAndOffset = buffer[pos + 1]; + final int fieldType = type(typeAndOffset); + final long fieldOffset = offset(typeAndOffset); + if (fieldType <= 17) { + // Proto2 optional fields have has-bits. + final int presenceMaskAndOffset = buffer[pos + 2]; + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + // We cache the 32-bit has-bits integer value and only write it back when parsing a field + // using a different has-bits integer. + if (presenceFieldOffset != currentPresenceFieldOffset) { + if (currentPresenceFieldOffset != -1) { + unsafe.putInt(message, (long) currentPresenceFieldOffset, currentPresenceField); + } + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = unsafe.getInt(message, (long) presenceFieldOffset); + } + switch (fieldType) { + case 0: // DOUBLE + if (wireType == WireFormat.WIRETYPE_FIXED64) { + UnsafeUtil.putDouble(message, fieldOffset, decodeDouble(data, position)); + position += 8; + currentPresenceField |= presenceMask; + continue; + } + break; + case 1: // FLOAT + if (wireType == WireFormat.WIRETYPE_FIXED32) { + UnsafeUtil.putFloat(message, fieldOffset, decodeFloat(data, position)); + position += 4; + currentPresenceField |= presenceMask; + continue; + } + break; + case 2: // INT64 + case 3: // UINT64 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong(message, fieldOffset, registers.long1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 4: // INT32 + case 11: // UINT32 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 5: // FIXED64 + case 14: // SFIXED64 + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putLong(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + currentPresenceField |= presenceMask; + continue; + } + break; + case 6: // FIXED32 + case 13: // SFIXED32 + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putInt(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + currentPresenceField |= presenceMask; + continue; + } + break; + case 7: // BOOL + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + UnsafeUtil.putBoolean(message, fieldOffset, registers.long1 != 0); + currentPresenceField |= presenceMask; + continue; + } + break; + case 8: // STRING + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeString(data, position, registers); + } else { + position = decodeStringRequireUtf8(data, position, registers); + } + unsafe.putObject(message, fieldOffset, registers.object1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 9: // MESSAGE + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(pos), data, position, limit, registers); + if ((currentPresenceField & presenceMask) == 0) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, + fieldOffset, + Internal.mergeMessage( + unsafe.getObject(message, fieldOffset), registers.object1)); + } + currentPresenceField |= presenceMask; + continue; + } + break; + case 10: // BYTES + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + currentPresenceField |= presenceMask; + continue; + } + break; + case 12: // ENUM + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + final int enumValue = registers.int1; + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null || enumVerifier.isInRange(enumValue)) { + unsafe.putInt(message, fieldOffset, enumValue); + currentPresenceField |= presenceMask; + } else { + // UnknownFieldSetLite requires varint to be represented as Long: + // https://cs.corp.google.com/piper///depot/google3/java/com/google/protobuf/UnknownFieldSetLite.java?l=202&rcl=167074986 + getMutableUnknownFields(message).storeField(tag, (long) enumValue); + } + continue; + } + break; + case 15: // SINT32 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt( + message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + currentPresenceField |= presenceMask; + continue; + } + break; + case 16: // SINT64 + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong( + message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + + currentPresenceField |= presenceMask; + continue; + } + break; + case 17: // GROUP + if (wireType == WireFormat.WIRETYPE_START_GROUP) { + final int endTag = (number << 3) | WireFormat.WIRETYPE_END_GROUP; + position = + decodeGroupField( + getMessageFieldSchema(pos), data, position, limit, endTag, registers); + if ((currentPresenceField & presenceMask) == 0) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, + fieldOffset, + Internal.mergeMessage( + unsafe.getObject(message, fieldOffset), registers.object1)); + } + + currentPresenceField |= presenceMask; + continue; + } + break; + default: + break; + } + } else if (fieldType == 27) { + // Handle repeated message fields. + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + ProtobufList<?> list = (ProtobufList<?>) unsafe.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + unsafe.putObject(message, fieldOffset, list); + } + position = + decodeMessageList( + getMessageFieldSchema(pos), tag, data, position, limit, list, registers); + continue; + } + } else if (fieldType <= 49) { + // Handle all other repeated fields. + final int oldPosition = position; + position = + parseRepeatedField( + message, + data, + position, + limit, + tag, + number, + wireType, + pos, + typeAndOffset, + fieldType, + fieldOffset, + registers); + if (position != oldPosition) { + continue; + } + } else if (fieldType == 50) { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + final int oldPosition = position; + position = parseMapField(message, data, position, limit, pos, fieldOffset, registers); + if (position != oldPosition) { + continue; + } + } + } else { + final int oldPosition = position; + position = + parseOneofField( + message, + data, + position, + limit, + tag, + number, + wireType, + typeAndOffset, + fieldType, + fieldOffset, + pos, + registers); + if (position != oldPosition) { + continue; + } + } + } + if (tag == endGroup && endGroup != 0) { + break; + } + + if (hasExtensions + && registers.extensionRegistry != ExtensionRegistryLite.getEmptyRegistry()) { + position = decodeExtensionOrUnknownField( + tag, data, position, limit, message, defaultInstance, + (UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite>) unknownFieldSchema, + registers); + } else { + position = decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } + } + if (currentPresenceFieldOffset != -1) { + unsafe.putInt(message, (long) currentPresenceFieldOffset, currentPresenceField); + } + UnknownFieldSetLite unknownFields = null; + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + unknownFields = + filterMapUnknownEnumValues( + message, + intArray[i], + unknownFields, + (UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite>) unknownFieldSchema); + } + if (unknownFields != null) { + ((UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite>) unknownFieldSchema) + .setBuilderToMessage(message, unknownFields); + } + if (endGroup == 0) { + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + } else { + if (position > limit || tag != endGroup) { + throw InvalidProtocolBufferException.parseFailure(); + } + } + return position; + } + + /** Parses a proto3 message and returns the limit if parsing is successful. */ + private int parseProto3Message( + T message, byte[] data, int position, int limit, Registers registers) throws IOException { + final sun.misc.Unsafe unsafe = UNSAFE; + int tag = 0; + int oldNumber = -1; + int pos = 0; + while (position < limit) { + tag = data[position++]; + if (tag < 0) { + position = decodeVarint32(tag, data, position, registers); + tag = registers.int1; + } + final int number = tag >>> 3; + final int wireType = tag & 0x7; + if (number > oldNumber) { + pos = positionForFieldNumber(number, pos / INTS_PER_FIELD); + } else { + pos = positionForFieldNumber(number); + } + oldNumber = number; + if (pos == -1) { + // need to reset + pos = 0; + } else { + final int typeAndOffset = buffer[pos + 1]; + final int fieldType = type(typeAndOffset); + final long fieldOffset = offset(typeAndOffset); + if (fieldType <= 17) { + switch (fieldType) { + case 0: // DOUBLE: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + UnsafeUtil.putDouble(message, fieldOffset, decodeDouble(data, position)); + position += 8; + continue; + } + break; + case 1: // FLOAT: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + UnsafeUtil.putFloat(message, fieldOffset, decodeFloat(data, position)); + position += 4; + continue; + } + break; + case 2: // INT64: + case 3: // UINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong(message, fieldOffset, registers.long1); + continue; + } + break; + case 4: // INT32: + case 11: // UINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + continue; + } + break; + case 5: // FIXED64: + case 14: // SFIXED64: + if (wireType == WireFormat.WIRETYPE_FIXED64) { + unsafe.putLong(message, fieldOffset, decodeFixed64(data, position)); + position += 8; + continue; + } + break; + case 6: // FIXED32: + case 13: // SFIXED32: + if (wireType == WireFormat.WIRETYPE_FIXED32) { + unsafe.putInt(message, fieldOffset, decodeFixed32(data, position)); + position += 4; + continue; + } + break; + case 7: // BOOL: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + UnsafeUtil.putBoolean(message, fieldOffset, registers.long1 != 0); + continue; + } + break; + case 8: // STRING: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + if ((typeAndOffset & ENFORCE_UTF8_MASK) == 0) { + position = decodeString(data, position, registers); + } else { + position = decodeStringRequireUtf8(data, position, registers); + } + unsafe.putObject(message, fieldOffset, registers.object1); + continue; + } + break; + case 9: // MESSAGE: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = + decodeMessageField( + getMessageFieldSchema(pos), data, position, limit, registers); + final Object oldValue = unsafe.getObject(message, fieldOffset); + if (oldValue == null) { + unsafe.putObject(message, fieldOffset, registers.object1); + } else { + unsafe.putObject( + message, fieldOffset, Internal.mergeMessage(oldValue, registers.object1)); + } + continue; + } + break; + case 10: // BYTES: + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = decodeBytes(data, position, registers); + unsafe.putObject(message, fieldOffset, registers.object1); + continue; + } + break; + case 12: // ENUM: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt(message, fieldOffset, registers.int1); + continue; + } + break; + case 15: // SINT32: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint32(data, position, registers); + unsafe.putInt( + message, fieldOffset, CodedInputStream.decodeZigZag32(registers.int1)); + continue; + } + break; + case 16: // SINT64: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = decodeVarint64(data, position, registers); + unsafe.putLong( + message, fieldOffset, CodedInputStream.decodeZigZag64(registers.long1)); + continue; + } + break; + default: + break; + } + } else if (fieldType == 27) { + // Handle repeated message field. + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + ProtobufList<?> list = (ProtobufList<?>) unsafe.getObject(message, fieldOffset); + if (!list.isModifiable()) { + final int size = list.size(); + list = + list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + unsafe.putObject(message, fieldOffset, list); + } + position = + decodeMessageList( + getMessageFieldSchema(pos), tag, data, position, limit, list, registers); + continue; + } + } else if (fieldType <= 49) { + // Handle all other repeated fields. + final int oldPosition = position; + position = + parseRepeatedField( + message, + data, + position, + limit, + tag, + number, + wireType, + pos, + typeAndOffset, + fieldType, + fieldOffset, + registers); + if (position != oldPosition) { + continue; + } + } else if (fieldType == 50) { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + final int oldPosition = position; + position = parseMapField(message, data, position, limit, pos, fieldOffset, registers); + if (position != oldPosition) { + continue; + } + } + } else { + final int oldPosition = position; + position = + parseOneofField( + message, + data, + position, + limit, + tag, + number, + wireType, + typeAndOffset, + fieldType, + fieldOffset, + pos, + registers); + if (position != oldPosition) { + continue; + } + } + } + position = decodeUnknownField( + tag, data, position, limit, getMutableUnknownFields(message), registers); + } + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + return position; + } + + @Override + public void mergeFrom(T message, byte[] data, int position, int limit, Registers registers) + throws IOException { + if (proto3) { + parseProto3Message(message, data, position, limit, registers); + } else { + parseProto2Message(message, data, position, limit, 0, registers); + } + } + + @Override + public void makeImmutable(T message) { + // Make all repeated/map fields immutable. + for (int i = checkInitializedCount; i < repeatedFieldOffsetStart; i++) { + long offset = offset(typeAndOffsetAt(intArray[i])); + Object mapField = UnsafeUtil.getObject(message, offset); + if (mapField == null) { + continue; + } + UnsafeUtil.putObject(message, offset, mapFieldSchema.toImmutable(mapField)); + } + final int length = intArray.length; + for (int i = repeatedFieldOffsetStart; i < length; i++) { + listFieldSchema.makeImmutableListAt(message, intArray[i]); + } + unknownFieldSchema.makeImmutable(message); + if (hasExtensions) { + extensionSchema.makeImmutable(message); + } + } + + @SuppressWarnings("unchecked") + private final <K, V> void mergeMap( + Object message, + int pos, + Object mapDefaultEntry, + ExtensionRegistryLite extensionRegistry, + Reader reader) + throws IOException { + long offset = offset(typeAndOffsetAt(pos)); + Object mapField = UnsafeUtil.getObject(message, offset); + // TODO(xiaofeng): Consider creating separate implementations for full and lite. In lite + // runtime map field will never be null but here we still need to check null because the + // code is shared by both full and lite. It might be better if full/lite uses different + // schema implementations. + if (mapField == null) { + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + UnsafeUtil.putObject(message, offset, mapField); + } else if (mapFieldSchema.isImmutable(mapField)) { + Object oldMapField = mapField; + mapField = mapFieldSchema.newMapField(mapDefaultEntry); + mapFieldSchema.mergeFrom(mapField, oldMapField); + UnsafeUtil.putObject(message, offset, mapField); + } + reader.readMap( + (Map<K, V>) mapFieldSchema.forMutableMapData(mapField), + (Metadata<K, V>) mapFieldSchema.forMapMetadata(mapDefaultEntry), + extensionRegistry); + } + + private final <UT, UB> UB filterMapUnknownEnumValues( + Object message, int pos, UB unknownFields, UnknownFieldSchema<UT, UB> unknownFieldSchema) { + int fieldNumber = numberAt(pos); + long offset = offset(typeAndOffsetAt(pos)); + Object mapField = UnsafeUtil.getObject(message, offset); + if (mapField == null) { + return unknownFields; + } + EnumVerifier enumVerifier = getEnumFieldVerifier(pos); + if (enumVerifier == null) { + return unknownFields; + } + Map<?, ?> mapData = mapFieldSchema.forMutableMapData(mapField); + // Filter unknown enum values. + unknownFields = + filterUnknownEnumMap( + pos, fieldNumber, mapData, enumVerifier, unknownFields, unknownFieldSchema); + return unknownFields; + } + + @SuppressWarnings("unchecked") + private final <K, V, UT, UB> UB filterUnknownEnumMap( + int pos, + int number, + Map<K, V> mapData, + EnumVerifier enumVerifier, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) { + Metadata<K, V> metadata = + (Metadata<K, V>) mapFieldSchema.forMapMetadata(getMapFieldDefaultEntry(pos)); + for (Iterator<Map.Entry<K, V>> it = mapData.entrySet().iterator(); it.hasNext(); ) { + Map.Entry<K, V> entry = it.next(); + if (!enumVerifier.isInRange((Integer) entry.getValue())) { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + int entrySize = + MapEntryLite.computeSerializedSize(metadata, entry.getKey(), entry.getValue()); + CodedBuilder codedBuilder = ByteString.newCodedBuilder(entrySize); + CodedOutputStream codedOutput = codedBuilder.getCodedOutput(); + try { + MapEntryLite.writeTo(codedOutput, metadata, entry.getKey(), entry.getValue()); + } catch (IOException e) { + // Writing to ByteString CodedOutputStream should not throw IOException. + throw new RuntimeException(e); + } + unknownFieldSchema.addLengthDelimited(unknownFields, number, codedBuilder.build()); + it.remove(); + } + } + return unknownFields; + } + + @Override + public final boolean isInitialized(T message) { + int currentPresenceFieldOffset = -1; + int currentPresenceField = 0; + for (int i = 0; i < checkInitializedCount; i++) { + final int pos = intArray[i]; + final int number = numberAt(pos); + + final int typeAndOffset = typeAndOffsetAt(pos); + + int presenceMaskAndOffset = 0; + int presenceMask = 0; + if (!proto3) { + presenceMaskAndOffset = buffer[pos + 2]; + final int presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + if (presenceFieldOffset != currentPresenceFieldOffset) { + currentPresenceFieldOffset = presenceFieldOffset; + currentPresenceField = UNSAFE.getInt(message, (long) presenceFieldOffset); + } + } + + if (isRequired(typeAndOffset)) { + if (!isFieldPresent(message, pos, currentPresenceField, presenceMask)) { + return false; + } + // If a required message field is set but has no required fields of it's own, we still + // proceed and check the message is initialized. It should be fairly cheap to check these + // messages but is worth documenting. + } + // Check nested message and groups. + switch (type(typeAndOffset)) { + case 9: // MESSAGE + case 17: // GROUP + if (isFieldPresent(message, pos, currentPresenceField, presenceMask) + && !isInitialized(message, typeAndOffset, getMessageFieldSchema(pos))) { + return false; + } + break; + case 27: // MESSAGE_LIST + case 49: // GROUP_LIST + if (!isListInitialized(message, typeAndOffset, pos)) { + return false; + } + break; + case 60: // ONEOF_MESSAGE + case 68: // ONEOF_GROUP + if (isOneofPresent(message, number, pos) + && !isInitialized(message, typeAndOffset, getMessageFieldSchema(pos))) { + return false; + } + break; + case 50: // MAP + if (!isMapInitialized(message, typeAndOffset, pos)) { + return false; + } + break; + default: + break; + } + } + + if (hasExtensions) { + if (!extensionSchema.getExtensions(message).isInitialized()) { + return false; + } + } + + return true; + } + + private static boolean isInitialized(Object message, int typeAndOffset, Schema schema) { + Object nested = UnsafeUtil.getObject(message, offset(typeAndOffset)); + return schema.isInitialized(nested); + } + + private <N> boolean isListInitialized(Object message, int typeAndOffset, int pos) { + @SuppressWarnings("unchecked") + List<N> list = (List<N>) UnsafeUtil.getObject(message, offset(typeAndOffset)); + if (list.isEmpty()) { + return true; + } + + Schema schema = getMessageFieldSchema(pos); + for (int i = 0; i < list.size(); i++) { + N nested = list.get(i); + if (!schema.isInitialized(nested)) { + return false; + } + } + return true; + } + + private boolean isMapInitialized(T message, int typeAndOffset, int pos) { + Map<?, ?> map = mapFieldSchema.forMapData(UnsafeUtil.getObject(message, offset(typeAndOffset))); + if (map.isEmpty()) { + return true; + } + Object mapDefaultEntry = getMapFieldDefaultEntry(pos); + MapEntryLite.Metadata<?, ?> metadata = mapFieldSchema.forMapMetadata(mapDefaultEntry); + if (metadata.valueType.getJavaType() != WireFormat.JavaType.MESSAGE) { + return true; + } + // TODO(dweis): Use schema cache. + Schema schema = null; + for (Object nested : map.values()) { + if (schema == null) { + schema = Protobuf.getInstance().schemaFor(nested.getClass()); + } + if (!schema.isInitialized(nested)) { + return false; + } + } + return true; + } + + private void writeString(int fieldNumber, Object value, Writer writer) throws IOException { + if (value instanceof String) { + writer.writeString(fieldNumber, (String) value); + } else { + writer.writeBytes(fieldNumber, (ByteString) value); + } + } + + private void readString(Object message, int typeAndOffset, Reader reader) throws IOException { + if (isEnforceUtf8(typeAndOffset)) { + // Enforce valid UTF-8 on the read. + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readStringRequireUtf8()); + } else if (lite) { + // Lite messages use String fields to store strings. Read a string but do not + // enforce UTF-8 + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readString()); + } else { + // Full runtime messages use Objects to store either a String or ByteString. Read + // the string as a ByteString and do not enforce UTF-8. + UnsafeUtil.putObject(message, offset(typeAndOffset), reader.readBytes()); + } + } + + private void readStringList(Object message, int typeAndOffset, Reader reader) throws IOException { + if (isEnforceUtf8(typeAndOffset)) { + reader.readStringListRequireUtf8( + listFieldSchema.<String>mutableListAt(message, offset(typeAndOffset))); + } else { + reader.readStringList(listFieldSchema.<String>mutableListAt(message, offset(typeAndOffset))); + } + } + + private <E> void readMessageList( + Object message, + int typeAndOffset, + Reader reader, + Schema<E> schema, + ExtensionRegistryLite extensionRegistry) + throws IOException { + long offset = offset(typeAndOffset); + reader.readMessageList( + listFieldSchema.<E>mutableListAt(message, offset), schema, extensionRegistry); + } + + private <E> void readGroupList( + Object message, + long offset, + Reader reader, + Schema<E> schema, + ExtensionRegistryLite extensionRegistry) + throws IOException { + reader.readGroupList( + listFieldSchema.<E>mutableListAt(message, offset), schema, extensionRegistry); + } + + private int numberAt(int pos) { + return buffer[pos]; + } + + private int typeAndOffsetAt(int pos) { + return buffer[pos + 1]; + } + + private int presenceMaskAndOffsetAt(int pos) { + return buffer[pos + 2]; + } + + private static int type(int value) { + return (value & FIELD_TYPE_MASK) >>> OFFSET_BITS; + } + + private static boolean isRequired(int value) { + return (value & REQUIRED_MASK) != 0; + } + + private static boolean isEnforceUtf8(int value) { + return (value & ENFORCE_UTF8_MASK) != 0; + } + + private static long offset(int value) { + return value & OFFSET_MASK; + } + + private static <T> double doubleAt(T message, long offset) { + return UnsafeUtil.getDouble(message, offset); + } + + private static <T> float floatAt(T message, long offset) { + return UnsafeUtil.getFloat(message, offset); + } + + private static <T> int intAt(T message, long offset) { + return UnsafeUtil.getInt(message, offset); + } + + private static <T> long longAt(T message, long offset) { + return UnsafeUtil.getLong(message, offset); + } + + private static <T> boolean booleanAt(T message, long offset) { + return UnsafeUtil.getBoolean(message, offset); + } + + private static <T> double oneofDoubleAt(T message, long offset) { + return ((Double) UnsafeUtil.getObject(message, offset)).doubleValue(); + } + + private static <T> float oneofFloatAt(T message, long offset) { + return ((Float) UnsafeUtil.getObject(message, offset)).floatValue(); + } + + private static <T> int oneofIntAt(T message, long offset) { + return ((Integer) UnsafeUtil.getObject(message, offset)).intValue(); + } + + private static <T> long oneofLongAt(T message, long offset) { + return ((Long) UnsafeUtil.getObject(message, offset)).longValue(); + } + + private static <T> boolean oneofBooleanAt(T message, long offset) { + return ((Boolean) UnsafeUtil.getObject(message, offset)).booleanValue(); + } + + /** Returns true the field is present in both messages, or neither. */ + private boolean arePresentForEquals(T message, T other, int pos) { + return isFieldPresent(message, pos) == isFieldPresent(other, pos); + } + + private boolean isFieldPresent(T message, int pos, int presenceField, int presenceMask) { + if (proto3) { + return isFieldPresent(message, pos); + } else { + return (presenceField & presenceMask) != 0; + } + } + + private boolean isFieldPresent(T message, int pos) { + if (proto3) { + final int typeAndOffset = typeAndOffsetAt(pos); + final long offset = offset(typeAndOffset); + switch (type(typeAndOffset)) { + case 0: // DOUBLE: + return UnsafeUtil.getDouble(message, offset) != 0D; + case 1: // FLOAT: + return UnsafeUtil.getFloat(message, offset) != 0F; + case 2: // INT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 3: // UINT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 4: // INT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 5: // FIXED64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 6: // FIXED32: + return UnsafeUtil.getInt(message, offset) != 0; + case 7: // BOOL: + return UnsafeUtil.getBoolean(message, offset); + case 8: // STRING: + Object value = UnsafeUtil.getObject(message, offset); + if (value instanceof String) { + return !((String) value).isEmpty(); + } else if (value instanceof ByteString) { + return !ByteString.EMPTY.equals(value); + } else { + throw new IllegalArgumentException(); + } + case 9: // MESSAGE: + return UnsafeUtil.getObject(message, offset) != null; + case 10: // BYTES: + return !ByteString.EMPTY.equals(UnsafeUtil.getObject(message, offset)); + case 11: // UINT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 12: // ENUM: + return UnsafeUtil.getInt(message, offset) != 0; + case 13: // SFIXED32: + return UnsafeUtil.getInt(message, offset) != 0; + case 14: // SFIXED64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 15: // SINT32: + return UnsafeUtil.getInt(message, offset) != 0; + case 16: // SINT64: + return UnsafeUtil.getLong(message, offset) != 0L; + case 17: // GROUP: + return UnsafeUtil.getObject(message, offset) != null; + default: + throw new IllegalArgumentException(); + } + } else { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + return (UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) & presenceMask) != 0; + } + } + + private void setFieldPresent(T message, int pos) { + if (proto3) { + // Proto3 doesn't have presence fields + return; + } + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + final int presenceMask = 1 << (presenceMaskAndOffset >>> OFFSET_BITS); + final long presenceFieldOffset = presenceMaskAndOffset & OFFSET_MASK; + UnsafeUtil.putInt( + message, + presenceFieldOffset, + UnsafeUtil.getInt(message, presenceFieldOffset) | presenceMask); + } + + private boolean isOneofPresent(T message, int fieldNumber, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + return UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) == fieldNumber; + } + + private boolean isOneofCaseEqual(T message, T other, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + return UnsafeUtil.getInt(message, presenceMaskAndOffset & OFFSET_MASK) + == UnsafeUtil.getInt(other, presenceMaskAndOffset & OFFSET_MASK); + } + + private void setOneofPresent(T message, int fieldNumber, int pos) { + int presenceMaskAndOffset = presenceMaskAndOffsetAt(pos); + UnsafeUtil.putInt(message, presenceMaskAndOffset & OFFSET_MASK, fieldNumber); + } + + private int positionForFieldNumber(final int number) { + if (number >= minFieldNumber && number <= maxFieldNumber) { + return slowPositionForFieldNumber(number, 0); + } + return -1; + } + + private int positionForFieldNumber(final int number, final int min) { + if (number >= minFieldNumber && number <= maxFieldNumber) { + return slowPositionForFieldNumber(number, min); + } + return -1; + } + + private int slowPositionForFieldNumber(final int number, int min) { + int max = buffer.length / INTS_PER_FIELD - 1; + while (min <= max) { + // Find the midpoint address. + final int mid = (max + min) >>> 1; + final int pos = mid * INTS_PER_FIELD; + final int midFieldNumber = numberAt(pos); + if (number == midFieldNumber) { + // Found the field. + return pos; + } + if (number < midFieldNumber) { + // Search the lower half. + max = mid - 1; + } else { + // Search the upper half. + min = mid + 1; + } + } + return -1; + } + + int getSchemaSize() { + return buffer.length * 3; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java new file mode 100755 index 0000000000..187dc8b8a5 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/MessageSetSchema.java @@ -0,0 +1,392 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map.Entry; + +/** Schema used for proto2 messages using message_set_wireformat. */ +final class MessageSetSchema<T> implements Schema<T> { + private final MessageLite defaultInstance; + private final UnknownFieldSchema<?, ?> unknownFieldSchema; + private final boolean hasExtensions; + private final ExtensionSchema<?> extensionSchema; + + private MessageSetSchema( + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MessageLite defaultInstance) { + this.unknownFieldSchema = unknownFieldSchema; + this.hasExtensions = extensionSchema.hasExtensions(defaultInstance); + this.extensionSchema = extensionSchema; + this.defaultInstance = defaultInstance; + } + + static <T> MessageSetSchema<T> newSchema( + UnknownFieldSchema<?, ?> unknownFieldSchema, + ExtensionSchema<?> extensionSchema, + MessageLite defaultInstance) { + return new MessageSetSchema<T>(unknownFieldSchema, extensionSchema, defaultInstance); + } + + @SuppressWarnings("unchecked") + @Override + public T newInstance() { + return (T) defaultInstance.newBuilderForType().buildPartial(); + } + + @Override + public boolean equals(T message, T other) { + Object messageUnknown = unknownFieldSchema.getFromMessage(message); + Object otherUnknown = unknownFieldSchema.getFromMessage(other); + if (!messageUnknown.equals(otherUnknown)) { + return false; + } + if (hasExtensions) { + FieldSet<?> messageExtensions = extensionSchema.getExtensions(message); + FieldSet<?> otherExtensions = extensionSchema.getExtensions(other); + return messageExtensions.equals(otherExtensions); + } + return true; + } + + @Override + public int hashCode(T message) { + int hashCode = unknownFieldSchema.getFromMessage(message).hashCode(); + if (hasExtensions) { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + hashCode = (hashCode * 53) + extensions.hashCode(); + } + return hashCode; + } + + @Override + public void mergeFrom(T message, T other) { + SchemaUtil.mergeUnknownFields(unknownFieldSchema, message, other); + if (hasExtensions) { + SchemaUtil.mergeExtensions(extensionSchema, message, other); + } + } + + @SuppressWarnings("unchecked") + @Override + public void writeTo(T message, Writer writer) throws IOException { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + Iterator<?> iterator = extensions.iterator(); + while (iterator.hasNext()) { + Entry<?, ?> extension = (Entry<?, ?>) iterator.next(); + FieldSet.FieldDescriptorLite<?> fd = (FieldSet.FieldDescriptorLite<?>) extension.getKey(); + if (fd.getLiteJavaType() != WireFormat.JavaType.MESSAGE || fd.isRepeated() || fd.isPacked()) { + throw new IllegalStateException("Found invalid MessageSet item."); + } + if (extension instanceof LazyField.LazyEntry) { + writer.writeMessageSetItem( + fd.getNumber(), ((LazyField.LazyEntry) extension).getField().toByteString()); + } else { + writer.writeMessageSetItem(fd.getNumber(), extension.getValue()); + } + } + writeUnknownFieldsHelper(unknownFieldSchema, message, writer); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + private <UT, UB> void writeUnknownFieldsHelper( + UnknownFieldSchema<UT, UB> unknownFieldSchema, T message, Writer writer) throws IOException { + unknownFieldSchema.writeAsMessageSetTo(unknownFieldSchema.getFromMessage(message), writer); + } + + @SuppressWarnings("ReferenceEquality") + @Override + public void mergeFrom( + T message, byte[] data, int position, int limit, ArrayDecoders.Registers registers) + throws IOException { + UnknownFieldSetLite unknownFields = ((GeneratedMessageLite) message).unknownFields; + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + ((GeneratedMessageLite) message).unknownFields = unknownFields; + } + final FieldSet<GeneratedMessageLite.ExtensionDescriptor> extensions = + ((GeneratedMessageLite.ExtendableMessage<?, ?>) message).ensureExtensionsAreMutable(); + GeneratedMessageLite.GeneratedExtension<?, ?> extension = null; + while (position < limit) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + final int startTag = registers.int1; + if (startTag != WireFormat.MESSAGE_SET_ITEM_TAG) { + if (WireFormat.getTagWireType(startTag) == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + extension = + (GeneratedMessageLite.GeneratedExtension<?, ?>) extensionSchema.findExtensionByNumber( + registers.extensionRegistry, defaultInstance, + WireFormat.getTagFieldNumber(startTag)); + if (extension != null) { + position = + ArrayDecoders.decodeMessageField( + Protobuf.getInstance().schemaFor( + extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + extensions.setField(extension.descriptor, registers.object1); + } else { + position = + ArrayDecoders.decodeUnknownField( + startTag, data, position, limit, unknownFields, registers); + } + } else { + position = ArrayDecoders.skipField(startTag, data, position, limit, registers); + } + continue; + } + + int typeId = 0; + ByteString rawBytes = null; + + while (position < limit) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + final int tag = registers.int1; + final int number = WireFormat.getTagFieldNumber(tag); + final int wireType = WireFormat.getTagWireType(tag); + switch (number) { + case WireFormat.MESSAGE_SET_TYPE_ID: + if (wireType == WireFormat.WIRETYPE_VARINT) { + position = ArrayDecoders.decodeVarint32(data, position, registers); + typeId = registers.int1; + extension = + (GeneratedMessageLite.GeneratedExtension<?, ?>) extensionSchema + .findExtensionByNumber(registers.extensionRegistry, defaultInstance, typeId); + continue; + } + break; + case WireFormat.MESSAGE_SET_MESSAGE: + if (extension != null) { + position = ArrayDecoders.decodeMessageField( + Protobuf.getInstance().schemaFor( + extension.getMessageDefaultInstance().getClass()), + data, position, limit, registers); + extensions.setField(extension.descriptor, registers.object1); + continue; + } else { + if (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + position = ArrayDecoders.decodeBytes(data, position, registers); + rawBytes = (ByteString) registers.object1; + continue; + } + break; + } + default: + break; + } + if (tag == WireFormat.MESSAGE_SET_ITEM_END_TAG) { + break; + } + position = ArrayDecoders.skipField(tag, data, position, limit, registers); + } + + if (rawBytes != null) { + unknownFields.storeField( + WireFormat.makeTag(typeId, WireFormat.WIRETYPE_LENGTH_DELIMITED), rawBytes); + } + } + if (position != limit) { + throw InvalidProtocolBufferException.parseFailure(); + } + } + + @Override + public void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + mergeFromHelper(unknownFieldSchema, extensionSchema, message, reader, extensionRegistry); + } + + /** + * A helper method for wildcard capture of {@code unknownFieldSchema}. See: + * https://docs.oracle.com/javase/tutorial/java/generics/capture.html + */ + @SuppressWarnings("unchecked") + private <UT, UB, ET extends FieldSet.FieldDescriptorLite<ET>> void mergeFromHelper( + UnknownFieldSchema<UT, UB> unknownFieldSchema, + ExtensionSchema<ET> extensionSchema, + T message, + Reader reader, + ExtensionRegistryLite extensionRegistry) + throws IOException { + UB unknownFields = unknownFieldSchema.getBuilderFromMessage(message); + FieldSet<ET> extensions = extensionSchema.getMutableExtensions(message); + try { + while (true) { + final int number = reader.getFieldNumber(); + if (number == Reader.READ_DONE) { + return; + } + if (parseMessageSetItemOrUnknownField( + reader, + extensionRegistry, + extensionSchema, + extensions, + unknownFieldSchema, + unknownFields)) { + continue; + } + // Done reading. + return; + } + } finally { + unknownFieldSchema.setBuilderToMessage(message, unknownFields); + } + } + + @Override + public void makeImmutable(T message) { + unknownFieldSchema.makeImmutable(message); + extensionSchema.makeImmutable(message); + } + + private <UT, UB, ET extends FieldSet.FieldDescriptorLite<ET>> + boolean parseMessageSetItemOrUnknownField( + Reader reader, + ExtensionRegistryLite extensionRegistry, + ExtensionSchema<ET> extensionSchema, + FieldSet<ET> extensions, + UnknownFieldSchema<UT, UB> unknownFieldSchema, + UB unknownFields) + throws IOException { + int startTag = reader.getTag(); + if (startTag != WireFormat.MESSAGE_SET_ITEM_TAG) { + if (WireFormat.getTagWireType(startTag) == WireFormat.WIRETYPE_LENGTH_DELIMITED) { + Object extension = + extensionSchema.findExtensionByNumber( + extensionRegistry, defaultInstance, WireFormat.getTagFieldNumber(startTag)); + if (extension != null) { + extensionSchema.parseLengthPrefixedMessageSetItem( + reader, extension, extensionRegistry, extensions); + return true; + } else { + return unknownFieldSchema.mergeOneFieldFrom(unknownFields, reader); + } + } else { + return reader.skipField(); + } + } + + // The wire format for MessageSet is: + // message MessageSet { + // repeated group Item = 1 { + // required int32 typeId = 2; + // required bytes message = 3; + // } + // } + // "typeId" is the extension's field number. The extension can only be + // a message type, where "message" contains the encoded bytes of that + // message. + // + // In practice, we will probably never see a MessageSet item in which + // the message appears before the type ID, or where either field does not + // appear exactly once. However, in theory such cases are valid, so we + // should be prepared to accept them. + + int typeId = 0; + ByteString rawBytes = null; // If we encounter "message" before "typeId" + Object extension = null; + + // Read bytes from input, if we get it's type first then parse it eagerly, + // otherwise we store the raw bytes in a local variable. + loop: + while (true) { + final int number = reader.getFieldNumber(); + if (number == Reader.READ_DONE) { + break; + } + + final int tag = reader.getTag(); + if (tag == WireFormat.MESSAGE_SET_TYPE_ID_TAG) { + typeId = reader.readUInt32(); + extension = + extensionSchema.findExtensionByNumber(extensionRegistry, defaultInstance, typeId); + continue; + } else if (tag == WireFormat.MESSAGE_SET_MESSAGE_TAG) { + if (extension != null) { + extensionSchema.parseLengthPrefixedMessageSetItem( + reader, extension, extensionRegistry, extensions); + continue; + } + // We haven't seen a type ID yet or we want parse message lazily. + rawBytes = reader.readBytes(); + continue; + } else { + if (!reader.skipField()) { + break loop; // End of group + } + } + } + + if (reader.getTag() != WireFormat.MESSAGE_SET_ITEM_END_TAG) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + + // If there are any rawBytes left, it means the message content appears before the type ID. + if (rawBytes != null) { + if (extension != null) { // We known the type + // TODO(xiaofeng): Instead of reading into a temporary ByteString, maybe there is a way + // to read directly from Reader to the submessage? + extensionSchema.parseMessageSetItem(rawBytes, extension, extensionRegistry, extensions); + } else { + unknownFieldSchema.addLengthDelimited(unknownFields, typeId, rawBytes); + } + } + return true; + } + + @Override + public final boolean isInitialized(T message) { + FieldSet<?> extensions = extensionSchema.getExtensions(message); + return extensions.isInitialized(); + } + + @Override + public int getSerializedSize(T message) { + int size = 0; + + size += getUnknownFieldsSerializedSize(unknownFieldSchema, message); + + if (hasExtensions) { + size += extensionSchema.getExtensions(message).getMessageSetSerializedSize(); + } + + return size; + } + + private <UT, UB> int getUnknownFieldsSerializedSize( + UnknownFieldSchema<UT, UB> schema, T message) { + UT unknowns = schema.getFromMessage(message); + return schema.getSerializedSizeAsMessageSet(unknowns); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java new file mode 100755 index 0000000000..f2dbb8ef9c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchema.java @@ -0,0 +1,36 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +interface NewInstanceSchema { + /** Create a new message instance given the default instance of the message type. */ + Object newInstance(Object defaultInstance); +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java new file mode 100755 index 0000000000..3433b657e6 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaFull.java @@ -0,0 +1,39 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +final class NewInstanceSchemaFull implements NewInstanceSchema { + @Override + public Object newInstance(Object defaultInstance) { + return ((GeneratedMessageV3) defaultInstance) + .newInstance(GeneratedMessageV3.UnusedPrivateParameter.INSTANCE); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java new file mode 100755 index 0000000000..9b92266763 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemaLite.java @@ -0,0 +1,39 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +final class NewInstanceSchemaLite implements NewInstanceSchema { + @Override + public Object newInstance(Object defaultInstance) { + return ((GeneratedMessageLite) defaultInstance) + .dynamicMethod(GeneratedMessageLite.MethodToInvoke.NEW_MUTABLE_INSTANCE); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java new file mode 100755 index 0000000000..eff45f67b9 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/NewInstanceSchemas.java @@ -0,0 +1,53 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +final class NewInstanceSchemas { + private static final NewInstanceSchema FULL_SCHEMA = loadSchemaForFullRuntime(); + private static final NewInstanceSchema LITE_SCHEMA = new NewInstanceSchemaLite(); + + static NewInstanceSchema full() { + return FULL_SCHEMA; + } + + static NewInstanceSchema lite() { + return LITE_SCHEMA; + } + + private static NewInstanceSchema loadSchemaForFullRuntime() { + try { + Class<?> clazz = Class.forName("com.google.protobuf.NewInstanceSchemaFull"); + return (NewInstanceSchema) clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/OneofInfo.java b/java/core/src/main/java/com/google/protobuf/OneofInfo.java new file mode 100755 index 0000000000..bc518fcadd --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/OneofInfo.java @@ -0,0 +1,66 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.lang.reflect.Field; + +/** Information for a oneof within a protobuf message. */ +// TODO(nathanmittler): make this private once all of experimental code is migrated to protobuf. +@ExperimentalApi +final class OneofInfo { + private final int id; + private final Field caseField; + private final Field valueField; + + public OneofInfo(int id, Field caseField, Field valueField) { + this.id = id; + this.caseField = caseField; + this.valueField = valueField; + } + + /** + * Returns the unique identifier of the oneof within the message. This is really just an index + * starting at zero. + */ + public int getId() { + return id; + } + + /** The {@code int} field containing the field number of the currently active member. */ + public Field getCaseField() { + return caseField; + } + + /** The {@link Object} field containing the value of the currently active member. */ + public Field getValueField() { + return valueField; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java b/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java new file mode 100755 index 0000000000..851b839f6f --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ProtoSyntax.java @@ -0,0 +1,38 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** Represents the syntax version of the message. */ +@ExperimentalApi +public enum ProtoSyntax { + PROTO2, + PROTO3; +} diff --git a/java/core/src/main/java/com/google/protobuf/Protobuf.java b/java/core/src/main/java/com/google/protobuf/Protobuf.java new file mode 100755 index 0000000000..adaa7fa8f2 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Protobuf.java @@ -0,0 +1,152 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Main runtime interface for protobuf. Applications should interact with this interface (rather + * than directly accessing internal APIs) in order to perform operations on protobuf messages. + */ +@ExperimentalApi +final class Protobuf { + private static final Protobuf INSTANCE = new Protobuf(); + + private final SchemaFactory schemaFactory; + + // TODO(nathanmittler): Consider using ClassValue instead. + private final ConcurrentMap<Class<?>, Schema<?>> schemaCache = + new ConcurrentHashMap<Class<?>, Schema<?>>(); + + /** Gets the singleton instance of the Protobuf runtime. */ + public static Protobuf getInstance() { + return INSTANCE; + } + + /** Writes the given message to the target {@link Writer}. */ + public <T> void writeTo(T message, Writer writer) throws IOException { + schemaFor(message).writeTo(message, writer); + } + + /** Reads fields from the given {@link Reader} and merges them into the message. */ + public <T> void mergeFrom(T message, Reader reader) throws IOException { + mergeFrom(message, reader, ExtensionRegistryLite.getEmptyRegistry()); + } + + /** Reads fields from the given {@link Reader} and merges them into the message. */ + public <T> void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException { + schemaFor(message).mergeFrom(message, reader, extensionRegistry); + } + + /** Marks repeated/map/extension/unknown fields as immutable. */ + public <T> void makeImmutable(T message) { + schemaFor(message).makeImmutable(message); + } + + /** + * Checks if all required fields are set. TODO(xiaofeng): Make this package private when the tests + * are moved to protobuf package. + */ + public <T> boolean isInitialized(T message) { + return schemaFor(message).isInitialized(message); + } + + /** Gets the schema for the given message type. */ + public <T> Schema<T> schemaFor(Class<T> messageType) { + checkNotNull(messageType, "messageType"); + @SuppressWarnings("unchecked") + Schema<T> schema = (Schema<T>) schemaCache.get(messageType); + if (schema == null) { + schema = schemaFactory.createSchema(messageType); + @SuppressWarnings("unchecked") + Schema<T> previous = (Schema<T>) registerSchema(messageType, schema); + if (previous != null) { + // A new schema was registered by another thread. + schema = previous; + } + } + return schema; + } + + /** Gets the schema for the given message. */ + @SuppressWarnings("unchecked") + public <T> Schema<T> schemaFor(T message) { + return schemaFor((Class<T>) message.getClass()); + } + + /** + * Registers the given schema for the message type only if a schema was not already registered. + * + * @param messageType the type of message on which the schema operates. + * @param schema the schema for the message type. + * @return the previously registered schema, or {@code null} if the given schema was successfully + * registered. + */ + public Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) { + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); + return schemaCache.putIfAbsent(messageType, schema); + } + + /** + * Visible for testing only. Registers the given schema for the message type. If a schema was + * previously registered, it will be replaced by the provided schema. + * + * @param messageType the type of message on which the schema operates. + * @param schema the schema for the message type. + * @return the previously registered schema, or {@code null} if no schema was registered + * previously. + */ + public Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema) { + checkNotNull(messageType, "messageType"); + checkNotNull(schema, "schema"); + return schemaCache.put(messageType, schema); + } + + private Protobuf() { + schemaFactory = new ManifestSchemaFactory(); + } + + int getTotalSchemaSize() { + int result = 0; + for (Schema<?> schema : schemaCache.values()) { + if (schema instanceof MessageSchema) { + result += ((MessageSchema) schema).getSchemaSize(); + } + } + return result; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/ProtobufLists.java b/java/core/src/main/java/com/google/protobuf/ProtobufLists.java new file mode 100755 index 0000000000..271c849b4d --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/ProtobufLists.java @@ -0,0 +1,94 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.Internal.BooleanList; +import com.google.protobuf.Internal.DoubleList; +import com.google.protobuf.Internal.FloatList; +import com.google.protobuf.Internal.IntList; +import com.google.protobuf.Internal.LongList; +import com.google.protobuf.Internal.ProtobufList; + +/** Utility class for construction of lists that extend {@link ProtobufList}. */ +@ExperimentalApi +final class ProtobufLists { + private ProtobufLists() {} + + public static <E> ProtobufList<E> emptyProtobufList() { + return ProtobufArrayList.emptyList(); + } + + public static <E> ProtobufList<E> mutableCopy(ProtobufList<E> list) { + int size = list.size(); + return list.mutableCopyWithCapacity( + size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + } + + public static BooleanList emptyBooleanList() { + return BooleanArrayList.emptyList(); + } + + public static BooleanList newBooleanList() { + return new BooleanArrayList(); + } + + public static IntList emptyIntList() { + return IntArrayList.emptyList(); + } + + public static IntList newIntList() { + return new IntArrayList(); + } + + public static LongList emptyLongList() { + return LongArrayList.emptyList(); + } + + public static LongList newLongList() { + return new LongArrayList(); + } + + public static FloatList emptyFloatList() { + return FloatArrayList.emptyList(); + } + + public static FloatList newFloatList() { + return new FloatArrayList(); + } + + public static DoubleList emptyDoubleList() { + return DoubleArrayList.emptyList(); + } + + public static DoubleList newDoubleList() { + return new DoubleArrayList(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java b/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java new file mode 100755 index 0000000000..3982d2e229 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/RawMessageInfo.java @@ -0,0 +1,226 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** + * RawMessageInfo stores the same amount of information as {@link MessageInfo} but in a more compact + * format. + */ +final class RawMessageInfo implements MessageInfo { + + private final MessageLite defaultInstance; + + /** + * The compact format packs everything in a String object and a Object[] array. The String object + * is encoded with field number, field type, hasbits offset, oneof index, etc., whereas the + * Object[] array contains field references, class references, instance references, etc. + * BEGIN GOOGLE-INTERNAL + * + * See + * https://docs.google.com/document/d/1_z6-tIa31Pq6MUi_79eA5S6214J13idRbAHTgn3vqXY/edit#heading=h.cifnozjwpoe9 + * for more information. + * END GOOGLE-INTERNAL + * + * <p>The String object encodes a sequence of integers into UTF-16 characters. For each int, it + * will be encoding into 1 to 3 UTF-16 characters depending on its unsigned value: + * + * <ul> + * <li>1 char: [c1: 0x0000 - 0xD7FF] = int of the same value. + * <li>2 chars: [c1: 0xE000 - 0xFFFF], [c2: 0x0000 - 0xD7FF] = (c2 << 13) | (c1 & 0x1FFF) + * <li>3 chars: [c1: 0xE000 - 0xFFFF], [c2: 0xE000 - 0xFFFF], [c3: 0x0000 - 0xD7FF] = (c3 << 26) + * | ((c2 & 0x1FFF) << 13) | (c1 & 0x1FFF) + * </ul> + * + * <p>Note that we don't use UTF-16 surrogate pairs [0xD800 - 0xDFFF] because they have to come in + * pairs to form a valid UTF-16char sequence and don't help us encode values more efficiently. + * + * <p>The integer sequence encoded in the String object has the following layout: + * + * <ul> + * <li>[0]: flags, flags & 0x1 = is proto2?, flags & 0x2 = is message?. + * <li>[1]: field count, if 0, this is the end of the integer sequence and the corresponding + * Object[] array should be null. + * <li>[2]: oneof count + * <li>[3]: hasbits count, how many hasbits integers are generated. + * <li>[4]: min field number + * <li>[5]: max field number + * <li>[6]: total number of entries need to allocate + * <li>[7]: map field count + * <li>[8]: repeated field count, this doesn't include map fields. + * <li>[9]: size of checkInitialized array + * <li>[...]: field entries + * </ul> + * + * <p>Each field entry starts with a field number and the field type: + * + * <ul> + * <li>[0]: field number + * <li>[1]: field type with extra bits: + * <ul> + * <li>v & 0xFF = field type as defined in the FieldType class + * <li>v & 0x100 = is required? + * <li>v & 0x200 = is checkUtf8? + * <li>v & 0x400 = needs isInitialized check? + * <li>v & 0x800 = is map field with proto2 enum value? + * </ul> + * </ul> + * + * If the file is proto2 and this is a singular field: + * + * <ul> + * <li>[2]: hasbits offset + * </ul> + * + * If the field is in an oneof: + * + * <ul> + * <li>[2]: oenof index + * </ul> + * + * For other types, the field entry only has field number and field type. + * + * <p>The Object[] array has 3 sections: + * + * <ul> + * <li>---- oneof section ---- + * <ul> + * <li>[0]: value field for oneof 1. + * <li>[1]: case field for oneof 1. + * <li>... + * <li>[.]: value field for oneof n. + * <li>[.]: case field for oneof n. + * </ul> + * <li>---- hasbits section ---- + * <ul> + * <li>[.]: hasbits field 1 + * <li>[.]: hasbits field 2 + * <li>... + * <li>[.]: hasbits field n + * </ul> + * <li>---- field section ---- + * <ul> + * <li>[...]: field entries + * </ul> + * </ul> + * + * <p>In the Object[] array, field entries are ordered in the same way as field entries in the + * String object. The size of each entry is determined by the field type. + * + * <ul> + * <li>Oneof field: + * <ul> + * <li>Oneof message field: + * <ul> + * <li>[0]: message class reference. + * </ul> + * <li>Oneof enum fieldin proto2: + * <ul> + * <li>[0]: EnumLiteMap + * </ul> + * <li>For all other oneof fields, field entry in the Object[] array is empty. + * </ul> + * <li>Repeated message field: + * <ul> + * <li>[0]: field reference + * <li>[1]: message class reference + * </ul> + * <li>Proto2 singular/repeated enum field: + * <ul> + * <li>[0]: field reference + * <li>[1]: EnumLiteMap + * </ul> + * <li>Map field with a proto2 enum value: + * <ul> + * <li>[0]: field reference + * <li>[1]: map default entry instance + * <li>[2]: EnumLiteMap + * </ul> + * <li>Map field with other value types: + * <ul> + * <li>[0]: field reference + * <li>[1]: map default entry instance + * </ul> + * <li>All other field type: + * <ul> + * <li>[0]: field reference + * </ul> + * </ul> + * + * <p>In order to read the field info from this compact format, a reader needs to progress through + * the String object and the Object[] array simultaneously. + */ + private final String info; + + private final Object[] objects; + private final int flags; + + RawMessageInfo(MessageLite defaultInstance, String info, Object[] objects) { + this.defaultInstance = defaultInstance; + this.info = info; + this.objects = objects; + int position = 0; + int value = (int) info.charAt(position++); + if (value < 0xD800) { + flags = value; + } else { + int result = value & 0x1FFF; + int shift = 13; + while ((value = info.charAt(position++)) >= 0xD800) { + result |= (value & 0x1FFF) << shift; + shift += 13; + } + flags = result | (value << shift); + } + } + + String getStringInfo() { + return info; + } + + Object[] getObjects() { + return objects; + } + + @Override + public MessageLite getDefaultInstance() { + return defaultInstance; + } + + @Override + public ProtoSyntax getSyntax() { + return (flags & 0x1) == 0x1 ? ProtoSyntax.PROTO2 : ProtoSyntax.PROTO3; + } + + @Override + public boolean isMessageSetWireFormat() { + return (flags & 0x2) == 0x2; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Reader.java b/java/core/src/main/java/com/google/protobuf/Reader.java new file mode 100755 index 0000000000..705096f2d8 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Reader.java @@ -0,0 +1,379 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** A reader of fields from a serialized protobuf message. */ +// TODO(nathanmittler): Refactor to allow the reader to allocate properly sized lists. +@ExperimentalApi +interface Reader { + /** Value used to indicate that the end of input has been reached. */ + int READ_DONE = Integer.MAX_VALUE; + + /** Value used to indicate that the reader does not know the tag about the field. */ + int TAG_UNKNOWN = 0; + + boolean shouldDiscardUnknownFields(); + + /** + * Gets the field number for the current field being read. + * + * <p>TODO(liujisi): Rename it to make it more explicit about the side effect on the underlying + * buffer. + * + * @return the current field number or {@link #READ_DONE} if the end of input has been reached. + */ + int getFieldNumber() throws IOException; + + /** + * Gets the wire tag of the current field. + * + * @return the current wire tag or {@link #TAG_UNKNOWN} if the reader does not know the tag of the + * current field. + */ + int getTag(); + + /** + * Skips the current field and advances the reader to the next field. + * + * @return {@code true} if there are more fields or {@code false} if the end of input has been + * reached. + */ + boolean skipField() throws IOException; + + /** + * Reads and returns the next field of type {@code DOUBLE} and advances the reader to the next + * field. + */ + double readDouble() throws IOException; + + /** + * Reads and returns the next field of type {@code FLOAT} and advances the reader to the next + * field. + */ + float readFloat() throws IOException; + + /** + * Reads and returns the next field of type {@code UINT64} and advances the reader to the next + * field. + */ + long readUInt64() throws IOException; + + /** + * Reads and returns the next field of type {@code INT64} and advances the reader to the next + * field. + */ + long readInt64() throws IOException; + + /** + * Reads and returns the next field of type {@code INT32} and advances the reader to the next + * field. + */ + int readInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code FIXED64} and advances the reader to the next + * field. + */ + long readFixed64() throws IOException; + + /** + * Reads and returns the next field of type {@code FIXED32} and advances the reader to the next + * field. + */ + int readFixed32() throws IOException; + + /** + * Reads and returns the next field of type {@code BOOL} and advances the reader to the next + * field. + */ + boolean readBool() throws IOException; + + /** + * Reads and returns the next field of type {@code STRING} and advances the reader to the next + * field. If the stream contains malformed UTF-8, replace the offending bytes with the standard + * UTF-8 replacement character. + */ + String readString() throws IOException; + + /** + * Reads and returns the next field of type {@code STRING} and advances the reader to the next + * field. If the stream contains malformed UTF-8, throw exception {@link + * InvalidProtocolBufferException}. + */ + String readStringRequireUtf8() throws IOException; + + // TODO(yilunchong): the lack of other opinions for whether to expose this on the interface + <T> T readMessageBySchemaWithCheck(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads and returns the next field of type {@code MESSAGE} and advances the reader to the next + * field. + */ + <T> T readMessage(Class<T> clazz, ExtensionRegistryLite extensionRegistry) throws IOException; + + /** + * Reads and returns the next field of type {@code GROUP} and advances the reader to the next + * field. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + <T> T readGroup(Class<T> clazz, ExtensionRegistryLite extensionRegistry) throws IOException; + + // TODO(yilunchong): the lack of other opinions for whether to expose this on the interface + @Deprecated + <T> T readGroupBySchemaWithCheck(Schema<T> schema, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads and returns the next field of type {@code BYTES} and advances the reader to the next + * field. + */ + ByteString readBytes() throws IOException; + + /** + * Reads and returns the next field of type {@code UINT32} and advances the reader to the next + * field. + */ + int readUInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code ENUM} and advances the reader to the next + * field. + */ + int readEnum() throws IOException; + + /** + * Reads and returns the next field of type {@code SFIXED32} and advances the reader to the next + * field. + */ + int readSFixed32() throws IOException; + + /** + * Reads and returns the next field of type {@code SFIXED64} and advances the reader to the next + * field. + */ + long readSFixed64() throws IOException; + + /** + * Reads and returns the next field of type {@code SINT32} and advances the reader to the next + * field. + */ + int readSInt32() throws IOException; + + /** + * Reads and returns the next field of type {@code SINT64} and advances the reader to the next + * field. + */ + long readSInt64() throws IOException; + + /** + * Reads the next field of type {@code DOUBLE_LIST} or {@code DOUBLE_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readDoubleList(List<Double> target) throws IOException; + + /** + * Reads the next field of type {@code FLOAT_LIST} or {@code FLOAT_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFloatList(List<Float> target) throws IOException; + + /** + * Reads the next field of type {@code UINT64_LIST} or {@code UINT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readUInt64List(List<Long> target) throws IOException; + + /** + * Reads the next field of type {@code INT64_LIST} or {@code INT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readInt64List(List<Long> target) throws IOException; + + /** + * Reads the next field of type {@code INT32_LIST} or {@code INT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readInt32List(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code FIXED64_LIST} or {@code FIXED64_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFixed64List(List<Long> target) throws IOException; + + /** + * Reads the next field of type {@code FIXED32_LIST} or {@code FIXED32_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readFixed32List(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code BOOL_LIST} or {@code BOOL_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readBoolList(List<Boolean> target) throws IOException; + + /** + * Reads the next field of type {@code STRING_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readStringList(List<String> target) throws IOException; + + /** + * Reads the next field of type {@code STRING_LIST} and advances the reader to the next field. If + * the stream contains malformed UTF-8, throw exception {@link InvalidProtocolBufferException}. + * + * @param target the list that will receive the read values. + */ + void readStringListRequireUtf8(List<String> target) throws IOException; + + /** + * Reads the next field of type {@code MESSAGE_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + * @param targetType the type of the elements stored in the {@code target} list. + */ + <T> void readMessageList( + List<T> target, Schema<T> schema, ExtensionRegistryLite extensionRegistry) throws IOException; + + <T> void readMessageList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads the next field of type {@code GROUP_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + * @param targetType the type of the elements stored in the {@code target} list. + * @deprecated groups fields are deprecated. + */ + @Deprecated + <T> void readGroupList( + List<T> target, Class<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + @Deprecated + <T> void readGroupList( + List<T> target, Schema<T> targetType, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Reads the next field of type {@code BYTES_LIST} and advances the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readBytesList(List<ByteString> target) throws IOException; + + /** + * Reads the next field of type {@code UINT32_LIST} or {@code UINT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readUInt32List(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code ENUM_LIST} or {@code ENUM_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readEnumList(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code SFIXED32_LIST} or {@code SFIXED32_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSFixed32List(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code SFIXED64_LIST} or {@code SFIXED64_LIST_PACKED} and advances + * the reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSFixed64List(List<Long> target) throws IOException; + + /** + * Reads the next field of type {@code SINT32_LIST} or {@code SINT32_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSInt32List(List<Integer> target) throws IOException; + + /** + * Reads the next field of type {@code SINT64_LIST} or {@code SINT64_LIST_PACKED} and advances the + * reader to the next field. + * + * @param target the list that will receive the read values. + */ + void readSInt64List(List<Long> target) throws IOException; + + /** + * Reads the next field of type {@code MAP} and advances the reader to the next field. + * + * @param target the mutable map that will receive the read values. + * @param mapDefaultEntry the default entry of the map field. + * @param extensionRegistry the extension registry for parsing message value fields. + */ + <K, V> void readMap( + Map<K, V> target, + MapEntryLite.Metadata<K, V> mapDefaultEntry, + ExtensionRegistryLite extensionRegistry) + throws IOException; +} diff --git a/java/core/src/main/java/com/google/protobuf/RopeByteString.java b/java/core/src/main/java/com/google/protobuf/RopeByteString.java index c3bec7b1a1..f294df3df3 100644 --- a/java/core/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/core/src/main/java/com/google/protobuf/RopeByteString.java @@ -445,6 +445,11 @@ final class RopeByteString extends ByteString { right.writeTo(output); } + @Override + void writeToReverse(ByteOutput output) throws IOException { + right.writeToReverse(output); + left.writeToReverse(output); + } @Override protected String toStringInternal(Charset charset) { diff --git a/java/core/src/main/java/com/google/protobuf/Schema.java b/java/core/src/main/java/com/google/protobuf/Schema.java new file mode 100755 index 0000000000..d0e1e26e5b --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Schema.java @@ -0,0 +1,85 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.ArrayDecoders.Registers; +import java.io.IOException; + +/** + * A runtime schema for a single protobuf message. A schema provides operations on message instances + * such as serialization/deserialization. + */ +@ExperimentalApi +interface Schema<T> { + /** Writes the given message to the target {@link Writer}. */ + void writeTo(T message, Writer writer) throws IOException; + + /** + * Reads fields from the given {@link Reader} and merges them into the message. It doesn't make + * the message immutable after parsing is done. To make the message immutable, use {@link + * #makeImmutable}. + */ + void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) + throws IOException; + + /** + * Like the above but parses from a byte[] without extensions. Entry point of fast path. Note that + * this method may throw IndexOutOfBoundsException if the input data is not valid protobuf wire + * format. Protobuf public API methods should catch and convert that exception to + * InvalidProtocolBufferException. + */ + void mergeFrom(T message, byte[] data, int position, int limit, Registers registers) + throws IOException; + + /** Marks repeated/map/extension/unknown fields as immutable. */ + void makeImmutable(T message); + + /** Checks whether all required fields are set. */ + boolean isInitialized(T message); + + /** Creates a new instance of the message class. */ + T newInstance(); + + /** Determine of the two messages are equal. */ + boolean equals(T message, T other); + + /** Compute a hashCode for the message. */ + int hashCode(T message); + + /** + * Merge values from {@code other} into {@code message}. This method doesn't make the message + * immutable. To make the message immutable after merging, use {@link #makeImmutable}. + */ + void mergeFrom(T message, T other); + + /** Compute the serialized size of the message. */ + int getSerializedSize(T message); +} diff --git a/java/core/src/main/java/com/google/protobuf/SchemaFactory.java b/java/core/src/main/java/com/google/protobuf/SchemaFactory.java new file mode 100755 index 0000000000..cf38dd6994 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/SchemaFactory.java @@ -0,0 +1,38 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** A factory that manufactures {@link Schema} instances for protobuf messages. */ +@ExperimentalApi +interface SchemaFactory { + /** Creates a schema instance for the given protobuf message type. */ + <T> Schema<T> createSchema(Class<T> messageType); +} diff --git a/java/core/src/main/java/com/google/protobuf/SchemaUtil.java b/java/core/src/main/java/com/google/protobuf/SchemaUtil.java new file mode 100755 index 0000000000..50957fcfdb --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/SchemaUtil.java @@ -0,0 +1,991 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.FieldSet.FieldDescriptorLite; +import com.google.protobuf.Internal.EnumLiteMap; +import com.google.protobuf.Internal.EnumVerifier; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.List; +import java.util.RandomAccess; + +/** Helper methods used by schemas. */ +@ExperimentalApi +final class SchemaUtil { + private static final Class<?> GENERATED_MESSAGE_CLASS = getGeneratedMessageClass(); + private static final UnknownFieldSchema<?, ?> PROTO2_UNKNOWN_FIELD_SET_SCHEMA = + getUnknownFieldSetSchema(false); + private static final UnknownFieldSchema<?, ?> PROTO3_UNKNOWN_FIELD_SET_SCHEMA = + getUnknownFieldSetSchema(true); + private static final UnknownFieldSchema<?, ?> UNKNOWN_FIELD_SET_LITE_SCHEMA = + new UnknownFieldSetLiteSchema(); + + private static final int DEFAULT_LOOK_UP_START_NUMBER = 40; + + private SchemaUtil() {} + + /** + * Requires that the given message extend {@link com.google.protobuf.GeneratedMessageV3} or {@link + * GeneratedMessageLite}. + */ + public static void requireGeneratedMessage(Class<?> messageType) { + if (!GeneratedMessageLite.class.isAssignableFrom(messageType) + && GENERATED_MESSAGE_CLASS != null + && !GENERATED_MESSAGE_CLASS.isAssignableFrom(messageType)) { + throw new IllegalArgumentException( + "Message classes must extend GeneratedMessage or GeneratedMessageLite"); + } + } + + public static void writeDouble(int fieldNumber, double value, Writer writer) throws IOException { + if (Double.compare(value, 0.0) != 0) { + writer.writeDouble(fieldNumber, value); + } + } + + public static void writeFloat(int fieldNumber, float value, Writer writer) throws IOException { + if (Float.compare(value, 0.0f) != 0) { + writer.writeFloat(fieldNumber, value); + } + } + + public static void writeInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeInt64(fieldNumber, value); + } + } + + public static void writeUInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeUInt64(fieldNumber, value); + } + } + + public static void writeSInt64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSInt64(fieldNumber, value); + } + } + + public static void writeFixed64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeFixed64(fieldNumber, value); + } + } + + public static void writeSFixed64(int fieldNumber, long value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSFixed64(fieldNumber, value); + } + } + + public static void writeInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeInt32(fieldNumber, value); + } + } + + public static void writeUInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeUInt32(fieldNumber, value); + } + } + + public static void writeSInt32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSInt32(fieldNumber, value); + } + } + + public static void writeFixed32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeFixed32(fieldNumber, value); + } + } + + public static void writeSFixed32(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeSFixed32(fieldNumber, value); + } + } + + public static void writeEnum(int fieldNumber, int value, Writer writer) throws IOException { + if (value != 0) { + writer.writeEnum(fieldNumber, value); + } + } + + public static void writeBool(int fieldNumber, boolean value, Writer writer) throws IOException { + if (value) { + writer.writeBool(fieldNumber, true); + } + } + + public static void writeString(int fieldNumber, Object value, Writer writer) throws IOException { + if (value instanceof String) { + writeStringInternal(fieldNumber, (String) value, writer); + } else { + writeBytes(fieldNumber, (ByteString) value, writer); + } + } + + private static void writeStringInternal(int fieldNumber, String value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeString(fieldNumber, value); + } + } + + public static void writeBytes(int fieldNumber, ByteString value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBytes(fieldNumber, value); + } + } + + public static void writeMessage(int fieldNumber, Object value, Writer writer) throws IOException { + if (value != null) { + writer.writeMessage(fieldNumber, value); + } + } + + public static void writeDoubleList( + int fieldNumber, List<Double> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeDoubleList(fieldNumber, value, packed); + } + } + + public static void writeFloatList( + int fieldNumber, List<Float> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFloatList(fieldNumber, value, packed); + } + } + + public static void writeInt64List( + int fieldNumber, List<Long> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeInt64List(fieldNumber, value, packed); + } + } + + public static void writeUInt64List( + int fieldNumber, List<Long> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeUInt64List(fieldNumber, value, packed); + } + } + + public static void writeSInt64List( + int fieldNumber, List<Long> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSInt64List(fieldNumber, value, packed); + } + } + + public static void writeFixed64List( + int fieldNumber, List<Long> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFixed64List(fieldNumber, value, packed); + } + } + + public static void writeSFixed64List( + int fieldNumber, List<Long> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSFixed64List(fieldNumber, value, packed); + } + } + + public static void writeInt32List( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeInt32List(fieldNumber, value, packed); + } + } + + public static void writeUInt32List( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeUInt32List(fieldNumber, value, packed); + } + } + + public static void writeSInt32List( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSInt32List(fieldNumber, value, packed); + } + } + + public static void writeFixed32List( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeFixed32List(fieldNumber, value, packed); + } + } + + public static void writeSFixed32List( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeSFixed32List(fieldNumber, value, packed); + } + } + + public static void writeEnumList( + int fieldNumber, List<Integer> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeEnumList(fieldNumber, value, packed); + } + } + + public static void writeBoolList( + int fieldNumber, List<Boolean> value, Writer writer, boolean packed) throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBoolList(fieldNumber, value, packed); + } + } + + public static void writeStringList(int fieldNumber, List<String> value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeStringList(fieldNumber, value); + } + } + + public static void writeBytesList(int fieldNumber, List<ByteString> value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeBytesList(fieldNumber, value); + } + } + + public static void writeMessageList(int fieldNumber, List<?> value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeMessageList(fieldNumber, value); + } + } + + public static void writeMessageList(int fieldNumber, List<?> value, Writer writer, Schema schema) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeMessageList(fieldNumber, value, schema); + } + } + + public static void writeLazyFieldList(int fieldNumber, List<?> value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + for (Object item : value) { + ((LazyFieldLite) item).writeTo(writer, fieldNumber); + } + } + } + + public static void writeGroupList(int fieldNumber, List<?> value, Writer writer) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeGroupList(fieldNumber, value); + } + } + + public static void writeGroupList(int fieldNumber, List<?> value, Writer writer, Schema schema) + throws IOException { + if (value != null && !value.isEmpty()) { + writer.writeGroupList(fieldNumber, value, schema); + } + } + + static int computeSizeInt64ListNoTag(List<Long> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeInt64List(int fieldNumber, List<Long> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (list.size() * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeUInt64ListNoTag(List<Long> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeUInt64List(int fieldNumber, List<Long> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeUInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeSInt64ListNoTag(List<Long> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof LongArrayList) { + final LongArrayList primitiveList = (LongArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt64SizeNoTag(primitiveList.getLong(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt64SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeSInt64List(int fieldNumber, List<Long> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeSInt64ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeEnumListNoTag(List<Integer> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeEnumSizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeEnumSizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeEnumList(int fieldNumber, List<Integer> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeEnumListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeInt32ListNoTag(List<Integer> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeInt32List(int fieldNumber, List<Integer> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeUInt32ListNoTag(List<Integer> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeUInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeUInt32List(int fieldNumber, List<Integer> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = computeSizeUInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeSInt32ListNoTag(List<Integer> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = 0; + + if (list instanceof IntArrayList) { + final IntArrayList primitiveList = (IntArrayList) list; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt32SizeNoTag(primitiveList.getInt(i)); + } + } else { + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeSInt32SizeNoTag(list.get(i)); + } + } + return size; + } + + static int computeSizeSInt32List(int fieldNumber, List<Integer> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + + int size = computeSizeSInt32ListNoTag(list); + + if (packed) { + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(size); + } else { + return size + (length * CodedOutputStream.computeTagSize(fieldNumber)); + } + } + + static int computeSizeFixed32ListNoTag(List<?> list) { + return list.size() * WireFormat.FIXED32_SIZE; + } + + static int computeSizeFixed32List(int fieldNumber, List<?> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + int dataSize = length * WireFormat.FIXED32_SIZE; + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(dataSize); + } else { + return length * CodedOutputStream.computeFixed32Size(fieldNumber, 0); + } + } + + static int computeSizeFixed64ListNoTag(List<?> list) { + return list.size() * WireFormat.FIXED64_SIZE; + } + + static int computeSizeFixed64List(int fieldNumber, List<?> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + final int dataSize = length * WireFormat.FIXED64_SIZE; + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(dataSize); + } else { + return length * CodedOutputStream.computeFixed64Size(fieldNumber, 0); + } + } + + static int computeSizeBoolListNoTag(List<?> list) { + // bools are 1 byte varints + return list.size(); + } + + static int computeSizeBoolList(int fieldNumber, List<?> list, boolean packed) { + final int length = list.size(); + if (length == 0) { + return 0; + } + if (packed) { + // bools are 1 byte varints + return CodedOutputStream.computeTagSize(fieldNumber) + + CodedOutputStream.computeLengthDelimitedFieldSize(length); + } else { + return length * CodedOutputStream.computeBoolSize(fieldNumber, true); + } + } + + static int computeSizeStringList(int fieldNumber, List<?> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + if (list instanceof LazyStringList) { + LazyStringList lazyList = ((LazyStringList) list); + for (int i = 0; i < length; i++) { + Object value = lazyList.getRaw(i); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSizeNoTag((ByteString) value); + } else { + size += CodedOutputStream.computeStringSizeNoTag((String) value); + } + } + } else { + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof ByteString) { + size += CodedOutputStream.computeBytesSizeNoTag((ByteString) value); + } else { + size += CodedOutputStream.computeStringSizeNoTag((String) value); + } + } + } + return size; + } + + static int computeSizeMessage(int fieldNumber, Object value, Schema schema) { + if (value instanceof LazyFieldLite) { + return CodedOutputStream.computeLazyFieldSize(fieldNumber, (LazyFieldLite) value); + } else { + return CodedOutputStream.computeMessageSize(fieldNumber, (MessageLite) value, schema); + } + } + + static int computeSizeMessageList(int fieldNumber, List<?> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof LazyFieldLite) { + size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value); + } else { + size += CodedOutputStream.computeMessageSizeNoTag((MessageLite) value); + } + } + return size; + } + + static int computeSizeMessageList(int fieldNumber, List<?> list, Schema schema) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < length; i++) { + Object value = list.get(i); + if (value instanceof LazyFieldLite) { + size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value); + } else { + size += CodedOutputStream.computeMessageSizeNoTag((MessageLite) value, schema); + } + } + return size; + } + + static int computeSizeByteStringList(int fieldNumber, List<ByteString> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = length * CodedOutputStream.computeTagSize(fieldNumber); + for (int i = 0; i < list.size(); i++) { + size += CodedOutputStream.computeBytesSizeNoTag(list.get(i)); + } + return size; + } + + static int computeSizeGroupList(int fieldNumber, List<MessageLite> list) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = 0; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeGroupSize(fieldNumber, list.get(i)); + } + return size; + } + + static int computeSizeGroupList(int fieldNumber, List<MessageLite> list, Schema schema) { + final int length = list.size(); + if (length == 0) { + return 0; + } + int size = 0; + for (int i = 0; i < length; i++) { + size += CodedOutputStream.computeGroupSize(fieldNumber, list.get(i), schema); + } + return size; + } + + /** + * Determines whether to issue tableswitch or lookupswitch for the mergeFrom method. + * + * @see #shouldUseTableSwitch(int, int, int) + */ + public static boolean shouldUseTableSwitch(FieldInfo[] fields) { + // Determine whether to issue a tableswitch or a lookupswitch + // instruction. + if (fields.length == 0) { + return false; + } + + int lo = fields[0].getFieldNumber(); + int hi = fields[fields.length - 1].getFieldNumber(); + return shouldUseTableSwitch(lo, hi, fields.length); + } + + /** + * Determines whether to issue tableswitch or lookupswitch for the mergeFrom method. This is based + * on the <a href= + * "http://hg.openjdk.java.net/jdk8/jdk8/langtools/file/30db5e0aaf83/src/share/classes/com/sun/tools/javac/jvm/Gen.java#l1159"> + * logic in the JDK</a>. + * + * @param lo the lowest fieldNumber contained within the message. + * @param hi the higest fieldNumber contained within the message. + * @param numFields the total number of fields in the message. + * @return {@code true} if tableswitch should be used, rather than lookupswitch. + */ + public static boolean shouldUseTableSwitch(int lo, int hi, int numFields) { + if (hi < DEFAULT_LOOK_UP_START_NUMBER) { + return true; + } + long tableSpaceCost = ((long) hi - lo + 1); // words + long tableTimeCost = 3; // comparisons + long lookupSpaceCost = 3 + 2 * (long) numFields; + long lookupTimeCost = 3 + (long) numFields; + return tableSpaceCost + 3 * tableTimeCost <= lookupSpaceCost + 3 * lookupTimeCost; + } + + public static UnknownFieldSchema<?, ?> proto2UnknownFieldSetSchema() { + return PROTO2_UNKNOWN_FIELD_SET_SCHEMA; + } + + public static UnknownFieldSchema<?, ?> proto3UnknownFieldSetSchema() { + return PROTO3_UNKNOWN_FIELD_SET_SCHEMA; + } + + public static UnknownFieldSchema<?, ?> unknownFieldSetLiteSchema() { + return UNKNOWN_FIELD_SET_LITE_SCHEMA; + } + + private static UnknownFieldSchema<?, ?> getUnknownFieldSetSchema(boolean proto3) { + try { + Class<?> clz = getUnknownFieldSetSchemaClass(); + if (clz == null) { + return null; + } + return (UnknownFieldSchema) clz.getConstructor(boolean.class).newInstance(proto3); + } catch (Throwable t) { + return null; + } + } + + private static Class<?> getGeneratedMessageClass() { + try { + return Class.forName("com.google.protobuf.GeneratedMessageV3"); + } catch (Throwable e) { + return null; + } + } + + private static Class<?> getUnknownFieldSetSchemaClass() { + try { + return Class.forName("com.google.protobuf.UnknownFieldSetSchema"); + } catch (Throwable e) { + return null; + } + } + + static Object getMapDefaultEntry(Class<?> clazz, String name) { + try { + Class<?> holder = + Class.forName(clazz.getName() + "$" + toCamelCase(name, true) + "DefaultEntryHolder"); + Field[] fields = holder.getDeclaredFields(); + if (fields.length != 1) { + throw new IllegalStateException( + "Unable to look up map field default entry holder class for " + + name + + " in " + + clazz.getName()); + } + return UnsafeUtil.getStaticObject(fields[0]); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + static String toCamelCase(String name, boolean capNext) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < name.length(); ++i) { + char c = name.charAt(i); + // Matches protoc field name function: + if ('a' <= c && c <= 'z') { + if (capNext) { + sb.append((char) (c + ('A' - 'a'))); + } else { + sb.append(c); + } + capNext = false; + } else if ('A' <= c && c <= 'Z') { + if (i == 0 && !capNext) { + // Force first letter to lower-case unless explicitly told to capitalize it. + sb.append((char) (c - ('A' - 'a'))); + } else { + sb.append(c); + } + capNext = false; + } else if ('0' <= c && c <= '9') { + sb.append(c); + capNext = true; + } else { + capNext = true; + } + } + return sb.toString(); + } + + /** Returns true if both are null or both are {@link Object#equals}. */ + static boolean safeEquals(Object a, Object b) { + return a == b || (a != null && a.equals(b)); + } + + static <T> void mergeMap(MapFieldSchema mapFieldSchema, T message, T o, long offset) { + Object merged = + mapFieldSchema.mergeFrom( + UnsafeUtil.getObject(message, offset), UnsafeUtil.getObject(o, offset)); + UnsafeUtil.putObject(message, offset, merged); + } + + static <T, FT extends FieldDescriptorLite<FT>> void mergeExtensions( + ExtensionSchema<FT> schema, T message, T other) { + FieldSet<FT> otherExtensions = schema.getExtensions(other); + if (!otherExtensions.isEmpty()) { + FieldSet<FT> messageExtensions = schema.getMutableExtensions(message); + messageExtensions.mergeFrom(otherExtensions); + } + } + + static <T, UT, UB> void mergeUnknownFields( + UnknownFieldSchema<UT, UB> schema, T message, T other) { + UT messageUnknowns = schema.getFromMessage(message); + UT otherUnknowns = schema.getFromMessage(other); + UT merged = schema.merge(messageUnknowns, otherUnknowns); + schema.setToMessage(message, merged); + } + + /** Filters unrecognized enum values in a list. */ + static <UT, UB> UB filterUnknownEnumList( + int number, + List<Integer> enumList, + EnumLiteMap<?> enumMap, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) { + if (enumMap == null) { + return unknownFields; + } + // TODO(dweis): Specialize for IntArrayList to avoid boxing. + if (enumList instanceof RandomAccess) { + int writePos = 0; + int size = enumList.size(); + for (int readPos = 0; readPos < size; ++readPos) { + int enumValue = enumList.get(readPos); + if (enumMap.findValueByNumber(enumValue) != null) { + if (readPos != writePos) { + enumList.set(writePos, enumValue); + } + ++writePos; + } else { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + } + } + if (writePos != size) { + enumList.subList(writePos, size).clear(); + } + } else { + for (Iterator<Integer> it = enumList.iterator(); it.hasNext(); ) { + int enumValue = it.next(); + if (enumMap.findValueByNumber(enumValue) == null) { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + it.remove(); + } + } + } + return unknownFields; + } + + /** Filters unrecognized enum values in a list. */ + static <UT, UB> UB filterUnknownEnumList( + int number, + List<Integer> enumList, + EnumVerifier enumVerifier, + UB unknownFields, + UnknownFieldSchema<UT, UB> unknownFieldSchema) { + if (enumVerifier == null) { + return unknownFields; + } + // TODO(dweis): Specialize for IntArrayList to avoid boxing. + if (enumList instanceof RandomAccess) { + int writePos = 0; + int size = enumList.size(); + for (int readPos = 0; readPos < size; ++readPos) { + int enumValue = enumList.get(readPos); + if (enumVerifier.isInRange(enumValue)) { + if (readPos != writePos) { + enumList.set(writePos, enumValue); + } + ++writePos; + } else { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + } + } + if (writePos != size) { + enumList.subList(writePos, size).clear(); + } + } else { + for (Iterator<Integer> it = enumList.iterator(); it.hasNext(); ) { + int enumValue = it.next(); + if (!enumVerifier.isInRange(enumValue)) { + unknownFields = storeUnknownEnum(number, enumValue, unknownFields, unknownFieldSchema); + it.remove(); + } + } + } + return unknownFields; + } + + /** Stores an unrecognized enum value as an unknown value. */ + static <UT, UB> UB storeUnknownEnum( + int number, int enumValue, UB unknownFields, UnknownFieldSchema<UT, UB> unknownFieldSchema) { + if (unknownFields == null) { + unknownFields = unknownFieldSchema.newBuilder(); + } + unknownFieldSchema.addVarint(unknownFields, number, enumValue); + return unknownFields; + } +} diff --git a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java index 6bd65d6f4e..546e56e856 100644 --- a/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java +++ b/java/core/src/main/java/com/google/protobuf/SmallSortedMap.java @@ -136,6 +136,8 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { // The EntrySet is a stateless view of the Map. It's initialized the first // time it is requested and reused henceforth. private volatile EntrySet lazyEntrySet; + private Map<K, V> overflowEntriesDescending; + private volatile DescendingEntrySet lazyDescendingEntrySet; /** * @code arraySize Size of the array in which the lexicographically smallest mappings are stored. @@ -145,6 +147,7 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { this.maxArraySize = arraySize; this.entryList = Collections.emptyList(); this.overflowEntries = Collections.emptyMap(); + this.overflowEntriesDescending = Collections.emptyMap(); } /** Make this map immutable from this point forward. */ @@ -158,6 +161,10 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { overflowEntries.isEmpty() ? Collections.<K, V>emptyMap() : Collections.unmodifiableMap(overflowEntries); + overflowEntriesDescending = + overflowEntriesDescending.isEmpty() + ? Collections.<K, V>emptyMap() + : Collections.unmodifiableMap(overflowEntriesDescending); isImmutable = true; } } @@ -189,6 +196,11 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { : overflowEntries.entrySet(); } + Iterable<Map.Entry<K, V>> getOverflowEntriesDescending() { + return overflowEntriesDescending.isEmpty() + ? EmptySet.<Map.Entry<K, V>>iterable() + : overflowEntriesDescending.entrySet(); + } @Override public int size() { @@ -344,6 +356,12 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { return lazyEntrySet; } + Set<Map.Entry<K, V>> descendingEntrySet() { + if (lazyDescendingEntrySet == null) { + lazyDescendingEntrySet = new DescendingEntrySet(); + } + return lazyDescendingEntrySet; + } /** @throws UnsupportedOperationException if {@link #makeImmutable()} has has been called. */ private void checkMutable() { @@ -361,6 +379,7 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { checkMutable(); if (overflowEntries.isEmpty() && !(overflowEntries instanceof TreeMap)) { overflowEntries = new TreeMap<K, V>(); + overflowEntriesDescending = ((TreeMap<K, V>) overflowEntries).descendingMap(); } return (SortedMap<K, V>) overflowEntries; } @@ -501,6 +520,12 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { } } + private class DescendingEntrySet extends EntrySet { + @Override + public Iterator<java.util.Map.Entry<K, V>> iterator() { + return new DescendingEntryIterator(); + } + } /** * Iterator implementation that switches from the entry array to the overflow entries @@ -557,6 +582,46 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> { } } + /** + * Reverse Iterator implementation that switches from the entry array to the overflow entries + * appropriately. + */ + private class DescendingEntryIterator implements Iterator<Map.Entry<K, V>> { + + private int pos = entryList.size(); + private Iterator<Map.Entry<K, V>> lazyOverflowIterator; + + @Override + public boolean hasNext() { + return (pos > 0 && pos <= entryList.size()) || getOverflowIterator().hasNext(); + } + + @Override + public Map.Entry<K, V> next() { + if (getOverflowIterator().hasNext()) { + return getOverflowIterator().next(); + } + return entryList.get(--pos); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + /** + * It is important to create the overflow iterator only after the array entries have been + * iterated over because the overflow entry set changes when the client calls remove() on the + * array entries, which invalidates any existing iterators. + */ + private Iterator<Map.Entry<K, V>> getOverflowIterator() { + if (lazyOverflowIterator == null) { + lazyOverflowIterator = overflowEntriesDescending.entrySet().iterator(); + } + return lazyOverflowIterator; + } + } + /** * Helper class that holds immutable instances of an Iterable/Iterator that we return when the * overflow entries is empty. This eliminates the creation of an Iterator object when there is diff --git a/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java new file mode 100755 index 0000000000..a32b1430ee --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/StructuralMessageInfo.java @@ -0,0 +1,167 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.Internal.checkNotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Information for the layout of a protobuf message class. This describes all of the fields + * contained within a message. + */ +@ExperimentalApi +final class StructuralMessageInfo implements MessageInfo { + private final ProtoSyntax syntax; + private final boolean messageSetWireFormat; + private final int[] checkInitialized; + private final FieldInfo[] fields; + private final MessageLite defaultInstance; + + /** + * Constructor. + * + * @param checkInitialized fields to check in isInitialized(). + * @param fields the set of fields for the message, in field number order. + */ + StructuralMessageInfo( + ProtoSyntax syntax, + boolean messageSetWireFormat, + int[] checkInitialized, + FieldInfo[] fields, + Object defaultInstance) { + this.syntax = syntax; + this.messageSetWireFormat = messageSetWireFormat; + this.checkInitialized = checkInitialized; + this.fields = fields; + this.defaultInstance = (MessageLite) checkNotNull(defaultInstance, "defaultInstance"); + } + + /** Gets the syntax for the message (e.g. PROTO2, PROTO3). */ + @Override + public ProtoSyntax getSyntax() { + return syntax; + } + + /** Indicates whether or not the message should be represented with message set wire format. */ + @Override + public boolean isMessageSetWireFormat() { + return messageSetWireFormat; + } + + /** An array of field numbers that need to be checked for isInitialized(). */ + public int[] getCheckInitialized() { + return checkInitialized; + } + + /** + * Gets the information for all fields within this message, sorted in ascending order by their + * field number. + */ + public FieldInfo[] getFields() { + return fields; + } + + @Override + public MessageLite getDefaultInstance() { + return defaultInstance; + } + + /** Helper method for creating a new builder for {@link MessageInfo}. */ + public static Builder newBuilder() { + return new Builder(); + } + + /** Helper method for creating a new builder for {@link MessageInfo}. */ + public static Builder newBuilder(int numFields) { + return new Builder(numFields); + } + + /** A builder of {@link MessageInfo} instances. */ + public static final class Builder { + private final List<FieldInfo> fields; + private ProtoSyntax syntax; + private boolean wasBuilt; + private boolean messageSetWireFormat; + private int[] checkInitialized = null; + private Object defaultInstance; + + public Builder() { + fields = new ArrayList<FieldInfo>(); + } + + public Builder(int numFields) { + fields = new ArrayList<FieldInfo>(numFields); + } + + public void withDefaultInstance(Object defaultInstance) { + this.defaultInstance = defaultInstance; + } + + public void withSyntax(ProtoSyntax syntax) { + this.syntax = checkNotNull(syntax, "syntax"); + } + + public void withMessageSetWireFormat(boolean messageSetWireFormat) { + this.messageSetWireFormat = messageSetWireFormat; + } + + public void withCheckInitialized(int[] checkInitialized) { + this.checkInitialized = checkInitialized; + } + + public void withField(FieldInfo field) { + if (wasBuilt) { + throw new IllegalStateException("Builder can only build once"); + } + fields.add(field); + } + + public StructuralMessageInfo build() { + if (wasBuilt) { + throw new IllegalStateException("Builder can only build once"); + } + if (syntax == null) { + throw new IllegalStateException("Must specify a proto syntax"); + } + wasBuilt = true; + Collections.sort(fields); + return new StructuralMessageInfo( + syntax, + messageSetWireFormat, + checkInitialized, + fields.toArray(new FieldInfo[0]), + defaultInstance); + } + } +} diff --git a/java/core/src/main/java/com/google/protobuf/TextFormat.java b/java/core/src/main/java/com/google/protobuf/TextFormat.java index f00c812fc5..2326a05ffb 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormat.java @@ -1247,6 +1247,18 @@ public final class TextFormat { SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES; private TextFormatParseInfoTree.Builder parseInfoTreeBuilder = null; + /** + * Set whether this parser will allow unknown fields. By default, an exception is thrown if an + * unknown field is encountered. If this is set, the parser will only log a warning. Allow + * unknown fields will also allow unknown extensions. + * + * <p>Use of this parameter is discouraged which may hide some errors (e.g. + * spelling error on field name). + */ + public Builder setAllowUnknownFields(boolean allowUnknownFields) { + this.allowUnknownFields = allowUnknownFields; + return this; + } /** * Set whether this parser will allow unknown extensions. By default, an @@ -1448,14 +1460,15 @@ public final class TextFormat { extension = target.findExtensionByName(extensionRegistry, name.toString()); if (extension == null) { - String message = (tokenizer.getPreviousLine() + 1) - + ":" - + (tokenizer.getPreviousColumn() + 1) - + ":\t" - + type.getFullName() - + ".[" - + name - + "]"; + String message = + (tokenizer.getPreviousLine() + 1) + + ":" + + (tokenizer.getPreviousColumn() + 1) + + ":\t" + + type.getFullName() + + ".[" + + name + + "]"; unknownFields.add(new UnknownField(message, UnknownField.Type.EXTENSION)); } else { if (extension.descriptor.getContainingType() != type) { @@ -1653,24 +1666,18 @@ public final class TextFormat { endToken = "}"; } - Message defaultInstance = (extension == null) ? null : extension.defaultInstance; - MessageReflection.MergeTarget subField = - target.newMergeTargetForField(field, defaultInstance); + Message defaultInstance = (extension == null) ? null : extension.defaultInstance; + MessageReflection.MergeTarget subField = + target.newMergeTargetForField(field, defaultInstance); - while (!tokenizer.tryConsume(endToken)) { - if (tokenizer.atEnd()) { - throw tokenizer.parseException("Expected \"" + endToken + "\"."); + while (!tokenizer.tryConsume(endToken)) { + if (tokenizer.atEnd()) { + throw tokenizer.parseException("Expected \"" + endToken + "\"."); + } + mergeField(tokenizer, extensionRegistry, subField, parseTreeBuilder, unknownFields); } - mergeField( - tokenizer, - extensionRegistry, - subField, - parseTreeBuilder, - unknownFields); - } - - value = subField.finish(); + value = subField.finish(); } else { switch (field.getType()) { case INT32: @@ -1776,6 +1783,7 @@ public final class TextFormat { } } + /** Skips the next field including the field's name and value. */ private void skipField(Tokenizer tokenizer) throws ParseException { if (tokenizer.tryConsume("[")) { diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java new file mode 100755 index 0000000000..e736d5ce9c --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSchema.java @@ -0,0 +1,133 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; + +@ExperimentalApi +abstract class UnknownFieldSchema<T, B> { + + /** Whether unknown fields should be dropped. */ + abstract boolean shouldDiscardUnknownFields(Reader reader); + + /** Adds a varint value to the unknown fields. */ + abstract void addVarint(B fields, int number, long value); + + /** Adds a fixed32 value to the unknown fields. */ + abstract void addFixed32(B fields, int number, int value); + + /** Adds a fixed64 value to the unknown fields. */ + abstract void addFixed64(B fields, int number, long value); + + /** Adds a length delimited value to the unknown fields. */ + abstract void addLengthDelimited(B fields, int number, ByteString value); + + /** Adds a group value to the unknown fields. */ + abstract void addGroup(B fields, int number, T subFieldSet); + + /** Create a new builder for unknown fields. */ + abstract B newBuilder(); + + /** Returns an immutable instance of the field container. */ + abstract T toImmutable(B fields); + + /** + * Sets the unknown fields into the message. Caller must take care of the mutability of the + * fields. + */ + abstract void setToMessage(Object message, T fields); + + /** Get the unknown fields from the message. */ + abstract T getFromMessage(Object message); + + /** Returns a builder for unknown fields in the message. */ + abstract B getBuilderFromMessage(Object message); + + /** Sets an unknown field builder into the message. */ + abstract void setBuilderToMessage(Object message, B builder); + + /** Marks unknown fields as immutable. */ + abstract void makeImmutable(Object message); + + /** Merges one field into the unknown fields. */ + final boolean mergeOneFieldFrom(B unknownFields, Reader reader) throws IOException { + int tag = reader.getTag(); + int fieldNumber = WireFormat.getTagFieldNumber(tag); + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + addVarint(unknownFields, fieldNumber, reader.readInt64()); + return true; + case WireFormat.WIRETYPE_FIXED32: + addFixed32(unknownFields, fieldNumber, reader.readFixed32()); + return true; + case WireFormat.WIRETYPE_FIXED64: + addFixed64(unknownFields, fieldNumber, reader.readFixed64()); + return true; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + addLengthDelimited(unknownFields, fieldNumber, reader.readBytes()); + return true; + case WireFormat.WIRETYPE_START_GROUP: + final B subFields = newBuilder(); + int endGroupTag = WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); + mergeFrom(subFields, reader); + if (endGroupTag != reader.getTag()) { + throw InvalidProtocolBufferException.invalidEndTag(); + } + addGroup(unknownFields, fieldNumber, toImmutable(subFields)); + return true; + case WireFormat.WIRETYPE_END_GROUP: + return false; + default: + throw InvalidProtocolBufferException.invalidWireType(); + } + } + + final void mergeFrom(B unknownFields, Reader reader) throws IOException { + while (true) { + if (reader.getFieldNumber() == Reader.READ_DONE + || !mergeOneFieldFrom(unknownFields, reader)) { + break; + } + } + } + + abstract void writeTo(T unknownFields, Writer writer) throws IOException; + + abstract void writeAsMessageSetTo(T unknownFields, Writer writer) throws IOException; + + /** Merges {@code source} into {@code destination} and returns the merged instance. */ + abstract T merge(T destination, T source); + + /** Get the serialized size for message set serialization. */ + abstract int getSerializedSizeAsMessageSet(T message); + + abstract int getSerializedSize(T unknowns); +} diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java index 39deec0481..bde1303db7 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java @@ -59,6 +59,7 @@ public final class UnknownFieldSet implements MessageLite { private UnknownFieldSet() { fields = null; + fieldsDescending = null; } /** Create a new {@link Builder}. */ @@ -90,10 +91,14 @@ public final class UnknownFieldSet implements MessageLite { */ UnknownFieldSet(final Map<Integer, Field> fields, final Map<Integer, Field> fieldsDescending) { this.fields = fields; + this.fieldsDescending = fieldsDescending; } private final Map<Integer, Field> fields; + /** A copy of {@link #fields} who's iterator order is reversed. */ + private final Map<Integer, Field> fieldsDescending; + @Override public boolean equals(final Object other) { @@ -212,6 +217,35 @@ public final class UnknownFieldSet implements MessageLite { } } + /** Serializes the set and writes it to {@code writer}. */ + void writeTo(Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (Map.Entry<Integer, Field> entry : fieldsDescending.entrySet()) { + entry.getValue().writeTo(entry.getKey(), writer); + } + } else { + // Write fields in ascending order. + for (Map.Entry<Integer, Field> entry : fields.entrySet()) { + entry.getValue().writeTo(entry.getKey(), writer); + } + } + } + + /** Serializes the set and writes it to {@code writer} using {@code MessageSet} wire format. */ + void writeAsMessageSetTo(final Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (final Map.Entry<Integer, Field> entry : fieldsDescending.entrySet()) { + entry.getValue().writeAsMessageSetExtensionTo(entry.getKey(), writer); + } + } else { + // Write fields in ascending order. + for (final Map.Entry<Integer, Field> entry : fields.entrySet()) { + entry.getValue().writeAsMessageSetExtensionTo(entry.getKey(), writer); + } + } + } /** Get the number of bytes required to encode this set using {@code MessageSet} wire format. */ public int getSerializedSizeAsMessageSet() { @@ -328,6 +362,8 @@ public final class UnknownFieldSet implements MessageLite { result = getDefaultInstance(); } else { Map<Integer, Field> descendingFields = null; + descendingFields = + Collections.unmodifiableMap(((TreeMap<Integer, Field>) fields).descendingMap()); result = new UnknownFieldSet(Collections.unmodifiableMap(fields), descendingFields); } fields = null; @@ -344,6 +380,8 @@ public final class UnknownFieldSet implements MessageLite { public Builder clone() { getFieldBuilder(0); // Force lastField to be built. Map<Integer, Field> descendingFields = null; + descendingFields = + Collections.unmodifiableMap(((TreeMap<Integer, Field>) fields).descendingMap()); return UnknownFieldSet.newBuilder().mergeFrom(new UnknownFieldSet(fields, descendingFields)); } @@ -808,6 +846,47 @@ public final class UnknownFieldSet implements MessageLite { } } + /** Serializes the field, including field number, and writes it to {@code writer}. */ + void writeTo(final int fieldNumber, final Writer writer) throws IOException { + writer.writeInt64List(fieldNumber, varint, false); + writer.writeFixed32List(fieldNumber, fixed32, false); + writer.writeFixed64List(fieldNumber, fixed64, false); + writer.writeBytesList(fieldNumber, lengthDelimited); + + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + for (int i = 0; i < group.size(); i++) { + writer.writeStartGroup(fieldNumber); + group.get(i).writeTo(writer); + writer.writeEndGroup(fieldNumber); + } + } else { + for (int i = group.size() - 1; i >= 0; i--) { + writer.writeEndGroup(fieldNumber); + group.get(i).writeTo(writer); + writer.writeStartGroup(fieldNumber); + } + } + } + + /** + * Serializes the field, including field number, and writes it to {@code writer}, using {@code + * MessageSet} wire format. + */ + private void writeAsMessageSetExtensionTo(final int fieldNumber, final Writer writer) + throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write in descending field order. + ListIterator<ByteString> iter = lengthDelimited.listIterator(lengthDelimited.size()); + while (iter.hasPrevious()) { + writer.writeMessageSetItem(fieldNumber, iter.previous()); + } + } else { + // Write in ascending field order. + for (final ByteString value : lengthDelimited) { + writer.writeMessageSetItem(fieldNumber, value); + } + } + } /** * Get the number of bytes required to encode this field, including field number, using {@code diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java index 7c5ec6e98c..2f6315c80d 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java @@ -168,6 +168,72 @@ public final class UnknownFieldSetLite { } } + /** Serializes the set and writes it to {@code writer} using {@code MessageSet} wire format. */ + void writeAsMessageSetTo(Writer writer) throws IOException { + if (writer.fieldOrder() == Writer.FieldOrder.DESCENDING) { + // Write fields in descending order. + for (int i = count - 1; i >= 0; i--) { + int fieldNumber = WireFormat.getTagFieldNumber(tags[i]); + writer.writeMessageSetItem(fieldNumber, objects[i]); + } + } else { + // Write fields in ascending order. + for (int i = 0; i < count; i++) { + int fieldNumber = WireFormat.getTagFieldNumber(tags[i]); + writer.writeMessageSetItem(fieldNumber, objects[i]); + } + } + } + + /** Serializes the set and writes it to {@code writer}. */ + public void writeTo(Writer writer) throws IOException { + if (count == 0) { + return; + } + + // TODO: tags are not sorted, so there's no write order guarantees + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + for (int i = 0; i < count; ++i) { + writeField(tags[i], objects[i], writer); + } + } else { + for (int i = count - 1; i >= 0; --i) { + writeField(tags[i], objects[i], writer); + } + } + } + + private static void writeField(int tag, Object object, Writer writer) throws IOException { + int fieldNumber = WireFormat.getTagFieldNumber(tag); + switch (WireFormat.getTagWireType(tag)) { + case WireFormat.WIRETYPE_VARINT: + writer.writeInt64(fieldNumber, (Long) object); + break; + case WireFormat.WIRETYPE_FIXED32: + writer.writeFixed32(fieldNumber, (Integer) object); + break; + case WireFormat.WIRETYPE_FIXED64: + writer.writeFixed64(fieldNumber, (Long) object); + break; + case WireFormat.WIRETYPE_LENGTH_DELIMITED: + writer.writeBytes(fieldNumber, (ByteString) object); + break; + case WireFormat.WIRETYPE_START_GROUP: + if (writer.fieldOrder() == Writer.FieldOrder.ASCENDING) { + writer.writeStartGroup(fieldNumber); + ((UnknownFieldSetLite) object).writeTo(writer); + writer.writeEndGroup(fieldNumber); + } else { + writer.writeEndGroup(fieldNumber); + ((UnknownFieldSetLite) object).writeTo(writer); + writer.writeStartGroup(fieldNumber); + } + break; + default: + // TODO(liujisi): Change writeTo to throw IOException? + throw new RuntimeException(InvalidProtocolBufferException.invalidWireType()); + } + } /** * Get the number of bytes required to encode this field, including field number, using {@code diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java new file mode 100755 index 0000000000..ffd7232308 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLiteSchema.java @@ -0,0 +1,140 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; + +class UnknownFieldSetLiteSchema + extends UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite> { + + UnknownFieldSetLiteSchema() {} + + @Override + boolean shouldDiscardUnknownFields(Reader reader) { + // We never drop unknown fields in lite. + return false; + } + + @Override + UnknownFieldSetLite newBuilder() { + return UnknownFieldSetLite.newInstance(); + } + + @Override + void addVarint(UnknownFieldSetLite fields, int number, long value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_VARINT), value); + } + + @Override + void addFixed32(UnknownFieldSetLite fields, int number, int value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_FIXED32), value); + } + + @Override + void addFixed64(UnknownFieldSetLite fields, int number, long value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_FIXED64), value); + } + + @Override + void addLengthDelimited(UnknownFieldSetLite fields, int number, ByteString value) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED), value); + } + + @Override + void addGroup(UnknownFieldSetLite fields, int number, UnknownFieldSetLite subFieldSet) { + fields.storeField(WireFormat.makeTag(number, WireFormat.WIRETYPE_START_GROUP), subFieldSet); + } + + @Override + UnknownFieldSetLite toImmutable(UnknownFieldSetLite fields) { + fields.makeImmutable(); + return fields; + } + + @Override + void setToMessage(Object message, UnknownFieldSetLite fields) { + ((GeneratedMessageLite<?, ?>) message).unknownFields = fields; + } + + @Override + UnknownFieldSetLite getFromMessage(Object message) { + return ((GeneratedMessageLite<?, ?>) message).unknownFields; + } + + @Override + UnknownFieldSetLite getBuilderFromMessage(Object message) { + UnknownFieldSetLite unknownFields = getFromMessage(message); + // When parsing into a lite message object, its UnknownFieldSet is either the default instance + // or mutable. It can't be in a state where it's immutable but not default instance. + if (unknownFields == UnknownFieldSetLite.getDefaultInstance()) { + unknownFields = UnknownFieldSetLite.newInstance(); + setToMessage(message, unknownFields); + } + return unknownFields; + } + + @Override + void setBuilderToMessage(Object message, UnknownFieldSetLite fields) { + setToMessage(message, fields); + } + + @Override + void makeImmutable(Object message) { + getFromMessage(message).makeImmutable(); + } + + @Override + void writeTo(UnknownFieldSetLite fields, Writer writer) throws IOException { + fields.writeTo(writer); + } + + @Override + void writeAsMessageSetTo(UnknownFieldSetLite fields, Writer writer) throws IOException { + fields.writeAsMessageSetTo(writer); + } + + @Override + UnknownFieldSetLite merge(UnknownFieldSetLite message, UnknownFieldSetLite other) { + return other.equals(UnknownFieldSetLite.getDefaultInstance()) + ? message + : UnknownFieldSetLite.mutableCopyOf(message, other); + } + + @Override + int getSerializedSize(UnknownFieldSetLite unknowns) { + return unknowns.getSerializedSize(); + } + + @Override + int getSerializedSizeAsMessageSet(UnknownFieldSetLite unknowns) { + return unknowns.getSerializedSizeAsMessageSet(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java new file mode 100755 index 0000000000..b838c4b54a --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetSchema.java @@ -0,0 +1,132 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; + +class UnknownFieldSetSchema extends UnknownFieldSchema<UnknownFieldSet, UnknownFieldSet.Builder> { + + private final boolean proto3; + + public UnknownFieldSetSchema(boolean proto3) { + this.proto3 = proto3; + } + + @Override + boolean shouldDiscardUnknownFields(Reader reader) { + return reader.shouldDiscardUnknownFields(); + } + + @Override + UnknownFieldSet.Builder newBuilder() { + return UnknownFieldSet.newBuilder(); + } + + @Override + void addVarint(UnknownFieldSet.Builder fields, int number, long value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addVarint(value).build()); + } + + @Override + void addFixed32(UnknownFieldSet.Builder fields, int number, int value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addFixed32(value).build()); + } + + @Override + void addFixed64(UnknownFieldSet.Builder fields, int number, long value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addFixed64(value).build()); + } + + @Override + void addLengthDelimited(UnknownFieldSet.Builder fields, int number, ByteString value) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addLengthDelimited(value).build()); + } + + @Override + void addGroup(UnknownFieldSet.Builder fields, int number, UnknownFieldSet subFieldSet) { + fields.mergeField(number, UnknownFieldSet.Field.newBuilder().addGroup(subFieldSet).build()); + } + + @Override + UnknownFieldSet toImmutable(UnknownFieldSet.Builder fields) { + return fields.build(); + } + + @Override + void writeTo(UnknownFieldSet message, Writer writer) throws IOException { + message.writeTo(writer); + } + + @Override + void writeAsMessageSetTo(UnknownFieldSet message, Writer writer) throws IOException { + message.writeAsMessageSetTo(writer); + } + + @Override + UnknownFieldSet getFromMessage(Object message) { + return ((GeneratedMessageV3) message).unknownFields; + } + + @Override + void setToMessage(Object message, UnknownFieldSet fields) { + ((GeneratedMessageV3) message).unknownFields = fields; + } + + @Override + UnknownFieldSet.Builder getBuilderFromMessage(Object message) { + return ((GeneratedMessageV3) message).unknownFields.toBuilder(); + } + + @Override + void setBuilderToMessage(Object message, UnknownFieldSet.Builder builder) { + ((GeneratedMessageV3) message).unknownFields = builder.build(); + } + + @Override + void makeImmutable(Object message) { + // Already immutable. + } + + @Override + UnknownFieldSet merge(UnknownFieldSet message, UnknownFieldSet other) { + return message.toBuilder().mergeFrom(other).build(); + } + + @Override + int getSerializedSize(UnknownFieldSet message) { + return message.getSerializedSize(); + } + + @Override + int getSerializedSizeAsMessageSet(UnknownFieldSet unknowns) { + return unknowns.getSerializedSizeAsMessageSet(); + } +} diff --git a/java/core/src/main/java/com/google/protobuf/Writer.java b/java/core/src/main/java/com/google/protobuf/Writer.java new file mode 100755 index 0000000000..3f95c325de --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/Writer.java @@ -0,0 +1,219 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** A writer that performs serialization of protobuf message fields. */ +@ExperimentalApi +interface Writer { + + /** The order in which the fields are written by a {@link Writer}. */ + enum FieldOrder { + /** Fields are written in ascending order by field number. */ + ASCENDING, + + /** Fields are written in descending order by field number. */ + DESCENDING + } + + /** Indicates the order in which the fields are written by this {@link Writer}. */ + FieldOrder fieldOrder(); + + /** Writes a field of type {@link FieldType#SFIXED32}. */ + void writeSFixed32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#INT64}. */ + void writeInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#SFIXED64}. */ + void writeSFixed64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#FLOAT}. */ + void writeFloat(int fieldNumber, float value) throws IOException; + + /** Writes a field of type {@link FieldType#DOUBLE}. */ + void writeDouble(int fieldNumber, double value) throws IOException; + + /** Writes a field of type {@link FieldType#ENUM}. */ + void writeEnum(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#UINT64}. */ + void writeUInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#INT32}. */ + void writeInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#FIXED64}. */ + void writeFixed64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#FIXED32}. */ + void writeFixed32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#BOOL}. */ + void writeBool(int fieldNumber, boolean value) throws IOException; + + /** Writes a field of type {@link FieldType#STRING}. */ + void writeString(int fieldNumber, String value) throws IOException; + + /** Writes a field of type {@link FieldType#BYTES}. */ + void writeBytes(int fieldNumber, ByteString value) throws IOException; + + /** Writes a field of type {@link FieldType#UINT32}. */ + void writeUInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#SINT32}. */ + void writeSInt32(int fieldNumber, int value) throws IOException; + + /** Writes a field of type {@link FieldType#SINT64}. */ + void writeSInt64(int fieldNumber, long value) throws IOException; + + /** Writes a field of type {@link FieldType#MESSAGE}. */ + void writeMessage(int fieldNumber, Object value) throws IOException; + + /** Writes a field of type {@link FieldType#MESSAGE}. */ + void writeMessage(int fieldNumber, Object value, Schema schema) throws IOException; + + /** + * Writes a field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroup(int fieldNumber, Object value) throws IOException; + + /** + * Writes a field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroup(int fieldNumber, Object value, Schema schema) throws IOException; + + /** + * Writes a single start group tag. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeStartGroup(int fieldNumber) throws IOException; + + /** + * Writes a single end group tag. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeEndGroup(int fieldNumber) throws IOException; + + /** Writes a list field of type {@link FieldType#INT32}. */ + void writeInt32List(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FIXED32}. */ + void writeFixed32List(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#INT64}. */ + void writeInt64List(int fieldNumber, List<Long> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#UINT64}. */ + void writeUInt64List(int fieldNumber, List<Long> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FIXED64}. */ + void writeFixed64List(int fieldNumber, List<Long> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#FLOAT}. */ + void writeFloatList(int fieldNumber, List<Float> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#DOUBLE}. */ + void writeDoubleList(int fieldNumber, List<Double> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#ENUM}. */ + void writeEnumList(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#BOOL}. */ + void writeBoolList(int fieldNumber, List<Boolean> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#STRING}. */ + void writeStringList(int fieldNumber, List<String> value) throws IOException; + + /** Writes a list field of type {@link FieldType#BYTES}. */ + void writeBytesList(int fieldNumber, List<ByteString> value) throws IOException; + + /** Writes a list field of type {@link FieldType#UINT32}. */ + void writeUInt32List(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SFIXED32}. */ + void writeSFixed32List(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SFIXED64}. */ + void writeSFixed64List(int fieldNumber, List<Long> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SINT32}. */ + void writeSInt32List(int fieldNumber, List<Integer> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#SINT64}. */ + void writeSInt64List(int fieldNumber, List<Long> value, boolean packed) throws IOException; + + /** Writes a list field of type {@link FieldType#MESSAGE}. */ + void writeMessageList(int fieldNumber, List<?> value) throws IOException; + + /** Writes a list field of type {@link FieldType#MESSAGE}. */ + void writeMessageList(int fieldNumber, List<?> value, Schema schema) throws IOException; + + /** + * Writes a list field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroupList(int fieldNumber, List<?> value) throws IOException; + + /** + * Writes a list field of type {@link FieldType#GROUP}. + * + * @deprecated groups fields are deprecated. + */ + @Deprecated + void writeGroupList(int fieldNumber, List<?> value, Schema schema) throws IOException; + + /** + * Writes a message field in {@code MessageSet} wire-format. + * + * @param value A message instance or an opaque {@link ByteString} for an unknown field. + */ + void writeMessageSetItem(int fieldNumber, Object value) throws IOException; + + /** Writes a map field. */ + <K, V> void writeMap(int fieldNumber, MapEntryLite.Metadata<K, V> metadata, Map<K, V> map) + throws IOException; +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java new file mode 100755 index 0000000000..5d3fd3bd69 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto2LiteSchemaTest.java @@ -0,0 +1,199 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.TestEnum; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto2MessageLite}. */ +public abstract class AbstractProto2LiteSchemaTest extends AbstractSchemaTest<Proto2MessageLite> { + + @Override + protected Proto2MessageLiteFactory messageFactory() { + return new Proto2MessageLiteFactory(10, 20, 2, 2); + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto2MessageLite message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto2MessageLite message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto2MessageLite message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto2MessageLite merged = + ExperimentalSerializationUtil.fromByteArray(data, Proto2MessageLite.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + roundtrip( + "Field 69", + newBuilder() + .setFieldGroup69( + Proto2MessageLite.FieldGroup69.newBuilder().setFieldInt3270(data().getInt())) + .build()); + } + + private Proto2MessageLite.Builder newBuilder() { + return messageFactory().newMessage().toBuilder(); + } + + @Override + protected List<Proto2MessageLite> newMessagesMissingRequiredFields() { + return messageFactory().newMessagesMissingRequiredFields(); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto2MessageLiteWithMaps", + new Proto2MessageLiteFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto2MessageLiteWithMaps.class)); + } + + @Test + public void unknownFieldsUnrecognized() throws Exception { + Proto2MessageLite expectedMessage = messageFactory().newMessage(); + byte[] serializedBytes = expectedMessage.toByteArray(); + Proto2EmptyLite empty = + ExperimentalSerializationUtil.fromByteArray(serializedBytes, Proto2EmptyLite.class); + + // Merge serialized bytes into an empty message, then reserialize and merge it to a new + // Proto2Message. Make sure the two messages equal. + byte[] roundtripBytes = ExperimentalSerializationUtil.toByteArray(empty); + Proto2MessageLite roundtripMessage = + ExperimentalSerializationUtil.fromByteArray(roundtripBytes, Proto2MessageLite.class); + assertEquals(expectedMessage, roundtripMessage); + } + + @Test + public void unknownEnum() throws IOException { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2MessageLite.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2MessageLite parsed = + ExperimentalSerializationUtil.fromByteArray(output, Proto2MessageLite.class); + assertFalse("out-of-range singular enum should not be in message", parsed.hasFieldEnum13()); + assertEquals( + "out-of-range repeated enum should not be in message", 2, parsed.getFieldEnumList30Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumList30(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumList30(1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getFieldEnumListPacked44Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumListPacked44(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumListPacked44(1)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java new file mode 100755 index 0000000000..0c16818666 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java @@ -0,0 +1,224 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto2Message}. */ +public abstract class AbstractProto2SchemaTest extends AbstractSchemaTest<Proto2Message> { + + @Override + protected Proto2MessageFactory messageFactory() { + return new Proto2MessageFactory(10, 20, 2, 2); + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto2Message message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto2Message message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto2Message message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto2Message merged = ExperimentalSerializationUtil.fromByteArray(data, Proto2Message.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + roundtrip( + "Field 69", + newBuilder() + .setFieldGroup69( + Proto2Message.FieldGroup69.newBuilder().setFieldInt3270(data().getInt())) + .build()); + } + + private Proto2Message.Builder newBuilder() { + return messageFactory().newMessage().toBuilder(); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto2MessageWithMaps", + new Proto2MessageFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto2MessageWithMaps.class)); + } + + @Test + public void unknownFieldsUnrecognized() throws Exception { + Proto2Message expectedMessage = messageFactory().newMessage(); + byte[] serializedBytes = expectedMessage.toByteArray(); + Proto2Empty empty = + ExperimentalSerializationUtil.fromByteArray(serializedBytes, Proto2Empty.class); + + // Merge serialized bytes into an empty message, then reserialize and merge it to a new + // Proto2Message. Make sure the two messages equal. + byte[] roundtripBytes = ExperimentalSerializationUtil.toByteArray(empty); + assertEquals(serializedBytes.length, roundtripBytes.length); + Proto2Message roundtripMessage = + ExperimentalSerializationUtil.fromByteArray(roundtripBytes, Proto2Message.class); + assertEquals(expectedMessage, roundtripMessage); + } + + @Test + public void unknownEnum() throws IOException { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2Message parsed = ExperimentalSerializationUtil.fromByteArray(output, Proto2Message.class); + assertFalse("out-of-range singular enum should not be in message", parsed.hasFieldEnum13()); + { + List<Long> singularEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, singularEnum.size()); + assertEquals((Long) (long) outOfRange, singularEnum.get(0)); + } + { + List<Long> repeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, repeatedEnum.size()); + assertEquals((Long) (long) outOfRange, repeatedEnum.get(0)); + } + { + List<Long> packedRepeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, packedRepeatedEnum.size()); + assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0)); + } + assertEquals( + "out-of-range repeated enum should not be in message", 2, parsed.getFieldEnumList30Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumList30(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumList30(1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getFieldEnumListPacked44Count()); + assertEquals(TestEnum.ONE, parsed.getFieldEnumListPacked44(0)); + assertEquals(TestEnum.TWO, parsed.getFieldEnumListPacked44(1)); + } + + @Override + protected List<Proto2Message> newMessagesMissingRequiredFields() { + return messageFactory().newMessagesMissingRequiredFields(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java new file mode 100755 index 0000000000..9cc04ec7ce --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto3LiteSchemaTest.java @@ -0,0 +1,143 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto3MessageLite}. */ +public abstract class AbstractProto3LiteSchemaTest extends AbstractSchemaTest<Proto3MessageLite> { + @Override + protected Proto3MessageLiteFactory messageFactory() { + return new Proto3MessageLiteFactory(10, 20, 2, 2); + } + + @Override + protected List<ByteBuffer> serializedBytesWithInvalidUtf8() throws IOException { + List<ByteBuffer> invalidBytes = new ArrayList<>(); + byte[] invalid = new byte[] {(byte) 0x80}; + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_9_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_LIST_26_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + return invalidBytes; + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto3MessageLite message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto3MessageLite message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto3MessageLite message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto3MessageLite merged = + ExperimentalSerializationUtil.fromByteArray(data, Proto3MessageLite.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + } + + @Test + public void retainUnknownFields() { + // Unknown fields are retained in lite runtime. + Proto3MessageLite expectedMessage = messageFactory().newMessage(); + Proto3EmptyLite empty = + ExperimentalSerializationUtil.fromByteArray( + expectedMessage.toByteArray(), Proto3EmptyLite.class); + assertEquals(expectedMessage.getSerializedSize(), empty.getSerializedSize()); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto3MessageLiteWithMaps", + new Proto3MessageLiteFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto3MessageLiteWithMaps.class)); + } + + private static Proto3MessageLite.Builder newBuilder() { + return Proto3MessageLite.newBuilder(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java new file mode 100755 index 0000000000..358f1e3b14 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractProto3SchemaTest.java @@ -0,0 +1,151 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +/** Base class for tests using {@link Proto3Message}. */ +public abstract class AbstractProto3SchemaTest extends AbstractSchemaTest<Proto3Message> { + @Override + protected Proto3MessageFactory messageFactory() { + return new Proto3MessageFactory(10, 20, 2, 2); + } + + @Override + protected List<ByteBuffer> serializedBytesWithInvalidUtf8() throws IOException { + List<ByteBuffer> invalidBytes = new ArrayList<>(); + byte[] invalid = new byte[] {(byte) 0x80}; + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3Message.FIELD_STRING_9_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + { + ByteBuffer buffer = ByteBuffer.allocate(100); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(buffer); + codedOutput.writeByteArray(Proto3Message.FIELD_STRING_LIST_26_FIELD_NUMBER, invalid); + codedOutput.flush(); + buffer.flip(); + invalidBytes.add(buffer); + } + return invalidBytes; + } + + @Test + public void mergeOptionalMessageFields() throws Exception { + Proto3Message message1 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build()) + .build(); + Proto3Message message2 = + newBuilder() + .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build()) + .build(); + Proto3Message message3 = + newBuilder() + .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build()) + .build(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + message1.writeTo(output); + message2.writeTo(output); + message3.writeTo(output); + byte[] data = output.toByteArray(); + + Proto3Message merged = ExperimentalSerializationUtil.fromByteArray(data, Proto3Message.class); + assertEquals(789, merged.getFieldMessage10().getFieldInt643()); + assertEquals(456, merged.getFieldMessage10().getFieldInt325()); + } + + @Test + public void oneofFieldsShouldRoundtrip() throws IOException { + roundtrip("Field 53", newBuilder().setFieldDouble53(100).build()); + roundtrip("Field 54", newBuilder().setFieldFloat54(100).build()); + roundtrip("Field 55", newBuilder().setFieldInt6455(100).build()); + roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build()); + roundtrip("Field 57", newBuilder().setFieldInt3257(100).build()); + roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build()); + roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build()); + roundtrip("Field 60", newBuilder().setFieldBool60(true).build()); + roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build()); + roundtrip( + "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build()); + roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build()); + roundtrip("Field 64", newBuilder().setFieldUint3264(100).build()); + roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build()); + roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build()); + roundtrip("Field 67", newBuilder().setFieldSint3267(100).build()); + roundtrip("Field 68", newBuilder().setFieldSint6468(100).build()); + } + + @Test + public void preserveUnknownFields() { + Proto3Message expectedMessage = messageFactory().newMessage(); + Proto3Empty empty = + ExperimentalSerializationUtil.fromByteArray( + expectedMessage.toByteArray(), Proto3Empty.class); + assertEquals(expectedMessage.getSerializedSize(), empty.getSerializedSize()); + assertEquals(expectedMessage.toByteString(), empty.toByteString()); + } + + @Test + public void preserveUnknownFieldsProto2() { + // Make sure we will be able to preserve valid proto2 wireformat, including those that are not + // supported in proto3, e.g. groups. + byte[] payload = new Proto2MessageFactory(10, 20, 2, 2).newMessage().toByteArray(); + Proto3Empty empty = ExperimentalSerializationUtil.fromByteArray(payload, Proto3Empty.class); + assertEquals(payload.length, empty.getSerializedSize()); + } + + @Test + public void mapsShouldRoundtrip() throws IOException { + roundtrip( + "Proto3MessageWithMaps", + new Proto3MessageFactory(2, 10, 2, 2).newMessageWithMaps(), + Protobuf.getInstance().schemaFor(Proto3MessageWithMaps.class)); + } + + private static Proto3Message.Builder newBuilder() { + return Proto3Message.newBuilder(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java b/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java new file mode 100755 index 0000000000..c69a4fd254 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/AbstractSchemaTest.java @@ -0,0 +1,157 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractSchemaTest<T extends MessageLite> { + private Schema<T> schema; + + @Before + public void setup() { + schema = schema(); + registerSchemas(); + } + + // Subclass should override this method if it needs to register more than one schemas. + protected void registerSchemas() { + // Register this schema with the runtime to support processing of nested messages. + Protobuf.getInstance().registerSchemaOverride(schema.newInstance().getClass(), schema); + } + + protected abstract Schema<T> schema(); + + protected abstract ExperimentalMessageFactory<? extends T> messageFactory(); + + @SuppressWarnings("unused") + protected List<ByteBuffer> serializedBytesWithInvalidUtf8() throws IOException { + return Collections.emptyList(); + } + + @Test + public void randomMessageShouldRoundtrip() throws IOException { + roundtrip("", messageFactory().newMessage()); + } + + @Test + public void invalidUtf8StringParsing() throws IOException { + for (ByteBuffer invalidUtf8Bytes : serializedBytesWithInvalidUtf8()) { + Reader reader = BinaryReader.newInstance(invalidUtf8Bytes, /* bufferIsImmutable= */ true); + + T newMsg = schema.newInstance(); + try { + schema.mergeFrom(newMsg, reader, ExtensionRegistryLite.getEmptyRegistry()); + fail("should throw invalid "); + } catch (InvalidProtocolBufferException expected) { + } + } + } + + @Test + public void mergeFromByteArrayFastPathMayThrowIndexOutOfBoundsException() throws IOException { + if (!Android.isOnAndroidDevice()) { + // Skip this test if not on Android. + return; + } + byte[] data = messageFactory().newMessage().toByteArray(); + int exceptionCount = 0; + for (int i = 0; i <= data.length; i++) { + byte[] truncatedData = Arrays.copyOf(data, i); + try { + T message = schema.newInstance(); + // Test that this method throws the expected exceptions. + schema.mergeFrom(message, truncatedData, 0, i, new ArrayDecoders.Registers()); + } catch (InvalidProtocolBufferException e) { + // Ignore expected exceptions. + } catch (IndexOutOfBoundsException e) { + exceptionCount += 1; + } + } + assertNotEquals(0, exceptionCount); + } + + protected static final <M extends MessageLite> void roundtrip( + String failureMessage, M msg, Schema<M> schema) throws IOException { + byte[] serializedBytes = ExperimentalSerializationUtil.toByteArray(msg, schema); + assertEquals(failureMessage, msg.getSerializedSize(), serializedBytes.length); + + // Now read it back in and verify it matches the original. + if (Android.isOnAndroidDevice()) { + // Test the fast path on Android. + M newMsg = schema.newInstance(); + schema.mergeFrom( + newMsg, serializedBytes, 0, serializedBytes.length, new ArrayDecoders.Registers()); + schema.makeImmutable(newMsg); + assertEquals(failureMessage, msg, newMsg); + } + M newMsg = schema.newInstance(); + Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(serializedBytes), true); + schema.mergeFrom(newMsg, reader, ExtensionRegistryLite.getEmptyRegistry()); + schema.makeImmutable(newMsg); + + assertEquals(failureMessage, msg, newMsg); + } + + protected final void roundtrip(String failureMessage, T msg) throws IOException { + roundtrip(failureMessage, msg, schema); + } + + protected final ExperimentalTestDataProvider data() { + return messageFactory().dataProvider(); + } + + protected List<T> newMessagesMissingRequiredFields() { + return Collections.emptyList(); + } + + @SuppressWarnings("unchecked") + @Test + public void testRequiredFields() throws Exception { + for (T msg : newMessagesMissingRequiredFields()) { + if (schema.isInitialized(msg)) { + assertEquals("", msg.toString()); + msg = (T) msg.toBuilder().build(); + } + assertFalse(schema.isInitialized(msg)); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java b/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java new file mode 100755 index 0000000000..037173b667 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java @@ -0,0 +1,236 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.ArrayDecoders.Registers; +import java.io.IOException; +import junit.framework.TestCase; + +public class ArrayDecodersTest extends TestCase { + + private static final int TAG = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED); + private static final ByteString NEGATIVE_SIZE_0 = generateNegativeLength(0); + private static final ByteString NEGATIVE_SIZE_1 = generateNegativeLength(1); + + private Registers registers; + + @Override + public void setUp() { + registers = new Registers(); + registers.int1 = TAG; + } + + public void testException_decodeString() { + try { + ArrayDecoders.decodeString(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringRequireUtf8() { + try { + ArrayDecoders.decodeStringRequireUtf8(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytes() { + try { + ArrayDecoders.decodeBytes(NEGATIVE_SIZE_0.toByteArray(), 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringList_first() { + try { + ArrayDecoders.decodeStringList( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringList_second() { + try { + ArrayDecoders.decodeStringList( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringListRequireUtf8_first() { + try { + ArrayDecoders.decodeStringListRequireUtf8( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeStringListRequireUtf8_second() { + try { + ArrayDecoders.decodeStringListRequireUtf8( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytesList_first() { + try { + ArrayDecoders.decodeBytesList( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeBytesList_second() { + try { + ArrayDecoders.decodeBytesList( + TAG, + NEGATIVE_SIZE_1.toByteArray(), + 0, + NEGATIVE_SIZE_1.size(), + new ProtobufArrayList<Object>(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeUnknownField() { + try { + ArrayDecoders.decodeUnknownField( + TAG, + NEGATIVE_SIZE_0.toByteArray(), + 0, + NEGATIVE_SIZE_0.size(), + UnknownFieldSetLite.newInstance(), + registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + public void testException_decodeHugeField() { + byte[] badBytes = + new byte[] { + (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xEF, 0x73, 0x74, 0x69, 0x6E, 0x67 + }; + try { + ArrayDecoders.decodeUnknownField( + TAG, badBytes, 0, badBytes.length, UnknownFieldSetLite.newInstance(), registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + + try { + ArrayDecoders.decodeBytes(badBytes, 0, registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + + byte[] badBytesList = + new byte[] { + 0x01, + 0x77, + 0x0A, + (byte) 0x80, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xEF, + 0x73, + 0x74, + 0x69, + 0x6E, + 0x67 + }; + try { + ArrayDecoders.decodeBytesList( + TAG, badBytesList, 0, badBytes.length, new ProtobufArrayList<>(), registers); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + + private static ByteString generateNegativeLength(int count) { + try { + ByteString.Output byteStringOutput = ByteString.newOutput(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(byteStringOutput); + + // Write out count - 1 valid 0 length fields; we only write out tags after the field since + // ArrayDecoders expects the first tag to already have been parsed. + for (int i = 0; i < count; i++) { + codedOutput.writeInt32NoTag(0); + codedOutput.writeInt32NoTag(TAG); + } + + // Write out a negative length + codedOutput.writeInt32NoTag(-1); + + codedOutput.flush(); + + return byteStringOutput.toByteString(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java b/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java new file mode 100755 index 0000000000..cda2998f5d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java @@ -0,0 +1,90 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class BinaryProtocolTest { + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance() + .registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema); + } + + @Test + public void proto3Roundtrip() throws Exception { + Proto3Message expected = new Proto3MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto3Message result = + ExperimentalSerializationUtil.fromByteArray(expectedBytes, Proto3Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = ExperimentalSerializationUtil.toByteArray(result); + Assert.assertEquals(expectedBytes.length, actualBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto3Message actual = Proto3Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + @Test + public void proto2Roundtrip() throws Exception { + Proto2Message expected = new Proto2MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto2Message result = + ExperimentalSerializationUtil.fromByteArray(expectedBytes, Proto2Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = ExperimentalSerializationUtil.toByteArray(result); + Assert.assertEquals(expectedBytes.length, actualBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto2Message actual = Proto2Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java b/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java new file mode 100755 index 0000000000..96319d93c5 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/CachedFieldSizeTest.java @@ -0,0 +1,65 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import protobuf_unittest.UnittestProto.TestPackedTypes; +import proto3_unittest.UnittestProto3; +import protobuf_unittest.TestCachedFieldSizeMessage; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class CachedFieldSizeTest { + // Regression test for b/74087933 + @Test + public void testCachedFieldSize() throws Exception { + TestCachedFieldSizeMessage.Builder builder = TestCachedFieldSizeMessage.newBuilder(); + builder.setProto2Child(TestUtil.getPackedSet()); + builder.setProto3Child( + UnittestProto3.TestPackedTypes.parseFrom(TestUtil.getPackedSet().toByteArray())); + TestCachedFieldSizeMessage message = builder.build(); + + // Serialize once to cache all field sizes. This will use the experimental runtime because + // the proto has optimize_for = CODE_SIZE. + message.toByteArray(); + // Serialize individual submessages. This will use the generated implementation. If the + // experimental runtime hasn't set the correct cached size, this will throw an exception. + byte[] data2 = message.getProto2Child().toByteArray(); + byte[] data3 = message.getProto3Child().toByteArray(); + + // Make sure the serialized data is correct. + assertEquals(message.getProto2Child(), TestPackedTypes.parseFrom(data2)); + assertEquals(message.getProto3Child(), UnittestProto3.TestPackedTypes.parseFrom(data3)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java b/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java new file mode 100755 index 0000000000..a24b48b51e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java @@ -0,0 +1,110 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import java.io.IOException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class CodedAdapterTest { + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance() + .registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema); + } + + @Test + public void proto3Roundtrip() throws Exception { + Proto3Message expected = new Proto3MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto3Message result = fromByteArray(expectedBytes, Proto3Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = toByteArray(result, expectedBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto3Message actual = Proto3Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + @Test + public void proto2Roundtrip() throws Exception { + Proto2Message expected = new Proto2MessageFactory(5, 10, 2, 2).newMessage(); + byte[] expectedBytes = expected.toByteArray(); + + // Deserialize with BinaryReader and verify that the message matches the original. + Proto2Message result = fromByteArray(expectedBytes, Proto2Message.class); + assertEquals(expected, result); + + // Now write it back out using BinaryWriter and verify the output length. + byte[] actualBytes = toByteArray(result, expectedBytes.length); + + // Read back in the bytes and verify that it matches the original message. + Proto2Message actual = Proto2Message.parseFrom(actualBytes); + assertEquals(expected, actual); + } + + public static <T> byte[] toByteArray(T msg, int size) throws Exception { + Schema<T> schema = Protobuf.getInstance().schemaFor(msg); + byte[] out = new byte[size]; + CodedOutputStreamWriter writer = + CodedOutputStreamWriter.forCodedOutput(CodedOutputStream.newInstance(out)); + schema.writeTo(msg, writer); + assertEquals(out.length, writer.getTotalBytesWritten()); + return out; + } + + public static <T> T fromByteArray(byte[] data, Class<T> messageType) { + Schema<T> schema = Protobuf.getInstance().schemaFor(messageType); + try { + T msg = schema.newInstance(); + schema.mergeFrom( + msg, + CodedInputStreamReader.forCodedInput(CodedInputStream.newInstance(data)), + ExtensionRegistryLite.EMPTY_REGISTRY_LITE); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java index 9c5d6dafb7..532052cdb3 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java @@ -44,6 +44,7 @@ import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import junit.framework.TestCase; /** @@ -1195,4 +1196,59 @@ public class CodedInputStreamTest extends TestCase { // Expected } } + + public void testMaliciousInputStream() throws Exception { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream); + codedOutputStream.writeByteArrayNoTag(new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }); + codedOutputStream.flush(); + final List<byte[]> maliciousCapture = new ArrayList<>(); + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()) { + @Override + public synchronized int read(byte[] b, int off, int len) { + maliciousCapture.add(b); + return super.read(b, off, len); + } + }; + + // test ByteString + + CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream, 1); + ByteString byteString = codedInputStream.readBytes(); + assertEquals(0x0, byteString.byteAt(0)); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x0, byteString.byteAt(0)); + + // test ByteBuffer + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + ByteBuffer byteBuffer = codedInputStream.readByteBuffer(); + assertEquals(0x0, byteBuffer.get(0)); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x0, byteBuffer.get(0)); + + + // test byte[] + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + byte[] byteArray = codedInputStream.readByteArray(); + assertEquals(0x0, byteArray[0]); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x9, byteArray[0]); // MODIFICATION! Should we fix? + + // test rawBytes + + inputStream.reset(); + maliciousCapture.clear(); + codedInputStream = CodedInputStream.newInstance(inputStream, 1); + int length = codedInputStream.readRawVarint32(); + byteArray = codedInputStream.readRawBytes(length); + assertEquals(0x0, byteArray[0]); + maliciousCapture.get(1)[0] = 0x9; + assertEquals(0x9, byteArray[0]); // MODIFICATION! Should we fix? + } } diff --git a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java index 5f91324019..c222ff3775 100644 --- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import protobuf_unittest.NestedExtension; +import protobuf_unittest.NonNestedExtension; import com.google.protobuf.DescriptorProtos.DescriptorProto; import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; @@ -779,4 +781,23 @@ public class DescriptorsTest extends TestCase { assertEquals("FIELDNAME5", d.getFields().get(4).getJsonName()); assertEquals("@type", d.getFields().get(5).getJsonName()); } + + public void testExtensionRenamesKeywords() { + assertTrue(NonNestedExtension.if_ instanceof GeneratedMessage.GeneratedExtension); + assertTrue( + NestedExtension.MyNestedExtension.default_ + instanceof GeneratedMessage.GeneratedExtension); + + NonNestedExtension.MessageToBeExtended msg = + NonNestedExtension.MessageToBeExtended.newBuilder() + .setExtension(NonNestedExtension.if_, "!fi") + .build(); + assertEquals("!fi", msg.getExtension(NonNestedExtension.if_)); + + msg = + NonNestedExtension.MessageToBeExtended.newBuilder() + .setExtension(NestedExtension.MyNestedExtension.default_, 8) + .build(); + assertEquals(8, msg.getExtension(NestedExtension.MyNestedExtension.default_).intValue()); + } } diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java b/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java new file mode 100755 index 0000000000..60c8609cf4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalMessageFactory.java @@ -0,0 +1,40 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +/** Interface for a test factory for messages. */ +public interface ExperimentalMessageFactory<T extends MessageLite> { + /** Creates a new random message instance. */ + T newMessage(); + + /** Gets the underlying data provider. */ + ExperimentalTestDataProvider dataProvider(); +} diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java b/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java new file mode 100755 index 0000000000..05aaa67521 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalSerializationUtil.java @@ -0,0 +1,113 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Queue; + +/** Utilities for serialization. */ +public class ExperimentalSerializationUtil { + + /** + * Serializes the given message to a byte array using {@link com.google.protobuf.BinaryWriter}. + */ + public static <T> byte[] toByteArray(T msg) throws IOException { + return toByteArray(msg, Protobuf.getInstance().schemaFor(msg)); + } + + /** + * Serializes the given message to a byte array using {@link com.google.protobuf.BinaryWriter} + * with a customized Schema. + */ + public static <T> byte[] toByteArray(T msg, Schema<T> schema) throws IOException { + BinaryWriter writer = BinaryWriter.newHeapInstance(BufferAllocator.unpooled()); + schema.writeTo(msg, writer); + + byte[] out = new byte[writer.getTotalBytesWritten()]; + int outPos = 0; + Queue<AllocatedBuffer> buffers = writer.complete(); + while (true) { + AllocatedBuffer buffer = buffers.poll(); + if (buffer == null) { + break; + } + int length = buffer.limit() - buffer.position(); + System.arraycopy( + buffer.array(), buffer.arrayOffset() + buffer.position(), out, outPos, length); + outPos += length; + } + if (out.length != outPos) { + throw new IllegalArgumentException("Failed to serialize test message"); + } + return out; + } + + /** Deserializes a message from the given byte array. */ + public static <T> T fromByteArray(byte[] data, Class<T> messageType) { + if (Android.isOnAndroidDevice()) { + return fromByteArrayFastPath(data, messageType); + } else { + return fromByteArray(data, messageType, ExtensionRegistryLite.getEmptyRegistry()); + } + } + + /** + * Deserializes a message from the given byte array using {@link com.google.protobuf.BinaryReader} + * with an extension registry and a customized Schema. + */ + public static <T> T fromByteArray( + byte[] data, Class<T> messageType, ExtensionRegistryLite extensionRegistry) { + try { + Schema<T> schema = Protobuf.getInstance().schemaFor(messageType); + T msg = schema.newInstance(); + schema.mergeFrom( + msg, BinaryReader.newInstance(ByteBuffer.wrap(data), true), extensionRegistry); + schema.makeImmutable(msg); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** Deserializes a lite message from the given byte array using fast path. */ + private static <T> T fromByteArrayFastPath(byte[] data, Class<T> messageType) { + try { + Schema<T> schema = Protobuf.getInstance().schemaFor(messageType); + T msg = schema.newInstance(); + schema.mergeFrom(msg, data, 0, data.length, new ArrayDecoders.Registers()); + schema.makeImmutable(msg); + return msg; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java b/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java new file mode 100755 index 0000000000..3ef93ac9f8 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ExperimentalTestDataProvider.java @@ -0,0 +1,189 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.util.Random; + +/** Utility class that provides data primitives for filling out protobuf messages. */ +public final class ExperimentalTestDataProvider { + private static final Random RANDOM = new Random(100); + + private final Varint32Provider varint32s = new Varint32Provider(); + private final Varint64Provider varint64s = new Varint64Provider(); + private final int stringLength; + + public ExperimentalTestDataProvider(int stringLength) { + this.stringLength = stringLength; + } + + public double getDouble() { + double value = 0.0; + while (Double.compare(0.0, value) == 0) { + value = RANDOM.nextDouble(); + } + return value; + } + + public float getFloat() { + float value = 0.0f; + while (Float.compare(0.0f, value) == 0) { + value = RANDOM.nextFloat(); + } + return value; + } + + public long getLong() { + return varint64s.getLong(); + } + + public int getInt() { + return varint32s.getInt(); + } + + public boolean getBool() { + return true; + } + + public int getEnum() { + return Math.abs(getInt()) % 3; + } + + public String getString() { + StringBuilder builder = new StringBuilder(stringLength); + for (int i = 0; i < stringLength; ++i) { + builder.append((char) (RANDOM.nextInt('z' - 'a') + 'a')); + } + return builder.toString(); + } + + public ByteString getBytes() { + return ByteString.copyFromUtf8(getString()); + } + + /** + * Iterator over integer values. Uses a simple distribution over 32-bit varints (generally + * favoring smaller values). + */ + private static final class Varint32Provider { + private static final int[][] VALUES = { + new int[] {1, 50, 100, 127}, // 1 byte values + new int[] {128, 500, 10000, 16383}, // 2 bytes values + new int[] {16384, 50000, 1000000, 2097151}, // 3 bytes values + new int[] {2097152, 10000000, 200000000, 268435455}, // 4 bytes values + new int[] {268435456, 0x30000000, 0x7FFFFFFF, 0xFFFFFFFF} // 5 bytes values + }; + + /** Number of samples that should be taken from each value array. */ + private static final int[] NUM_SAMPLES = {3, 2, 1, 1, 2}; + + /** + * The index into the {@link #VALUES} array that identifies the list of samples currently being + * iterated over. + */ + private int listIndex; + + /** The index of the next sample within a list. */ + private int sampleIndex; + + /** The number of successive samples that have been taken from the current list. */ + private int samplesTaken; + + public int getInt() { + if (samplesTaken++ > NUM_SAMPLES[listIndex]) { + // Done taking samples from this list. Go to the next one. + listIndex = (listIndex + 1) % VALUES.length; + sampleIndex = 0; + samplesTaken = 0; + } + + int value = VALUES[listIndex][sampleIndex]; + + // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation + // with masking. + sampleIndex = (sampleIndex + 1) & 3; + + return value; + } + } + + /** + * Iterator over integer values. Uses a simple distribution over 64-bit varints (generally + * favoring smaller values). + */ + private static final class Varint64Provider { + private static final long[][] VALUES = { + new long[] {1, 50, 100, 127}, + new long[] {128, 500, 10000, 16383}, + new long[] {16384, 50000, 1000000, 2097151}, + new long[] {2097152, 10000000, 200000000, 268435455}, + new long[] {268435456, 0x30000000, 0x7FFFFFFF, 34359738367L}, + new long[] {34359738368L, 2000000000000L, 4000000000000L, 4398046511103L}, + new long[] {4398046511104L, 200000000000000L, 500000000000000L, 562949953421311L}, + new long[] {0x4000000000000L, 0x5000000000000L, 0x6000000000000L, 0x0FFFFFFFFFFFFFFL}, + new long[] {0x100000000000000L, 0x3FFFFFFFFFFFFFFFL, 0x5FFFFFFFFFFFFFFL, 0x7FFFFFFFFFFFFFFFL}, + new long[] { + 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL + } + }; + + /** Number of samples that should be taken from each value array. */ + private static final int[] NUM_SAMPLES = {4, 2, 2, 1, 1, 1, 1, 2, 2, 4}; + + /** + * The index into the {@link #VALUES} array that identifies the list of samples currently being + * iterated over. + */ + private int listIndex; + + /** The index of the next sample within a list. */ + private int sampleIndex; + + /** The number of successive samples that have been taken from the current list. */ + private int samplesTaken; + + public long getLong() { + if (samplesTaken++ > NUM_SAMPLES[listIndex]) { + // Done taking samples from this list. Go to the next one. + listIndex = (listIndex + 1) % VALUES.length; + sampleIndex = 0; + samplesTaken = 0; + } + + long value = VALUES[listIndex][sampleIndex]; + + // All lists are exactly 4 long (i.e. power of 2), so we can optimize the mod operation + // with masking. + sampleIndex = (sampleIndex + 1) & 3; + + return value; + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java index 30fd147948..3eb0cedc4d 100644 --- a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java +++ b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java @@ -30,8 +30,6 @@ package com.google.protobuf; -import protobuf_unittest.NestedExtension; -import protobuf_unittest.NestedExtensionLite; import protobuf_unittest.NonNestedExtension; import protobuf_unittest.NonNestedExtensionLite; import java.lang.reflect.Method; @@ -71,8 +69,6 @@ public class ExtensionRegistryFactoryTest extends TestCase { void testAdd(); void testAdd_immutable(); - - void testExtensionRenamesKeywords(); } /** Test implementations for the non-Lite usage of ExtensionRegistryFactory. */ @@ -160,23 +156,6 @@ public class ExtensionRegistryFactoryTest extends TestCase { } catch (IllegalArgumentException expected) { } } - - @Override - public void testExtensionRenamesKeywords() { - assertTrue(NonNestedExtension.if_ instanceof GeneratedMessage.GeneratedExtension); - assertTrue(NestedExtension.MyNestedExtension.default_ instanceof GeneratedMessage.GeneratedExtension); - - NonNestedExtension.MessageToBeExtended msg = - NonNestedExtension.MessageToBeExtended.newBuilder() - .setExtension(NonNestedExtension.if_, "!fi") - .build(); - assertEquals("!fi", msg.getExtension(NonNestedExtension.if_)); - - msg = NonNestedExtension.MessageToBeExtended.newBuilder() - .setExtension(NestedExtension.MyNestedExtension.default_, 8) - .build(); - assertEquals(8, msg.getExtension(NestedExtension.MyNestedExtension.default_).intValue()); - } } /** Test implementations for the Lite usage of ExtensionRegistryFactory. */ @@ -223,23 +202,6 @@ public class ExtensionRegistryFactoryTest extends TestCase { } catch (UnsupportedOperationException expected) { } } - - @Override - public void testExtensionRenamesKeywords() { - assertTrue(NonNestedExtensionLite.package_ instanceof GeneratedMessageLite.GeneratedExtension); - assertTrue(NestedExtensionLite.MyNestedExtensionLite.private_ instanceof GeneratedMessageLite.GeneratedExtension); - - NonNestedExtensionLite.MessageLiteToBeExtended msg = - NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() - .setExtension(NonNestedExtensionLite.package_, true) - .build(); - assertTrue(msg.getExtension(NonNestedExtensionLite.package_)); - - msg = NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() - .setExtension(NestedExtensionLite.MyNestedExtensionLite.private_, 2.4) - .build(); - assertEquals(2.4, msg.getExtension(NestedExtensionLite.MyNestedExtensionLite.private_), 0.001); - } } /** Defines a suite of tests which the JUnit3 runner retrieves by reflection. */ @@ -311,7 +273,10 @@ public class ExtensionRegistryFactoryTest extends TestCase { resolveClass(loadedClass); } } - } catch (ClassNotFoundException e) { + } catch (ClassNotFoundException | SecurityException e) { + // Java 8+ would throw a SecurityException if we attempt to find a loaded class from + // java.lang.* package. We don't really care about those anyway, so just delegate to the + // parent class loader. loadedClass = super.loadClass(name, resolve); } return loadedClass; diff --git a/java/core/src/test/java/com/google/protobuf/MapLiteTest.java b/java/core/src/test/java/com/google/protobuf/MapLiteTest.java new file mode 100755 index 0000000000..d18fd13e0a --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/MapLiteTest.java @@ -0,0 +1,894 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertArrayEquals; + +import map_lite_test.MapTestProto.BizarroTestMap; +import map_lite_test.MapTestProto.TestMap; +import map_lite_test.MapTestProto.TestMap.MessageValue; +import map_lite_test.MapTestProto.TestMapOrBuilder; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import junit.framework.TestCase; + +/** Unit tests for map fields. */ +public final class MapLiteTest extends TestCase { + + private void setMapValues(TestMap.Builder builder) { + builder + .putInt32ToInt32Field(1, 11) + .putInt32ToInt32Field(2, 22) + .putInt32ToInt32Field(3, 33) + .putInt32ToStringField(1, "11") + .putInt32ToStringField(2, "22") + .putInt32ToStringField(3, "33") + .putInt32ToBytesField(1, TestUtil.toBytes("11")) + .putInt32ToBytesField(2, TestUtil.toBytes("22")) + .putInt32ToBytesField(3, TestUtil.toBytes("33")) + .putInt32ToEnumField(1, TestMap.EnumValue.FOO) + .putInt32ToEnumField(2, TestMap.EnumValue.BAR) + .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) + .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build()) + .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build()) + .putStringToInt32Field("1", 11) + .putStringToInt32Field("2", 22) + .putStringToInt32Field("3", 33); + } + + public void testSetMapValues() { + TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); + setMapValues(usingMutableMapBuilder); + TestMap usingMutableMap = usingMutableMapBuilder.build(); + assertMapValuesSet(usingMutableMap); + + TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); + setMapValues(usingAccessorsBuilder); + TestMap usingAccessors = usingAccessorsBuilder.build(); + assertMapValuesSet(usingAccessors); + assertEquals(usingAccessors, usingMutableMap); + } + + private void copyMapValues(TestMap source, TestMap.Builder destination) { + destination + .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) + .putAllInt32ToStringField(source.getInt32ToStringField()) + .putAllInt32ToBytesField(source.getInt32ToBytesField()) + .putAllInt32ToEnumField(source.getInt32ToEnumField()) + .putAllInt32ToMessageField(source.getInt32ToMessageField()) + .putAllStringToInt32Field(source.getStringToInt32Field()); + } + + private void assertMapValuesSet(TestMap message) { + assertEquals(3, message.getInt32ToInt32Field().size()); + assertEquals(11, message.getInt32ToInt32Field().get(1).intValue()); + assertEquals(22, message.getInt32ToInt32Field().get(2).intValue()); + assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); + + assertEquals(3, message.getInt32ToStringField().size()); + assertEquals("11", message.getInt32ToStringField().get(1)); + assertEquals("22", message.getInt32ToStringField().get(2)); + assertEquals("33", message.getInt32ToStringField().get(3)); + + assertEquals(3, message.getInt32ToBytesField().size()); + assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1)); + assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2)); + assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); + + assertEquals(3, message.getInt32ToEnumField().size()); + assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2)); + assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); + + assertEquals(3, message.getInt32ToMessageField().size()); + assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); + assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); + assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); + + assertEquals(3, message.getStringToInt32Field().size()); + assertEquals(11, message.getStringToInt32Field().get("1").intValue()); + assertEquals(22, message.getStringToInt32Field().get("2").intValue()); + assertEquals(33, message.getStringToInt32Field().get("3").intValue()); + } + + private void updateMapValues(TestMap.Builder builder) { + builder + .putInt32ToInt32Field(1, 111) + .removeInt32ToInt32Field(2) + .putInt32ToInt32Field(4, 44) + .putInt32ToStringField(1, "111") + .removeInt32ToStringField(2) + .putInt32ToStringField(4, "44") + .putInt32ToBytesField(1, TestUtil.toBytes("111")) + .removeInt32ToBytesField(2) + .putInt32ToBytesField(4, TestUtil.toBytes("44")) + .putInt32ToEnumField(1, TestMap.EnumValue.BAR) + .removeInt32ToEnumField(2) + .putInt32ToEnumField(4, TestMap.EnumValue.QUX) + .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build()) + .removeInt32ToMessageField(2) + .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build()) + .putStringToInt32Field("1", 111) + .removeStringToInt32Field("2") + .putStringToInt32Field("4", 44); + } + + public void testUpdateMapValues() { + TestMap.Builder mapBuilder = TestMap.newBuilder(); + setMapValues(mapBuilder); + TestMap map = mapBuilder.build(); + assertMapValuesSet(map); + + mapBuilder = map.toBuilder(); + updateMapValues(mapBuilder); + map = mapBuilder.build(); + assertMapValuesUpdated(map); + } + + private void assertMapValuesUpdated(TestMap message) { + assertEquals(3, message.getInt32ToInt32Field().size()); + assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); + assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); + assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); + + assertEquals(3, message.getInt32ToStringField().size()); + assertEquals("111", message.getInt32ToStringField().get(1)); + assertEquals("33", message.getInt32ToStringField().get(3)); + assertEquals("44", message.getInt32ToStringField().get(4)); + + assertEquals(3, message.getInt32ToBytesField().size()); + assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1)); + assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); + assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4)); + + assertEquals(3, message.getInt32ToEnumField().size()); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); + assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4)); + + assertEquals(3, message.getInt32ToMessageField().size()); + assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); + assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); + assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); + + assertEquals(3, message.getStringToInt32Field().size()); + assertEquals(111, message.getStringToInt32Field().get("1").intValue()); + assertEquals(33, message.getStringToInt32Field().get("3").intValue()); + assertEquals(44, message.getStringToInt32Field().get("4").intValue()); + } + + private void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder) { + assertEquals(0, testMapOrBuilder.getInt32ToInt32Field().size()); + assertEquals(0, testMapOrBuilder.getInt32ToInt32FieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToStringField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToStringFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToBytesField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToBytesFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToEnumField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToEnumFieldCount()); + assertEquals(0, testMapOrBuilder.getInt32ToMessageField().size()); + assertEquals(0, testMapOrBuilder.getInt32ToMessageFieldCount()); + assertEquals(0, testMapOrBuilder.getStringToInt32Field().size()); + assertEquals(0, testMapOrBuilder.getStringToInt32FieldCount()); + } + + public void testSanityCopyOnWrite() throws InvalidProtocolBufferException { + // Since builders are implemented as a thin wrapper around a message + // instance, we attempt to verify that we can't cause the builder to modify + // a produced message. + + TestMap.Builder builder = TestMap.newBuilder(); + TestMap message = builder.build(); + builder.putInt32ToInt32Field(1, 2); + assertTrue(message.getInt32ToInt32Field().isEmpty()); + assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); + message = builder.build(); + builder.putInt32ToInt32Field(2, 3); + assertEquals(newMap(1, 2), message.getInt32ToInt32Field()); + assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); + } + + public void testGetMapIsImmutable() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapsAreImmutable(builder); + assertMapsAreImmutable(builder.build()); + + setMapValues(builder); + assertMapsAreImmutable(builder); + assertMapsAreImmutable(builder.build()); + } + + private void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder) { + assertImmutable(testMapOrBuilder.getInt32ToInt32Field(), 1, 2); + assertImmutable(testMapOrBuilder.getInt32ToStringField(), 1, "2"); + assertImmutable(testMapOrBuilder.getInt32ToBytesField(), 1, TestUtil.toBytes("2")); + assertImmutable(testMapOrBuilder.getInt32ToEnumField(), 1, TestMap.EnumValue.FOO); + assertImmutable( + testMapOrBuilder.getInt32ToMessageField(), 1, MessageValue.getDefaultInstance()); + assertImmutable(testMapOrBuilder.getStringToInt32Field(), "1", 2); + } + + private <K, V> void assertImmutable(Map<K, V> map, K key, V value) { + try { + map.put(key, value); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + if (!map.isEmpty()) { + try { + map.entrySet().remove(map.entrySet().iterator().next()); + fail(); + } catch (UnsupportedOperationException e) { + // expected + } + } + } + + public void testMapFieldClear() { + TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2); + builder.clearInt32ToInt32Field(); + assertEquals(0, builder.getInt32ToInt32FieldCount()); + } + + public void testMutableMapLifecycle() { + TestMap.Builder builder = TestMap.newBuilder().putInt32ToInt32Field(1, 2); + assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); + assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); + builder.putInt32ToInt32Field(2, 3); + assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); + + builder.putInt32ToEnumField(1, TestMap.EnumValue.BAR); + assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField()); + assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()); + builder.putInt32ToEnumField(2, TestMap.EnumValue.FOO); + assertEquals( + newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField()); + + builder.putInt32ToStringField(1, "1"); + assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); + assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); + builder.putInt32ToStringField(2, "2"); + assertEquals(newMap(1, "1", 2, "2"), builder.getInt32ToStringField()); + + builder.putInt32ToMessageField(1, TestMap.MessageValue.getDefaultInstance()); + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), + builder.build().getInt32ToMessageField()); + assertEquals( + newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); + builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance()); + assertEquals( + newMap( + 1, + TestMap.MessageValue.getDefaultInstance(), + 2, + TestMap.MessageValue.getDefaultInstance()), + builder.getInt32ToMessageField()); + } + + public void testGettersAndSetters() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + TestMap message = builder.build(); + assertMapValuesCleared(message); + + builder = message.toBuilder(); + setMapValues(builder); + message = builder.build(); + assertMapValuesSet(message); + + builder = message.toBuilder(); + updateMapValues(builder); + message = builder.build(); + assertMapValuesUpdated(message); + + builder = message.toBuilder(); + builder.clear(); + assertMapValuesCleared(builder); + message = builder.build(); + assertMapValuesCleared(message); + } + + public void testPutAll() throws Exception { + TestMap.Builder sourceBuilder = TestMap.newBuilder(); + setMapValues(sourceBuilder); + TestMap source = sourceBuilder.build(); + assertMapValuesSet(source); + + TestMap.Builder destination = TestMap.newBuilder(); + copyMapValues(source, destination); + assertMapValuesSet(destination.build()); + } + + public void testPutAllForUnknownEnumValues() throws Exception { + TestMap.Builder sourceBuilder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putAllInt32ToEnumFieldValue(newMap(2, 1000)); // unknown value. + TestMap source = sourceBuilder.build(); + + TestMap.Builder destinationBuilder = TestMap.newBuilder(); + destinationBuilder.putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()); + TestMap destination = destinationBuilder.build(); + + assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); + assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); + assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue()); + assertEquals(3, destination.getInt32ToEnumFieldCount()); + } + + public void testPutForUnknownEnumValues() throws Exception { + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000); // unknown value. + TestMap message = builder.build(); + + assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); + assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); + assertEquals(1000, message.getInt32ToEnumFieldValueOrThrow(2)); + assertEquals(3, message.getInt32ToEnumFieldCount()); + } + + public void testPutChecksNullKeysAndValues() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + + try { + builder.putInt32ToStringField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToBytesField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToEnumField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putInt32ToMessageField(1, null); + fail(); + } catch (NullPointerException e) { + // expected. + } + + try { + builder.putStringToInt32Field(null, 1); + fail(); + } catch (NullPointerException e) { + // expected. + } + } + + public void testSerializeAndParse() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesSet(message); + + builder = message.toBuilder(); + updateMapValues(builder); + message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesUpdated(message); + + builder = message.toBuilder(); + builder.clear(); + message = builder.build(); + assertEquals(message.getSerializedSize(), message.toByteString().size()); + message = TestMap.parser().parseFrom(message.toByteString()); + assertMapValuesCleared(message); + } + + private TestMap tryParseTestMap(BizarroTestMap bizarroMap) throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); + bizarroMap.writeTo(output); + output.flush(); + return TestMap.parser().parseFrom(ByteString.copyFrom(byteArrayOutputStream.toByteArray())); + } + + public void testParseError() throws Exception { + ByteString bytes = TestUtil.toBytes("SOME BYTES"); + String stringKey = "a string key"; + + TestMap map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToInt32Field(5, bytes).build()); + assertEquals(0, map.getInt32ToInt32FieldOrDefault(5, -1)); + + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToStringField(stringKey, 5).build()); + assertEquals("", map.getInt32ToStringFieldOrDefault(0, null)); + + map = tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToBytesField(stringKey, 5).build()); + assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); + + map = + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToEnumField(stringKey, bytes).build()); + assertEquals(TestMap.EnumValue.FOO, map.getInt32ToEnumFieldOrDefault(0, null)); + + try { + tryParseTestMap(BizarroTestMap.newBuilder().putInt32ToMessageField(stringKey, bytes).build()); + fail(); + } catch (InvalidProtocolBufferException expected) { + assertTrue(expected.getUnfinishedMessage() instanceof TestMap); + map = (TestMap) expected.getUnfinishedMessage(); + assertTrue(map.getInt32ToMessageField().isEmpty()); + } + + map = + tryParseTestMap( + BizarroTestMap.newBuilder().putStringToInt32Field(stringKey, bytes).build()); + assertEquals(0, map.getStringToInt32FieldOrDefault(stringKey, -1)); + } + + public void testMergeFrom() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + + TestMap.Builder other = TestMap.newBuilder(); + other.mergeFrom(message); + assertMapValuesSet(other.build()); + } + + public void testEqualsAndHashCode() throws Exception { + // Test that generated equals() and hashCode() will disregard the order + // of map entries when comparing/hashing map fields. + + // We can't control the order of elements in a HashMap. The best we can do + // here is to add elements in different order. + TestMap.Builder b1 = + TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToInt32Field(5, 6); + TestMap m1 = b1.build(); + + TestMap.Builder b2 = + TestMap.newBuilder() + .putInt32ToInt32Field(5, 6) + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4); + TestMap m2 = b2.build(); + + assertEquals(m1, m2); + assertEquals(m1.hashCode(), m2.hashCode()); + + // Make sure we did compare map fields. + b2.putInt32ToInt32Field(1, 0); + m2 = b2.build(); + assertFalse(m1.equals(m2)); + // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed + // to be different. + + // Regression test for b/18549190: if a map is a subset of the other map, + // equals() should return false. + b2.removeInt32ToInt32Field(1); + m2 = b2.build(); + assertFalse(m1.equals(m2)); + assertFalse(m2.equals(m1)); + } + + public void testUnknownEnumValues() throws Exception { + TestMap.Builder builder = + TestMap.newBuilder() + .putInt32ToEnumFieldValue(0, 0) + .putInt32ToEnumFieldValue(1, 1) + .putInt32ToEnumFieldValue(2, 1000); // unknown value. + TestMap message = builder.build(); + + assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(0)); + assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, message.getInt32ToEnumField().get(2)); + + builder.putAllInt32ToEnumFieldValue(newMap(2, 1000)); // unknown value. + message = builder.build(); + assertEquals(TestMap.EnumValue.UNRECOGNIZED, message.getInt32ToEnumField().get(2)); + + // Unknown enum values should be preserved after: + // 1. Serialization and parsing. + // 2. toBuild(). + // 3. mergeFrom(). + message = TestMap.parseFrom(message.toByteString()); + assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); + builder = message.toBuilder(); + assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); + builder = TestMap.newBuilder().mergeFrom(message); + assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); + + // hashCode()/equals() should take unknown enum values into account. + builder.putAllInt32ToEnumFieldValue(newMap(2, 1001)); + TestMap message2 = builder.build(); + assertFalse(message.hashCode() == message2.hashCode()); + assertFalse(message.equals(message2)); + // Unknown values will be converted to UNRECOGNIZED so the resulted enum map + // should be the same. + assertEquals(message2.getInt32ToEnumField(), message.getInt32ToEnumField()); + } + + public void testIterationOrder() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + + assertEquals( + Arrays.asList("1", "2", "3"), new ArrayList<>(message.getStringToInt32Field().keySet())); + } + + public void testGetMap() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + TestMap message = builder.build(); + assertEquals(message.getStringToInt32Field(), message.getStringToInt32FieldMap()); + assertEquals(message.getInt32ToBytesField(), message.getInt32ToBytesFieldMap()); + assertEquals(message.getInt32ToEnumField(), message.getInt32ToEnumFieldMap()); + assertEquals(message.getInt32ToEnumFieldValue(), message.getInt32ToEnumFieldValueMap()); + assertEquals(message.getInt32ToMessageField(), message.getInt32ToMessageFieldMap()); + } + + public void testContains() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + assertMapContainsSetValues(builder); + assertMapContainsSetValues(builder.build()); + } + + private void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder) { + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(1)); + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(2)); + assertTrue(testMapOrBuilder.containsInt32ToInt32Field(3)); + assertFalse(testMapOrBuilder.containsInt32ToInt32Field(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToStringField(1)); + assertTrue(testMapOrBuilder.containsInt32ToStringField(2)); + assertTrue(testMapOrBuilder.containsInt32ToStringField(3)); + assertFalse(testMapOrBuilder.containsInt32ToStringField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToBytesField(1)); + assertTrue(testMapOrBuilder.containsInt32ToBytesField(2)); + assertTrue(testMapOrBuilder.containsInt32ToBytesField(3)); + assertFalse(testMapOrBuilder.containsInt32ToBytesField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToEnumField(1)); + assertTrue(testMapOrBuilder.containsInt32ToEnumField(2)); + assertTrue(testMapOrBuilder.containsInt32ToEnumField(3)); + assertFalse(testMapOrBuilder.containsInt32ToEnumField(-1)); + + assertTrue(testMapOrBuilder.containsInt32ToMessageField(1)); + assertTrue(testMapOrBuilder.containsInt32ToMessageField(2)); + assertTrue(testMapOrBuilder.containsInt32ToMessageField(3)); + assertFalse(testMapOrBuilder.containsInt32ToMessageField(-1)); + + assertTrue(testMapOrBuilder.containsStringToInt32Field("1")); + assertTrue(testMapOrBuilder.containsStringToInt32Field("2")); + assertTrue(testMapOrBuilder.containsStringToInt32Field("3")); + assertFalse(testMapOrBuilder.containsStringToInt32Field("-1")); + } + + public void testCount() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + + setMapValues(builder); + assertMapCounts(3, builder); + + TestMap message = builder.build(); + assertMapCounts(3, message); + + builder = message.toBuilder().putInt32ToInt32Field(4, 44); + assertEquals(4, builder.getInt32ToInt32FieldCount()); + assertEquals(4, builder.build().getInt32ToInt32FieldCount()); + + // already present - should be unchanged + builder.putInt32ToInt32Field(4, 44); + assertEquals(4, builder.getInt32ToInt32FieldCount()); + } + + private void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuilder) { + assertEquals(expectedCount, testMapOrBuilder.getInt32ToInt32FieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToStringFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToBytesFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToEnumFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getInt32ToMessageFieldCount()); + assertEquals(expectedCount, testMapOrBuilder.getStringToInt32FieldCount()); + } + + public void testGetOrDefault() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + setMapValues(builder); + doTestGetOrDefault(builder); + doTestGetOrDefault(builder.build()); + } + + public void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder) { + assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(1, -11)); + assertEquals(-11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(-1, -11)); + + assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrDefault(1, "-11")); + assertNull("-11", testMapOrBuilder.getInt32ToStringFieldOrDefault(-1, null)); + + assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToBytesFieldOrDefault(-1, null)); + + assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); + + assertEquals( + TestMap.EnumValue.BAR.getNumber(), + testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); + assertEquals(-1, testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(-1000, -1)); + + assertEquals( + MessageValue.newBuilder().setValue(11).build(), + testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); + assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); + + assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrDefault("1", -11)); + assertEquals(-11, testMapOrBuilder.getStringToInt32FieldOrDefault("-1", -11)); + + try { + testMapOrBuilder.getStringToInt32FieldOrDefault(null, -11); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + public void testGetOrThrow() { + TestMap.Builder builder = TestMap.newBuilder(); + assertMapCounts(0, builder); + setMapValues(builder); + doTestGetOrDefault(builder); + doTestGetOrDefault(builder.build()); + } + + public void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder) { + assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToInt32FieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrThrow(1)); + + try { + testMapOrBuilder.getInt32ToStringFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOrThrow(1)); + + try { + testMapOrBuilder.getInt32ToBytesFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToEnumFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals( + TestMap.EnumValue.BAR.getNumber(), testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(2)); + try { + testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals( + MessageValue.newBuilder().setValue(11).build(), + testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); + try { + testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrThrow("1")); + try { + testMapOrBuilder.getStringToInt32FieldOrThrow("-1"); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + try { + testMapOrBuilder.getStringToInt32FieldOrThrow(null); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + public void testPut() { + TestMap.Builder builder = TestMap.newBuilder(); + builder.putInt32ToInt32Field(1, 11); + assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); + + builder.putInt32ToStringField(1, "a"); + assertEquals("a", builder.getInt32ToStringFieldOrThrow(1)); + try { + builder.putInt32ToStringField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putInt32ToBytesField(1, TestUtil.toBytes("11")); + assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1)); + try { + builder.putInt32ToBytesField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putInt32ToEnumField(1, TestMap.EnumValue.FOO); + assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); + try { + builder.putInt32ToEnumField(1, null); + fail(); + } catch (NullPointerException e) { + // expected + } + + builder.putStringToInt32Field("a", 1); + assertEquals(1, builder.getStringToInt32FieldOrThrow("a")); + try { + builder.putStringToInt32Field(null, -1); + } catch (NullPointerException e) { + // expected + } + } + + public void testRemove() { + TestMap.Builder builder = TestMap.newBuilder(); + setMapValues(builder); + assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToInt32Field(1); + assertEquals(-1, builder.getInt32ToInt32FieldOrDefault(1, -1)); + } + + assertEquals("11", builder.getInt32ToStringFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToStringField(1); + assertNull(builder.getInt32ToStringFieldOrDefault(1, null)); + } + + assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToBytesField(1); + assertNull(builder.getInt32ToBytesFieldOrDefault(1, null)); + } + + assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); + for (int times = 0; times < 2; times++) { + builder.removeInt32ToEnumField(1); + assertNull(builder.getInt32ToEnumFieldOrDefault(1, null)); + } + + assertEquals(11, builder.getStringToInt32FieldOrThrow("1")); + for (int times = 0; times < 2; times++) { + builder.removeStringToInt32Field("1"); + assertEquals(-1, builder.getStringToInt32FieldOrDefault("1", -1)); + } + + try { + builder.removeStringToInt32Field(null); + fail(); + } catch (NullPointerException e) { + // expected + } + } + + private static <K, V> Map<K, V> newMap(K key1, V value1) { + Map<K, V> map = new HashMap<>(); + map.put(key1, value1); + return map; + } + + private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) { + Map<K, V> map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + return map; + } + + public void testMap_withNulls() { + TestMap.Builder builder = TestMap.newBuilder(); + + try { + builder.putStringToInt32Field(null, 3); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllStringToInt32Field(newMap(null, 3, "hi", 4)); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putInt32ToMessageField(3, null); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllInt32ToMessageField( + MapLiteTest.<Integer, MessageValue>newMap(4, null, 5, null)); + fail(); + } catch (NullPointerException expected) { + } + + try { + builder.putAllInt32ToMessageField(null); + fail(); + } catch (NullPointerException expected) { + } + + assertArrayEquals(new byte[0], builder.build().toByteArray()); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java b/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java new file mode 100755 index 0000000000..2397d2ebc3 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/PackedFieldTest.java @@ -0,0 +1,232 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.PackedFieldTestProto.TestAllTypes; +import com.google.protobuf.PackedFieldTestProto.TestAllTypes.NestedEnum; +import com.google.protobuf.PackedFieldTestProto.TestUnpackedTypes; +import junit.framework.TestCase; + +/** Tests primitive repeated fields in proto3 are packed in wire format. */ +public class PackedFieldTest extends TestCase { + static final ByteString expectedPackedRawBytes = + ByteString.copyFrom( + new byte[] { + (byte) 0xFA, + 0x01, + 0x01, + 0x01, // repeated int32 + (byte) 0x82, + 0x02, + 0x01, + 0x01, // repeated int64 + (byte) 0x8A, + 0x02, + 0x01, + 0x01, // repeated uint32 + (byte) 0x92, + 0x02, + 0x01, + 0x01, // repeated uint64 + (byte) 0x9A, + 0x02, + 0x01, + 0x02, // repeated sint32 + (byte) 0xA2, + 0x02, + 0x01, + 0x02, // repeated sint64 + (byte) 0xAA, + 0x02, + 0x04, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed32 + (byte) 0xB2, + 0x02, + 0x08, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed64 + 0x00, + 0x00, + 0x00, + 0x00, + (byte) 0xBA, + 0x02, + 0x04, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed32 + (byte) 0xC2, + 0x02, + 0x08, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed64 + 0x00, + 0x00, + 0x00, + 0x00, + (byte) 0xCA, + 0x02, + 0x04, + 0x00, + 0x00, + (byte) 0x80, + 0x3f, // repeated float + (byte) 0xD2, + 0x02, + 0x08, + 0x00, + 0x00, + 0x00, + 0x00, // repeated double + 0x00, + 0x00, + (byte) 0xf0, + 0x3f, + (byte) 0xDA, + 0x02, + 0x01, + 0x01, // repeated bool + (byte) 0x9A, + 0x03, + 0x01, + 0x01 // repeated nested enum + }); + + static final ByteString expectedUnpackedRawBytes = + ByteString.copyFrom( + new byte[] { + 0x08, + 0x01, // repeated int32 + 0x10, + 0x01, // repeated int64 + 0x18, + 0x01, // repeated uint32 + 0x20, + 0x01, // repeated uint64 + 0x28, + 0x02, // repeated sint32 + 0x30, + 0x02, // repeated sint64 + 0x3D, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed32 + 0x41, + 0x01, + 0x00, + 0x00, + 0x00, // repeated fixed64 + 0x00, + 0x00, + 0x00, + 0x00, + 0x4D, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed32 + 0x51, + 0x01, + 0x00, + 0x00, + 0x00, // repeated sfixed64 + 0x00, + 0x00, + 0x00, + 0x00, + 0x5D, + 0x00, + 0x00, + (byte) 0x80, + 0x3f, // repeated float + 0x61, + 0x00, + 0x00, + 0x00, + 0x00, // repeated double + 0x00, + 0x00, + (byte) 0xf0, + 0x3f, + 0x68, + 0x01, // repeated bool + 0x70, + 0x01, // repeated nested enum + }); + + public void testPackedGeneratedMessage() throws Exception { + TestAllTypes message = TestAllTypes.parseFrom(expectedPackedRawBytes); + assertEquals(expectedPackedRawBytes, message.toByteString()); + } + + public void testPackedDynamicMessageSerialize() throws Exception { + DynamicMessage message = + DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), expectedPackedRawBytes); + assertEquals(expectedPackedRawBytes, message.toByteString()); + } + + public void testUnpackedGeneratedMessage() throws Exception { + TestUnpackedTypes message = TestUnpackedTypes.parseFrom(expectedUnpackedRawBytes); + assertEquals(expectedUnpackedRawBytes, message.toByteString()); + } + + public void testUnPackedDynamicMessageSerialize() throws Exception { + DynamicMessage message = + DynamicMessage.parseFrom(TestUnpackedTypes.getDescriptor(), expectedUnpackedRawBytes); + assertEquals(expectedUnpackedRawBytes, message.toByteString()); + } + + // Make sure we haven't screwed up the code generation for packing fields by default. + public void testPackedSerialization() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedInt32(1234) + .addRepeatedNestedEnum(NestedEnum.BAR) + .build(); + + CodedInputStream in = CodedInputStream.newInstance(message.toByteArray()); + + while (!in.isAtEnd()) { + int tag = in.readTag(); + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag)); + in.skipField(tag); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java new file mode 100755 index 0000000000..eb2dc3dd24 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/ParserLiteTest.java @@ -0,0 +1,191 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.UnittestLite.TestAllTypesLite; +import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; +import com.google.protobuf.UnittestLite.TestParsingMergeLite; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import junit.framework.TestCase; + +public class ParserLiteTest extends TestCase { + private void assertRoundTripEquals(MessageLite message, ExtensionRegistryLite registry) + throws Exception { + final byte[] data = message.toByteArray(); + final int offset = 20; + final int length = data.length; + final int padding = 30; + Parser<? extends MessageLite> parser = message.getParserForType(); + assertEquals(message, parser.parseFrom(data, registry)); + assertEquals( + message, + parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length, registry)); + assertEquals(message, parser.parseFrom(message.toByteString(), registry)); + assertEquals(message, parser.parseFrom(new ByteArrayInputStream(data), registry)); + assertEquals(message, parser.parseFrom(CodedInputStream.newInstance(data), registry)); + assertEquals( + message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer(), registry)); + } + + @SuppressWarnings("unchecked") + private void assertRoundTripEquals(MessageLite message) throws Exception { + final byte[] data = message.toByteArray(); + final int offset = 20; + final int length = data.length; + final int padding = 30; + + Parser<MessageLite> parser = (Parser<MessageLite>) message.getParserForType(); + assertEquals(message, parser.parseFrom(data)); + assertEquals( + message, parser.parseFrom(generatePaddingArray(data, offset, padding), offset, length)); + assertEquals(message, parser.parseFrom(message.toByteString())); + assertEquals(message, parser.parseFrom(new ByteArrayInputStream(data))); + assertEquals(message, parser.parseFrom(CodedInputStream.newInstance(data))); + assertEquals(message, parser.parseFrom(message.toByteString().asReadOnlyByteBuffer())); + } + + private byte[] generatePaddingArray(byte[] data, int offset, int padding) { + byte[] result = new byte[offset + data.length + padding]; + System.arraycopy(data, 0, result, offset, data.length); + return result; + } + + public void testParseExtensionsLite() throws Exception { + assertRoundTripEquals( + TestUtilLite.getAllLiteExtensionsSet(), TestUtilLite.getExtensionRegistryLite()); + } + + public void testParsePacked() throws Exception { + assertRoundTripEquals(TestUtil.getPackedSet()); + assertRoundTripEquals(TestUtil.getPackedExtensionsSet(), TestUtil.getExtensionRegistry()); + } + + public void testParsePackedLite() throws Exception { + assertRoundTripEquals( + TestUtilLite.getLitePackedExtensionsSet(), TestUtilLite.getExtensionRegistryLite()); + } + + public void testParseDelimitedToLite() throws Exception { + // Write MessageLite with packed extension fields. + TestPackedExtensionsLite packedMessage = TestUtilLite.getLitePackedExtensionsSet(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + packedMessage.writeDelimitedTo(output); + packedMessage.writeDelimitedTo(output); + + InputStream input = new ByteArrayInputStream(output.toByteArray()); + assertEquals( + packedMessage, + packedMessage + .getParserForType() + .parseDelimitedFrom(input, TestUtilLite.getExtensionRegistryLite())); + assertEquals( + packedMessage, + packedMessage + .getParserForType() + .parseDelimitedFrom(input, TestUtilLite.getExtensionRegistryLite())); + } + + /** Helper method for {@link #testParsingMergeLite()}. */ + private void assertMessageMerged(TestAllTypesLite allTypes) throws Exception { + assertEquals(3, allTypes.getOptionalInt32()); + assertEquals(2, allTypes.getOptionalInt64()); + assertEquals("hello", allTypes.getOptionalString()); + } + + public void testParsingMergeLite() throws Exception { + // Build messages. + TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder(); + TestAllTypesLite msg1 = builder.setOptionalInt32(1).build(); + builder.clear(); + TestAllTypesLite msg2 = builder.setOptionalInt64(2).build(); + builder.clear(); + TestAllTypesLite msg3 = builder.setOptionalInt32(3).setOptionalString("hello").build(); + + // Build groups. + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG1 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg1).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG2 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg2).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group1 optionalG3 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group1.newBuilder().setField1(msg3).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG1 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg1).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG2 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg2).build(); + TestParsingMergeLite.RepeatedFieldsGenerator.Group2 repeatedG3 = + TestParsingMergeLite.RepeatedFieldsGenerator.Group2.newBuilder().setField1(msg3).build(); + + // Assign and serialize RepeatedFieldsGenerator. + ByteString data = + TestParsingMergeLite.RepeatedFieldsGenerator.newBuilder() + .addField1(msg1) + .addField1(msg2) + .addField1(msg3) + .addField2(msg1) + .addField2(msg2) + .addField2(msg3) + .addField3(msg1) + .addField3(msg2) + .addField3(msg3) + .addGroup1(optionalG1) + .addGroup1(optionalG2) + .addGroup1(optionalG3) + .addGroup2(repeatedG1) + .addGroup2(repeatedG2) + .addGroup2(repeatedG3) + .addExt1(msg1) + .addExt1(msg2) + .addExt1(msg3) + .addExt2(msg1) + .addExt2(msg2) + .addExt2(msg3) + .build() + .toByteString(); + + // Parse TestParsingMergeLite. + ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); + UnittestLite.registerAllExtensions(registry); + TestParsingMergeLite parsingMerge = TestParsingMergeLite.parser().parseFrom(data, registry); + + // Required and optional fields should be merged. + assertMessageMerged(parsingMerge.getRequiredAllTypes()); + assertMessageMerged(parsingMerge.getOptionalAllTypes()); + assertMessageMerged(parsingMerge.getOptionalGroup().getOptionalGroupAllTypes()); + assertMessageMerged(parsingMerge.getExtension(TestParsingMergeLite.optionalExt)); + + // Repeated fields should not be merged. + assertEquals(3, parsingMerge.getRepeatedAllTypesCount()); + assertEquals(3, parsingMerge.getRepeatedGroupCount()); + assertEquals(3, parsingMerge.getExtensionCount(TestParsingMergeLite.repeatedExt)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java new file mode 100755 index 0000000000..dfda4b3cbd --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java @@ -0,0 +1,171 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2ExtensionLookupSchemaTest { + private byte[] data; + private ExtensionRegistry extensionRegistry; + + @Before + public void setup() { + TestSchemas.registerGenericProto2Schemas(); + + Protobuf.getInstance().schemaFor(Proto2MessageWithExtensions.class); + data = new Proto2MessageFactory(10, 20, 1, 1).newMessage().toByteArray(); + extensionRegistry = ExtensionRegistry.newInstance(); + Proto2Testing.registerAllExtensions(extensionRegistry); + } + + @Test + public void testExtensions() throws Exception { + Proto2MessageWithExtensions base = + Proto2MessageWithExtensions.parseFrom(data, extensionRegistry); + + Proto2MessageWithExtensions message = + ExperimentalSerializationUtil.fromByteArray( + data, Proto2MessageWithExtensions.class, extensionRegistry); + assertEquals(base, message); + + Proto2MessageWithExtensions roundtripMessage = + ExperimentalSerializationUtil.fromByteArray( + ExperimentalSerializationUtil.toByteArray(message), + Proto2MessageWithExtensions.class, + extensionRegistry); + assertEquals(base, roundtripMessage); + } + + @Test + public void testUnknownEnum() throws Exception { + // Use unknown fields to hold invalid enum values. + UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance(); + final int outOfRange = 1000; + assertNull(TestEnum.forNumber(outOfRange)); + unknowns.storeField( + WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.ONE_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) outOfRange); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT), + (long) TestEnum.TWO_VALUE); + + { + // Construct a packed enum list. + int packedSize = + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE) + + CodedOutputStream.computeUInt32SizeNoTag(outOfRange) + + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE); + ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize); + CodedOutputStream packedOut = packedBuilder.getCodedOutput(); + packedOut.writeEnumNoTag(TestEnum.ONE_VALUE); + packedOut.writeEnumNoTag(outOfRange); + packedOut.writeEnumNoTag(TestEnum.TWO_VALUE); + unknowns.storeField( + WireFormat.makeTag( + Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER, + WireFormat.WIRETYPE_LENGTH_DELIMITED), + packedBuilder.build()); + } + int size = unknowns.getSerializedSize(); + byte[] output = new byte[size]; + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + unknowns.writeTo(codedOutput); + codedOutput.flush(); + + Proto2MessageWithExtensions parsed = + ExperimentalSerializationUtil.fromByteArray( + output, Proto2MessageWithExtensions.class, extensionRegistry); + assertFalse( + "out-of-range singular enum should not be in message", + parsed.hasExtension(Proto2Testing.fieldEnum13)); + { + List<Long> singularEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, singularEnum.size()); + assertEquals((Long) (long) outOfRange, singularEnum.get(0)); + } + { + List<Long> repeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, repeatedEnum.size()); + assertEquals((Long) (long) outOfRange, repeatedEnum.get(0)); + } + { + List<Long> packedRepeatedEnum = + parsed + .getUnknownFields() + .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER) + .getVarintList(); + assertEquals(1, packedRepeatedEnum.size()); + assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0)); + } + assertEquals( + "out-of-range repeated enum should not be in message", + 2, + parsed.getExtension(Proto2Testing.fieldEnumList30).size()); + assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumList30, 0)); + assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumList30, 1)); + assertEquals( + "out-of-range packed repeated enum should not be in message", + 2, + parsed.getExtension(Proto2Testing.fieldEnumListPacked44).size()); + assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 0)); + assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 1)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java new file mode 100755 index 0000000000..8242f847ee --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java @@ -0,0 +1,49 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2LiteSchemaTest extends AbstractProto2LiteSchemaTest { + + @Override + protected Schema<Proto2MessageLite> schema() { + return TestSchemasLite.genericProto2LiteSchema; + } + + @Override + protected void registerSchemas() { + TestSchemasLite.registerGenericProto2LiteSchemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java new file mode 100755 index 0000000000..b5c61ed998 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageFactory.java @@ -0,0 +1,555 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Creates instances of {@link Proto2Message} based on the tree configuration. */ +public final class Proto2MessageFactory implements ExperimentalMessageFactory<Proto2Message> { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto2MessageFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto2MessageFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto2MessageFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = new Proto2MessageFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto2Message newMessage() { + Proto2Message.Builder builder = Proto2Message.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44(Proto2Message.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + builder.setFieldGroup49(Proto2Message.FieldGroup49.newBuilder().setFieldInt3250(data.getInt())); + + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldGroupList51( + Proto2Message.FieldGroupList51.newBuilder().setFieldInt3252(data.getInt())); + } + + // Set all required fields. + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider<T> { + public T getValue(); + } + + private final MapValueProvider<Integer> integerProvider = + new MapValueProvider<Integer>() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider<Long> longProvider = + new MapValueProvider<Long>() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider<String> stringProvider = + new MapValueProvider<String>() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider<ByteString> bytesProvider = + new MapValueProvider<ByteString>() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider<Boolean> booleanProvider = + new MapValueProvider<Boolean>() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider<Float> floatProvider = + new MapValueProvider<Float>() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider<Double> doubleProvider = + new MapValueProvider<Double>() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider<Proto2Message> messageProvider = + new MapValueProvider<Proto2Message>() { + @Override + public Proto2Message getValue() { + return newMessage(); + } + }; + private final MapValueProvider<Proto2Message.TestEnum> enumProvider = + new MapValueProvider<Proto2Message.TestEnum>() { + @Override + public Proto2Message.TestEnum getValue() { + return Proto2Message.TestEnum.forNumber(data.getEnum()); + } + }; + + private <V> Map<Integer, V> populateIntegerMap(MapValueProvider<V> provider) { + Map<Integer, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private <V> Map<Long, V> populateLongMap(MapValueProvider<V> provider) { + Map<Long, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private <V> Map<String, V> populateStringMap(MapValueProvider<V> provider) { + Map<String, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private <V> Map<Boolean, V> populateBooleanMap(MapValueProvider<V> provider) { + Map<Boolean, V> map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto2MessageWithMaps newMessageWithMaps() { + Proto2MessageWithMaps.Builder builder = Proto2MessageWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } + + public List<Proto2Message> newMessagesMissingRequiredFields() { + List<Proto2Message> results = new ArrayList<>(); + for (int i = 71; i <= 88; ++i) { + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, i); + results.add(builder.buildPartial()); + } + { + // A nested optional message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage10(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested repeated message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.addFieldMessageList27(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested oneof message field is missing required fields. + Proto2Message.Builder builder = Proto2Message.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage62(Proto2Message.getDefaultInstance()); + results.add(builder.buildPartial()); + } + return results; + } + + // 0 is not a valid field number so we use it to mean no fields are excluded. + private static final int INCLUDE_ALL_REQUIRED_FIELDS = 0; + + private void populateRequiredFields(Proto2Message.Builder builder, int excludedFieldNumber) { + if (excludedFieldNumber != 71) { + builder.setFieldRequiredDouble71(data.getDouble()); + } + if (excludedFieldNumber != 72) { + builder.setFieldRequiredFloat72(data.getFloat()); + } + if (excludedFieldNumber != 73) { + builder.setFieldRequiredInt6473(data.getLong()); + } + if (excludedFieldNumber != 74) { + builder.setFieldRequiredUint6474(data.getLong()); + } + if (excludedFieldNumber != 75) { + builder.setFieldRequiredInt3275(data.getInt()); + } + if (excludedFieldNumber != 76) { + builder.setFieldRequiredFixed6476(data.getLong()); + } + if (excludedFieldNumber != 77) { + builder.setFieldRequiredFixed3277(data.getInt()); + } + if (excludedFieldNumber != 78) { + builder.setFieldRequiredBool78(data.getBool()); + } + if (excludedFieldNumber != 79) { + builder.setFieldRequiredString79(data.getString()); + } + if (excludedFieldNumber != 80) { + builder.setFieldRequiredMessage80( + Proto2Message.RequiredNestedMessage.newBuilder().setValue(data.getInt())); + } + if (excludedFieldNumber != 81) { + builder.setFieldRequiredBytes81(data.getBytes()); + } + if (excludedFieldNumber != 82) { + builder.setFieldRequiredUint3282(data.getInt()); + } + if (excludedFieldNumber != 83) { + builder.setFieldRequiredEnum83(Proto2Message.TestEnum.forNumber(data.getEnum())); + } + if (excludedFieldNumber != 84) { + builder.setFieldRequiredSfixed3284(data.getInt()); + } + if (excludedFieldNumber != 85) { + builder.setFieldRequiredSfixed6485(data.getLong()); + } + if (excludedFieldNumber != 86) { + builder.setFieldRequiredSint3286(data.getInt()); + } + if (excludedFieldNumber != 87) { + builder.setFieldRequiredSint6487(data.getLong()); + } + if (excludedFieldNumber != 88) { + builder.setFieldRequiredGroup88( + Proto2Message.FieldRequiredGroup88.newBuilder().setFieldInt3289(data.getInt())); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java new file mode 100755 index 0000000000..af671b22e4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageInfoFactory.java @@ -0,0 +1,892 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroup49; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroup69; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldGroupList51; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.FieldRequiredGroup88; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.RequiredNestedMessage; +import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded message info for {@link Proto2Message}. */ +public final class Proto2MessageInfoFactory implements MessageInfoFactory { + private static final Proto2MessageInfoFactory INSTANCE = new Proto2MessageInfoFactory(); + + private Proto2MessageInfoFactory() {} + + public static Proto2MessageInfoFactory getInstance() { + return INSTANCE; + } + + @Override + public boolean isSupported(Class<?> clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + if (Proto2Message.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2Message(); + } else if (FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup49(); + } else if (FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroupList51(); + } else if (FieldGroup69.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup69(); + } else if (FieldRequiredGroup88.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldRequiredGroup88(); + } else if (RequiredNestedMessage.class.isAssignableFrom(clazz)) { + return newMessageInfoForRequiredNestedMessage(); + } else if (Proto2Empty.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2Empty(); + } else if (Proto2MessageWithExtensions.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageWithExtensions(); + } else if (Proto2Testing.FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroup49(); + } else if (Proto2Testing.FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroupList51(); + } else if (Proto2Testing.Proto2MessageWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto2Message}. Each time this is called, we manually + * go through the entire process of what a message would do if it self-registered its own info, + * including looking up each field by name. This is done for benchmarking purposes, so that we get + * a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto2Message() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(50); + builder.withCheckInitialized( + new int[] { + 10, 27, 62, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + }); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + Field bitField0 = field("bitField0_"); + + builder.withDefaultInstance(Proto2Message.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField( + forProto2OptionalField( + field("fieldDouble1_"), 1, FieldType.DOUBLE, bitField0, 0x00000001, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFloat2_"), 2, FieldType.FLOAT, bitField0, 0x00000002, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldInt643_"), 3, FieldType.INT64, bitField0, 0x00000004, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldUint644_"), 4, FieldType.UINT64, bitField0, 0x00000008, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldInt325_"), 5, FieldType.INT32, bitField0, 0x00000010, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFixed646_"), 6, FieldType.FIXED64, bitField0, 0x00000020, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldFixed327_"), 7, FieldType.FIXED32, bitField0, 0x00000040, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldBool8_"), 8, FieldType.BOOL, bitField0, 0x00000080, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldString9_"), 9, FieldType.STRING, bitField0, 0x00000100, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldMessage10_"), 10, FieldType.MESSAGE, bitField0, 0x00000200, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldBytes11_"), 11, FieldType.BYTES, bitField0, 0x00000400, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldUint3212_"), 12, FieldType.UINT32, bitField0, 0x00000800, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldEnum13_"), + 13, + FieldType.ENUM, + bitField0, + 0x00001000, + false, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forProto2OptionalField( + field("fieldSfixed3214_"), 14, FieldType.SFIXED32, bitField0, 0x00002000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSfixed6415_"), 15, FieldType.SFIXED64, bitField0, 0x00004000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSint3216_"), 16, FieldType.SINT32, bitField0, 0x00008000, false, null)); + builder.withField( + forProto2OptionalField( + field("fieldSint6417_"), 17, FieldType.SINT64, bitField0, 0x00010000, false, null)); + builder.withField(forField(field("fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, false)); + builder.withField(forField(field("fieldFloatList19_"), 19, FieldType.FLOAT_LIST, false)); + builder.withField(forField(field("fieldInt64List20_"), 20, FieldType.INT64_LIST, false)); + builder.withField(forField(field("fieldUint64List21_"), 21, FieldType.UINT64_LIST, false)); + builder.withField(forField(field("fieldInt32List22_"), 22, FieldType.INT32_LIST, false)); + builder.withField(forField(field("fieldFixed64List23_"), 23, FieldType.FIXED64_LIST, false)); + builder.withField(forField(field("fieldFixed32List24_"), 24, FieldType.FIXED32_LIST, false)); + builder.withField(forField(field("fieldBoolList25_"), 25, FieldType.BOOL_LIST, false)); + builder.withField(forField(field("fieldStringList26_"), 26, FieldType.STRING_LIST, false)); + builder.withField( + forRepeatedMessageField( + field("fieldMessageList27_"), 27, FieldType.MESSAGE_LIST, Proto2Message.class)); + builder.withField(forField(field("fieldBytesList28_"), 28, FieldType.BYTES_LIST, false)); + builder.withField(forField(field("fieldUint32List29_"), 29, FieldType.UINT32_LIST, false)); + builder.withField( + forFieldWithEnumVerifier( + field("fieldEnumList30_"), + 30, + FieldType.ENUM_LIST, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField(forField(field("fieldSfixed32List31_"), 31, FieldType.SFIXED32_LIST, false)); + builder.withField(forField(field("fieldSfixed64List32_"), 32, FieldType.SFIXED64_LIST, false)); + builder.withField(forField(field("fieldSint32List33_"), 33, FieldType.SINT32_LIST, false)); + builder.withField(forField(field("fieldSint64List34_"), 34, FieldType.SINT64_LIST, false)); + builder.withField( + forField(field("fieldDoubleListPacked35_"), 35, FieldType.DOUBLE_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFloatListPacked36_"), 36, FieldType.FLOAT_LIST_PACKED, false)); + builder.withField( + forField(field("fieldInt64ListPacked37_"), 37, FieldType.INT64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldUint64ListPacked38_"), 38, FieldType.UINT64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldInt32ListPacked39_"), 39, FieldType.INT32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFixed64ListPacked40_"), 40, FieldType.FIXED64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldFixed32ListPacked41_"), 41, FieldType.FIXED32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldBoolListPacked42_"), 42, FieldType.BOOL_LIST_PACKED, false)); + builder.withField( + forField(field("fieldUint32ListPacked43_"), 43, FieldType.UINT32_LIST_PACKED, false)); + builder.withField( + forFieldWithEnumVerifier( + field("fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forField(field("fieldSfixed32ListPacked45_"), 45, FieldType.SFIXED32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSfixed64ListPacked46_"), 46, FieldType.SFIXED64_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSint32ListPacked47_"), 47, FieldType.SINT32_LIST_PACKED, false)); + builder.withField( + forField(field("fieldSint64ListPacked48_"), 48, FieldType.SINT64_LIST_PACKED, false)); + + builder.withField( + forProto2OptionalField( + field("fieldGroup49_"), 49, FieldType.GROUP, bitField0, 0x00020000, false, null)); + builder.withField( + forRepeatedMessageField( + field("fieldGroupList51_"), + 51, + FieldType.GROUP_LIST, + Proto2Message.FieldGroupList51.class)); + + OneofInfo oneof = new OneofInfo(0, field("testOneofCase_"), field("testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, false, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, false, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, false, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, false, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto2Message.class, false, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, false, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, false, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField( + 69, FieldType.GROUP, oneof, Proto2Message.FieldGroup69.class, false, null)); + + Field bitField1 = field("bitField1_"); + builder.withField( + forProto2RequiredField( + field("fieldRequiredDouble71_"), + 71, + FieldType.DOUBLE, + bitField1, + 0x00000008, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFloat72_"), + 72, + FieldType.FLOAT, + bitField1, + 0x00000010, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredInt6473_"), + 73, + FieldType.INT64, + bitField1, + 0x00000020, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredUint6474_"), + 74, + FieldType.UINT64, + bitField1, + 0x00000040, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredInt3275_"), + 75, + FieldType.INT32, + bitField1, + 0x00000080, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFixed6476_"), + 76, + FieldType.FIXED64, + bitField1, + 0x00000100, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredFixed3277_"), + 77, + FieldType.FIXED32, + bitField1, + 0x00000200, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredBool78_"), 78, FieldType.BOOL, bitField1, 0x00000400, false, null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredString79_"), + 79, + FieldType.STRING, + bitField1, + 0x00000800, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredMessage80_"), + 80, + FieldType.MESSAGE, + bitField1, + 0x00001000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredBytes81_"), + 81, + FieldType.BYTES, + bitField1, + 0x00002000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredUint3282_"), + 82, + FieldType.UINT32, + bitField1, + 0x00004000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredEnum83_"), + 83, + FieldType.ENUM, + bitField1, + 0x00008000, + false, + asVerifier(TestEnum.internalGetValueMap()))); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSfixed3284_"), + 84, + FieldType.SFIXED32, + bitField1, + 0x00010000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSfixed6485_"), + 85, + FieldType.SFIXED64, + bitField1, + 0x00020000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSint3286_"), + 86, + FieldType.SINT32, + bitField1, + 0x00040000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredSint6487_"), + 87, + FieldType.SINT64, + bitField1, + 0x00080000, + false, + null)); + builder.withField( + forProto2RequiredField( + field("fieldRequiredGroup88_"), + 88, + FieldType.GROUP, + bitField1, + 0x00100000, + false, + null)); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroupList51.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup69() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup69.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup69.class, "fieldInt3270_"), + 70, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForRequiredNestedMessage() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(RequiredNestedMessage.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(RequiredNestedMessage.class, "value_"), + 1, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldRequiredGroup88() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldRequiredGroup88.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldRequiredGroup88.class, "fieldInt3289_"), + 89, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2Empty() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageWithExtensions() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(0); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2Testing.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2Testing.FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2Testing.FieldGroupList51.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2Testing.FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withCheckInitialized( + new int[] { + 10, 27, 44, 61, 78, 95, 112, 129, 146, 163, 180, 197, + }); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_double_156", 156)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_float_160", 160)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField(mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static FieldInfo mapFieldInfo(Class<?> clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + fieldName.contains("_enum_") ? asVerifier(TestEnum.internalGetValueMap()) : null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + + private static Field field(String name) { + return field(Proto2Message.class, name); + } + + private static Field field(Class<?> clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static Internal.EnumVerifier asVerifier(final Internal.EnumLiteMap<?> map) { + return new Internal.EnumVerifier() { + @Override + public boolean isInRange(int number) { + return map.findValueByNumber(number) != null; + } + }; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java b/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java new file mode 100755 index 0000000000..2da349424e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2MessageLiteFactory.java @@ -0,0 +1,558 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Creates instances of {@link Proto2MessageLite} based on the tree configuration. */ +public final class Proto2MessageLiteFactory + implements ExperimentalMessageFactory<Proto2MessageLite> { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto2MessageLiteFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto2MessageLiteFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto2MessageLiteFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = + new Proto2MessageLiteFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto2MessageLite newMessage() { + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + builder.setFieldGroup49( + Proto2MessageLite.FieldGroup49.newBuilder().setFieldInt3250(data.getInt())); + + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldGroupList51( + Proto2MessageLite.FieldGroupList51.newBuilder().setFieldInt3252(data.getInt())); + } + + // Set all required fields. + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider<T> { + public T getValue(); + } + + private final MapValueProvider<Integer> integerProvider = + new MapValueProvider<Integer>() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider<Long> longProvider = + new MapValueProvider<Long>() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider<String> stringProvider = + new MapValueProvider<String>() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider<ByteString> bytesProvider = + new MapValueProvider<ByteString>() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider<Boolean> booleanProvider = + new MapValueProvider<Boolean>() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider<Float> floatProvider = + new MapValueProvider<Float>() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider<Double> doubleProvider = + new MapValueProvider<Double>() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider<Proto2MessageLite> messageProvider = + new MapValueProvider<Proto2MessageLite>() { + @Override + public Proto2MessageLite getValue() { + return newMessage(); + } + }; + private final MapValueProvider<Proto2MessageLite.TestEnum> enumProvider = + new MapValueProvider<Proto2MessageLite.TestEnum>() { + @Override + public Proto2MessageLite.TestEnum getValue() { + return Proto2MessageLite.TestEnum.forNumber(data.getEnum()); + } + }; + + private <V> Map<Integer, V> populateIntegerMap(MapValueProvider<V> provider) { + Map<Integer, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private <V> Map<Long, V> populateLongMap(MapValueProvider<V> provider) { + Map<Long, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private <V> Map<String, V> populateStringMap(MapValueProvider<V> provider) { + Map<String, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private <V> Map<Boolean, V> populateBooleanMap(MapValueProvider<V> provider) { + Map<Boolean, V> map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto2MessageLiteWithMaps newMessageWithMaps() { + Proto2MessageLiteWithMaps.Builder builder = Proto2MessageLiteWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } + + public List<Proto2MessageLite> newMessagesMissingRequiredFields() { + List<Proto2MessageLite> results = new ArrayList<>(); + for (int i = 71; i <= 88; ++i) { + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, i); + results.add(builder.buildPartial()); + } + { + // A nested optional message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage10(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested repeated message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.addFieldMessageList27(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + { + // A nested oneof message field is missing required fields. + Proto2MessageLite.Builder builder = Proto2MessageLite.newBuilder(); + populateRequiredFields(builder, INCLUDE_ALL_REQUIRED_FIELDS); + builder.setFieldMessage62(Proto2MessageLite.getDefaultInstance()); + results.add(builder.buildPartial()); + } + return results; + } + + // 0 is not a valid field number so we use it to mean no fields are excluded. + private static final int INCLUDE_ALL_REQUIRED_FIELDS = 0; + + private void populateRequiredFields(Proto2MessageLite.Builder builder, int excludedFieldNumber) { + if (excludedFieldNumber != 71) { + builder.setFieldRequiredDouble71(data.getDouble()); + } + if (excludedFieldNumber != 72) { + builder.setFieldRequiredFloat72(data.getFloat()); + } + if (excludedFieldNumber != 73) { + builder.setFieldRequiredInt6473(data.getLong()); + } + if (excludedFieldNumber != 74) { + builder.setFieldRequiredUint6474(data.getLong()); + } + if (excludedFieldNumber != 75) { + builder.setFieldRequiredInt3275(data.getInt()); + } + if (excludedFieldNumber != 76) { + builder.setFieldRequiredFixed6476(data.getLong()); + } + if (excludedFieldNumber != 77) { + builder.setFieldRequiredFixed3277(data.getInt()); + } + if (excludedFieldNumber != 78) { + builder.setFieldRequiredBool78(data.getBool()); + } + if (excludedFieldNumber != 79) { + builder.setFieldRequiredString79(data.getString()); + } + if (excludedFieldNumber != 80) { + builder.setFieldRequiredMessage80( + Proto2MessageLite.RequiredNestedMessage.newBuilder().setValue(data.getInt())); + } + if (excludedFieldNumber != 81) { + builder.setFieldRequiredBytes81(data.getBytes()); + } + if (excludedFieldNumber != 82) { + builder.setFieldRequiredUint3282(data.getInt()); + } + if (excludedFieldNumber != 83) { + builder.setFieldRequiredEnum83(Proto2MessageLite.TestEnum.forNumber(data.getEnum())); + } + if (excludedFieldNumber != 84) { + builder.setFieldRequiredSfixed3284(data.getInt()); + } + if (excludedFieldNumber != 85) { + builder.setFieldRequiredSfixed6485(data.getLong()); + } + if (excludedFieldNumber != 86) { + builder.setFieldRequiredSint3286(data.getInt()); + } + if (excludedFieldNumber != 87) { + builder.setFieldRequiredSint6487(data.getLong()); + } + if (excludedFieldNumber != 88) { + builder.setFieldRequiredGroup88( + Proto2MessageLite.FieldRequiredGroup88.newBuilder().setFieldInt3289(data.getInt())); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java new file mode 100755 index 0000000000..098e7fe182 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2SchemaTest.java @@ -0,0 +1,49 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto2SchemaTest extends AbstractProto2SchemaTest { + + @Override + protected Schema<Proto2Message> schema() { + return TestSchemas.genericProto2Schema; + } + + @Override + protected void registerSchemas() { + TestSchemas.registerGenericProto2Schemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java b/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java new file mode 100755 index 0000000000..57ac0695e4 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java @@ -0,0 +1,111 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.Descriptors.FieldDescriptor; +import protobuf_unittest.UnittestProto; +import protobuf_unittest.UnittestProto.TestAllExtensions; +import protobuf_unittest.UnittestProto.TestAllTypes; +import junit.framework.TestCase; + +/** Unit tests for proto2 that treats unknown enum values as unknown fields. */ +public class Proto2UnknownEnumValueTest extends TestCase { + FieldDescriptor singularField = + TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); + FieldDescriptor repeatedField = + TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); + byte[] payload = buildPayloadWithUnknownEnumValues(); + + private byte[] buildPayloadWithUnknownEnumValues() { + // Builds a payload with unknown enum values. + UnknownFieldSet.Builder builder = UnknownFieldSet.newBuilder(); + builder.addField( + singularField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) + .addVarint(1901 /* unknown enum value */) + .build()); + builder.addField( + repeatedField.getNumber(), + UnknownFieldSet.Field.newBuilder() + .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) + .addVarint(1902 /* unknown enum value */) + .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) + .addVarint(1903 /* unknown enum value */) + .build()); + return builder.build().toByteArray(); + } + + public void testUnknownEnumValues() throws Exception { + TestAllTypes message = TestAllTypes.parseFrom(payload); + + // Known enum values should be preserved. + assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); + assertEquals(2, message.getRepeatedNestedEnumList().size()); + assertEquals(TestAllTypes.NestedEnum.FOO, message.getRepeatedNestedEnum(0)); + assertEquals(TestAllTypes.NestedEnum.BAZ, message.getRepeatedNestedEnum(1)); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = message.getUnknownFields(); + assertEquals( + 1901, unknown.getField(singularField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1902, unknown.getField(repeatedField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1903, unknown.getField(repeatedField.getNumber()).getVarintList().get(1).longValue()); + } + + public void testExtensionUnknownEnumValues() throws Exception { + ExtensionRegistry registry = ExtensionRegistry.newInstance(); + UnittestProto.registerAllExtensions(registry); + TestAllExtensions message = TestAllExtensions.parseFrom(payload, registry); + + assertEquals( + TestAllTypes.NestedEnum.BAR, + message.getExtension(UnittestProto.optionalNestedEnumExtension)); + assertEquals(2, message.getExtension(UnittestProto.repeatedNestedEnumExtension).size()); + assertEquals( + TestAllTypes.NestedEnum.FOO, + message.getExtension(UnittestProto.repeatedNestedEnumExtension, 0)); + assertEquals( + TestAllTypes.NestedEnum.BAZ, + message.getExtension(UnittestProto.repeatedNestedEnumExtension, 1)); + + // Unknown enum values should be found in UnknownFieldSet. + UnknownFieldSet unknown = message.getUnknownFields(); + assertEquals( + 1901, unknown.getField(singularField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1902, unknown.getField(repeatedField.getNumber()).getVarintList().get(0).longValue()); + assertEquals( + 1903, unknown.getField(repeatedField.getNumber()).getVarintList().get(1).longValue()); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java new file mode 100755 index 0000000000..ac9d6df70e --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java @@ -0,0 +1,49 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto3LiteSchemaTest extends AbstractProto3LiteSchemaTest { + + @Override + protected Schema<Proto3MessageLite> schema() { + return TestSchemasLite.genericProto3LiteSchema; + } + + @Override + protected void registerSchemas() { + TestSchemasLite.registerGenericProto3LiteSchemas(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java new file mode 100755 index 0000000000..33d5fe0fcc --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageFactory.java @@ -0,0 +1,450 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.util.HashMap; +import java.util.Map; + +/** Creates instances of {@link Proto3Message} based on the tree configuration. */ +public final class Proto3MessageFactory implements ExperimentalMessageFactory<Proto3Message> { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto3MessageFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto3MessageFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto3MessageFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = new Proto3MessageFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto3Message newMessage() { + Proto3Message.Builder builder = Proto3Message.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13Value(data.getEnum()); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30Value(data.getEnum()); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44Value(data.getEnum()); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider<T> { + public T getValue(); + } + + private final MapValueProvider<Integer> integerProvider = + new MapValueProvider<Integer>() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider<Long> longProvider = + new MapValueProvider<Long>() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider<String> stringProvider = + new MapValueProvider<String>() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider<ByteString> bytesProvider = + new MapValueProvider<ByteString>() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider<Boolean> booleanProvider = + new MapValueProvider<Boolean>() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider<Float> floatProvider = + new MapValueProvider<Float>() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider<Double> doubleProvider = + new MapValueProvider<Double>() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider<Proto3Message> messageProvider = + new MapValueProvider<Proto3Message>() { + @Override + public Proto3Message getValue() { + return newMessage(); + } + }; + private final MapValueProvider<Proto3Message.TestEnum> enumProvider = + new MapValueProvider<Proto3Message.TestEnum>() { + @Override + public Proto3Message.TestEnum getValue() { + return Proto3Message.TestEnum.forNumber(data.getEnum()); + } + }; + + private <V> Map<Integer, V> populateIntegerMap(MapValueProvider<V> provider) { + Map<Integer, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private <V> Map<Long, V> populateLongMap(MapValueProvider<V> provider) { + Map<Long, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private <V> Map<String, V> populateStringMap(MapValueProvider<V> provider) { + Map<String, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private <V> Map<Boolean, V> populateBooleanMap(MapValueProvider<V> provider) { + Map<Boolean, V> map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto3MessageWithMaps newMessageWithMaps() { + Proto3MessageWithMaps.Builder builder = Proto3MessageWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java new file mode 100755 index 0000000000..364071b35d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageInfoFactory.java @@ -0,0 +1,506 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto3Message}. */ +public final class Proto3MessageInfoFactory implements MessageInfoFactory { + private static final Proto3MessageInfoFactory INSTANCE = new Proto3MessageInfoFactory(); + + private Proto3MessageInfoFactory() {} + + public static Proto3MessageInfoFactory getInstance() { + return INSTANCE; + } + + @Override + public boolean isSupported(Class<?> clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + if (Proto3Message.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3Message(); + } else if (Proto3Empty.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3Empty(); + } else if (Proto3MessageWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto3Message}. Each time this is called, we manually + * go through the entire process of what a message would do if it self-registered its own info, + * including looking up each field by name. This is done for benchmarking purposes, so that we get + * a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto3Message() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + builder.withDefaultInstance(Proto3Message.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField(forField(field("fieldDouble1_"), 1, FieldType.DOUBLE, true)); + builder.withField(forField(field("fieldFloat2_"), 2, FieldType.FLOAT, true)); + builder.withField(forField(field("fieldInt643_"), 3, FieldType.INT64, true)); + builder.withField(forField(field("fieldUint644_"), 4, FieldType.UINT64, true)); + builder.withField(forField(field("fieldInt325_"), 5, FieldType.INT32, true)); + builder.withField(forField(field("fieldFixed646_"), 6, FieldType.FIXED64, true)); + builder.withField(forField(field("fieldFixed327_"), 7, FieldType.FIXED32, true)); + builder.withField(forField(field("fieldBool8_"), 8, FieldType.BOOL, true)); + builder.withField(forField(field("fieldString9_"), 9, FieldType.STRING, true)); + builder.withField(forField(field("fieldMessage10_"), 10, FieldType.MESSAGE, true)); + builder.withField(forField(field("fieldBytes11_"), 11, FieldType.BYTES, true)); + builder.withField(forField(field("fieldUint3212_"), 12, FieldType.UINT32, true)); + builder.withField(forField(field("fieldEnum13_"), 13, FieldType.ENUM, true)); + builder.withField(forField(field("fieldSfixed3214_"), 14, FieldType.SFIXED32, true)); + builder.withField(forField(field("fieldSfixed6415_"), 15, FieldType.SFIXED64, true)); + builder.withField(forField(field("fieldSint3216_"), 16, FieldType.SINT32, true)); + builder.withField(forField(field("fieldSint6417_"), 17, FieldType.SINT64, true)); + builder.withField(forField(field("fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, true)); + builder.withField(forField(field("fieldFloatList19_"), 19, FieldType.FLOAT_LIST, true)); + builder.withField(forField(field("fieldInt64List20_"), 20, FieldType.INT64_LIST, true)); + builder.withField(forField(field("fieldUint64List21_"), 21, FieldType.UINT64_LIST, true)); + builder.withField(forField(field("fieldInt32List22_"), 22, FieldType.INT32_LIST, true)); + builder.withField(forField(field("fieldFixed64List23_"), 23, FieldType.FIXED64_LIST, true)); + builder.withField(forField(field("fieldFixed32List24_"), 24, FieldType.FIXED32_LIST, true)); + builder.withField(forField(field("fieldBoolList25_"), 25, FieldType.BOOL_LIST, true)); + builder.withField(forField(field("fieldStringList26_"), 26, FieldType.STRING_LIST, true)); + builder.withField( + forRepeatedMessageField( + field("fieldMessageList27_"), 27, FieldType.MESSAGE_LIST, Proto3Message.class)); + builder.withField(forField(field("fieldBytesList28_"), 28, FieldType.BYTES_LIST, true)); + builder.withField(forField(field("fieldUint32List29_"), 29, FieldType.UINT32_LIST, true)); + builder.withField(forField(field("fieldEnumList30_"), 30, FieldType.ENUM_LIST, true)); + builder.withField(forField(field("fieldSfixed32List31_"), 31, FieldType.SFIXED32_LIST, true)); + builder.withField(forField(field("fieldSfixed64List32_"), 32, FieldType.SFIXED64_LIST, true)); + builder.withField(forField(field("fieldSint32List33_"), 33, FieldType.SINT32_LIST, true)); + builder.withField(forField(field("fieldSint64List34_"), 34, FieldType.SINT64_LIST, true)); + builder.withField( + forField(field("fieldDoubleListPacked35_"), 35, FieldType.DOUBLE_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFloatListPacked36_"), 36, FieldType.FLOAT_LIST_PACKED, true)); + builder.withField( + forField(field("fieldInt64ListPacked37_"), 37, FieldType.INT64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldUint64ListPacked38_"), 38, FieldType.UINT64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldInt32ListPacked39_"), 39, FieldType.INT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFixed64ListPacked40_"), 40, FieldType.FIXED64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldFixed32ListPacked41_"), 41, FieldType.FIXED32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldBoolListPacked42_"), 42, FieldType.BOOL_LIST_PACKED, true)); + builder.withField( + forField(field("fieldUint32ListPacked43_"), 43, FieldType.UINT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldEnumListPacked44_"), 44, FieldType.ENUM_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSfixed32ListPacked45_"), 45, FieldType.SFIXED32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSfixed64ListPacked46_"), 46, FieldType.SFIXED64_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSint32ListPacked47_"), 47, FieldType.SINT32_LIST_PACKED, true)); + builder.withField( + forField(field("fieldSint64ListPacked48_"), 48, FieldType.SINT64_LIST_PACKED, true)); + + OneofInfo oneof = new OneofInfo(0, field("testOneofCase_"), field("testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, true, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, true, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, true, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, true, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto3Message.class, true, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, true, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, true, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, true, null)); + } + + private StructuralMessageInfo newMessageInfoForProto3Empty() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO3); + return builder.build(); + } + + private StructuralMessageInfo newMessageInfoForProto3MessageWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_double_156", 156)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_float_160", 160)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField(mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto3MessageWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageWithMaps.class, "field_map_uint64_uint64_204", 204)); + return builder.build(); + } + + private static Field field(String name) { + return field(Proto3Message.class, name); + } + + private static Field field(Class<?> clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class<?> clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java new file mode 100755 index 0000000000..2f6947da4f --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteFactory.java @@ -0,0 +1,452 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.util.HashMap; +import java.util.Map; + +/** Creates instances of {@link Proto3MessageLite} based on the tree configuration. */ +public final class Proto3MessageLiteFactory + implements ExperimentalMessageFactory<Proto3MessageLite> { + private final int numRepeatedFields; + private final int branchingFactor; + private final Proto3MessageLiteFactory nextLevel; + private final ExperimentalTestDataProvider data; + + public Proto3MessageLiteFactory( + int numRepeatedFields, int stringLength, int branchingFactor, int treeDepth) { + this( + new ExperimentalTestDataProvider(stringLength), + numRepeatedFields, + branchingFactor, + treeDepth); + } + + private Proto3MessageLiteFactory( + ExperimentalTestDataProvider data, + int numRepeatedFields, + int branchingFactor, + int treeDepth) { + this.numRepeatedFields = numRepeatedFields; + this.branchingFactor = branchingFactor; + this.data = data; + if (treeDepth > 0) { + nextLevel = + new Proto3MessageLiteFactory(data, numRepeatedFields, branchingFactor, treeDepth - 1); + } else { + nextLevel = null; + } + } + + @Override + public ExperimentalTestDataProvider dataProvider() { + return data; + } + + @Override + public Proto3MessageLite newMessage() { + Proto3MessageLite.Builder builder = Proto3MessageLite.newBuilder(); + builder.setFieldDouble1(data.getDouble()); + builder.setFieldFloat2(data.getFloat()); + builder.setFieldInt643(data.getLong()); + builder.setFieldUint644(data.getLong()); + builder.setFieldInt325(data.getInt()); + builder.setFieldFixed646(data.getLong()); + builder.setFieldFixed327(data.getInt()); + builder.setFieldBool8(data.getBool()); + builder.setFieldString9(data.getString()); + // We don't populate the message field. Instead we apply the branching factor to the + // repeated message field below. + builder.setFieldBytes11(data.getBytes()); + builder.setFieldUint3212(data.getInt()); + builder.setFieldEnum13Value(data.getEnum()); + builder.setFieldSfixed3214(data.getInt()); + builder.setFieldSfixed6415(data.getLong()); + builder.setFieldSint3216(data.getInt()); + builder.setFieldSint6417(data.getLong()); + + for (int i = 0; i < numRepeatedFields; ++i) { + builder.addFieldDoubleList18(data.getDouble()); + builder.addFieldFloatList19(data.getFloat()); + builder.addFieldInt64List20(data.getLong()); + builder.addFieldUint64List21(data.getLong()); + builder.addFieldInt32List22(data.getInt()); + builder.addFieldFixed64List23(data.getLong()); + builder.addFieldFixed32List24(data.getInt()); + builder.addFieldBoolList25(data.getBool()); + builder.addFieldStringList26(data.getString()); + // Repeated message field is controlled by the branching factor below. + builder.addFieldBytesList28(data.getBytes()); + builder.addFieldUint32List29(data.getInt()); + builder.addFieldEnumList30Value(data.getEnum()); + builder.addFieldSfixed32List31(data.getInt()); + builder.addFieldSfixed64List32(data.getLong()); + builder.addFieldSint32List33(data.getInt()); + builder.addFieldSint64List34(data.getLong()); + + builder.addFieldDoubleListPacked35(data.getDouble()); + builder.addFieldFloatListPacked36(data.getFloat()); + builder.addFieldInt64ListPacked37(data.getLong()); + builder.addFieldUint64ListPacked38(data.getLong()); + builder.addFieldInt32ListPacked39(data.getInt()); + builder.addFieldFixed64ListPacked40(data.getLong()); + builder.addFieldFixed32ListPacked41(data.getInt()); + builder.addFieldBoolListPacked42(data.getBool()); + builder.addFieldUint32ListPacked43(data.getInt()); + builder.addFieldEnumListPacked44Value(data.getEnum()); + builder.addFieldSfixed32ListPacked45(data.getInt()); + builder.addFieldSfixed64ListPacked46(data.getLong()); + builder.addFieldSint32ListPacked47(data.getInt()); + builder.addFieldSint64ListPacked48(data.getLong()); + } + + // Handle the branching factor. + if (nextLevel != null) { + for (int i = 0; i < branchingFactor; ++i) { + builder.addFieldMessageList27(nextLevel.newMessage()); + } + } + + return builder.build(); + } + + private interface MapValueProvider<T> { + public T getValue(); + } + + private final MapValueProvider<Integer> integerProvider = + new MapValueProvider<Integer>() { + @Override + public Integer getValue() { + return data.getInt(); + } + }; + private final MapValueProvider<Long> longProvider = + new MapValueProvider<Long>() { + @Override + public Long getValue() { + return data.getLong(); + } + }; + private final MapValueProvider<String> stringProvider = + new MapValueProvider<String>() { + @Override + public String getValue() { + return data.getString(); + } + }; + private final MapValueProvider<ByteString> bytesProvider = + new MapValueProvider<ByteString>() { + @Override + public ByteString getValue() { + return data.getBytes(); + } + }; + private final MapValueProvider<Boolean> booleanProvider = + new MapValueProvider<Boolean>() { + @Override + public Boolean getValue() { + return data.getBool(); + } + }; + private final MapValueProvider<Float> floatProvider = + new MapValueProvider<Float>() { + @Override + public Float getValue() { + return data.getFloat(); + } + }; + private final MapValueProvider<Double> doubleProvider = + new MapValueProvider<Double>() { + @Override + public Double getValue() { + return data.getDouble(); + } + }; + private final MapValueProvider<Proto3MessageLite> messageProvider = + new MapValueProvider<Proto3MessageLite>() { + @Override + public Proto3MessageLite getValue() { + return newMessage(); + } + }; + private final MapValueProvider<Proto3MessageLite.TestEnum> enumProvider = + new MapValueProvider<Proto3MessageLite.TestEnum>() { + @Override + public Proto3MessageLite.TestEnum getValue() { + return Proto3MessageLite.TestEnum.forNumber(data.getEnum()); + } + }; + + private <V> Map<Integer, V> populateIntegerMap(MapValueProvider<V> provider) { + Map<Integer, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getInt(), provider.getValue()); + } + return map; + } + + private <V> Map<Long, V> populateLongMap(MapValueProvider<V> provider) { + Map<Long, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getLong(), provider.getValue()); + } + return map; + } + + private <V> Map<String, V> populateStringMap(MapValueProvider<V> provider) { + Map<String, V> map = new HashMap<>(); + for (int i = 0; i < numRepeatedFields; ++i) { + map.put(data.getString(), provider.getValue()); + } + return map; + } + + private <V> Map<Boolean, V> populateBooleanMap(MapValueProvider<V> provider) { + Map<Boolean, V> map = new HashMap<>(); + map.put(false, provider.getValue()); + map.put(true, provider.getValue()); + return map; + } + + public Proto3MessageLiteWithMaps newMessageWithMaps() { + Proto3MessageLiteWithMaps.Builder builder = Proto3MessageLiteWithMaps.newBuilder(); + + builder.putAllFieldMapBoolBool1(populateBooleanMap(booleanProvider)); + builder.putAllFieldMapBoolBytes2(populateBooleanMap(bytesProvider)); + builder.putAllFieldMapBoolDouble3(populateBooleanMap(doubleProvider)); + builder.putAllFieldMapBoolEnum4(populateBooleanMap(enumProvider)); + builder.putAllFieldMapBoolFixed325(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolFixed646(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolFloat7(populateBooleanMap(floatProvider)); + builder.putAllFieldMapBoolInt328(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolInt649(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolMessage10(populateBooleanMap(messageProvider)); + builder.putAllFieldMapBoolSfixed3211(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSfixed6412(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolSint3213(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolSint6414(populateBooleanMap(longProvider)); + builder.putAllFieldMapBoolString15(populateBooleanMap(stringProvider)); + builder.putAllFieldMapBoolUint3216(populateBooleanMap(integerProvider)); + builder.putAllFieldMapBoolUint6417(populateBooleanMap(longProvider)); + builder.putAllFieldMapFixed32Bool18(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapFixed32Bytes19(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapFixed32Double20(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapFixed32Enum21(populateIntegerMap(enumProvider)); + builder.putAllFieldMapFixed32Fixed3222(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Fixed6423(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Float24(populateIntegerMap(floatProvider)); + builder.putAllFieldMapFixed32Int3225(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Int6426(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Message27(populateIntegerMap(messageProvider)); + builder.putAllFieldMapFixed32Sfixed3228(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sfixed6429(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32Sint3230(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Sint6431(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed32String32(populateIntegerMap(stringProvider)); + builder.putAllFieldMapFixed32Uint3233(populateIntegerMap(integerProvider)); + builder.putAllFieldMapFixed32Uint6434(populateIntegerMap(longProvider)); + builder.putAllFieldMapFixed64Bool35(populateLongMap(booleanProvider)); + builder.putAllFieldMapFixed64Bytes36(populateLongMap(bytesProvider)); + builder.putAllFieldMapFixed64Double37(populateLongMap(doubleProvider)); + builder.putAllFieldMapFixed64Enum38(populateLongMap(enumProvider)); + builder.putAllFieldMapFixed64Fixed3239(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Fixed6440(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Float41(populateLongMap(floatProvider)); + builder.putAllFieldMapFixed64Int3242(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Int6443(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Message44(populateLongMap(messageProvider)); + builder.putAllFieldMapFixed64Sfixed3245(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sfixed6446(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64Sint3247(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Sint6448(populateLongMap(longProvider)); + builder.putAllFieldMapFixed64String49(populateLongMap(stringProvider)); + builder.putAllFieldMapFixed64Uint3250(populateLongMap(integerProvider)); + builder.putAllFieldMapFixed64Uint6451(populateLongMap(longProvider)); + builder.putAllFieldMapInt32Bool52(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapInt32Bytes53(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapInt32Double54(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapInt32Enum55(populateIntegerMap(enumProvider)); + builder.putAllFieldMapInt32Fixed3256(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Fixed6457(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Float58(populateIntegerMap(floatProvider)); + builder.putAllFieldMapInt32Int3259(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Int6460(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Message61(populateIntegerMap(messageProvider)); + builder.putAllFieldMapInt32Sfixed3262(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sfixed6463(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32Sint3264(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Sint6465(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt32String66(populateIntegerMap(stringProvider)); + builder.putAllFieldMapInt32Uint3267(populateIntegerMap(integerProvider)); + builder.putAllFieldMapInt32Uint6468(populateIntegerMap(longProvider)); + builder.putAllFieldMapInt64Bool69(populateLongMap(booleanProvider)); + builder.putAllFieldMapInt64Bytes70(populateLongMap(bytesProvider)); + builder.putAllFieldMapInt64Double71(populateLongMap(doubleProvider)); + builder.putAllFieldMapInt64Enum72(populateLongMap(enumProvider)); + builder.putAllFieldMapInt64Fixed3273(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Fixed6474(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Float75(populateLongMap(floatProvider)); + builder.putAllFieldMapInt64Int3276(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Int6477(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Message78(populateLongMap(messageProvider)); + builder.putAllFieldMapInt64Sfixed3279(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sfixed6480(populateLongMap(longProvider)); + builder.putAllFieldMapInt64Sint3281(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Sint6482(populateLongMap(longProvider)); + builder.putAllFieldMapInt64String83(populateLongMap(stringProvider)); + builder.putAllFieldMapInt64Uint3284(populateLongMap(integerProvider)); + builder.putAllFieldMapInt64Uint6485(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed32Bool86(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSfixed32Bytes87(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSfixed32Double88(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSfixed32Enum89(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSfixed32Fixed3290(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Fixed6491(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Float92(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSfixed32Int3293(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Int6494(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Message95(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSfixed32Sfixed3296(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sfixed6497(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32Sint3298(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Sint6499(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed32String100(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSfixed32Uint32101(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSfixed32Uint64102(populateIntegerMap(longProvider)); + builder.putAllFieldMapSfixed64Bool103(populateLongMap(booleanProvider)); + builder.putAllFieldMapSfixed64Bytes104(populateLongMap(bytesProvider)); + builder.putAllFieldMapSfixed64Double105(populateLongMap(doubleProvider)); + builder.putAllFieldMapSfixed64Enum106(populateLongMap(enumProvider)); + builder.putAllFieldMapSfixed64Fixed32107(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Fixed64108(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Float109(populateLongMap(floatProvider)); + builder.putAllFieldMapSfixed64Int32110(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Int64111(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Message112(populateLongMap(messageProvider)); + builder.putAllFieldMapSfixed64Sfixed32113(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sfixed64114(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64Sint32115(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Sint64116(populateLongMap(longProvider)); + builder.putAllFieldMapSfixed64String117(populateLongMap(stringProvider)); + builder.putAllFieldMapSfixed64Uint32118(populateLongMap(integerProvider)); + builder.putAllFieldMapSfixed64Uint64119(populateLongMap(longProvider)); + builder.putAllFieldMapSint32Bool120(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapSint32Bytes121(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapSint32Double122(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapSint32Enum123(populateIntegerMap(enumProvider)); + builder.putAllFieldMapSint32Fixed32124(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Fixed64125(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Float126(populateIntegerMap(floatProvider)); + builder.putAllFieldMapSint32Int32127(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Int64128(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Message129(populateIntegerMap(messageProvider)); + builder.putAllFieldMapSint32Sfixed32130(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sfixed64131(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32Sint32132(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Sint64133(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint32String134(populateIntegerMap(stringProvider)); + builder.putAllFieldMapSint32Uint32135(populateIntegerMap(integerProvider)); + builder.putAllFieldMapSint32Uint64136(populateIntegerMap(longProvider)); + builder.putAllFieldMapSint64Bool137(populateLongMap(booleanProvider)); + builder.putAllFieldMapSint64Bytes138(populateLongMap(bytesProvider)); + builder.putAllFieldMapSint64Double139(populateLongMap(doubleProvider)); + builder.putAllFieldMapSint64Enum140(populateLongMap(enumProvider)); + builder.putAllFieldMapSint64Fixed32141(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Fixed64142(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Float143(populateLongMap(floatProvider)); + builder.putAllFieldMapSint64Int32144(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Int64145(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Message146(populateLongMap(messageProvider)); + builder.putAllFieldMapSint64Sfixed32147(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sfixed64148(populateLongMap(longProvider)); + builder.putAllFieldMapSint64Sint32149(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Sint64150(populateLongMap(longProvider)); + builder.putAllFieldMapSint64String151(populateLongMap(stringProvider)); + builder.putAllFieldMapSint64Uint32152(populateLongMap(integerProvider)); + builder.putAllFieldMapSint64Uint64153(populateLongMap(longProvider)); + builder.putAllFieldMapStringBool154(populateStringMap(booleanProvider)); + builder.putAllFieldMapStringBytes155(populateStringMap(bytesProvider)); + builder.putAllFieldMapStringDouble156(populateStringMap(doubleProvider)); + builder.putAllFieldMapStringEnum157(populateStringMap(enumProvider)); + builder.putAllFieldMapStringFixed32158(populateStringMap(integerProvider)); + builder.putAllFieldMapStringFixed64159(populateStringMap(longProvider)); + builder.putAllFieldMapStringFloat160(populateStringMap(floatProvider)); + builder.putAllFieldMapStringInt32161(populateStringMap(integerProvider)); + builder.putAllFieldMapStringInt64162(populateStringMap(longProvider)); + builder.putAllFieldMapStringMessage163(populateStringMap(messageProvider)); + builder.putAllFieldMapStringSfixed32164(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSfixed64165(populateStringMap(longProvider)); + builder.putAllFieldMapStringSint32166(populateStringMap(integerProvider)); + builder.putAllFieldMapStringSint64167(populateStringMap(longProvider)); + builder.putAllFieldMapStringString168(populateStringMap(stringProvider)); + builder.putAllFieldMapStringUint32169(populateStringMap(integerProvider)); + builder.putAllFieldMapStringUint64170(populateStringMap(longProvider)); + builder.putAllFieldMapUint32Bool171(populateIntegerMap(booleanProvider)); + builder.putAllFieldMapUint32Bytes172(populateIntegerMap(bytesProvider)); + builder.putAllFieldMapUint32Double173(populateIntegerMap(doubleProvider)); + builder.putAllFieldMapUint32Enum174(populateIntegerMap(enumProvider)); + builder.putAllFieldMapUint32Fixed32175(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Fixed64176(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Float177(populateIntegerMap(floatProvider)); + builder.putAllFieldMapUint32Int32178(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Int64179(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Message180(populateIntegerMap(messageProvider)); + builder.putAllFieldMapUint32Sfixed32181(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sfixed64182(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32Sint32183(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Sint64184(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint32String185(populateIntegerMap(stringProvider)); + builder.putAllFieldMapUint32Uint32186(populateIntegerMap(integerProvider)); + builder.putAllFieldMapUint32Uint64187(populateIntegerMap(longProvider)); + builder.putAllFieldMapUint64Bool188(populateLongMap(booleanProvider)); + builder.putAllFieldMapUint64Bytes189(populateLongMap(bytesProvider)); + builder.putAllFieldMapUint64Double190(populateLongMap(doubleProvider)); + builder.putAllFieldMapUint64Enum191(populateLongMap(enumProvider)); + builder.putAllFieldMapUint64Fixed32192(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Fixed64193(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Float194(populateLongMap(floatProvider)); + builder.putAllFieldMapUint64Int32195(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Int64196(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Message197(populateLongMap(messageProvider)); + builder.putAllFieldMapUint64Sfixed32198(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sfixed64199(populateLongMap(longProvider)); + builder.putAllFieldMapUint64Sint32200(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Sint64201(populateLongMap(longProvider)); + builder.putAllFieldMapUint64String202(populateLongMap(stringProvider)); + builder.putAllFieldMapUint64Uint32203(populateLongMap(integerProvider)); + builder.putAllFieldMapUint64Uint64204(populateLongMap(longProvider)); + + return builder.build(); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java new file mode 100755 index 0000000000..66e406af7d --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3MessageLiteInfoFactory.java @@ -0,0 +1,816 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto3MessageLite}. */ +public final class Proto3MessageLiteInfoFactory implements MessageInfoFactory { + private static final Proto3MessageLiteInfoFactory instanceForRawMessageInfo = + new Proto3MessageLiteInfoFactory(true); + private static final Proto3MessageLiteInfoFactory instanceForStructuralMessageInfo = + new Proto3MessageLiteInfoFactory(false); + + public static Proto3MessageLiteInfoFactory getInstanceForRawMessageInfo() { + return instanceForRawMessageInfo; + } + + public static Proto3MessageLiteInfoFactory getInstanceForStructuralMessageInfo() { + return instanceForStructuralMessageInfo; + } + + private final boolean produceRawMessageInfo; + + private Proto3MessageLiteInfoFactory(boolean produceRawMessageInfo) { + this.produceRawMessageInfo = produceRawMessageInfo; + } + + @Override + public boolean isSupported(Class<?> clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + return produceRawMessageInfo ? rawMessageInfoFor(clazz) : structuralMessageInfoFor(clazz); + } + + private MessageInfo rawMessageInfoFor(Class<?> clazz) { + if (Proto3MessageLite.class.isAssignableFrom(clazz)) { + return newRawMessageInfoForProto3MessageLite(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + private MessageInfo newRawMessageInfoForProto3MessageLite() { + java.lang.Object[] objects = + new java.lang.Object[] { + "testOneof_", + "testOneofCase_", + "fieldDouble1_", + "fieldFloat2_", + "fieldInt643_", + "fieldUint644_", + "fieldInt325_", + "fieldFixed646_", + "fieldFixed327_", + "fieldBool8_", + "fieldString9_", + "fieldMessage10_", + "fieldBytes11_", + "fieldUint3212_", + "fieldEnum13_", + "fieldSfixed3214_", + "fieldSfixed6415_", + "fieldSint3216_", + "fieldSint6417_", + "fieldDoubleList18_", + "fieldFloatList19_", + "fieldInt64List20_", + "fieldUint64List21_", + "fieldInt32List22_", + "fieldFixed64List23_", + "fieldFixed32List24_", + "fieldBoolList25_", + "fieldStringList26_", + "fieldMessageList27_", + Proto3MessageLite.class, + "fieldBytesList28_", + "fieldUint32List29_", + "fieldEnumList30_", + "fieldSfixed32List31_", + "fieldSfixed64List32_", + "fieldSint32List33_", + "fieldSint64List34_", + "fieldDoubleListPacked35_", + "fieldFloatListPacked36_", + "fieldInt64ListPacked37_", + "fieldUint64ListPacked38_", + "fieldInt32ListPacked39_", + "fieldFixed64ListPacked40_", + "fieldFixed32ListPacked41_", + "fieldBoolListPacked42_", + "fieldUint32ListPacked43_", + "fieldEnumListPacked44_", + "fieldSfixed32ListPacked45_", + "fieldSfixed64ListPacked46_", + "fieldSint32ListPacked47_", + "fieldSint64ListPacked48_", + Proto3MessageLite.class, + }; + // To update this after a proto change, run protoc on proto3_message_lite.proto and copy over + // the content of the generated buildMessageInfo() method here. + java.lang.String info = + "\u0000@\u0001\u0000\u0001D@\u0000\u001f\u0000\u0001\u0000\u0002\u0001\u0003\u0002" + + "\u0004\u0003\u0005\u0004\u0006\u0005\u0007\u0006\b\u0007\t\u0208\n\t\u000b\n\f\u000b" + + "\r\f\u000e\r\u000f\u000e\u0010\u000f\u0011\u0010\u0012\u0012\u0013\u0013\u0014\u0014" + + "\u0015\u0015\u0016\u0016\u0017\u0017\u0018\u0018\u0019\u0019\u001a\u021a\u001b\u001b" + + "\u001c\u001c\u001d\u001d\u001e\u001e\u001f\u001f !!\"\"##$$%%&&\'\'(())**++,,--" + + "..//0053\u000064\u000075\u000086\u000097\u0000:8\u0000;9\u0000<:\u0000=\u023b\u0000" + + "><\u0000?=\u0000@>\u0000A@\u0000BA\u0000CB\u0000DC\u0000"; + return new RawMessageInfo(Proto3MessageLite.getDefaultInstance(), info, objects); + } + + private MessageInfo structuralMessageInfoFor(Class<?> clazz) { + if (Proto3MessageLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageLite(); + } else if (Proto3EmptyLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3EmptyLite(); + } else if (Proto3MessageLiteWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto3MessageLiteWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto3MessageLite}. Each time this is called, we + * manually go through the entire process of what a message would do if it self-registered its own + * info, including looking up each field by name. This is done for benchmarking purposes, so that + * we get a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto3MessageLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + builder.withDefaultInstance(Proto3MessageLite.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO3); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldDouble1_"), 1, FieldType.DOUBLE, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFloat2_"), 2, FieldType.FLOAT, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldInt643_"), 3, FieldType.INT64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldUint644_"), 4, FieldType.UINT64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldInt325_"), 5, FieldType.INT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFixed646_"), 6, FieldType.FIXED64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldFixed327_"), 7, FieldType.FIXED32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldBool8_"), 8, FieldType.BOOL, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldString9_"), 9, FieldType.STRING, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldMessage10_"), 10, FieldType.MESSAGE, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldBytes11_"), 11, FieldType.BYTES, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldUint3212_"), 12, FieldType.UINT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldEnum13_"), 13, FieldType.ENUM, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSfixed3214_"), 14, FieldType.SFIXED32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSfixed6415_"), 15, FieldType.SFIXED64, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSint3216_"), 16, FieldType.SINT32, true)); + builder.withField( + forField(field(Proto3MessageLite.class, "fieldSint6417_"), 17, FieldType.SINT64, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldDoubleList18_"), 18, FieldType.DOUBLE_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFloatList19_"), 19, FieldType.FLOAT_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt64List20_"), 20, FieldType.INT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint64List21_"), 21, FieldType.UINT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt32List22_"), 22, FieldType.INT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed64List23_"), + 23, + FieldType.FIXED64_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed32List24_"), + 24, + FieldType.FIXED32_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBoolList25_"), 25, FieldType.BOOL_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldStringList26_"), 26, FieldType.STRING_LIST, true)); + builder.withField( + forRepeatedMessageField( + field(Proto3MessageLite.class, "fieldMessageList27_"), + 27, + FieldType.MESSAGE_LIST, + Proto3MessageLite.class)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBytesList28_"), 28, FieldType.BYTES_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint32List29_"), 29, FieldType.UINT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldEnumList30_"), 30, FieldType.ENUM_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed32List31_"), + 31, + FieldType.SFIXED32_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed64List32_"), + 32, + FieldType.SFIXED64_LIST, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint32List33_"), 33, FieldType.SINT32_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint64List34_"), 34, FieldType.SINT64_LIST, true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldDoubleListPacked35_"), + 35, + FieldType.DOUBLE_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFloatListPacked36_"), + 36, + FieldType.FLOAT_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt64ListPacked37_"), + 37, + FieldType.INT64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint64ListPacked38_"), + 38, + FieldType.UINT64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldInt32ListPacked39_"), + 39, + FieldType.INT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed64ListPacked40_"), + 40, + FieldType.FIXED64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldFixed32ListPacked41_"), + 41, + FieldType.FIXED32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldBoolListPacked42_"), + 42, + FieldType.BOOL_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldUint32ListPacked43_"), + 43, + FieldType.UINT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed32ListPacked45_"), + 45, + FieldType.SFIXED32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSfixed64ListPacked46_"), + 46, + FieldType.SFIXED64_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint32ListPacked47_"), + 47, + FieldType.SINT32_LIST_PACKED, + true)); + builder.withField( + forField( + field(Proto3MessageLite.class, "fieldSint64ListPacked48_"), + 48, + FieldType.SINT64_LIST_PACKED, + true)); + + OneofInfo oneof = + new OneofInfo( + 0, + field(Proto3MessageLite.class, "testOneofCase_"), + field(Proto3MessageLite.class, "testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, true, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, true, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, true, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, true, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto3MessageLite.class, true, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, true, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, true, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, true, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, true, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, true, null)); + } + + private StructuralMessageInfo newMessageInfoForProto3EmptyLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO3); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto3MessageLiteWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_double_156", 156)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_float_160", 160)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto3MessageLiteWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static Field field(Class<?> clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class<?> clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java b/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java new file mode 100755 index 0000000000..5cd145be74 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java @@ -0,0 +1,48 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class Proto3SchemaTest extends AbstractProto3SchemaTest { + @Override + protected void registerSchemas() { + TestSchemas.registerGenericProto3Schemas(); + } + + @Override + protected Schema<Proto3Message> schema() { + return TestSchemas.genericProto3Schema; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/TestSchemas.java b/java/core/src/test/java/com/google/protobuf/TestSchemas.java new file mode 100755 index 0000000000..ab0ced4cdc --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/TestSchemas.java @@ -0,0 +1,94 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2Testing; +import com.google.protobuf.testing.Proto2Testing.Proto2Empty; +import com.google.protobuf.testing.Proto2Testing.Proto2Message; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions; +import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps; +import com.google.protobuf.testing.Proto3Testing.Proto3Empty; +import com.google.protobuf.testing.Proto3Testing.Proto3Message; +import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps; + +/** Schemas to support testing. */ +public class TestSchemas { + public static final Schema<Proto2Message> genericProto2Schema = + new ManifestSchemaFactory().createSchema(Proto2Message.class); + public static final Schema<Proto3Message> genericProto3Schema = + new ManifestSchemaFactory().createSchema(Proto3Message.class); + + public static void registerGenericProto2Schemas() { + registerProto2Schemas(); + } + + public static void registerGenericProto3Schemas() { + registerProto3Schemas(); + } + + private static void registerProto2Schemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride(Proto2Message.class, factory.createSchema(Proto2Message.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroup49.class, factory.createSchema(Proto2Message.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroupList51.class, + factory.createSchema(Proto2Message.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldGroup69.class, factory.createSchema(Proto2Message.FieldGroup69.class)); + protobuf.registerSchemaOverride( + Proto2Message.RequiredNestedMessage.class, + factory.createSchema(Proto2Message.RequiredNestedMessage.class)); + protobuf.registerSchemaOverride( + Proto2Message.FieldRequiredGroup88.class, + factory.createSchema(Proto2Message.FieldRequiredGroup88.class)); + protobuf.registerSchemaOverride(Proto2Empty.class, factory.createSchema(Proto2Empty.class)); + protobuf.registerSchemaOverride( + Proto2MessageWithExtensions.class, factory.createSchema(Proto2MessageWithExtensions.class)); + protobuf.registerSchemaOverride( + Proto2Testing.FieldGroup49.class, factory.createSchema(Proto2Testing.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2Testing.FieldGroupList51.class, + factory.createSchema(Proto2Testing.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageWithMaps.class, factory.createSchema(Proto2MessageWithMaps.class)); + } + + private static void registerProto3Schemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride(Proto3Message.class, factory.createSchema(Proto3Message.class)); + protobuf.registerSchemaOverride(Proto3Empty.class, factory.createSchema(Proto3Empty.class)); + protobuf.registerSchemaOverride( + Proto3MessageWithMaps.class, factory.createSchema(Proto3MessageWithMaps.class)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java b/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java new file mode 100755 index 0000000000..28a89d72a1 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/TestSchemasLite.java @@ -0,0 +1,103 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.testing.Proto2TestingLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite; +import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps; + +/** Schemas to support testing. */ +public final class TestSchemasLite { + + public static final Schema<Proto2MessageLite> genericProto2LiteSchema = + new ManifestSchemaFactory().createSchema(Proto2MessageLite.class); + public static final Schema<Proto3MessageLite> genericProto3LiteSchema = + new ManifestSchemaFactory().createSchema(Proto3MessageLite.class); + + public static void registerGenericProto2LiteSchemas() { + registerProto2LiteSchemas(); + } + + public static void registerGenericProto3LiteSchemas() { + registerProto3LiteSchemas(); + } + + private static void registerProto2LiteSchemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride( + Proto2MessageLite.class, factory.createSchema(Proto2MessageLite.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroup49.class, + factory.createSchema(Proto2MessageLite.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroupList51.class, + factory.createSchema(Proto2MessageLite.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldGroup69.class, + factory.createSchema(Proto2MessageLite.FieldGroup69.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.RequiredNestedMessage.class, + factory.createSchema(Proto2MessageLite.RequiredNestedMessage.class)); + protobuf.registerSchemaOverride( + Proto2MessageLite.FieldRequiredGroup88.class, + factory.createSchema(Proto2MessageLite.FieldRequiredGroup88.class)); + protobuf.registerSchemaOverride( + Proto2EmptyLite.class, factory.createSchema(Proto2EmptyLite.class)); + protobuf.registerSchemaOverride( + Proto2MessageLiteWithExtensions.class, + factory.createSchema(Proto2MessageLiteWithExtensions.class)); + protobuf.registerSchemaOverride( + Proto2TestingLite.FieldGroup49.class, + factory.createSchema(Proto2TestingLite.FieldGroup49.class)); + protobuf.registerSchemaOverride( + Proto2TestingLite.FieldGroupList51.class, + factory.createSchema(Proto2TestingLite.FieldGroupList51.class)); + protobuf.registerSchemaOverride( + Proto2MessageLiteWithMaps.class, factory.createSchema(Proto2MessageLiteWithMaps.class)); + } + + private static void registerProto3LiteSchemas() { + Protobuf protobuf = Protobuf.getInstance(); + ManifestSchemaFactory factory = new ManifestSchemaFactory(); + protobuf.registerSchemaOverride( + Proto3MessageLite.class, factory.createSchema(Proto3MessageLite.class)); + protobuf.registerSchemaOverride( + Proto3EmptyLite.class, factory.createSchema(Proto3EmptyLite.class)); + protobuf.registerSchemaOverride( + Proto3MessageLiteWithMaps.class, factory.createSchema(Proto3MessageLiteWithMaps.class)); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index 2fad0b957e..3ed1243489 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -132,6 +132,9 @@ public class TextFormatTest extends TestCase { + " i: 456\n" + "}\n"; + private final TextFormat.Parser parserAllowingUnknownFields = + TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build(); + private final TextFormat.Parser parserAllowingUnknownExtensions = TextFormat.Parser.newBuilder().setAllowUnknownExtensions(true).build(); @@ -502,6 +505,7 @@ public class TextFormatTest extends TestCase { assertEquals(2, builder.getOptionalInt64()); } + private void assertParseError(String error, String text) { // Test merge(). TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -521,6 +525,22 @@ public class TextFormatTest extends TestCase { } } + private void assertParseErrorWithUnknownFields(String error, String text) { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + try { + parserAllowingUnknownFields.merge(text, TestUtil.getFullExtensionRegistry(), builder); + fail("Expected parse exception."); + } catch (TextFormat.ParseException e) { + assertEquals(error, e.getMessage()); + } + } + + private TestAllTypes assertParseSuccessWithUnknownFields(String text) + throws TextFormat.ParseException { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + parserAllowingUnknownFields.merge(text, TestUtil.getFullExtensionRegistry(), builder); + return builder.build(); + } private void assertParseErrorWithUnknownExtensions(String error, String text) { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -1223,6 +1243,7 @@ public class TextFormatTest extends TestCase { // Set to allow unknown fields TextFormat.Parser parser = TextFormat.Parser.newBuilder() + .setAllowUnknownFields(true) .setParseInfoTreeBuilder(treeBuilder) .build(); diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java deleted file mode 100644 index a947d271b0..0000000000 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ /dev/null @@ -1,601 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -package com.google.protobuf; - -import static junit.framework.TestCase.assertEquals; - -import com.google.protobuf.UnittestLite.TestAllExtensionsLite; -import com.google.protobuf.UnittestLite.TestAllTypesLite; -import protobuf_unittest.UnittestProto; -import protobuf_unittest.UnittestProto.ForeignEnum; -import protobuf_unittest.UnittestProto.TestAllExtensions; -import protobuf_unittest.UnittestProto.TestAllTypes; -import protobuf_unittest.UnittestProto.TestEmptyMessage; -import protobuf_unittest.UnittestProto.TestPackedExtensions; -import protobuf_unittest.UnittestProto.TestPackedTypes; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; -import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.Arrays; -import java.util.Map; -import junit.framework.TestCase; - -/** - * Tests for {@link UnknownFieldSetLite}. - * - * @author dweis@google.com (Daniel Weis) - */ -public class UnknownFieldSetLiteTest extends TestCase { - @Override - public void setUp() throws Exception { - allFields = TestUtil.getAllSet(); - allFieldsData = allFields.toByteString(); - emptyMessage = TestEmptyMessage.parseFrom(allFieldsData); - unknownFields = emptyMessage.getUnknownFields(); - } - - TestAllTypes allFields; - ByteString allFieldsData; - - // Constructs a protocol buffer which contains fields with all the same - // numbers as allFieldsData except that each field is some other wire - // type. - private ByteString getBizarroData() throws Exception { - UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.newBuilder(); - - UnknownFieldSet.Field varintField = UnknownFieldSet.Field.newBuilder().addVarint(1).build(); - UnknownFieldSet.Field fixed32Field = UnknownFieldSet.Field.newBuilder().addFixed32(1).build(); - - for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) { - if (entry.getValue().getVarintList().isEmpty()) { - // Original field is not a varint, so use a varint. - bizarroFields.addField(entry.getKey(), varintField); - } else { - // Original field *is* a varint, so use something else. - bizarroFields.addField(entry.getKey(), fixed32Field); - } - } - - return bizarroFields.build().toByteString(); - } - - // An empty message that has been parsed from allFieldsData. So, it has - // unknown fields of every type. - TestEmptyMessage emptyMessage; - UnknownFieldSet unknownFields; - - public void testDefaultInstance() { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance(); - - assertEquals(0, unknownFields.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(unknownFields)); - } - - public void testEmptyInstance() { - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - - assertEquals(0, instance.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(instance)); - assertEquals(UnknownFieldSetLite.getDefaultInstance(), instance); - } - - public void testMergeFieldFrom() throws IOException { - Foo foo = Foo.newBuilder().setValue(2).build(); - - CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray()); - - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - instance.mergeFieldFrom(input.readTag(), input); - - assertEquals(foo.toByteString(), toByteString(instance)); - } - - public void testSerializedSize() throws IOException { - Foo foo = Foo.newBuilder().setValue(2).build(); - - CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray()); - - UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance(); - instance.mergeFieldFrom(input.readTag(), input); - - assertEquals(foo.toByteString().size(), instance.getSerializedSize()); - } - - public void testHashCodeAfterDeserialization() throws IOException { - Foo foo = Foo.newBuilder().setValue(2).build(); - - Foo fooDeserialized = Foo.parseFrom(foo.toByteArray()); - - assertEquals(fooDeserialized, foo); - assertEquals(foo.hashCode(), fooDeserialized.hashCode()); - } - - public void testNewInstanceHashCode() { - UnknownFieldSetLite emptyFieldSet = UnknownFieldSetLite.getDefaultInstance(); - UnknownFieldSetLite paddedFieldSet = UnknownFieldSetLite.newInstance(); - - assertEquals(emptyFieldSet, paddedFieldSet); - assertEquals(emptyFieldSet.hashCode(), paddedFieldSet.hashCode()); - } - - public void testMergeVarintField() throws IOException { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.mergeVarintField(10, 2); - - CodedInputStream input = - CodedInputStream.newInstance(toByteString(unknownFields).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testMergeVarintField_negative() throws IOException { - UnknownFieldSetLite builder = UnknownFieldSetLite.newInstance(); - builder.mergeVarintField(10, -6); - - CodedInputStream input = CodedInputStream.newInstance(toByteString(builder).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(-6, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testEqualsAndHashCode() { - UnknownFieldSetLite unknownFields1 = UnknownFieldSetLite.newInstance(); - unknownFields1.mergeVarintField(10, 2); - - UnknownFieldSetLite unknownFields2 = UnknownFieldSetLite.newInstance(); - unknownFields2.mergeVarintField(10, 2); - - assertEquals(unknownFields1, unknownFields2); - assertEquals(unknownFields1.hashCode(), unknownFields2.hashCode()); - assertFalse(unknownFields1.equals(UnknownFieldSetLite.getDefaultInstance())); - assertFalse(unknownFields1.hashCode() == UnknownFieldSetLite.getDefaultInstance().hashCode()); - } - - public void testMutableCopyOf() throws IOException { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.mergeVarintField(10, 2); - unknownFields = UnknownFieldSetLite.mutableCopyOf(unknownFields, unknownFields); - unknownFields.checkMutable(); - - CodedInputStream input = - CodedInputStream.newInstance(toByteString(unknownFields).toByteArray()); - - int tag = input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertFalse(input.isAtEnd()); - input.readTag(); - assertEquals(10, WireFormat.getTagFieldNumber(tag)); - assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag)); - assertEquals(2, input.readUInt64()); - assertTrue(input.isAtEnd()); - } - - public void testMutableCopyOf_empty() { - UnknownFieldSetLite unknownFields = - UnknownFieldSetLite.mutableCopyOf( - UnknownFieldSetLite.getDefaultInstance(), UnknownFieldSetLite.getDefaultInstance()); - unknownFields.checkMutable(); - - assertEquals(0, unknownFields.getSerializedSize()); - assertEquals(ByteString.EMPTY, toByteString(unknownFields)); - } - - public void testRoundTrips() throws InvalidProtocolBufferException { - Foo foo = - Foo.newBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) - .setExtension(LiteEqualsAndHash.varint, 22) - .setExtension(LiteEqualsAndHash.fixed32, 44) - .setExtension(LiteEqualsAndHash.fixed64, 66L) - .setExtension( - LiteEqualsAndHash.myGroup, - LiteEqualsAndHash.MyGroup.newBuilder().setGroupValue("value").build()) - .build(); - - Foo copy = Foo.parseFrom(foo.toByteArray()); - - assertEquals(foo.getSerializedSize(), copy.getSerializedSize()); - assertFalse(foo.equals(copy)); - - Foo secondCopy = Foo.parseFrom(foo.toByteArray()); - assertEquals(copy, secondCopy); - - ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); - LiteEqualsAndHash.registerAllExtensions(extensionRegistry); - Foo copyOfCopy = Foo.parseFrom(copy.toByteArray(), extensionRegistry); - - assertEquals(foo, copyOfCopy); - } - - public void testMalformedBytes() throws Exception { - try { - Foo.parseFrom("this is a malformed protocol buffer".getBytes(Internal.UTF_8)); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMissingStartGroupTag() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.writeTag(100, WireFormat.WIRETYPE_END_GROUP); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMissingEndGroupTag() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeTag(100, WireFormat.WIRETYPE_START_GROUP); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMismatchingGroupTags() throws IOException { - ByteString.Output byteStringOutput = ByteString.newOutput(); - CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput); - output.writeTag(100, WireFormat.WIRETYPE_START_GROUP); - output.writeGroupNoTag(Foo.newBuilder().setValue(11).build()); - output.writeTag(101, WireFormat.WIRETYPE_END_GROUP); - output.flush(); - - try { - Foo.parseFrom(byteStringOutput.toByteString()); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testTruncatedInput() { - Foo foo = - Foo.newBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) - .setExtension(LiteEqualsAndHash.varint, 22) - .setExtension( - LiteEqualsAndHash.myGroup, - LiteEqualsAndHash.MyGroup.newBuilder().setGroupValue("value").build()) - .build(); - - try { - Foo.parseFrom(foo.toByteString().substring(0, foo.toByteString().size() - 10)); - fail(); - } catch (InvalidProtocolBufferException e) { - // Expected. - } - } - - public void testMakeImmutable() throws Exception { - UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); - unknownFields.makeImmutable(); - - try { - unknownFields.mergeVarintField(1, 1); - fail(); - } catch (UnsupportedOperationException expected) { - } - - try { - unknownFields.mergeLengthDelimitedField(2, ByteString.copyFromUtf8("hello")); - fail(); - } catch (UnsupportedOperationException expected) { - } - - try { - unknownFields.mergeFieldFrom(1, CodedInputStream.newInstance(new byte[0])); - fail(); - } catch (UnsupportedOperationException expected) { - } - } - - public void testEndToEnd() throws Exception { - TestAllTypesLite testAllTypes = TestAllTypesLite.getDefaultInstance(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) { - } - - testAllTypes = TestAllTypesLite.parseFrom(new byte[0]); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) { - } - - testAllTypes = TestAllTypesLite.newBuilder().build(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) { - } - - testAllTypes = TestAllTypesLite.newBuilder().setDefaultBool(true).build(); - try { - testAllTypes.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) { - } - - TestAllExtensionsLite testAllExtensions = - TestAllExtensionsLite.newBuilder() - .mergeFrom( - TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 2) - .build() - .toByteArray()) - .build(); - try { - testAllExtensions.unknownFields.checkMutable(); - fail(); - } catch (UnsupportedOperationException expected) { - } - } - - private ByteString toByteString(UnknownFieldSetLite unknownFields) { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); - try { - unknownFields.writeTo(output); - output.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return ByteString.copyFrom(byteArrayOutputStream.toByteArray()); - } - - public void testSerializeLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - assertEquals(allFieldsData.size(), emptyMessageLite.getSerializedSize()); - ByteString data = emptyMessageLite.toByteString(); - TestAllTypes message = TestAllTypes.parseFrom(data); - TestUtil.assertAllFieldsSet(message); - assertEquals(allFieldsData, data); - } - - public void testAllExtensionsLite() throws Exception { - TestAllExtensions allExtensions = TestUtil.getAllExtensionsSet(); - ByteString allExtensionsData = allExtensions.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parser().parseFrom(allExtensionsData); - ByteString data = emptyMessageLite.toByteString(); - TestAllExtensions message = TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertAllExtensionsSet(message); - assertEquals(allExtensionsData, data); - } - - public void testAllPackedFieldsLite() throws Exception { - TestPackedTypes allPackedFields = TestUtil.getPackedSet(); - ByteString allPackedData = allPackedFields.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allPackedData); - ByteString data = emptyMessageLite.toByteString(); - TestPackedTypes message = TestPackedTypes.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertPackedFieldsSet(message); - assertEquals(allPackedData, data); - } - - public void testAllPackedExtensionsLite() throws Exception { - TestPackedExtensions allPackedExtensions = TestUtil.getPackedExtensionsSet(); - ByteString allPackedExtensionsData = allPackedExtensions.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allPackedExtensionsData); - ByteString data = emptyMessageLite.toByteString(); - TestPackedExtensions message = - TestPackedExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - TestUtil.assertPackedExtensionsSet(message); - assertEquals(allPackedExtensionsData, data); - } - - public void testCopyFromLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.newBuilder().mergeFrom(emptyMessageLite).build(); - assertEquals(emptyMessageLite.toByteString(), emptyMessageLite2.toByteString()); - } - - public void testMergeFromLite() throws Exception { - TestAllTypes message1 = - TestAllTypes.newBuilder() - .setOptionalInt32(1) - .setOptionalString("foo") - .addRepeatedString("bar") - .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ) - .build(); - - TestAllTypes message2 = - TestAllTypes.newBuilder() - .setOptionalInt64(2) - .setOptionalString("baz") - .addRepeatedString("qux") - .setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ) - .build(); - - ByteString data1 = message1.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.parseFrom(data1); - ByteString data2 = message2.toByteString(); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.parseFrom(data2); - - message1 = TestAllTypes.newBuilder(message1).mergeFrom(message2).build(); - emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.newBuilder(emptyMessageLite1) - .mergeFrom(emptyMessageLite2) - .build(); - - data1 = emptyMessageLite1.toByteString(); - message2 = TestAllTypes.parseFrom(data1); - - assertEquals(message1, message2); - } - - public void testWrongTypeTreatedAsUnknownLite() throws Exception { - // Test that fields of the wrong wire type are treated like unknown fields - // when parsing. - - ByteString bizarroData = getBizarroData(); - TestAllTypes allTypesMessage = TestAllTypes.parseFrom(bizarroData); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData); - ByteString data = emptyMessageLite.toByteString(); - TestAllTypes allTypesMessage2 = TestAllTypes.parseFrom(data); - - assertEquals(allTypesMessage.toString(), allTypesMessage2.toString()); - } - - public void testUnknownExtensionsLite() throws Exception { - // Make sure fields are properly parsed to the UnknownFieldSet even when - // they are declared as extension numbers. - - UnittestLite.TestEmptyMessageWithExtensionsLite message = - UnittestLite.TestEmptyMessageWithExtensionsLite.parseFrom(allFieldsData); - - assertEquals(allFieldsData, message.toByteString()); - } - - public void testWrongExtensionTypeTreatedAsUnknownLite() throws Exception { - // Test that fields of the wrong wire type are treated like unknown fields - // when parsing extensions. - - ByteString bizarroData = getBizarroData(); - TestAllExtensions allExtensionsMessage = TestAllExtensions.parseFrom(bizarroData); - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData); - - // All fields should have been interpreted as unknown, so the byte strings - // should be the same. - assertEquals(emptyMessageLite.toByteString(), allExtensionsMessage.toByteString()); - } - - public void testParseUnknownEnumValueLite() throws Exception { - Descriptors.FieldDescriptor singularField = - TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum"); - Descriptors.FieldDescriptor repeatedField = - TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum"); - assertNotNull(singularField); - assertNotNull(repeatedField); - - ByteString data = - UnknownFieldSet.newBuilder() - .addField( - singularField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.BAR.getNumber()) - .addVarint(5) // not valid - .build()) - .addField( - repeatedField.getNumber(), - UnknownFieldSet.Field.newBuilder() - .addVarint(TestAllTypes.NestedEnum.FOO.getNumber()) - .addVarint(4) // not valid - .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber()) - .addVarint(6) // not valid - .build()) - .build() - .toByteString(); - - UnittestLite.TestEmptyMessageLite emptyMessageLite = - UnittestLite.TestEmptyMessageLite.parseFrom(data); - data = emptyMessageLite.toByteString(); - - { - TestAllTypes message = TestAllTypes.parseFrom(data); - assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum()); - assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getRepeatedNestedEnumList()); - assertEquals( - Arrays.asList(5L), - message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); - assertEquals( - Arrays.asList(4L, 6L), - message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); - } - - { - TestAllExtensions message = - TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry()); - assertEquals( - TestAllTypes.NestedEnum.BAR, - message.getExtension(UnittestProto.optionalNestedEnumExtension)); - assertEquals( - Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ), - message.getExtension(UnittestProto.repeatedNestedEnumExtension)); - assertEquals( - Arrays.asList(5L), - message.getUnknownFields().getField(singularField.getNumber()).getVarintList()); - assertEquals( - Arrays.asList(4L, 6L), - message.getUnknownFields().getField(repeatedField.getNumber()).getVarintList()); - } - } - - public void testClearLite() throws Exception { - UnittestLite.TestEmptyMessageLite emptyMessageLite1 = - UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData); - UnittestLite.TestEmptyMessageLite emptyMessageLite2 = - UnittestLite.TestEmptyMessageLite.newBuilder().mergeFrom(emptyMessageLite1).clear().build(); - assertEquals(0, emptyMessageLite2.getSerializedSize()); - ByteString data = emptyMessageLite2.toByteString(); - assertEquals(0, data.size()); - } -} diff --git a/java/core/src/test/java/com/google/protobuf/Utf8Test.java b/java/core/src/test/java/com/google/protobuf/Utf8Test.java new file mode 100755 index 0000000000..bc3c985104 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Utf8Test.java @@ -0,0 +1,232 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Random; +import java.util.regex.Pattern; +import junit.framework.TestCase; + +/** Unit tests for {@link Utf8}. */ +public class Utf8Test extends TestCase { + private static final int NUM_CHARS = 16384; + + private static final Utf8.Processor safeProcessor = new Utf8.SafeProcessor(); + private static final Utf8.Processor unsafeProcessor = new Utf8.UnsafeProcessor(); + + public void testEncode() { + assertEncoding(randomString(0x80)); + assertEncoding(randomString(0x90)); + assertEncoding(randomString(0x800)); + assertEncoding(randomString(0x10000)); + assertEncoding(randomString(0x10ffff)); + } + + public void testEncode_insufficientSpace() { + assertEncoding_insufficientSpace(randomString(0x80)); + assertEncoding_insufficientSpace(randomString(0x90)); + assertEncoding_insufficientSpace(randomString(0x800)); + assertEncoding_insufficientSpace(randomString(0x10000)); + assertEncoding_insufficientSpace(randomString(0x10ffff)); + } + + public void testValid() { + assertIsValid(new byte[] {(byte) 0xE0, (byte) 0xB9, (byte) 0x96}, true); + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0xB2, (byte) 0x83, (byte) 0xBC}, true); + } + + public void testOverlongIsInvalid() { + assertIsValid(new byte[] {(byte) 0xC0, (byte) 0x81}, false); + assertIsValid(new byte[] {(byte) 0xE0, (byte) 0x81, (byte) 0x81}, false); + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + } + + public void testMaxCodepointExceeded() { + // byte1 > 0xF4 + assertIsValid(new byte[] {(byte) 0xF5, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + } + + public void testInvalidSurrogateCodepoint() { + assertIsValid(new byte[] {(byte) 0xED, (byte) 0xA1, (byte) 0x81}, false); + + // byte1 == 0xF0 && byte2 < 0x90 + assertIsValid(new byte[] {(byte) 0xF0, (byte) 0x81, (byte) 0x81, (byte) 0x81}, false); + // byte1 == 0xF4 && byte2 > 0x8F + assertIsValid(new byte[] {(byte) 0xF4, (byte) 0x90, (byte) 0x81, (byte) 0x81}, false); + } + + private static String randomString(int maxCodePoint) { + final long seed = 99; + final Random rnd = new Random(seed); + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < NUM_CHARS; j++) { + int codePoint; + do { + codePoint = rnd.nextInt(maxCodePoint); + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + return sb.toString(); + } + + private static void assertIsValid(byte[] data, boolean valid) { + assertEquals("isValidUtf8[ARRAY]", valid, safeProcessor.isValidUtf8(data, 0, data.length)); + assertEquals( + "isValidUtf8[ARRAY_UNSAFE]", valid, unsafeProcessor.isValidUtf8(data, 0, data.length)); + + ByteBuffer buffer = ByteBuffer.wrap(data); + assertEquals( + "isValidUtf8[NIO_HEAP]", + valid, + safeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + + // Direct buffers. + buffer = ByteBuffer.allocateDirect(data.length); + buffer.put(data); + buffer.flip(); + assertEquals( + "isValidUtf8[NIO_DEFAULT]", + valid, + safeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + assertEquals( + "isValidUtf8[NIO_UNSAFE]", + valid, + unsafeProcessor.isValidUtf8(buffer, buffer.position(), buffer.remaining())); + } + + private static void assertEncoding(String message) { + byte[] expected = message.getBytes(Internal.UTF_8); + byte[] output = encodeToByteArray(message, expected.length, safeProcessor); + assertTrue("encodeUtf8[ARRAY]", Arrays.equals(expected, output)); + + output = encodeToByteArray(message, expected.length, unsafeProcessor); + assertTrue("encodeUtf8[ARRAY_UNSAFE]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, false, safeProcessor); + assertTrue("encodeUtf8[NIO_HEAP]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, true, safeProcessor); + assertTrue("encodeUtf8[NIO_DEFAULT]", Arrays.equals(expected, output)); + + output = encodeToByteBuffer(message, expected.length, true, unsafeProcessor); + assertTrue("encodeUtf8[NIO_UNSAFE]", Arrays.equals(expected, output)); + } + + private void assertEncoding_insufficientSpace(String message) { + final int length = message.length() - 1; + Class<ArrayIndexOutOfBoundsException> clazz = ArrayIndexOutOfBoundsException.class; + + try { + encodeToByteArray(message, length, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // byte[] + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteArray(message, length, unsafeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + assertExceptionType(t, clazz); + // byte[] + unsafeProcessor will exit early, so we have can match the message. + assertExceptionMessage(t, length); + } + + try { + encodeToByteBuffer(message, length, false, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // ByteBuffer + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteBuffer(message, length, true, safeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // ByteBuffer + safeProcessor will not exit early. We can't match the message since we don't + // know which char/index due to random input. + } + + try { + encodeToByteBuffer(message, length, true, unsafeProcessor); + fail("Expected " + clazz.getSimpleName()); + } catch (Throwable t) { + // Expected + assertExceptionType(t, clazz); + // Direct ByteBuffer + unsafeProcessor will exit early if it's not on Android, so we can + // match the message. On Android, a direct ByteBuffer will have hasArray() being true and + // it will take a different code path and produces a different message. + if (!Android.isOnAndroidDevice()) { + assertExceptionMessage(t, length); + } + } + } + + private static byte[] encodeToByteArray(String message, int length, Utf8.Processor processor) { + byte[] output = new byte[length]; + processor.encodeUtf8(message, output, 0, output.length); + return output; + } + + private static byte[] encodeToByteBuffer( + String message, int length, boolean direct, Utf8.Processor processor) { + ByteBuffer buffer = direct ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); + + processor.encodeUtf8(message, buffer); + buffer.flip(); + + byte[] output = new byte[buffer.remaining()]; + buffer.get(output); + return output; + } + + private <T extends Throwable> void assertExceptionType(Throwable t, Class<T> expected) { + if (!expected.isAssignableFrom(t.getClass())) { + fail("Expected " + expected.getSimpleName() + ", but found " + t.getClass().getSimpleName()); + } + } + + private void assertExceptionMessage(Throwable t, int index) { + String pattern = "Failed writing (.) at index " + index; + assertTrue( + t.getMessage() + " does not match pattern " + pattern, + Pattern.matches(pattern, t.getMessage())); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/Utf8Utils.java b/java/core/src/test/java/com/google/protobuf/Utf8Utils.java new file mode 100755 index 0000000000..dbd3d3961a --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/Utf8Utils.java @@ -0,0 +1,193 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static java.lang.Character.MIN_HIGH_SURROGATE; +import static java.lang.Character.MIN_LOW_SURROGATE; +import static java.lang.Character.MIN_SURROGATE; + +import java.util.Random; + +/** Utilities for benchmarking UTF-8. */ +final class Utf8Utils { + private Utf8Utils() {} + + static class MaxCodePoint { + final int value; + + /** + * Convert the input string to a code point. Accepts regular decimal numerals, hex strings, and + * some symbolic names meaningful to humans. + */ + private static int decode(String userFriendly) { + try { + return Integer.decode(userFriendly); + } catch (NumberFormatException ignored) { + if (userFriendly.matches("(?i)(?:American|English|ASCII)")) { + // 1-byte UTF-8 sequences - "American" ASCII text + return 0x80; + } else if (userFriendly.matches("(?i)(?:Danish|Latin|Western.*European)")) { + // Mostly 1-byte UTF-8 sequences, mixed with occasional 2-byte + // sequences - "Western European" text + return 0x90; + } else if (userFriendly.matches("(?i)(?:Greek|Cyrillic|European|ISO.?8859)")) { + // Mostly 2-byte UTF-8 sequences - "European" text + return 0x800; + } else if (userFriendly.matches("(?i)(?:Chinese|Han|Asian|BMP)")) { + // Mostly 3-byte UTF-8 sequences - "Asian" text + return Character.MIN_SUPPLEMENTARY_CODE_POINT; + } else if (userFriendly.matches("(?i)(?:Cuneiform|rare|exotic|supplementary.*)")) { + // Mostly 4-byte UTF-8 sequences - "rare exotic" text + return Character.MAX_CODE_POINT; + } else { + throw new IllegalArgumentException("Can't decode codepoint " + userFriendly); + } + } + } + + public static MaxCodePoint valueOf(String userFriendly) { + return new MaxCodePoint(userFriendly); + } + + public MaxCodePoint(String userFriendly) { + value = decode(userFriendly); + } + } + + /** + * The Utf8 distribution of real data. The distribution is an array with length 4. + * "distribution[i]" means the total number of characters who are encoded with (i + 1) bytes. + * + * <p>GMM_UTF8_DISTRIBUTION is the distribution of gmm data set. GSR_UTF8_DISTRIBUTION is the + * distribution of gsreq/gsresp data set + */ + public enum Utf8Distribution { + GMM_UTF8_DISTRIBUTION { + @Override + public int[] getDistribution() { + return new int[] {53059, 104, 0, 0}; + } + }, + GSR_UTF8_DISTRIBUTION { + @Override + public int[] getDistribution() { + return new int[] {119458, 74, 2706, 0}; + } + }; + + public abstract int[] getDistribution(); + } + + /** + * Creates an array of random strings. + * + * @param stringCount the number of strings to be created. + * @param charCount the number of characters per string. + * @param maxCodePoint the maximum code point for the characters in the strings. + * @return an array of random strings. + */ + static String[] randomStrings(int stringCount, int charCount, MaxCodePoint maxCodePoint) { + final long seed = 99; + final Random rnd = new Random(seed); + String[] strings = new String[stringCount]; + for (int i = 0; i < stringCount; i++) { + strings[i] = randomString(rnd, charCount, maxCodePoint); + } + return strings; + } + + /** + * Creates a random string + * + * @param rnd the random generator. + * @param charCount the number of characters per string. + * @param maxCodePoint the maximum code point for the characters in the strings. + */ + static String randomString(Random rnd, int charCount, MaxCodePoint maxCodePoint) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < charCount; i++) { + int codePoint; + do { + codePoint = rnd.nextInt(maxCodePoint.value); + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + return sb.toString(); + } + + /** Character.isSurrogate was added in Java SE 7. */ + static boolean isSurrogate(int c) { + return Character.MIN_HIGH_SURROGATE <= c && c <= Character.MAX_LOW_SURROGATE; + } + + /** + * Creates an array of random strings according to UTF8 distribution. + * + * @param stringCount the number of strings to be created. + * @param charCount the number of characters per string. + */ + static String[] randomStringsWithDistribution( + int stringCount, int charCount, Utf8Distribution utf8Distribution) { + final int[] distribution = utf8Distribution.getDistribution(); + for (int i = 0; i < 3; i++) { + distribution[i + 1] += distribution[i]; + } + final long seed = 99; + final Random rnd = new Random(seed); + String[] strings = new String[stringCount]; + for (int i = 0; i < stringCount; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < charCount; j++) { + int codePoint; + do { + codePoint = rnd.nextInt(distribution[3]); + if (codePoint < distribution[0]) { + // 1 bytes + sb.append(0x7F); + } else if (codePoint < distribution[1]) { + // 2 bytes + sb.append(0x7FF); + } else if (codePoint < distribution[2]) { + // 3 bytes + sb.append(MIN_SURROGATE - 1); + } else { + // 4 bytes + sb.append(MIN_HIGH_SURROGATE); + sb.append(MIN_LOW_SURROGATE); + } + } while (Utf8Utils.isSurrogate(codePoint)); + sb.appendCodePoint(codePoint); + } + strings[i] = sb.toString(); + } + return strings; + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java b/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java new file mode 100755 index 0000000000..a725d413ff --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WireFormatLiteTest.java @@ -0,0 +1,1023 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.UnittestLite.optionalForeignEnumExtensionLite; + +import com.google.protobuf.UnittestLite.ForeignEnumLite; +import com.google.protobuf.UnittestLite.TestAllExtensionsLite; +import com.google.protobuf.UnittestLite.TestPackedExtensionsLite; +import map_test.MapForProto2TestProto; +import map_test.MapTestProto.TestMap; +import protobuf_unittest.UnittestMset.RawMessageSet; +import protobuf_unittest.UnittestMset.TestMessageSetExtension1; +import protobuf_unittest.UnittestMset.TestMessageSetExtension2; +import protobuf_unittest.UnittestProto; +import protobuf_unittest.UnittestProto.TestAllExtensions; +import protobuf_unittest.UnittestProto.TestAllTypes; +import protobuf_unittest.UnittestProto.TestExtensionInsideTable; +import protobuf_unittest.UnittestProto.TestFieldOrderings; +import protobuf_unittest.UnittestProto.TestOneof2; +import protobuf_unittest.UnittestProto.TestOneofBackwardsCompatible; +import protobuf_unittest.UnittestProto.TestPackedExtensions; +import protobuf_unittest.UnittestProto.TestPackedTypes; +import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; +import proto3_unittest.UnittestProto3; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import junit.framework.TestCase; + +public class WireFormatLiteTest extends TestCase { + public void testSerializeExtensionsLite() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllExtensions then parse it as TestAllTypes + // it should work. + + TestAllExtensionsLite message = TestUtilLite.getAllLiteExtensionsSet(); + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializePackedExtensionsLite() throws Exception { + // TestPackedTypes and TestPackedExtensions should have compatible wire + // formats; check that they serialize to the same string. + TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + TestPackedTypes message2 = TestUtil.getPackedSet(); + ByteString rawBytes2 = message2.toByteString(); + + assertEquals(rawBytes, rawBytes2); + } + + public void testParseExtensionsLite() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllTypes then parse it as TestAllExtensions + // it should work. + + TestAllTypes message = TestUtil.getAllSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registryLite = TestUtilLite.getExtensionRegistryLite(); + + TestAllExtensionsLite message2 = TestAllExtensionsLite.parseFrom(rawBytes, registryLite); + TestUtil.assertAllExtensionsSet(message2); + message2 = TestAllExtensionsLite.parseFrom(message.toByteArray(), registryLite); + TestUtil.assertAllExtensionsSet(message2); + } + + public void testParsePackedExtensionsLite() throws Exception { + // Ensure that packed extensions can be properly parsed. + TestPackedExtensionsLite message = TestUtilLite.getLitePackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtilLite.getExtensionRegistryLite(); + + TestPackedExtensionsLite message2 = TestPackedExtensionsLite.parseFrom(rawBytes, registry); + TestUtil.assertPackedExtensionsSet(message2); + message2 = TestPackedExtensionsLite.parseFrom(message.toByteArray(), registry); + TestUtil.assertPackedExtensionsSet(message2); + } + + public void testSerialization() throws Exception { + TestAllTypes message = TestUtil.getAllSet(); + + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializationPacked() throws Exception { + TestPackedTypes message = TestUtil.getPackedSet(); + + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes); + + TestUtil.assertPackedFieldsSet(message2); + } + + public void testSerializeExtensions() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllExtensions then parse it as TestAllTypes + // it should work. + + TestAllExtensions message = TestUtil.getAllExtensionsSet(); + ByteString rawBytes = message.toByteString(); + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); + + TestUtil.assertAllFieldsSet(message2); + } + + public void testSerializePackedExtensions() throws Exception { + // TestPackedTypes and TestPackedExtensions should have compatible wire + // formats; check that they serialize to the same string. + TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + TestPackedTypes message2 = TestUtil.getPackedSet(); + ByteString rawBytes2 = message2.toByteString(); + + assertEquals(rawBytes, rawBytes2); + } + + public void testSerializationPackedWithoutGetSerializedSize() throws Exception { + // Write directly to an OutputStream, without invoking getSerializedSize() + // This used to be a bug where the size of a packed field was incorrect, + // since getSerializedSize() was never invoked. + TestPackedTypes message = TestUtil.getPackedSet(); + + // Directly construct a CodedOutputStream around the actual OutputStream, + // in case writeTo(OutputStream output) invokes getSerializedSize(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(outputStream); + + message.writeTo(codedOutput); + + codedOutput.flush(); + + TestPackedTypes message2 = TestPackedTypes.parseFrom(outputStream.toByteArray()); + + TestUtil.assertPackedFieldsSet(message2); + } + + public void testParseExtensions() throws Exception { + // TestAllTypes and TestAllExtensions should have compatible wire formats, + // so if we serialize a TestAllTypes then parse it as TestAllExtensions + // it should work. + + TestAllTypes message = TestUtil.getAllSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); + + TestAllExtensions message2 = TestAllExtensions.parseFrom(rawBytes, registry); + + TestUtil.assertAllExtensionsSet(message2); + } + + public void testParsePackedExtensions() throws Exception { + // Ensure that packed extensions can be properly parsed. + TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); + ByteString rawBytes = message.toByteString(); + + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); + + TestPackedExtensions message2 = TestPackedExtensions.parseFrom(rawBytes, registry); + + TestUtil.assertPackedExtensionsSet(message2); + } + + public void testSerializeDelimited() throws Exception { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + TestUtil.getAllSet().writeDelimitedTo(output); + output.write(12); + TestUtil.getPackedSet().writeDelimitedTo(output); + output.write(34); + + ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); + + TestUtil.assertAllFieldsSet(TestAllTypes.parseDelimitedFrom(input)); + assertEquals(12, input.read()); + TestUtil.assertPackedFieldsSet(TestPackedTypes.parseDelimitedFrom(input)); + assertEquals(34, input.read()); + assertEquals(-1, input.read()); + + // We're at EOF, so parsing again should return null. + assertNull(TestAllTypes.parseDelimitedFrom(input)); + } + + private ExtensionRegistryLite getTestFieldOrderingsRegistry() { + ExtensionRegistryLite result = ExtensionRegistryLite.newInstance(); + result.add(UnittestProto.myExtensionInt); + result.add(UnittestProto.myExtensionString); + return result; + } + + public void testParseMultipleExtensionRanges() throws Exception { + // Make sure we can parse a message that contains multiple extensions + // ranges. + TestFieldOrderings source = + TestFieldOrderings.newBuilder() + .setMyInt(1) + .setMyString("foo") + .setMyFloat(1.0F) + .setExtension(UnittestProto.myExtensionInt, 23) + .setExtension(UnittestProto.myExtensionString, "bar") + .build(); + TestFieldOrderings dest = + TestFieldOrderings.parseFrom(source.toByteString(), getTestFieldOrderingsRegistry()); + assertEquals(source, dest); + } + + private static ExtensionRegistryLite getTestExtensionInsideTableRegistry() { + ExtensionRegistryLite result = ExtensionRegistryLite.newInstance(); + result.add(UnittestProto.testExtensionInsideTableExtension); + return result; + } + + public void testExtensionInsideTable() throws Exception { + // Make sure the extension within the range of table is parsed correctly in experimental + // runtime. + TestExtensionInsideTable source = + TestExtensionInsideTable.newBuilder() + .setField1(1) + .setExtension(UnittestProto.testExtensionInsideTableExtension, 23) + .build(); + TestExtensionInsideTable dest = + TestExtensionInsideTable.parseFrom( + source.toByteString(), getTestExtensionInsideTableRegistry()); + assertEquals(source, dest); + } + + private static final int UNKNOWN_TYPE_ID = 1550055; + private static final int TYPE_ID_1 = 1545008; + private static final int TYPE_ID_2 = 1547769; + + public void testSerializeMessageSetEagerly() throws Exception { + testSerializeMessageSetWithFlag(true); + } + + public void testSerializeMessageSetNotEagerly() throws Exception { + testSerializeMessageSetWithFlag(false); + } + + private void testSerializeMessageSetWithFlag(boolean eagerParsing) throws Exception { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream); + output.writeRawMessageSetExtension(UNKNOWN_TYPE_ID, ByteString.copyFromUtf8("bar")); + output.flush(); + byte[] messageSetBytes = byteArrayOutputStream.toByteArray(); + + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + // Set up a TestMessageSet with two known messages and an unknown one. + TestMessageSet messageSet = + TestMessageSet.newBuilder() + .setExtension( + TestMessageSetExtension1.messageSetExtension, + TestMessageSetExtension1.newBuilder().setI(123).build()) + .setExtension( + TestMessageSetExtension2.messageSetExtension, + TestMessageSetExtension2.newBuilder().setStr("foo").build()) + .mergeFrom(messageSetBytes) + .build(); + + ByteString data = messageSet.toByteString(); + + // Parse back using RawMessageSet and check the contents. + RawMessageSet raw = RawMessageSet.parseFrom(data); + + assertEquals(3, raw.getItemCount()); + assertEquals(TYPE_ID_1, raw.getItem(0).getTypeId()); + assertEquals(TYPE_ID_2, raw.getItem(1).getTypeId()); + assertEquals(UNKNOWN_TYPE_ID, raw.getItem(2).getTypeId()); + + TestMessageSetExtension1 message1 = + TestMessageSetExtension1.parseFrom(raw.getItem(0).getMessage()); + assertEquals(123, message1.getI()); + + TestMessageSetExtension2 message2 = + TestMessageSetExtension2.parseFrom(raw.getItem(1).getMessage()); + assertEquals("foo", message2.getStr()); + + assertEquals("bar", raw.getItem(2).getMessage().toStringUtf8()); + } + + public void testParseMessageSetEagerly() throws Exception { + testParseMessageSetWithFlag(true); + } + + public void testParseMessageSetNotEagerly() throws Exception { + testParseMessageSetWithFlag(false); + } + + private void testParseMessageSetWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + extensionRegistry.add(TestMessageSetExtension2.messageSetExtension); + + // Set up a RawMessageSet with two known messages and an unknown one. + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(TYPE_ID_2) + .setMessage( + TestMessageSetExtension2.newBuilder().setStr("foo").build().toByteString()) + .build()) + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(UNKNOWN_TYPE_ID) + .setMessage(ByteString.copyFromUtf8("bar")) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and check the contents. + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + assertEquals( + "foo", messageSet.getExtension(TestMessageSetExtension2.messageSetExtension).getStr()); + } + + public void testParseMessageSetExtensionEagerly() throws Exception { + testParseMessageSetExtensionWithFlag(true); + } + + public void testParseMessageSetExtensionNotEagerly() throws Exception { + testParseMessageSetExtensionWithFlag(false); + } + + private void testParseMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and check the contents. + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + public void testMergeLazyMessageSetExtensionEagerly() throws Exception { + testMergeLazyMessageSetExtensionWithFlag(true); + } + + public void testMergeLazyMessageSetExtensionNotEagerly() throws Exception { + testMergeLazyMessageSetExtensionWithFlag(false); + } + + private void testMergeLazyMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + ByteString data = raw.toByteString(); + + // Parse as a TestMessageSet and store value into lazy field + TestMessageSet messageSet = TestMessageSet.parseFrom(data, extensionRegistry); + // Merge lazy field check the contents. + messageSet = messageSet.toBuilder().mergeFrom(data, extensionRegistry).build(); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + public void testMergeMessageSetExtensionEagerly() throws Exception { + testMergeMessageSetExtensionWithFlag(true); + } + + public void testMergeMessageSetExtensionNotEagerly() throws Exception { + testMergeMessageSetExtensionWithFlag(false); + } + + private void testMergeMessageSetExtensionWithFlag(boolean eagerParsing) throws Exception { + ExtensionRegistryLite.setEagerlyParseMessageSets(eagerParsing); + ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance(); + extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); + + // Set up a RawMessageSet with a known messages. + int typeId1 = 1545008; + RawMessageSet raw = + RawMessageSet.newBuilder() + .addItem( + RawMessageSet.Item.newBuilder() + .setTypeId(typeId1) + .setMessage( + TestMessageSetExtension1.newBuilder().setI(123).build().toByteString()) + .build()) + .build(); + + // Serialize RawMessageSet unnormally (message value before type id) + ByteString.CodedBuilder out = ByteString.newCodedBuilder(raw.getSerializedSize()); + CodedOutputStream output = out.getCodedOutput(); + List<RawMessageSet.Item> items = raw.getItemList(); + for (RawMessageSet.Item item : items) { + output.writeTag(1, WireFormat.WIRETYPE_START_GROUP); + output.writeBytes(3, item.getMessage()); + output.writeInt32(2, item.getTypeId()); + output.writeTag(1, WireFormat.WIRETYPE_END_GROUP); + } + ByteString data = out.build(); + + // Merge bytes into TestMessageSet and check the contents. + TestMessageSet messageSet = + TestMessageSet.newBuilder().mergeFrom(data, extensionRegistry).build(); + assertEquals(123, messageSet.getExtension(TestMessageSetExtension1.messageSetExtension).getI()); + } + + // ================================================================ + // oneof + public void testOneofWireFormat() throws Exception { + TestOneof2.Builder builder = TestOneof2.newBuilder(); + TestUtil.setOneof(builder); + TestOneof2 message = builder.build(); + ByteString rawBytes = message.toByteString(); + + assertEquals(rawBytes.size(), message.getSerializedSize()); + + TestOneof2 message2 = TestOneof2.parseFrom(rawBytes); + TestUtil.assertOneofSet(message2); + } + + public void testOneofOnlyLastSet() throws Exception { + TestOneofBackwardsCompatible source = + TestOneofBackwardsCompatible.newBuilder().setFooInt(100).setFooString("101").build(); + + ByteString rawBytes = source.toByteString(); + TestOneof2 message = TestOneof2.parseFrom(rawBytes); + assertFalse(message.hasFooInt()); + assertTrue(message.hasFooString()); + } + + private void assertInvalidWireFormat( + MessageLite defaultInstance, byte[] data, int offset, int length) { + // Test all combinations: (builder vs parser) x (byte[] vs. InputStream). + try { + defaultInstance.newBuilderForType().mergeFrom(data, offset, length); + fail("Expected exception"); + } catch (InvalidProtocolBufferException e) { + // Pass. + } + try { + defaultInstance.getParserForType().parseFrom(data, offset, length); + fail("Expected exception"); + } catch (InvalidProtocolBufferException e) { + // Pass. + } + try { + InputStream input = new ByteArrayInputStream(data, offset, length); + defaultInstance.newBuilderForType().mergeFrom(input); + fail("Expected exception"); + } catch (IOException e) { + // Pass. + } + try { + InputStream input = new ByteArrayInputStream(data, offset, length); + defaultInstance.getParserForType().parseFrom(input); + fail("Expected exception"); + } catch (IOException e) { + // Pass. + } + } + + private void assertInvalidWireFormat(MessageLite defaultInstance, byte[] data) { + assertInvalidWireFormat(defaultInstance, data, 0, data.length); + } + + private void assertInvalidWireFormat(byte[] data) { + assertInvalidWireFormat(TestAllTypes.getDefaultInstance(), data); + assertInvalidWireFormat(UnittestProto3.TestAllTypes.getDefaultInstance(), data); + } + + public void testParserRejectInvalidTag() throws Exception { + byte[] invalidTags = + new byte[] { + // Zero tag is not allowed. + 0, + // Invalid wire types. + (byte) WireFormat.makeTag(1, 6), + (byte) WireFormat.makeTag(1, 7), + // Field number 0 is not allowed. + (byte) WireFormat.makeTag(0, WireFormat.WIRETYPE_VARINT), + }; + for (byte invalidTag : invalidTags) { + // Add a trailing 0 to make sure the parsing actually fails on the tag. + byte[] data = new byte[] {invalidTag, 0}; + assertInvalidWireFormat(data); + + // Invalid tag in an unknown group field. + data = + new byte[] { + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_START_GROUP), + invalidTag, + 0, + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_END_GROUP), + }; + assertInvalidWireFormat(data); + + // Invalid tag in a MessageSet item. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + invalidTag, + 0, + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + + // Invalid tag inside a MessageSet item's unknown group. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_START_GROUP), + invalidTag, + 0, + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_END_GROUP), + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + + // Invalid tag inside a map field. + data = + new byte[] { + (byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), 2, invalidTag, 0, + }; + assertInvalidWireFormat(TestMap.getDefaultInstance(), data); + } + } + + public void testUnmatchedGroupTag() throws Exception { + int startTag = WireFormat.makeTag(16, WireFormat.WIRETYPE_START_GROUP); + byte[] data = + new byte[] { + (byte) ((startTag & 0x7F) | 0x80), (byte) ((startTag >>> 7) & 0x7F), + }; + assertInvalidWireFormat(data); + + // Unmatched group tags inside a MessageSet item. + data = + new byte[] { + (byte) WireFormat.MESSAGE_SET_ITEM_TAG, + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.makeTag(4, WireFormat.WIRETYPE_START_GROUP), + }; + assertInvalidWireFormat(TestMessageSet.getDefaultInstance(), data); + } + + private void assertAccepted(MessageLite defaultInstance, byte[] data) throws Exception { + MessageLite message1 = defaultInstance.newBuilderForType().mergeFrom(data).build(); + MessageLite message2 = defaultInstance.getParserForType().parseFrom(data); + MessageLite message3 = + defaultInstance.newBuilderForType().mergeFrom(new ByteArrayInputStream(data)).build(); + MessageLite message4 = + defaultInstance.getParserForType().parseFrom(new ByteArrayInputStream(data)); + assertEquals(message1, message2); + assertEquals(message2, message3); + assertEquals(message3, message4); + } + + public void testUnmatchedWireType() throws Exception { + // Build a payload with all fields from 1 to 128 being varints. Parsing it into TestAllTypes + // or other message types should succeed even though the wire type doesn't match for some + // fields. + ByteArrayOutputStream output = new ByteArrayOutputStream(); + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + for (int i = 1; i <= 128; i++) { + codedOutput.writeInt32(i, 0); + } + codedOutput.flush(); + byte[] data = output.toByteArray(); + // It can be parsed into any message type that doesn't have required fields. + assertAccepted(TestAllTypes.getDefaultInstance(), data); + assertAccepted(UnittestProto3.TestAllTypes.getDefaultInstance(), data); + assertAccepted(TestMap.getDefaultInstance(), data); + assertAccepted(MapForProto2TestProto.TestMap.getDefaultInstance(), data); + } + + public void testParseTruncatedPackedFields() throws Exception { + TestPackedTypes all = TestUtil.getPackedSet(); + TestPackedTypes[] messages = + new TestPackedTypes[] { + TestPackedTypes.newBuilder().addAllPackedInt32(all.getPackedInt32List()).build(), + TestPackedTypes.newBuilder().addAllPackedInt64(all.getPackedInt64List()).build(), + TestPackedTypes.newBuilder().addAllPackedUint32(all.getPackedUint32List()).build(), + TestPackedTypes.newBuilder().addAllPackedUint64(all.getPackedUint64List()).build(), + TestPackedTypes.newBuilder().addAllPackedSint32(all.getPackedSint32List()).build(), + TestPackedTypes.newBuilder().addAllPackedSint64(all.getPackedSint64List()).build(), + TestPackedTypes.newBuilder().addAllPackedFixed32(all.getPackedFixed32List()).build(), + TestPackedTypes.newBuilder().addAllPackedFixed64(all.getPackedFixed64List()).build(), + TestPackedTypes.newBuilder().addAllPackedSfixed32(all.getPackedSfixed32List()).build(), + TestPackedTypes.newBuilder().addAllPackedSfixed64(all.getPackedSfixed64List()).build(), + TestPackedTypes.newBuilder().addAllPackedFloat(all.getPackedFloatList()).build(), + TestPackedTypes.newBuilder().addAllPackedDouble(all.getPackedDoubleList()).build(), + TestPackedTypes.newBuilder().addAllPackedEnum(all.getPackedEnumList()).build(), + }; + for (TestPackedTypes message : messages) { + byte[] data = message.toByteArray(); + // Parsing truncated payload should fail. + for (int i = 1; i < data.length; i++) { + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data, 0, i); + } + } + } + + public void testParsePackedFieldsWithIncorrectLength() throws Exception { + // Set the length-prefix to 1 with a 4-bytes payload to test what happens when reading a packed + // element moves the reading position past the given length limit. It should result in an + // InvalidProtocolBufferException but an implementation may forget to check it especially for + // packed varint fields. + byte[] data = + new byte[] { + 0, + 0, // first two bytes is reserved for the tag. + 1, // length is 1 + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x01, // a 4-bytes varint + }; + // All fields that can read a 4-bytes varint (all varint fields and fixed 32-bit fields). + int[] fieldNumbers = + new int[] { + TestPackedTypes.PACKED_INT32_FIELD_NUMBER, + TestPackedTypes.PACKED_INT64_FIELD_NUMBER, + TestPackedTypes.PACKED_UINT32_FIELD_NUMBER, + TestPackedTypes.PACKED_UINT64_FIELD_NUMBER, + TestPackedTypes.PACKED_SINT32_FIELD_NUMBER, + TestPackedTypes.PACKED_SINT64_FIELD_NUMBER, + TestPackedTypes.PACKED_FIXED32_FIELD_NUMBER, + TestPackedTypes.PACKED_SFIXED32_FIELD_NUMBER, + TestPackedTypes.PACKED_FLOAT_FIELD_NUMBER, + TestPackedTypes.PACKED_BOOL_FIELD_NUMBER, + TestPackedTypes.PACKED_ENUM_FIELD_NUMBER, + }; + for (int number : fieldNumbers) { + // Set the tag. + data[0] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) & 0x7F) | 0x80); + data[1] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) >>> 7) & 0x7F); + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data); + } + + // Data with 8-bytes payload to test some fixed 64-bit fields. + byte[] data8Bytes = + new byte[] { + 0, + 0, // first two bytes is reserved for the tag. + 1, // length is 1 + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x80, + (byte) 0x01, // a 8-bytes varint + }; + // All fields that can only read 8-bytes data. + int[] fieldNumbers8Bytes = + new int[] { + TestPackedTypes.PACKED_FIXED64_FIELD_NUMBER, + TestPackedTypes.PACKED_SFIXED64_FIELD_NUMBER, + TestPackedTypes.PACKED_DOUBLE_FIELD_NUMBER, + }; + for (int number : fieldNumbers8Bytes) { + // Set the tag. + data8Bytes[0] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) & 0x7F) | 0x80); + data8Bytes[1] = + (byte) ((WireFormat.makeTag(number, WireFormat.WIRETYPE_LENGTH_DELIMITED) >>> 7) & 0x7F); + assertInvalidWireFormat(TestPackedTypes.getDefaultInstance(), data8Bytes); + } + } + + public void testParseVarintMinMax() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .setOptionalInt32(Integer.MIN_VALUE) + .addRepeatedInt32(Integer.MAX_VALUE) + .setOptionalInt64(Long.MIN_VALUE) + .addRepeatedInt64(Long.MAX_VALUE) + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(Integer.MIN_VALUE, parsed.getOptionalInt32()); + assertEquals(Integer.MAX_VALUE, parsed.getRepeatedInt32(0)); + assertEquals(Long.MIN_VALUE, parsed.getOptionalInt64()); + assertEquals(Long.MAX_VALUE, parsed.getRepeatedInt64(0)); + } + + public void testParseAllVarintBits() throws Exception { + for (int i = 0; i < 32; i++) { + final int value = 1 << i; + TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(value).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(value, parsed.getOptionalInt32()); + } + for (int i = 0; i < 64; i++) { + final long value = 1L << i; + TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt64(value).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(value, parsed.getOptionalInt64()); + } + } + + public void testParseEmptyUnknownLengthDelimitedField() throws Exception { + byte[] data = + new byte[] {(byte) WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), 0}; + TestAllTypes parsed = TestAllTypes.parseFrom(data); + assertTrue(Arrays.equals(data, parsed.toByteArray())); + } + + public void testParseEmptyString() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("").build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals("", parsed.getOptionalString()); + } + + public void testParseEmptyStringProto3() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalString("").build(); + // Note that we are parsing from a proto2 proto to a proto3 proto because empty string field is + // not serialized in proto3. + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals("", parsed.getOptionalString()); + } + + public void testParseEmptyBytes() throws Exception { + TestAllTypes message = TestAllTypes.newBuilder().setOptionalBytes(ByteString.EMPTY).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(ByteString.EMPTY, parsed.getOptionalBytes()); + } + + public void testParseEmptyRepeatedStringField() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedString("") + .addRepeatedString("") + .addRepeatedString("0") + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedStringCount()); + assertEquals("", parsed.getRepeatedString(0)); + assertEquals("", parsed.getRepeatedString(1)); + assertEquals("0", parsed.getRepeatedString(2)); + } + + public void testParseEmptyRepeatedStringFieldProto3() throws Exception { + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedString("") + .addRepeatedString("") + .addRepeatedString("0") + .addRepeatedBytes(ByteString.EMPTY) + .build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedStringCount()); + assertEquals("", parsed.getRepeatedString(0)); + assertEquals("", parsed.getRepeatedString(1)); + assertEquals("0", parsed.getRepeatedString(2)); + } + + public void testParseEmptyRepeatedBytesField() throws Exception { + ByteString oneByte = ByteString.copyFrom(new byte[] {1}); + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedBytes(ByteString.EMPTY) + .addRepeatedBytes(ByteString.EMPTY) + .addRepeatedBytes(oneByte) + .build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(3, parsed.getRepeatedBytesCount()); + assertEquals(ByteString.EMPTY, parsed.getRepeatedBytes(0)); + assertEquals(ByteString.EMPTY, parsed.getRepeatedBytes(1)); + assertEquals(oneByte, parsed.getRepeatedBytes(2)); + } + + public void testSkipUnknownFieldInMessageSetItem() throws Exception { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + // MessageSet item's start tag. + output.write((byte) WireFormat.MESSAGE_SET_ITEM_TAG); + // Put all field types into the item. + TestUtil.getAllSet().writeTo(output); + // Closing the item with the real payload and closing tag. + output.write( + new byte[] { + (byte) WireFormat.MESSAGE_SET_TYPE_ID_TAG, + 100, // TYPE_ID = 100 + (byte) WireFormat.MESSAGE_SET_MESSAGE_TAG, + 0, // empty payload + (byte) WireFormat.MESSAGE_SET_ITEM_END_TAG, + }); + byte[] data = output.toByteArray(); + TestMessageSet parsed = TestMessageSet.parseFrom(data); + + // Convert to RawMessageSet for inspection. + RawMessageSet raw = RawMessageSet.parseFrom(parsed.toByteArray()); + assertEquals(1, raw.getItemCount()); + assertEquals(100, raw.getItem(0).getTypeId()); + assertEquals(0, raw.getItem(0).getMessage().size()); + } + + public void testProto2UnknownEnumValuesInOptionalField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().setOptionalNestedEnumValue(4321).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertFalse(parsed.hasOptionalNestedEnum()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestAllTypes actual = + UnittestProto3.TestAllTypes.parseFrom(parsed.toByteArray()); + assertEquals(4321, actual.getOptionalNestedEnumValue()); + } + + public void testProto2UnknownEnumValuesInRepeatedField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().addRepeatedNestedEnumValue(5432).build(); + TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(0, parsed.getRepeatedNestedEnumCount()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestAllTypes actual = + UnittestProto3.TestAllTypes.parseFrom(parsed.toByteArray()); + assertEquals(1, actual.getRepeatedNestedEnumCount()); + assertEquals(5432, actual.getRepeatedNestedEnumValue(0)); + } + + public void testProto2UnknownEnumValuesInMapField() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + TestMap message = TestMap.newBuilder().putInt32ToEnumFieldValue(1, 4321).build(); + MapForProto2TestProto.TestMap parsed = + MapForProto2TestProto.TestMap.parseFrom(message.toByteArray()); + assertEquals(0, parsed.getInt32ToEnumFieldMap().size()); + // Make sure unknown enum values are preserved. + TestMap actual = TestMap.parseFrom(parsed.toByteArray()); + assertEquals(1, actual.getInt32ToEnumFieldMap().size()); + assertEquals(4321, actual.getInt32ToEnumFieldValueOrThrow(1)); + } + + public void testProto2UnknownEnumValuesInOneof() throws Exception { + // Proto2 doesn't allow setting unknown enum values so we use proto3 to build a message with + // unknown enum values + UnittestProto3.TestOneof2 message = + UnittestProto3.TestOneof2.newBuilder().setFooEnumValue(1234).build(); + TestOneof2 parsed = TestOneof2.parseFrom(message.toByteArray()); + assertFalse(parsed.hasFooEnum()); + // Make sure unknown enum values are preserved. + UnittestProto3.TestOneof2 actual = UnittestProto3.TestOneof2.parseFrom(parsed.toByteArray()); + assertEquals(1234, actual.getFooEnumValue()); + } + + public void testProto2UnknownEnumValuesInExtension() throws Exception { + ExtensionRegistryLite extensionRegistry = TestUtilLite.getExtensionRegistryLite(); + // Raw bytes for "[.optional_foreign_enum_extension_lite]: 10" + final byte[] rawBytes = new byte[]{-80, 1, 10}; + TestAllExtensionsLite testAllExtensionsLite = + TestAllExtensionsLite.parseFrom(rawBytes, extensionRegistry); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, + testAllExtensionsLite.getExtension(optionalForeignEnumExtensionLite)); + final byte[] resultRawBytes = testAllExtensionsLite.toByteArray(); + assertEquals(rawBytes.length, resultRawBytes.length); + for (int i = 0; i < rawBytes.length; i++) { + assertEquals(rawBytes[i], resultRawBytes[i]); + } + } + + public void testProto3UnknownEnumValuesInOptionalField() throws Exception { + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().setOptionalNestedEnumValue(4321).build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(4321, parsed.getOptionalNestedEnumValue()); + } + + public void testProto3UnknownEnumValuesInRepeatedField() throws Exception { + UnittestProto3.TestAllTypes message = + UnittestProto3.TestAllTypes.newBuilder().addRepeatedNestedEnumValue(5432).build(); + UnittestProto3.TestAllTypes parsed = + UnittestProto3.TestAllTypes.parseFrom(message.toByteArray()); + assertEquals(1, parsed.getRepeatedNestedEnumCount()); + assertEquals(5432, parsed.getRepeatedNestedEnumValue(0)); + } + + public void testProto3UnknownEnumValuesInMapField() throws Exception { + TestMap message = TestMap.newBuilder().putInt32ToEnumFieldValue(1, 4321).build(); + TestMap parsed = TestMap.parseFrom(message.toByteArray()); + assertEquals(1, parsed.getInt32ToEnumFieldMap().size()); + assertEquals(4321, parsed.getInt32ToEnumFieldValueOrThrow(1)); + } + + public void testProto3UnknownEnumValuesInOneof() throws Exception { + UnittestProto3.TestOneof2 message = + UnittestProto3.TestOneof2.newBuilder().setFooEnumValue(1234).build(); + UnittestProto3.TestOneof2 parsed = UnittestProto3.TestOneof2.parseFrom(message.toByteArray()); + assertEquals(1234, parsed.getFooEnumValue()); + } + + public void testProto3MessageFieldMergeBehavior() throws Exception { + UnittestProto3.NestedTestAllTypes message1 = + UnittestProto3.NestedTestAllTypes.newBuilder() + .setPayload( + UnittestProto3.TestAllTypes.newBuilder() + .setOptionalInt32(1234) + .setOptionalInt64(5678)) + .build(); + UnittestProto3.NestedTestAllTypes message2 = + UnittestProto3.NestedTestAllTypes.newBuilder() + .setPayload( + UnittestProto3.TestAllTypes.newBuilder() + .setOptionalInt32(4321) + .setOptionalUint32(8765)) + .build(); + + UnittestProto3.NestedTestAllTypes merged = + UnittestProto3.NestedTestAllTypes.newBuilder() + .mergeFrom(message1.toByteArray()) + .mergeFrom(message2.toByteArray()) + .build(); + // Field values coming later in the stream override earlier values. + assertEquals(4321, merged.getPayload().getOptionalInt32()); + // Field values present in either message should be present in the merged result. + assertEquals(5678, merged.getPayload().getOptionalInt64()); + assertEquals(8765, merged.getPayload().getOptionalUint32()); + } + + public void testMergeFromPartialByteArray() throws Exception { + byte[] data = TestUtil.getAllSet().toByteArray(); + byte[] dataWithPaddings = new byte[data.length + 2]; + System.arraycopy(data, 0, dataWithPaddings, 1, data.length); + // Parsing will fail if the builder (or parser) interprets offset or length incorrectly. + TestAllTypes.newBuilder().mergeFrom(dataWithPaddings, 1, data.length); + TestAllTypes.parser().parseFrom(dataWithPaddings, 1, data.length); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java new file mode 100755 index 0000000000..4a3c7647f1 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WrappersLiteOfMethodTest.java @@ -0,0 +1,61 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; +import junit.framework.TestCase; + +public class WrappersLiteOfMethodTest extends TestCase { + + public void testOf() throws Exception { + TopLevelMessage.Builder builder = TopLevelMessage.newBuilder(); + builder.setFieldDouble(DoubleValue.of(2.333)); + builder.setFieldFloat(FloatValue.of(2.333f)); + builder.setFieldInt32(Int32Value.of(2333)); + builder.setFieldInt64(Int64Value.of(23333333333333L)); + builder.setFieldUint32(UInt32Value.of(2333)); + builder.setFieldUint64(UInt64Value.of(23333333333333L)); + builder.setFieldBool(BoolValue.of(true)); + builder.setFieldString(StringValue.of("23333")); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + + TopLevelMessage message = builder.build(); + assertTrue(2.333 == message.getFieldDouble().getValue()); + assertTrue(2.333f == message.getFieldFloat().getValue()); + assertTrue(2333 == message.getFieldInt32().getValue()); + assertTrue(23333333333333L == message.getFieldInt64().getValue()); + assertTrue(2333 == message.getFieldUint32().getValue()); + assertTrue(23333333333333L == message.getFieldUint64().getValue()); + assertTrue(true == message.getFieldBool().getValue()); + assertTrue(message.getFieldString().getValue().equals("23333")); + assertTrue(message.getFieldBytes().getValue().toStringUtf8().equals("233")); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java new file mode 100755 index 0000000000..f0d662d0c6 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/WrappersOfMethodTest.java @@ -0,0 +1,61 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import com.google.protobuf.wrapperstest.WrappersTestProto.TopLevelMessage; +import junit.framework.TestCase; + +public class WrappersOfMethodTest extends TestCase { + + public void testOf() throws Exception { + TopLevelMessage.Builder builder = TopLevelMessage.newBuilder(); + builder.setFieldDouble(DoubleValue.of(2.333)); + builder.setFieldFloat(FloatValue.of(2.333f)); + builder.setFieldInt32(Int32Value.of(2333)); + builder.setFieldInt64(Int64Value.of(23333333333333L)); + builder.setFieldUint32(UInt32Value.of(2333)); + builder.setFieldUint64(UInt64Value.of(23333333333333L)); + builder.setFieldBool(BoolValue.of(true)); + builder.setFieldString(StringValue.of("23333")); + builder.setFieldBytes(BytesValue.of(ByteString.wrap("233".getBytes(Internal.UTF_8)))); + + TopLevelMessage message = builder.build(); + assertTrue(2.333 == message.getFieldDouble().getValue()); + assertTrue(2.333f == message.getFieldFloat().getValue()); + assertTrue(2333 == message.getFieldInt32().getValue()); + assertTrue(23333333333333L == message.getFieldInt64().getValue()); + assertTrue(2333 == message.getFieldUint32().getValue()); + assertTrue(23333333333333L == message.getFieldUint64().getValue()); + assertTrue(true == message.getFieldBool().getValue()); + assertTrue(message.getFieldString().getValue().equals("23333")); + assertTrue(message.getFieldBytes().getValue().toStringUtf8().equals("233")); + } +} diff --git a/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto b/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto new file mode 100755 index 0000000000..ce78b34c60 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/cached_field_size_test.proto @@ -0,0 +1,45 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// A regression test for b/74087933 +syntax = "proto2"; + +package protobuf_unittest; + +option optimize_for = CODE_SIZE; +option java_multiple_files = true; + +import "google/protobuf/unittest.proto"; +import "google/protobuf/unittest_proto3.proto"; + +message TestCachedFieldSizeMessage { + optional protobuf_unittest.TestPackedTypes proto2_child = 1; + optional proto3_unittest.TestPackedTypes proto3_child = 2; +} diff --git a/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto b/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto new file mode 100755 index 0000000000..f985ab0f54 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/message_lite_extension_util_test.proto @@ -0,0 +1,47 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Proto definitions used to test MessageLiteExtensionUtil +syntax = "proto2"; + +package protobuf_unittest; + +option java_outer_classname = "MessageLiteExtensionTestProtos"; + +message Car { + optional string make = 1; + extensions 1000 to max; +} + +extend Car { + optional bool turbo = 1001; + optional bool self_driving = 1002; + optional string plate = 9999; +} diff --git a/java/core/src/test/proto/com/google/protobuf/nested_extension.proto b/java/core/src/test/proto/com/google/protobuf/nested_extension.proto index 037040b5bc..2da6d08e3a 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_extension.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_extension.proto @@ -35,10 +35,10 @@ syntax = "proto2"; -import "com/google/protobuf/non_nested_extension.proto"; - package protobuf_unittest; +import "com/google/protobuf/non_nested_extension.proto"; + message MyNestedExtension { extend MessageToBeExtended { diff --git a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto index 40d0f19051..024097cb5e 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto @@ -38,10 +38,10 @@ syntax = "proto2"; package protobuf_unittest; -option optimize_for = LITE_RUNTIME; - import "com/google/protobuf/non_nested_extension_lite.proto"; +option optimize_for = LITE_RUNTIME; + message MyNestedExtensionLite { extend MessageLiteToBeExtended { optional MessageLiteToBeExtended recursiveExtensionLite = 3; diff --git a/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto b/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto new file mode 100755 index 0000000000..7935322b72 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/packed_field_test.proto @@ -0,0 +1,76 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package packed_field_test; + +option java_package = "com.google.protobuf"; +option java_outer_classname = "PackedFieldTestProto"; + +message TestAllTypes { + enum NestedEnum { + FOO = 0; + BAR = 1; + BAZ = 2; + } + + repeated int32 repeated_int32 = 31; + repeated int64 repeated_int64 = 32; + repeated uint32 repeated_uint32 = 33; + repeated uint64 repeated_uint64 = 34; + repeated sint32 repeated_sint32 = 35; + repeated sint64 repeated_sint64 = 36; + repeated fixed32 repeated_fixed32 = 37; + repeated fixed64 repeated_fixed64 = 38; + repeated sfixed32 repeated_sfixed32 = 39; + repeated sfixed64 repeated_sfixed64 = 40; + repeated float repeated_float = 41; + repeated double repeated_double = 42; + repeated bool repeated_bool = 43; + repeated NestedEnum repeated_nested_enum = 51; +} + +message TestUnpackedTypes { + repeated int32 repeated_int32 = 1 [packed = false]; + repeated int64 repeated_int64 = 2 [packed = false]; + repeated uint32 repeated_uint32 = 3 [packed = false]; + repeated uint64 repeated_uint64 = 4 [packed = false]; + repeated sint32 repeated_sint32 = 5 [packed = false]; + repeated sint64 repeated_sint64 = 6 [packed = false]; + repeated fixed32 repeated_fixed32 = 7 [packed = false]; + repeated fixed64 repeated_fixed64 = 8 [packed = false]; + repeated sfixed32 repeated_sfixed32 = 9 [packed = false]; + repeated sfixed64 repeated_sfixed64 = 10 [packed = false]; + repeated float repeated_float = 11 [packed = false]; + repeated double repeated_double = 12 [packed = false]; + repeated bool repeated_bool = 13 [packed = false]; + repeated TestAllTypes.NestedEnum repeated_nested_enum = 14 [packed = false]; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto2_message.proto b/java/core/src/test/proto/com/google/protobuf/proto2_message.proto new file mode 100755 index 0000000000..005a911d93 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto2_message.proto @@ -0,0 +1,429 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// LINT: ALLOW_GROUPS +syntax = "proto2"; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto2Testing"; +package protobuf.experimental; + + +message Proto2SpecialFieldName { + optional double regular_name = 1; + optional int32 cached_size = 2; + optional int64 serialized_size = 3; + optional string class = 4; +} + +message Proto2Message { + + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2Message field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto2Message field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + group FieldGroup69 = 69 { + optional int32 field_int32_70 = 70; + } + } + + message RequiredNestedMessage { + optional int32 value = 1; + } + + required double field_required_double_71 = 71; + required float field_required_float_72 = 72; + required int64 field_required_int64_73 = 73; + required uint64 field_required_uint64_74 = 74; + required int32 field_required_int32_75 = 75; + required fixed64 field_required_fixed64_76 = 76; + required fixed32 field_required_fixed32_77 = 77; + required bool field_required_bool_78 = 78; + required string field_required_string_79 = 79; + required RequiredNestedMessage field_required_message_80 = 80; + required bytes field_required_bytes_81 = 81; + required uint32 field_required_uint32_82 = 82; + required TestEnum field_required_enum_83 = 83; + required sfixed32 field_required_sfixed32_84 = 84; + required sfixed64 field_required_sfixed64_85 = 85; + required sint32 field_required_sint32_86 = 86; + required sint64 field_required_sint64_87 = 87; + required group FieldRequiredGroup88 = 88 { + optional int32 field_int32_89 = 89; + } +} + +message Proto2Empty {} + +message Proto2MessageWithExtensions { + extensions 1 to 10000; +} + +extend Proto2MessageWithExtensions { + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2Message field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional Proto2Message.TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated Proto2Message.TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated Proto2Message.TestEnum field_enum_list_packed_44 = 44 + [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } +} + +message Proto2MessageWithMaps { + map<bool, bool> field_map_bool_bool_1 = 1; + map<bool, bytes> field_map_bool_bytes_2 = 2; + map<bool, double> field_map_bool_double_3 = 3; + map<bool, Proto2Message.TestEnum> field_map_bool_enum_4 = 4; + map<bool, fixed32> field_map_bool_fixed32_5 = 5; + map<bool, fixed64> field_map_bool_fixed64_6 = 6; + map<bool, float> field_map_bool_float_7 = 7; + map<bool, int32> field_map_bool_int32_8 = 8; + map<bool, int64> field_map_bool_int64_9 = 9; + map<bool, Proto2Message> field_map_bool_message_10 = 10; + map<bool, sfixed32> field_map_bool_sfixed32_11 = 11; + map<bool, sfixed64> field_map_bool_sfixed64_12 = 12; + map<bool, sint32> field_map_bool_sint32_13 = 13; + map<bool, sint64> field_map_bool_sint64_14 = 14; + map<bool, string> field_map_bool_string_15 = 15; + map<bool, uint32> field_map_bool_uint32_16 = 16; + map<bool, uint64> field_map_bool_uint64_17 = 17; + map<fixed32, bool> field_map_fixed32_bool_18 = 18; + map<fixed32, bytes> field_map_fixed32_bytes_19 = 19; + map<fixed32, double> field_map_fixed32_double_20 = 20; + map<fixed32, Proto2Message.TestEnum> field_map_fixed32_enum_21 = 21; + map<fixed32, fixed32> field_map_fixed32_fixed32_22 = 22; + map<fixed32, fixed64> field_map_fixed32_fixed64_23 = 23; + map<fixed32, float> field_map_fixed32_float_24 = 24; + map<fixed32, int32> field_map_fixed32_int32_25 = 25; + map<fixed32, int64> field_map_fixed32_int64_26 = 26; + map<fixed32, Proto2Message> field_map_fixed32_message_27 = 27; + map<fixed32, sfixed32> field_map_fixed32_sfixed32_28 = 28; + map<fixed32, sfixed64> field_map_fixed32_sfixed64_29 = 29; + map<fixed32, sint32> field_map_fixed32_sint32_30 = 30; + map<fixed32, sint64> field_map_fixed32_sint64_31 = 31; + map<fixed32, string> field_map_fixed32_string_32 = 32; + map<fixed32, uint32> field_map_fixed32_uint32_33 = 33; + map<fixed32, uint64> field_map_fixed32_uint64_34 = 34; + map<fixed64, bool> field_map_fixed64_bool_35 = 35; + map<fixed64, bytes> field_map_fixed64_bytes_36 = 36; + map<fixed64, double> field_map_fixed64_double_37 = 37; + map<fixed64, Proto2Message.TestEnum> field_map_fixed64_enum_38 = 38; + map<fixed64, fixed32> field_map_fixed64_fixed32_39 = 39; + map<fixed64, fixed64> field_map_fixed64_fixed64_40 = 40; + map<fixed64, float> field_map_fixed64_float_41 = 41; + map<fixed64, int32> field_map_fixed64_int32_42 = 42; + map<fixed64, int64> field_map_fixed64_int64_43 = 43; + map<fixed64, Proto2Message> field_map_fixed64_message_44 = 44; + map<fixed64, sfixed32> field_map_fixed64_sfixed32_45 = 45; + map<fixed64, sfixed64> field_map_fixed64_sfixed64_46 = 46; + map<fixed64, sint32> field_map_fixed64_sint32_47 = 47; + map<fixed64, sint64> field_map_fixed64_sint64_48 = 48; + map<fixed64, string> field_map_fixed64_string_49 = 49; + map<fixed64, uint32> field_map_fixed64_uint32_50 = 50; + map<fixed64, uint64> field_map_fixed64_uint64_51 = 51; + map<int32, bool> field_map_int32_bool_52 = 52; + map<int32, bytes> field_map_int32_bytes_53 = 53; + map<int32, double> field_map_int32_double_54 = 54; + map<int32, Proto2Message.TestEnum> field_map_int32_enum_55 = 55; + map<int32, fixed32> field_map_int32_fixed32_56 = 56; + map<int32, fixed64> field_map_int32_fixed64_57 = 57; + map<int32, float> field_map_int32_float_58 = 58; + map<int32, int32> field_map_int32_int32_59 = 59; + map<int32, int64> field_map_int32_int64_60 = 60; + map<int32, Proto2Message> field_map_int32_message_61 = 61; + map<int32, sfixed32> field_map_int32_sfixed32_62 = 62; + map<int32, sfixed64> field_map_int32_sfixed64_63 = 63; + map<int32, sint32> field_map_int32_sint32_64 = 64; + map<int32, sint64> field_map_int32_sint64_65 = 65; + map<int32, string> field_map_int32_string_66 = 66; + map<int32, uint32> field_map_int32_uint32_67 = 67; + map<int32, uint64> field_map_int32_uint64_68 = 68; + map<int64, bool> field_map_int64_bool_69 = 69; + map<int64, bytes> field_map_int64_bytes_70 = 70; + map<int64, double> field_map_int64_double_71 = 71; + map<int64, Proto2Message.TestEnum> field_map_int64_enum_72 = 72; + map<int64, fixed32> field_map_int64_fixed32_73 = 73; + map<int64, fixed64> field_map_int64_fixed64_74 = 74; + map<int64, float> field_map_int64_float_75 = 75; + map<int64, int32> field_map_int64_int32_76 = 76; + map<int64, int64> field_map_int64_int64_77 = 77; + map<int64, Proto2Message> field_map_int64_message_78 = 78; + map<int64, sfixed32> field_map_int64_sfixed32_79 = 79; + map<int64, sfixed64> field_map_int64_sfixed64_80 = 80; + map<int64, sint32> field_map_int64_sint32_81 = 81; + map<int64, sint64> field_map_int64_sint64_82 = 82; + map<int64, string> field_map_int64_string_83 = 83; + map<int64, uint32> field_map_int64_uint32_84 = 84; + map<int64, uint64> field_map_int64_uint64_85 = 85; + map<sfixed32, bool> field_map_sfixed32_bool_86 = 86; + map<sfixed32, bytes> field_map_sfixed32_bytes_87 = 87; + map<sfixed32, double> field_map_sfixed32_double_88 = 88; + map<sfixed32, Proto2Message.TestEnum> field_map_sfixed32_enum_89 = 89; + map<sfixed32, fixed32> field_map_sfixed32_fixed32_90 = 90; + map<sfixed32, fixed64> field_map_sfixed32_fixed64_91 = 91; + map<sfixed32, float> field_map_sfixed32_float_92 = 92; + map<sfixed32, int32> field_map_sfixed32_int32_93 = 93; + map<sfixed32, int64> field_map_sfixed32_int64_94 = 94; + map<sfixed32, Proto2Message> field_map_sfixed32_message_95 = 95; + map<sfixed32, sfixed32> field_map_sfixed32_sfixed32_96 = 96; + map<sfixed32, sfixed64> field_map_sfixed32_sfixed64_97 = 97; + map<sfixed32, sint32> field_map_sfixed32_sint32_98 = 98; + map<sfixed32, sint64> field_map_sfixed32_sint64_99 = 99; + map<sfixed32, string> field_map_sfixed32_string_100 = 100; + map<sfixed32, uint32> field_map_sfixed32_uint32_101 = 101; + map<sfixed32, uint64> field_map_sfixed32_uint64_102 = 102; + map<sfixed64, bool> field_map_sfixed64_bool_103 = 103; + map<sfixed64, bytes> field_map_sfixed64_bytes_104 = 104; + map<sfixed64, double> field_map_sfixed64_double_105 = 105; + map<sfixed64, Proto2Message.TestEnum> field_map_sfixed64_enum_106 = 106; + map<sfixed64, fixed32> field_map_sfixed64_fixed32_107 = 107; + map<sfixed64, fixed64> field_map_sfixed64_fixed64_108 = 108; + map<sfixed64, float> field_map_sfixed64_float_109 = 109; + map<sfixed64, int32> field_map_sfixed64_int32_110 = 110; + map<sfixed64, int64> field_map_sfixed64_int64_111 = 111; + map<sfixed64, Proto2Message> field_map_sfixed64_message_112 = 112; + map<sfixed64, sfixed32> field_map_sfixed64_sfixed32_113 = 113; + map<sfixed64, sfixed64> field_map_sfixed64_sfixed64_114 = 114; + map<sfixed64, sint32> field_map_sfixed64_sint32_115 = 115; + map<sfixed64, sint64> field_map_sfixed64_sint64_116 = 116; + map<sfixed64, string> field_map_sfixed64_string_117 = 117; + map<sfixed64, uint32> field_map_sfixed64_uint32_118 = 118; + map<sfixed64, uint64> field_map_sfixed64_uint64_119 = 119; + map<sint32, bool> field_map_sint32_bool_120 = 120; + map<sint32, bytes> field_map_sint32_bytes_121 = 121; + map<sint32, double> field_map_sint32_double_122 = 122; + map<sint32, Proto2Message.TestEnum> field_map_sint32_enum_123 = 123; + map<sint32, fixed32> field_map_sint32_fixed32_124 = 124; + map<sint32, fixed64> field_map_sint32_fixed64_125 = 125; + map<sint32, float> field_map_sint32_float_126 = 126; + map<sint32, int32> field_map_sint32_int32_127 = 127; + map<sint32, int64> field_map_sint32_int64_128 = 128; + map<sint32, Proto2Message> field_map_sint32_message_129 = 129; + map<sint32, sfixed32> field_map_sint32_sfixed32_130 = 130; + map<sint32, sfixed64> field_map_sint32_sfixed64_131 = 131; + map<sint32, sint32> field_map_sint32_sint32_132 = 132; + map<sint32, sint64> field_map_sint32_sint64_133 = 133; + map<sint32, string> field_map_sint32_string_134 = 134; + map<sint32, uint32> field_map_sint32_uint32_135 = 135; + map<sint32, uint64> field_map_sint32_uint64_136 = 136; + map<sint64, bool> field_map_sint64_bool_137 = 137; + map<sint64, bytes> field_map_sint64_bytes_138 = 138; + map<sint64, double> field_map_sint64_double_139 = 139; + map<sint64, Proto2Message.TestEnum> field_map_sint64_enum_140 = 140; + map<sint64, fixed32> field_map_sint64_fixed32_141 = 141; + map<sint64, fixed64> field_map_sint64_fixed64_142 = 142; + map<sint64, float> field_map_sint64_float_143 = 143; + map<sint64, int32> field_map_sint64_int32_144 = 144; + map<sint64, int64> field_map_sint64_int64_145 = 145; + map<sint64, Proto2Message> field_map_sint64_message_146 = 146; + map<sint64, sfixed32> field_map_sint64_sfixed32_147 = 147; + map<sint64, sfixed64> field_map_sint64_sfixed64_148 = 148; + map<sint64, sint32> field_map_sint64_sint32_149 = 149; + map<sint64, sint64> field_map_sint64_sint64_150 = 150; + map<sint64, string> field_map_sint64_string_151 = 151; + map<sint64, uint32> field_map_sint64_uint32_152 = 152; + map<sint64, uint64> field_map_sint64_uint64_153 = 153; + map<string, bool> field_map_string_bool_154 = 154; + map<string, bytes> field_map_string_bytes_155 = 155; + map<string, double> field_map_string_double_156 = 156; + map<string, Proto2Message.TestEnum> field_map_string_enum_157 = 157; + map<string, fixed32> field_map_string_fixed32_158 = 158; + map<string, fixed64> field_map_string_fixed64_159 = 159; + map<string, float> field_map_string_float_160 = 160; + map<string, int32> field_map_string_int32_161 = 161; + map<string, int64> field_map_string_int64_162 = 162; + map<string, Proto2Message> field_map_string_message_163 = 163; + map<string, sfixed32> field_map_string_sfixed32_164 = 164; + map<string, sfixed64> field_map_string_sfixed64_165 = 165; + map<string, sint32> field_map_string_sint32_166 = 166; + map<string, sint64> field_map_string_sint64_167 = 167; + map<string, string> field_map_string_string_168 = 168; + map<string, uint32> field_map_string_uint32_169 = 169; + map<string, uint64> field_map_string_uint64_170 = 170; + map<uint32, bool> field_map_uint32_bool_171 = 171; + map<uint32, bytes> field_map_uint32_bytes_172 = 172; + map<uint32, double> field_map_uint32_double_173 = 173; + map<uint32, Proto2Message.TestEnum> field_map_uint32_enum_174 = 174; + map<uint32, fixed32> field_map_uint32_fixed32_175 = 175; + map<uint32, fixed64> field_map_uint32_fixed64_176 = 176; + map<uint32, float> field_map_uint32_float_177 = 177; + map<uint32, int32> field_map_uint32_int32_178 = 178; + map<uint32, int64> field_map_uint32_int64_179 = 179; + map<uint32, Proto2Message> field_map_uint32_message_180 = 180; + map<uint32, sfixed32> field_map_uint32_sfixed32_181 = 181; + map<uint32, sfixed64> field_map_uint32_sfixed64_182 = 182; + map<uint32, sint32> field_map_uint32_sint32_183 = 183; + map<uint32, sint64> field_map_uint32_sint64_184 = 184; + map<uint32, string> field_map_uint32_string_185 = 185; + map<uint32, uint32> field_map_uint32_uint32_186 = 186; + map<uint32, uint64> field_map_uint32_uint64_187 = 187; + map<uint64, bool> field_map_uint64_bool_188 = 188; + map<uint64, bytes> field_map_uint64_bytes_189 = 189; + map<uint64, double> field_map_uint64_double_190 = 190; + map<uint64, Proto2Message.TestEnum> field_map_uint64_enum_191 = 191; + map<uint64, fixed32> field_map_uint64_fixed32_192 = 192; + map<uint64, fixed64> field_map_uint64_fixed64_193 = 193; + map<uint64, float> field_map_uint64_float_194 = 194; + map<uint64, int32> field_map_uint64_int32_195 = 195; + map<uint64, int64> field_map_uint64_int64_196 = 196; + map<uint64, Proto2Message> field_map_uint64_message_197 = 197; + map<uint64, sfixed32> field_map_uint64_sfixed32_198 = 198; + map<uint64, sfixed64> field_map_uint64_sfixed64_199 = 199; + map<uint64, sint32> field_map_uint64_sint32_200 = 200; + map<uint64, sint64> field_map_uint64_sint64_201 = 201; + map<uint64, string> field_map_uint64_string_202 = 202; + map<uint64, uint32> field_map_uint64_uint32_203 = 203; + map<uint64, uint64> field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto b/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto new file mode 100755 index 0000000000..8385dfdcef --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto2_message_lite.proto @@ -0,0 +1,422 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// LINT: ALLOW_GROUPS +syntax = "proto2"; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto2TestingLite"; +package protobuf.experimental.lite; + + +message Proto2MessageLite { + + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2MessageLite field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto2MessageLite field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + group FieldGroup69 = 69 { + optional int32 field_int32_70 = 70; + } + } + + message RequiredNestedMessage { + optional int32 value = 1; + } + + required double field_required_double_71 = 71; + required float field_required_float_72 = 72; + required int64 field_required_int64_73 = 73; + required uint64 field_required_uint64_74 = 74; + required int32 field_required_int32_75 = 75; + required fixed64 field_required_fixed64_76 = 76; + required fixed32 field_required_fixed32_77 = 77; + required bool field_required_bool_78 = 78; + required string field_required_string_79 = 79; + required RequiredNestedMessage field_required_message_80 = 80; + required bytes field_required_bytes_81 = 81; + required uint32 field_required_uint32_82 = 82; + required TestEnum field_required_enum_83 = 83; + required sfixed32 field_required_sfixed32_84 = 84; + required sfixed64 field_required_sfixed64_85 = 85; + required sint32 field_required_sint32_86 = 86; + required sint64 field_required_sint64_87 = 87; + required group FieldRequiredGroup88 = 88 { + optional int32 field_int32_89 = 89; + } +} + +message Proto2EmptyLite {} + +message Proto2MessageLiteWithExtensions { + extensions 1 to max; +} + +extend Proto2MessageLiteWithExtensions { + optional double field_double_1 = 1; + optional float field_float_2 = 2; + optional int64 field_int64_3 = 3; + optional uint64 field_uint64_4 = 4; + optional int32 field_int32_5 = 5; + optional fixed64 field_fixed64_6 = 6; + optional fixed32 field_fixed32_7 = 7; + optional bool field_bool_8 = 8; + optional string field_string_9 = 9; + optional Proto2MessageLite field_message_10 = 10; + optional bytes field_bytes_11 = 11; + optional uint32 field_uint32_12 = 12; + optional Proto2MessageLite.TestEnum field_enum_13 = 13; + optional sfixed32 field_sfixed32_14 = 14; + optional sfixed64 field_sfixed64_15 = 15; + optional sint32 field_sint32_16 = 16; + optional sint64 field_sint64_17 = 17; + + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto2MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated Proto2MessageLite.TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated Proto2MessageLite.TestEnum field_enum_list_packed_44 = 44 + [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + + optional group FieldGroup49 = 49 { + optional int32 field_int32_50 = 50; + } + + repeated group FieldGroupList51 = 51 { + optional int32 field_int32_52 = 52; + } +} + +message Proto2MessageLiteWithMaps { + map<bool, bool> field_map_bool_bool_1 = 1; + map<bool, bytes> field_map_bool_bytes_2 = 2; + map<bool, double> field_map_bool_double_3 = 3; + map<bool, Proto2MessageLite.TestEnum> field_map_bool_enum_4 = 4; + map<bool, fixed32> field_map_bool_fixed32_5 = 5; + map<bool, fixed64> field_map_bool_fixed64_6 = 6; + map<bool, float> field_map_bool_float_7 = 7; + map<bool, int32> field_map_bool_int32_8 = 8; + map<bool, int64> field_map_bool_int64_9 = 9; + map<bool, Proto2MessageLite> field_map_bool_message_10 = 10; + map<bool, sfixed32> field_map_bool_sfixed32_11 = 11; + map<bool, sfixed64> field_map_bool_sfixed64_12 = 12; + map<bool, sint32> field_map_bool_sint32_13 = 13; + map<bool, sint64> field_map_bool_sint64_14 = 14; + map<bool, string> field_map_bool_string_15 = 15; + map<bool, uint32> field_map_bool_uint32_16 = 16; + map<bool, uint64> field_map_bool_uint64_17 = 17; + map<fixed32, bool> field_map_fixed32_bool_18 = 18; + map<fixed32, bytes> field_map_fixed32_bytes_19 = 19; + map<fixed32, double> field_map_fixed32_double_20 = 20; + map<fixed32, Proto2MessageLite.TestEnum> field_map_fixed32_enum_21 = 21; + map<fixed32, fixed32> field_map_fixed32_fixed32_22 = 22; + map<fixed32, fixed64> field_map_fixed32_fixed64_23 = 23; + map<fixed32, float> field_map_fixed32_float_24 = 24; + map<fixed32, int32> field_map_fixed32_int32_25 = 25; + map<fixed32, int64> field_map_fixed32_int64_26 = 26; + map<fixed32, Proto2MessageLite> field_map_fixed32_message_27 = 27; + map<fixed32, sfixed32> field_map_fixed32_sfixed32_28 = 28; + map<fixed32, sfixed64> field_map_fixed32_sfixed64_29 = 29; + map<fixed32, sint32> field_map_fixed32_sint32_30 = 30; + map<fixed32, sint64> field_map_fixed32_sint64_31 = 31; + map<fixed32, string> field_map_fixed32_string_32 = 32; + map<fixed32, uint32> field_map_fixed32_uint32_33 = 33; + map<fixed32, uint64> field_map_fixed32_uint64_34 = 34; + map<fixed64, bool> field_map_fixed64_bool_35 = 35; + map<fixed64, bytes> field_map_fixed64_bytes_36 = 36; + map<fixed64, double> field_map_fixed64_double_37 = 37; + map<fixed64, Proto2MessageLite.TestEnum> field_map_fixed64_enum_38 = 38; + map<fixed64, fixed32> field_map_fixed64_fixed32_39 = 39; + map<fixed64, fixed64> field_map_fixed64_fixed64_40 = 40; + map<fixed64, float> field_map_fixed64_float_41 = 41; + map<fixed64, int32> field_map_fixed64_int32_42 = 42; + map<fixed64, int64> field_map_fixed64_int64_43 = 43; + map<fixed64, Proto2MessageLite> field_map_fixed64_message_44 = 44; + map<fixed64, sfixed32> field_map_fixed64_sfixed32_45 = 45; + map<fixed64, sfixed64> field_map_fixed64_sfixed64_46 = 46; + map<fixed64, sint32> field_map_fixed64_sint32_47 = 47; + map<fixed64, sint64> field_map_fixed64_sint64_48 = 48; + map<fixed64, string> field_map_fixed64_string_49 = 49; + map<fixed64, uint32> field_map_fixed64_uint32_50 = 50; + map<fixed64, uint64> field_map_fixed64_uint64_51 = 51; + map<int32, bool> field_map_int32_bool_52 = 52; + map<int32, bytes> field_map_int32_bytes_53 = 53; + map<int32, double> field_map_int32_double_54 = 54; + map<int32, Proto2MessageLite.TestEnum> field_map_int32_enum_55 = 55; + map<int32, fixed32> field_map_int32_fixed32_56 = 56; + map<int32, fixed64> field_map_int32_fixed64_57 = 57; + map<int32, float> field_map_int32_float_58 = 58; + map<int32, int32> field_map_int32_int32_59 = 59; + map<int32, int64> field_map_int32_int64_60 = 60; + map<int32, Proto2MessageLite> field_map_int32_message_61 = 61; + map<int32, sfixed32> field_map_int32_sfixed32_62 = 62; + map<int32, sfixed64> field_map_int32_sfixed64_63 = 63; + map<int32, sint32> field_map_int32_sint32_64 = 64; + map<int32, sint64> field_map_int32_sint64_65 = 65; + map<int32, string> field_map_int32_string_66 = 66; + map<int32, uint32> field_map_int32_uint32_67 = 67; + map<int32, uint64> field_map_int32_uint64_68 = 68; + map<int64, bool> field_map_int64_bool_69 = 69; + map<int64, bytes> field_map_int64_bytes_70 = 70; + map<int64, double> field_map_int64_double_71 = 71; + map<int64, Proto2MessageLite.TestEnum> field_map_int64_enum_72 = 72; + map<int64, fixed32> field_map_int64_fixed32_73 = 73; + map<int64, fixed64> field_map_int64_fixed64_74 = 74; + map<int64, float> field_map_int64_float_75 = 75; + map<int64, int32> field_map_int64_int32_76 = 76; + map<int64, int64> field_map_int64_int64_77 = 77; + map<int64, Proto2MessageLite> field_map_int64_message_78 = 78; + map<int64, sfixed32> field_map_int64_sfixed32_79 = 79; + map<int64, sfixed64> field_map_int64_sfixed64_80 = 80; + map<int64, sint32> field_map_int64_sint32_81 = 81; + map<int64, sint64> field_map_int64_sint64_82 = 82; + map<int64, string> field_map_int64_string_83 = 83; + map<int64, uint32> field_map_int64_uint32_84 = 84; + map<int64, uint64> field_map_int64_uint64_85 = 85; + map<sfixed32, bool> field_map_sfixed32_bool_86 = 86; + map<sfixed32, bytes> field_map_sfixed32_bytes_87 = 87; + map<sfixed32, double> field_map_sfixed32_double_88 = 88; + map<sfixed32, Proto2MessageLite.TestEnum> field_map_sfixed32_enum_89 = 89; + map<sfixed32, fixed32> field_map_sfixed32_fixed32_90 = 90; + map<sfixed32, fixed64> field_map_sfixed32_fixed64_91 = 91; + map<sfixed32, float> field_map_sfixed32_float_92 = 92; + map<sfixed32, int32> field_map_sfixed32_int32_93 = 93; + map<sfixed32, int64> field_map_sfixed32_int64_94 = 94; + map<sfixed32, Proto2MessageLite> field_map_sfixed32_message_95 = 95; + map<sfixed32, sfixed32> field_map_sfixed32_sfixed32_96 = 96; + map<sfixed32, sfixed64> field_map_sfixed32_sfixed64_97 = 97; + map<sfixed32, sint32> field_map_sfixed32_sint32_98 = 98; + map<sfixed32, sint64> field_map_sfixed32_sint64_99 = 99; + map<sfixed32, string> field_map_sfixed32_string_100 = 100; + map<sfixed32, uint32> field_map_sfixed32_uint32_101 = 101; + map<sfixed32, uint64> field_map_sfixed32_uint64_102 = 102; + map<sfixed64, bool> field_map_sfixed64_bool_103 = 103; + map<sfixed64, bytes> field_map_sfixed64_bytes_104 = 104; + map<sfixed64, double> field_map_sfixed64_double_105 = 105; + map<sfixed64, Proto2MessageLite.TestEnum> field_map_sfixed64_enum_106 = 106; + map<sfixed64, fixed32> field_map_sfixed64_fixed32_107 = 107; + map<sfixed64, fixed64> field_map_sfixed64_fixed64_108 = 108; + map<sfixed64, float> field_map_sfixed64_float_109 = 109; + map<sfixed64, int32> field_map_sfixed64_int32_110 = 110; + map<sfixed64, int64> field_map_sfixed64_int64_111 = 111; + map<sfixed64, Proto2MessageLite> field_map_sfixed64_message_112 = 112; + map<sfixed64, sfixed32> field_map_sfixed64_sfixed32_113 = 113; + map<sfixed64, sfixed64> field_map_sfixed64_sfixed64_114 = 114; + map<sfixed64, sint32> field_map_sfixed64_sint32_115 = 115; + map<sfixed64, sint64> field_map_sfixed64_sint64_116 = 116; + map<sfixed64, string> field_map_sfixed64_string_117 = 117; + map<sfixed64, uint32> field_map_sfixed64_uint32_118 = 118; + map<sfixed64, uint64> field_map_sfixed64_uint64_119 = 119; + map<sint32, bool> field_map_sint32_bool_120 = 120; + map<sint32, bytes> field_map_sint32_bytes_121 = 121; + map<sint32, double> field_map_sint32_double_122 = 122; + map<sint32, Proto2MessageLite.TestEnum> field_map_sint32_enum_123 = 123; + map<sint32, fixed32> field_map_sint32_fixed32_124 = 124; + map<sint32, fixed64> field_map_sint32_fixed64_125 = 125; + map<sint32, float> field_map_sint32_float_126 = 126; + map<sint32, int32> field_map_sint32_int32_127 = 127; + map<sint32, int64> field_map_sint32_int64_128 = 128; + map<sint32, Proto2MessageLite> field_map_sint32_message_129 = 129; + map<sint32, sfixed32> field_map_sint32_sfixed32_130 = 130; + map<sint32, sfixed64> field_map_sint32_sfixed64_131 = 131; + map<sint32, sint32> field_map_sint32_sint32_132 = 132; + map<sint32, sint64> field_map_sint32_sint64_133 = 133; + map<sint32, string> field_map_sint32_string_134 = 134; + map<sint32, uint32> field_map_sint32_uint32_135 = 135; + map<sint32, uint64> field_map_sint32_uint64_136 = 136; + map<sint64, bool> field_map_sint64_bool_137 = 137; + map<sint64, bytes> field_map_sint64_bytes_138 = 138; + map<sint64, double> field_map_sint64_double_139 = 139; + map<sint64, Proto2MessageLite.TestEnum> field_map_sint64_enum_140 = 140; + map<sint64, fixed32> field_map_sint64_fixed32_141 = 141; + map<sint64, fixed64> field_map_sint64_fixed64_142 = 142; + map<sint64, float> field_map_sint64_float_143 = 143; + map<sint64, int32> field_map_sint64_int32_144 = 144; + map<sint64, int64> field_map_sint64_int64_145 = 145; + map<sint64, Proto2MessageLite> field_map_sint64_message_146 = 146; + map<sint64, sfixed32> field_map_sint64_sfixed32_147 = 147; + map<sint64, sfixed64> field_map_sint64_sfixed64_148 = 148; + map<sint64, sint32> field_map_sint64_sint32_149 = 149; + map<sint64, sint64> field_map_sint64_sint64_150 = 150; + map<sint64, string> field_map_sint64_string_151 = 151; + map<sint64, uint32> field_map_sint64_uint32_152 = 152; + map<sint64, uint64> field_map_sint64_uint64_153 = 153; + map<string, bool> field_map_string_bool_154 = 154; + map<string, bytes> field_map_string_bytes_155 = 155; + map<string, double> field_map_string_double_156 = 156; + map<string, Proto2MessageLite.TestEnum> field_map_string_enum_157 = 157; + map<string, fixed32> field_map_string_fixed32_158 = 158; + map<string, fixed64> field_map_string_fixed64_159 = 159; + map<string, float> field_map_string_float_160 = 160; + map<string, int32> field_map_string_int32_161 = 161; + map<string, int64> field_map_string_int64_162 = 162; + map<string, Proto2MessageLite> field_map_string_message_163 = 163; + map<string, sfixed32> field_map_string_sfixed32_164 = 164; + map<string, sfixed64> field_map_string_sfixed64_165 = 165; + map<string, sint32> field_map_string_sint32_166 = 166; + map<string, sint64> field_map_string_sint64_167 = 167; + map<string, string> field_map_string_string_168 = 168; + map<string, uint32> field_map_string_uint32_169 = 169; + map<string, uint64> field_map_string_uint64_170 = 170; + map<uint32, bool> field_map_uint32_bool_171 = 171; + map<uint32, bytes> field_map_uint32_bytes_172 = 172; + map<uint32, double> field_map_uint32_double_173 = 173; + map<uint32, Proto2MessageLite.TestEnum> field_map_uint32_enum_174 = 174; + map<uint32, fixed32> field_map_uint32_fixed32_175 = 175; + map<uint32, fixed64> field_map_uint32_fixed64_176 = 176; + map<uint32, float> field_map_uint32_float_177 = 177; + map<uint32, int32> field_map_uint32_int32_178 = 178; + map<uint32, int64> field_map_uint32_int64_179 = 179; + map<uint32, Proto2MessageLite> field_map_uint32_message_180 = 180; + map<uint32, sfixed32> field_map_uint32_sfixed32_181 = 181; + map<uint32, sfixed64> field_map_uint32_sfixed64_182 = 182; + map<uint32, sint32> field_map_uint32_sint32_183 = 183; + map<uint32, sint64> field_map_uint32_sint64_184 = 184; + map<uint32, string> field_map_uint32_string_185 = 185; + map<uint32, uint32> field_map_uint32_uint32_186 = 186; + map<uint32, uint64> field_map_uint32_uint64_187 = 187; + map<uint64, bool> field_map_uint64_bool_188 = 188; + map<uint64, bytes> field_map_uint64_bytes_189 = 189; + map<uint64, double> field_map_uint64_double_190 = 190; + map<uint64, Proto2MessageLite.TestEnum> field_map_uint64_enum_191 = 191; + map<uint64, fixed32> field_map_uint64_fixed32_192 = 192; + map<uint64, fixed64> field_map_uint64_fixed64_193 = 193; + map<uint64, float> field_map_uint64_float_194 = 194; + map<uint64, int32> field_map_uint64_int32_195 = 195; + map<uint64, int64> field_map_uint64_int64_196 = 196; + map<uint64, Proto2MessageLite> field_map_uint64_message_197 = 197; + map<uint64, sfixed32> field_map_uint64_sfixed32_198 = 198; + map<uint64, sfixed64> field_map_uint64_sfixed64_199 = 199; + map<uint64, sint32> field_map_uint64_sint32_200 = 200; + map<uint64, sint64> field_map_uint64_sint64_201 = 201; + map<uint64, string> field_map_uint64_string_202 = 202; + map<uint64, uint32> field_map_uint64_uint32_203 = 203; + map<uint64, uint64> field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto3_message.proto b/java/core/src/test/proto/com/google/protobuf/proto3_message.proto new file mode 100755 index 0000000000..01c7deb7c9 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto3_message.proto @@ -0,0 +1,326 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto3Testing"; +package protobuf.experimental; + +message Proto3SpecialFieldName { + double regular_name = 1; + int32 cached_size = 2; + int64 serialized_size = 3; + string class = 4; +} + +message Proto3Message { + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + double field_double_1 = 1; + float field_float_2 = 2; + int64 field_int64_3 = 3; + uint64 field_uint64_4 = 4; + int32 field_int32_5 = 5; + fixed64 field_fixed64_6 = 6; + fixed32 field_fixed32_7 = 7; + bool field_bool_8 = 8; + string field_string_9 = 9; + Proto3Message field_message_10 = 10; + bytes field_bytes_11 = 11; + uint32 field_uint32_12 = 12; + TestEnum field_enum_13 = 13; + sfixed32 field_sfixed32_14 = 14; + sfixed64 field_sfixed64_15 = 15; + sint32 field_sint32_16 = 16; + sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto3Message field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto3Message field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + } +} + +message Proto3Empty {} + +message Proto3MessageWithMaps { + map<bool, bool> field_map_bool_bool_1 = 1; + map<bool, bytes> field_map_bool_bytes_2 = 2; + map<bool, double> field_map_bool_double_3 = 3; + map<bool, Proto3Message.TestEnum> field_map_bool_enum_4 = 4; + map<bool, fixed32> field_map_bool_fixed32_5 = 5; + map<bool, fixed64> field_map_bool_fixed64_6 = 6; + map<bool, float> field_map_bool_float_7 = 7; + map<bool, int32> field_map_bool_int32_8 = 8; + map<bool, int64> field_map_bool_int64_9 = 9; + map<bool, Proto3Message> field_map_bool_message_10 = 10; + map<bool, sfixed32> field_map_bool_sfixed32_11 = 11; + map<bool, sfixed64> field_map_bool_sfixed64_12 = 12; + map<bool, sint32> field_map_bool_sint32_13 = 13; + map<bool, sint64> field_map_bool_sint64_14 = 14; + map<bool, string> field_map_bool_string_15 = 15; + map<bool, uint32> field_map_bool_uint32_16 = 16; + map<bool, uint64> field_map_bool_uint64_17 = 17; + map<fixed32, bool> field_map_fixed32_bool_18 = 18; + map<fixed32, bytes> field_map_fixed32_bytes_19 = 19; + map<fixed32, double> field_map_fixed32_double_20 = 20; + map<fixed32, Proto3Message.TestEnum> field_map_fixed32_enum_21 = 21; + map<fixed32, fixed32> field_map_fixed32_fixed32_22 = 22; + map<fixed32, fixed64> field_map_fixed32_fixed64_23 = 23; + map<fixed32, float> field_map_fixed32_float_24 = 24; + map<fixed32, int32> field_map_fixed32_int32_25 = 25; + map<fixed32, int64> field_map_fixed32_int64_26 = 26; + map<fixed32, Proto3Message> field_map_fixed32_message_27 = 27; + map<fixed32, sfixed32> field_map_fixed32_sfixed32_28 = 28; + map<fixed32, sfixed64> field_map_fixed32_sfixed64_29 = 29; + map<fixed32, sint32> field_map_fixed32_sint32_30 = 30; + map<fixed32, sint64> field_map_fixed32_sint64_31 = 31; + map<fixed32, string> field_map_fixed32_string_32 = 32; + map<fixed32, uint32> field_map_fixed32_uint32_33 = 33; + map<fixed32, uint64> field_map_fixed32_uint64_34 = 34; + map<fixed64, bool> field_map_fixed64_bool_35 = 35; + map<fixed64, bytes> field_map_fixed64_bytes_36 = 36; + map<fixed64, double> field_map_fixed64_double_37 = 37; + map<fixed64, Proto3Message.TestEnum> field_map_fixed64_enum_38 = 38; + map<fixed64, fixed32> field_map_fixed64_fixed32_39 = 39; + map<fixed64, fixed64> field_map_fixed64_fixed64_40 = 40; + map<fixed64, float> field_map_fixed64_float_41 = 41; + map<fixed64, int32> field_map_fixed64_int32_42 = 42; + map<fixed64, int64> field_map_fixed64_int64_43 = 43; + map<fixed64, Proto3Message> field_map_fixed64_message_44 = 44; + map<fixed64, sfixed32> field_map_fixed64_sfixed32_45 = 45; + map<fixed64, sfixed64> field_map_fixed64_sfixed64_46 = 46; + map<fixed64, sint32> field_map_fixed64_sint32_47 = 47; + map<fixed64, sint64> field_map_fixed64_sint64_48 = 48; + map<fixed64, string> field_map_fixed64_string_49 = 49; + map<fixed64, uint32> field_map_fixed64_uint32_50 = 50; + map<fixed64, uint64> field_map_fixed64_uint64_51 = 51; + map<int32, bool> field_map_int32_bool_52 = 52; + map<int32, bytes> field_map_int32_bytes_53 = 53; + map<int32, double> field_map_int32_double_54 = 54; + map<int32, Proto3Message.TestEnum> field_map_int32_enum_55 = 55; + map<int32, fixed32> field_map_int32_fixed32_56 = 56; + map<int32, fixed64> field_map_int32_fixed64_57 = 57; + map<int32, float> field_map_int32_float_58 = 58; + map<int32, int32> field_map_int32_int32_59 = 59; + map<int32, int64> field_map_int32_int64_60 = 60; + map<int32, Proto3Message> field_map_int32_message_61 = 61; + map<int32, sfixed32> field_map_int32_sfixed32_62 = 62; + map<int32, sfixed64> field_map_int32_sfixed64_63 = 63; + map<int32, sint32> field_map_int32_sint32_64 = 64; + map<int32, sint64> field_map_int32_sint64_65 = 65; + map<int32, string> field_map_int32_string_66 = 66; + map<int32, uint32> field_map_int32_uint32_67 = 67; + map<int32, uint64> field_map_int32_uint64_68 = 68; + map<int64, bool> field_map_int64_bool_69 = 69; + map<int64, bytes> field_map_int64_bytes_70 = 70; + map<int64, double> field_map_int64_double_71 = 71; + map<int64, Proto3Message.TestEnum> field_map_int64_enum_72 = 72; + map<int64, fixed32> field_map_int64_fixed32_73 = 73; + map<int64, fixed64> field_map_int64_fixed64_74 = 74; + map<int64, float> field_map_int64_float_75 = 75; + map<int64, int32> field_map_int64_int32_76 = 76; + map<int64, int64> field_map_int64_int64_77 = 77; + map<int64, Proto3Message> field_map_int64_message_78 = 78; + map<int64, sfixed32> field_map_int64_sfixed32_79 = 79; + map<int64, sfixed64> field_map_int64_sfixed64_80 = 80; + map<int64, sint32> field_map_int64_sint32_81 = 81; + map<int64, sint64> field_map_int64_sint64_82 = 82; + map<int64, string> field_map_int64_string_83 = 83; + map<int64, uint32> field_map_int64_uint32_84 = 84; + map<int64, uint64> field_map_int64_uint64_85 = 85; + map<sfixed32, bool> field_map_sfixed32_bool_86 = 86; + map<sfixed32, bytes> field_map_sfixed32_bytes_87 = 87; + map<sfixed32, double> field_map_sfixed32_double_88 = 88; + map<sfixed32, Proto3Message.TestEnum> field_map_sfixed32_enum_89 = 89; + map<sfixed32, fixed32> field_map_sfixed32_fixed32_90 = 90; + map<sfixed32, fixed64> field_map_sfixed32_fixed64_91 = 91; + map<sfixed32, float> field_map_sfixed32_float_92 = 92; + map<sfixed32, int32> field_map_sfixed32_int32_93 = 93; + map<sfixed32, int64> field_map_sfixed32_int64_94 = 94; + map<sfixed32, Proto3Message> field_map_sfixed32_message_95 = 95; + map<sfixed32, sfixed32> field_map_sfixed32_sfixed32_96 = 96; + map<sfixed32, sfixed64> field_map_sfixed32_sfixed64_97 = 97; + map<sfixed32, sint32> field_map_sfixed32_sint32_98 = 98; + map<sfixed32, sint64> field_map_sfixed32_sint64_99 = 99; + map<sfixed32, string> field_map_sfixed32_string_100 = 100; + map<sfixed32, uint32> field_map_sfixed32_uint32_101 = 101; + map<sfixed32, uint64> field_map_sfixed32_uint64_102 = 102; + map<sfixed64, bool> field_map_sfixed64_bool_103 = 103; + map<sfixed64, bytes> field_map_sfixed64_bytes_104 = 104; + map<sfixed64, double> field_map_sfixed64_double_105 = 105; + map<sfixed64, Proto3Message.TestEnum> field_map_sfixed64_enum_106 = 106; + map<sfixed64, fixed32> field_map_sfixed64_fixed32_107 = 107; + map<sfixed64, fixed64> field_map_sfixed64_fixed64_108 = 108; + map<sfixed64, float> field_map_sfixed64_float_109 = 109; + map<sfixed64, int32> field_map_sfixed64_int32_110 = 110; + map<sfixed64, int64> field_map_sfixed64_int64_111 = 111; + map<sfixed64, Proto3Message> field_map_sfixed64_message_112 = 112; + map<sfixed64, sfixed32> field_map_sfixed64_sfixed32_113 = 113; + map<sfixed64, sfixed64> field_map_sfixed64_sfixed64_114 = 114; + map<sfixed64, sint32> field_map_sfixed64_sint32_115 = 115; + map<sfixed64, sint64> field_map_sfixed64_sint64_116 = 116; + map<sfixed64, string> field_map_sfixed64_string_117 = 117; + map<sfixed64, uint32> field_map_sfixed64_uint32_118 = 118; + map<sfixed64, uint64> field_map_sfixed64_uint64_119 = 119; + map<sint32, bool> field_map_sint32_bool_120 = 120; + map<sint32, bytes> field_map_sint32_bytes_121 = 121; + map<sint32, double> field_map_sint32_double_122 = 122; + map<sint32, Proto3Message.TestEnum> field_map_sint32_enum_123 = 123; + map<sint32, fixed32> field_map_sint32_fixed32_124 = 124; + map<sint32, fixed64> field_map_sint32_fixed64_125 = 125; + map<sint32, float> field_map_sint32_float_126 = 126; + map<sint32, int32> field_map_sint32_int32_127 = 127; + map<sint32, int64> field_map_sint32_int64_128 = 128; + map<sint32, Proto3Message> field_map_sint32_message_129 = 129; + map<sint32, sfixed32> field_map_sint32_sfixed32_130 = 130; + map<sint32, sfixed64> field_map_sint32_sfixed64_131 = 131; + map<sint32, sint32> field_map_sint32_sint32_132 = 132; + map<sint32, sint64> field_map_sint32_sint64_133 = 133; + map<sint32, string> field_map_sint32_string_134 = 134; + map<sint32, uint32> field_map_sint32_uint32_135 = 135; + map<sint32, uint64> field_map_sint32_uint64_136 = 136; + map<sint64, bool> field_map_sint64_bool_137 = 137; + map<sint64, bytes> field_map_sint64_bytes_138 = 138; + map<sint64, double> field_map_sint64_double_139 = 139; + map<sint64, Proto3Message.TestEnum> field_map_sint64_enum_140 = 140; + map<sint64, fixed32> field_map_sint64_fixed32_141 = 141; + map<sint64, fixed64> field_map_sint64_fixed64_142 = 142; + map<sint64, float> field_map_sint64_float_143 = 143; + map<sint64, int32> field_map_sint64_int32_144 = 144; + map<sint64, int64> field_map_sint64_int64_145 = 145; + map<sint64, Proto3Message> field_map_sint64_message_146 = 146; + map<sint64, sfixed32> field_map_sint64_sfixed32_147 = 147; + map<sint64, sfixed64> field_map_sint64_sfixed64_148 = 148; + map<sint64, sint32> field_map_sint64_sint32_149 = 149; + map<sint64, sint64> field_map_sint64_sint64_150 = 150; + map<sint64, string> field_map_sint64_string_151 = 151; + map<sint64, uint32> field_map_sint64_uint32_152 = 152; + map<sint64, uint64> field_map_sint64_uint64_153 = 153; + map<string, bool> field_map_string_bool_154 = 154; + map<string, bytes> field_map_string_bytes_155 = 155; + map<string, double> field_map_string_double_156 = 156; + map<string, Proto3Message.TestEnum> field_map_string_enum_157 = 157; + map<string, fixed32> field_map_string_fixed32_158 = 158; + map<string, fixed64> field_map_string_fixed64_159 = 159; + map<string, float> field_map_string_float_160 = 160; + map<string, int32> field_map_string_int32_161 = 161; + map<string, int64> field_map_string_int64_162 = 162; + map<string, Proto3Message> field_map_string_message_163 = 163; + map<string, sfixed32> field_map_string_sfixed32_164 = 164; + map<string, sfixed64> field_map_string_sfixed64_165 = 165; + map<string, sint32> field_map_string_sint32_166 = 166; + map<string, sint64> field_map_string_sint64_167 = 167; + map<string, string> field_map_string_string_168 = 168; + map<string, uint32> field_map_string_uint32_169 = 169; + map<string, uint64> field_map_string_uint64_170 = 170; + map<uint32, bool> field_map_uint32_bool_171 = 171; + map<uint32, bytes> field_map_uint32_bytes_172 = 172; + map<uint32, double> field_map_uint32_double_173 = 173; + map<uint32, Proto3Message.TestEnum> field_map_uint32_enum_174 = 174; + map<uint32, fixed32> field_map_uint32_fixed32_175 = 175; + map<uint32, fixed64> field_map_uint32_fixed64_176 = 176; + map<uint32, float> field_map_uint32_float_177 = 177; + map<uint32, int32> field_map_uint32_int32_178 = 178; + map<uint32, int64> field_map_uint32_int64_179 = 179; + map<uint32, Proto3Message> field_map_uint32_message_180 = 180; + map<uint32, sfixed32> field_map_uint32_sfixed32_181 = 181; + map<uint32, sfixed64> field_map_uint32_sfixed64_182 = 182; + map<uint32, sint32> field_map_uint32_sint32_183 = 183; + map<uint32, sint64> field_map_uint32_sint64_184 = 184; + map<uint32, string> field_map_uint32_string_185 = 185; + map<uint32, uint32> field_map_uint32_uint32_186 = 186; + map<uint32, uint64> field_map_uint32_uint64_187 = 187; + map<uint64, bool> field_map_uint64_bool_188 = 188; + map<uint64, bytes> field_map_uint64_bytes_189 = 189; + map<uint64, double> field_map_uint64_double_190 = 190; + map<uint64, Proto3Message.TestEnum> field_map_uint64_enum_191 = 191; + map<uint64, fixed32> field_map_uint64_fixed32_192 = 192; + map<uint64, fixed64> field_map_uint64_fixed64_193 = 193; + map<uint64, float> field_map_uint64_float_194 = 194; + map<uint64, int32> field_map_uint64_int32_195 = 195; + map<uint64, int64> field_map_uint64_int64_196 = 196; + map<uint64, Proto3Message> field_map_uint64_message_197 = 197; + map<uint64, sfixed32> field_map_uint64_sfixed32_198 = 198; + map<uint64, sfixed64> field_map_uint64_sfixed64_199 = 199; + map<uint64, sint32> field_map_uint64_sint32_200 = 200; + map<uint64, sint64> field_map_uint64_sint64_201 = 201; + map<uint64, string> field_map_uint64_string_202 = 202; + map<uint64, uint32> field_map_uint64_uint32_203 = 203; + map<uint64, uint64> field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto b/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto new file mode 100755 index 0000000000..ed364ec9f3 --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/proto3_message_lite.proto @@ -0,0 +1,319 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +option java_package = "com.google.protobuf.testing"; +option java_outer_classname = "Proto3TestingLite"; +package protobuf.experimental; + +message Proto3MessageLite { + enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; + } + + double field_double_1 = 1; + float field_float_2 = 2; + int64 field_int64_3 = 3; + uint64 field_uint64_4 = 4; + int32 field_int32_5 = 5; + fixed64 field_fixed64_6 = 6; + fixed32 field_fixed32_7 = 7; + bool field_bool_8 = 8; + string field_string_9 = 9; + Proto3MessageLite field_message_10 = 10; + bytes field_bytes_11 = 11; + uint32 field_uint32_12 = 12; + TestEnum field_enum_13 = 13; + sfixed32 field_sfixed32_14 = 14; + sfixed64 field_sfixed64_15 = 15; + sint32 field_sint32_16 = 16; + sint64 field_sint64_17 = 17; + repeated double field_double_list_18 = 18 [packed = false]; + repeated float field_float_list_19 = 19 [packed = false]; + repeated int64 field_int64_list_20 = 20 [packed = false]; + repeated uint64 field_uint64_list_21 = 21 [packed = false]; + repeated int32 field_int32_list_22 = 22 [packed = false]; + repeated fixed64 field_fixed64_list_23 = 23 [packed = false]; + repeated fixed32 field_fixed32_list_24 = 24 [packed = false]; + repeated bool field_bool_list_25 = 25 [packed = false]; + repeated string field_string_list_26 = 26 [packed = false]; + repeated Proto3MessageLite field_message_list_27 = 27 [packed = false]; + repeated bytes field_bytes_list_28 = 28 [packed = false]; + repeated uint32 field_uint32_list_29 = 29 [packed = false]; + repeated TestEnum field_enum_list_30 = 30 [packed = false]; + repeated sfixed32 field_sfixed32_list_31 = 31 [packed = false]; + repeated sfixed64 field_sfixed64_list_32 = 32 [packed = false]; + repeated sint32 field_sint32_list_33 = 33 [packed = false]; + repeated sint64 field_sint64_list_34 = 34 [packed = false]; + repeated double field_double_list_packed_35 = 35 [packed = true]; + repeated float field_float_list_packed_36 = 36 [packed = true]; + repeated int64 field_int64_list_packed_37 = 37 [packed = true]; + repeated uint64 field_uint64_list_packed_38 = 38 [packed = true]; + repeated int32 field_int32_list_packed_39 = 39 [packed = true]; + repeated fixed64 field_fixed64_list_packed_40 = 40 [packed = true]; + repeated fixed32 field_fixed32_list_packed_41 = 41 [packed = true]; + repeated bool field_bool_list_packed_42 = 42 [packed = true]; + repeated uint32 field_uint32_list_packed_43 = 43 [packed = true]; + repeated TestEnum field_enum_list_packed_44 = 44 [packed = true]; + repeated sfixed32 field_sfixed32_list_packed_45 = 45 [packed = true]; + repeated sfixed64 field_sfixed64_list_packed_46 = 46 [packed = true]; + repeated sint32 field_sint32_list_packed_47 = 47 [packed = true]; + repeated sint64 field_sint64_list_packed_48 = 48 [packed = true]; + oneof test_oneof { + double field_double_53 = 53; + float field_float_54 = 54; + int64 field_int64_55 = 55; + uint64 field_uint64_56 = 56; + int32 field_int32_57 = 57; + fixed64 field_fixed64_58 = 58; + fixed32 field_fixed32_59 = 59; + bool field_bool_60 = 60; + string field_string_61 = 61; + Proto3MessageLite field_message_62 = 62; + bytes field_bytes_63 = 63; + uint32 field_uint32_64 = 64; + sfixed32 field_sfixed32_65 = 65; + sfixed64 field_sfixed64_66 = 66; + sint32 field_sint32_67 = 67; + sint64 field_sint64_68 = 68; + } +} + +message Proto3EmptyLite {} + +message Proto3MessageLiteWithMaps { + map<bool, bool> field_map_bool_bool_1 = 1; + map<bool, bytes> field_map_bool_bytes_2 = 2; + map<bool, double> field_map_bool_double_3 = 3; + map<bool, Proto3MessageLite.TestEnum> field_map_bool_enum_4 = 4; + map<bool, fixed32> field_map_bool_fixed32_5 = 5; + map<bool, fixed64> field_map_bool_fixed64_6 = 6; + map<bool, float> field_map_bool_float_7 = 7; + map<bool, int32> field_map_bool_int32_8 = 8; + map<bool, int64> field_map_bool_int64_9 = 9; + map<bool, Proto3MessageLite> field_map_bool_message_10 = 10; + map<bool, sfixed32> field_map_bool_sfixed32_11 = 11; + map<bool, sfixed64> field_map_bool_sfixed64_12 = 12; + map<bool, sint32> field_map_bool_sint32_13 = 13; + map<bool, sint64> field_map_bool_sint64_14 = 14; + map<bool, string> field_map_bool_string_15 = 15; + map<bool, uint32> field_map_bool_uint32_16 = 16; + map<bool, uint64> field_map_bool_uint64_17 = 17; + map<fixed32, bool> field_map_fixed32_bool_18 = 18; + map<fixed32, bytes> field_map_fixed32_bytes_19 = 19; + map<fixed32, double> field_map_fixed32_double_20 = 20; + map<fixed32, Proto3MessageLite.TestEnum> field_map_fixed32_enum_21 = 21; + map<fixed32, fixed32> field_map_fixed32_fixed32_22 = 22; + map<fixed32, fixed64> field_map_fixed32_fixed64_23 = 23; + map<fixed32, float> field_map_fixed32_float_24 = 24; + map<fixed32, int32> field_map_fixed32_int32_25 = 25; + map<fixed32, int64> field_map_fixed32_int64_26 = 26; + map<fixed32, Proto3MessageLite> field_map_fixed32_message_27 = 27; + map<fixed32, sfixed32> field_map_fixed32_sfixed32_28 = 28; + map<fixed32, sfixed64> field_map_fixed32_sfixed64_29 = 29; + map<fixed32, sint32> field_map_fixed32_sint32_30 = 30; + map<fixed32, sint64> field_map_fixed32_sint64_31 = 31; + map<fixed32, string> field_map_fixed32_string_32 = 32; + map<fixed32, uint32> field_map_fixed32_uint32_33 = 33; + map<fixed32, uint64> field_map_fixed32_uint64_34 = 34; + map<fixed64, bool> field_map_fixed64_bool_35 = 35; + map<fixed64, bytes> field_map_fixed64_bytes_36 = 36; + map<fixed64, double> field_map_fixed64_double_37 = 37; + map<fixed64, Proto3MessageLite.TestEnum> field_map_fixed64_enum_38 = 38; + map<fixed64, fixed32> field_map_fixed64_fixed32_39 = 39; + map<fixed64, fixed64> field_map_fixed64_fixed64_40 = 40; + map<fixed64, float> field_map_fixed64_float_41 = 41; + map<fixed64, int32> field_map_fixed64_int32_42 = 42; + map<fixed64, int64> field_map_fixed64_int64_43 = 43; + map<fixed64, Proto3MessageLite> field_map_fixed64_message_44 = 44; + map<fixed64, sfixed32> field_map_fixed64_sfixed32_45 = 45; + map<fixed64, sfixed64> field_map_fixed64_sfixed64_46 = 46; + map<fixed64, sint32> field_map_fixed64_sint32_47 = 47; + map<fixed64, sint64> field_map_fixed64_sint64_48 = 48; + map<fixed64, string> field_map_fixed64_string_49 = 49; + map<fixed64, uint32> field_map_fixed64_uint32_50 = 50; + map<fixed64, uint64> field_map_fixed64_uint64_51 = 51; + map<int32, bool> field_map_int32_bool_52 = 52; + map<int32, bytes> field_map_int32_bytes_53 = 53; + map<int32, double> field_map_int32_double_54 = 54; + map<int32, Proto3MessageLite.TestEnum> field_map_int32_enum_55 = 55; + map<int32, fixed32> field_map_int32_fixed32_56 = 56; + map<int32, fixed64> field_map_int32_fixed64_57 = 57; + map<int32, float> field_map_int32_float_58 = 58; + map<int32, int32> field_map_int32_int32_59 = 59; + map<int32, int64> field_map_int32_int64_60 = 60; + map<int32, Proto3MessageLite> field_map_int32_message_61 = 61; + map<int32, sfixed32> field_map_int32_sfixed32_62 = 62; + map<int32, sfixed64> field_map_int32_sfixed64_63 = 63; + map<int32, sint32> field_map_int32_sint32_64 = 64; + map<int32, sint64> field_map_int32_sint64_65 = 65; + map<int32, string> field_map_int32_string_66 = 66; + map<int32, uint32> field_map_int32_uint32_67 = 67; + map<int32, uint64> field_map_int32_uint64_68 = 68; + map<int64, bool> field_map_int64_bool_69 = 69; + map<int64, bytes> field_map_int64_bytes_70 = 70; + map<int64, double> field_map_int64_double_71 = 71; + map<int64, Proto3MessageLite.TestEnum> field_map_int64_enum_72 = 72; + map<int64, fixed32> field_map_int64_fixed32_73 = 73; + map<int64, fixed64> field_map_int64_fixed64_74 = 74; + map<int64, float> field_map_int64_float_75 = 75; + map<int64, int32> field_map_int64_int32_76 = 76; + map<int64, int64> field_map_int64_int64_77 = 77; + map<int64, Proto3MessageLite> field_map_int64_message_78 = 78; + map<int64, sfixed32> field_map_int64_sfixed32_79 = 79; + map<int64, sfixed64> field_map_int64_sfixed64_80 = 80; + map<int64, sint32> field_map_int64_sint32_81 = 81; + map<int64, sint64> field_map_int64_sint64_82 = 82; + map<int64, string> field_map_int64_string_83 = 83; + map<int64, uint32> field_map_int64_uint32_84 = 84; + map<int64, uint64> field_map_int64_uint64_85 = 85; + map<sfixed32, bool> field_map_sfixed32_bool_86 = 86; + map<sfixed32, bytes> field_map_sfixed32_bytes_87 = 87; + map<sfixed32, double> field_map_sfixed32_double_88 = 88; + map<sfixed32, Proto3MessageLite.TestEnum> field_map_sfixed32_enum_89 = 89; + map<sfixed32, fixed32> field_map_sfixed32_fixed32_90 = 90; + map<sfixed32, fixed64> field_map_sfixed32_fixed64_91 = 91; + map<sfixed32, float> field_map_sfixed32_float_92 = 92; + map<sfixed32, int32> field_map_sfixed32_int32_93 = 93; + map<sfixed32, int64> field_map_sfixed32_int64_94 = 94; + map<sfixed32, Proto3MessageLite> field_map_sfixed32_message_95 = 95; + map<sfixed32, sfixed32> field_map_sfixed32_sfixed32_96 = 96; + map<sfixed32, sfixed64> field_map_sfixed32_sfixed64_97 = 97; + map<sfixed32, sint32> field_map_sfixed32_sint32_98 = 98; + map<sfixed32, sint64> field_map_sfixed32_sint64_99 = 99; + map<sfixed32, string> field_map_sfixed32_string_100 = 100; + map<sfixed32, uint32> field_map_sfixed32_uint32_101 = 101; + map<sfixed32, uint64> field_map_sfixed32_uint64_102 = 102; + map<sfixed64, bool> field_map_sfixed64_bool_103 = 103; + map<sfixed64, bytes> field_map_sfixed64_bytes_104 = 104; + map<sfixed64, double> field_map_sfixed64_double_105 = 105; + map<sfixed64, Proto3MessageLite.TestEnum> field_map_sfixed64_enum_106 = 106; + map<sfixed64, fixed32> field_map_sfixed64_fixed32_107 = 107; + map<sfixed64, fixed64> field_map_sfixed64_fixed64_108 = 108; + map<sfixed64, float> field_map_sfixed64_float_109 = 109; + map<sfixed64, int32> field_map_sfixed64_int32_110 = 110; + map<sfixed64, int64> field_map_sfixed64_int64_111 = 111; + map<sfixed64, Proto3MessageLite> field_map_sfixed64_message_112 = 112; + map<sfixed64, sfixed32> field_map_sfixed64_sfixed32_113 = 113; + map<sfixed64, sfixed64> field_map_sfixed64_sfixed64_114 = 114; + map<sfixed64, sint32> field_map_sfixed64_sint32_115 = 115; + map<sfixed64, sint64> field_map_sfixed64_sint64_116 = 116; + map<sfixed64, string> field_map_sfixed64_string_117 = 117; + map<sfixed64, uint32> field_map_sfixed64_uint32_118 = 118; + map<sfixed64, uint64> field_map_sfixed64_uint64_119 = 119; + map<sint32, bool> field_map_sint32_bool_120 = 120; + map<sint32, bytes> field_map_sint32_bytes_121 = 121; + map<sint32, double> field_map_sint32_double_122 = 122; + map<sint32, Proto3MessageLite.TestEnum> field_map_sint32_enum_123 = 123; + map<sint32, fixed32> field_map_sint32_fixed32_124 = 124; + map<sint32, fixed64> field_map_sint32_fixed64_125 = 125; + map<sint32, float> field_map_sint32_float_126 = 126; + map<sint32, int32> field_map_sint32_int32_127 = 127; + map<sint32, int64> field_map_sint32_int64_128 = 128; + map<sint32, Proto3MessageLite> field_map_sint32_message_129 = 129; + map<sint32, sfixed32> field_map_sint32_sfixed32_130 = 130; + map<sint32, sfixed64> field_map_sint32_sfixed64_131 = 131; + map<sint32, sint32> field_map_sint32_sint32_132 = 132; + map<sint32, sint64> field_map_sint32_sint64_133 = 133; + map<sint32, string> field_map_sint32_string_134 = 134; + map<sint32, uint32> field_map_sint32_uint32_135 = 135; + map<sint32, uint64> field_map_sint32_uint64_136 = 136; + map<sint64, bool> field_map_sint64_bool_137 = 137; + map<sint64, bytes> field_map_sint64_bytes_138 = 138; + map<sint64, double> field_map_sint64_double_139 = 139; + map<sint64, Proto3MessageLite.TestEnum> field_map_sint64_enum_140 = 140; + map<sint64, fixed32> field_map_sint64_fixed32_141 = 141; + map<sint64, fixed64> field_map_sint64_fixed64_142 = 142; + map<sint64, float> field_map_sint64_float_143 = 143; + map<sint64, int32> field_map_sint64_int32_144 = 144; + map<sint64, int64> field_map_sint64_int64_145 = 145; + map<sint64, Proto3MessageLite> field_map_sint64_message_146 = 146; + map<sint64, sfixed32> field_map_sint64_sfixed32_147 = 147; + map<sint64, sfixed64> field_map_sint64_sfixed64_148 = 148; + map<sint64, sint32> field_map_sint64_sint32_149 = 149; + map<sint64, sint64> field_map_sint64_sint64_150 = 150; + map<sint64, string> field_map_sint64_string_151 = 151; + map<sint64, uint32> field_map_sint64_uint32_152 = 152; + map<sint64, uint64> field_map_sint64_uint64_153 = 153; + map<string, bool> field_map_string_bool_154 = 154; + map<string, bytes> field_map_string_bytes_155 = 155; + map<string, double> field_map_string_double_156 = 156; + map<string, Proto3MessageLite.TestEnum> field_map_string_enum_157 = 157; + map<string, fixed32> field_map_string_fixed32_158 = 158; + map<string, fixed64> field_map_string_fixed64_159 = 159; + map<string, float> field_map_string_float_160 = 160; + map<string, int32> field_map_string_int32_161 = 161; + map<string, int64> field_map_string_int64_162 = 162; + map<string, Proto3MessageLite> field_map_string_message_163 = 163; + map<string, sfixed32> field_map_string_sfixed32_164 = 164; + map<string, sfixed64> field_map_string_sfixed64_165 = 165; + map<string, sint32> field_map_string_sint32_166 = 166; + map<string, sint64> field_map_string_sint64_167 = 167; + map<string, string> field_map_string_string_168 = 168; + map<string, uint32> field_map_string_uint32_169 = 169; + map<string, uint64> field_map_string_uint64_170 = 170; + map<uint32, bool> field_map_uint32_bool_171 = 171; + map<uint32, bytes> field_map_uint32_bytes_172 = 172; + map<uint32, double> field_map_uint32_double_173 = 173; + map<uint32, Proto3MessageLite.TestEnum> field_map_uint32_enum_174 = 174; + map<uint32, fixed32> field_map_uint32_fixed32_175 = 175; + map<uint32, fixed64> field_map_uint32_fixed64_176 = 176; + map<uint32, float> field_map_uint32_float_177 = 177; + map<uint32, int32> field_map_uint32_int32_178 = 178; + map<uint32, int64> field_map_uint32_int64_179 = 179; + map<uint32, Proto3MessageLite> field_map_uint32_message_180 = 180; + map<uint32, sfixed32> field_map_uint32_sfixed32_181 = 181; + map<uint32, sfixed64> field_map_uint32_sfixed64_182 = 182; + map<uint32, sint32> field_map_uint32_sint32_183 = 183; + map<uint32, sint64> field_map_uint32_sint64_184 = 184; + map<uint32, string> field_map_uint32_string_185 = 185; + map<uint32, uint32> field_map_uint32_uint32_186 = 186; + map<uint32, uint64> field_map_uint32_uint64_187 = 187; + map<uint64, bool> field_map_uint64_bool_188 = 188; + map<uint64, bytes> field_map_uint64_bytes_189 = 189; + map<uint64, double> field_map_uint64_double_190 = 190; + map<uint64, Proto3MessageLite.TestEnum> field_map_uint64_enum_191 = 191; + map<uint64, fixed32> field_map_uint64_fixed32_192 = 192; + map<uint64, fixed64> field_map_uint64_fixed64_193 = 193; + map<uint64, float> field_map_uint64_float_194 = 194; + map<uint64, int32> field_map_uint64_int32_195 = 195; + map<uint64, int64> field_map_uint64_int64_196 = 196; + map<uint64, Proto3MessageLite> field_map_uint64_message_197 = 197; + map<uint64, sfixed32> field_map_uint64_sfixed32_198 = 198; + map<uint64, sfixed64> field_map_uint64_sfixed64_199 = 199; + map<uint64, sint32> field_map_uint64_sint32_200 = 200; + map<uint64, sint64> field_map_uint64_sint64_201 = 201; + map<uint64, string> field_map_uint64_string_202 = 202; + map<uint64, uint32> field_map_uint64_uint32_203 = 203; + map<uint64, uint64> field_map_uint64_uint64_204 = 204; +} diff --git a/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto b/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto new file mode 100755 index 0000000000..f151edf8ed --- /dev/null +++ b/java/core/src/test/proto/com/google/protobuf/wrappers_test.proto @@ -0,0 +1,49 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package wrappers_test; +option java_package = "com.google.protobuf.wrapperstest"; +option java_outer_classname = "WrappersTestProto"; +import "google/protobuf/wrappers.proto"; + +message TopLevelMessage { + int32 field1 = 1; + .google.protobuf.DoubleValue field_double = 2; + .google.protobuf.FloatValue field_float = 3; + .google.protobuf.Int64Value field_int64 = 4; + .google.protobuf.UInt64Value field_uint64 = 5; + .google.protobuf.Int32Value field_int32 = 6; + .google.protobuf.UInt32Value field_uint32 = 7; + .google.protobuf.BoolValue field_bool = 8; + .google.protobuf.StringValue field_string = 9; + .google.protobuf.BytesValue field_bytes = 10; +} diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/lite/src/test/java/com/google/protobuf/LiteTest.java old mode 100644 new mode 100755 similarity index 99% rename from java/core/src/test/java/com/google/protobuf/LiteTest.java rename to java/lite/src/test/java/com/google/protobuf/LiteTest.java index 89213a76c1..960b2037c1 --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java @@ -50,6 +50,8 @@ import com.google.protobuf.UnittestLite.TestHugeFieldNumbersLite; import com.google.protobuf.UnittestLite.TestNestedExtensionLite; import map_lite_test.MapTestProto.TestMap; import map_lite_test.MapTestProto.TestMap.MessageValue; +import protobuf_unittest.NestedExtensionLite; +import protobuf_unittest.NonNestedExtensionLite; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; @@ -2338,6 +2340,26 @@ public class LiteTest extends TestCase { } } + public void testExtensionRenamesKeywords() { + assertTrue(NonNestedExtensionLite.package_ instanceof GeneratedMessageLite.GeneratedExtension); + assertTrue( + NestedExtensionLite.MyNestedExtensionLite.private_ + instanceof GeneratedMessageLite.GeneratedExtension); + + NonNestedExtensionLite.MessageLiteToBeExtended msg = + NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() + .setExtension(NonNestedExtensionLite.package_, true) + .build(); + assertTrue(msg.getExtension(NonNestedExtensionLite.package_)); + + msg = + NonNestedExtensionLite.MessageLiteToBeExtended.newBuilder() + .setExtension(NestedExtensionLite.MyNestedExtensionLite.private_, 2.4) + .build(); + assertEquals( + 2.4, msg.getExtension(NestedExtensionLite.MyNestedExtensionLite.private_), 0.001); + } + private static final class OneTimeIterableList<T> extends ArrayList<T> { private boolean wasIterated = false; diff --git a/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java b/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java new file mode 100755 index 0000000000..4a1d89b937 --- /dev/null +++ b/java/lite/src/test/java/com/google/protobuf/Proto2MessageLiteInfoFactory.java @@ -0,0 +1,1327 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf; + +import static com.google.protobuf.FieldInfo.forField; +import static com.google.protobuf.FieldInfo.forFieldWithEnumVerifier; +import static com.google.protobuf.FieldInfo.forMapField; +import static com.google.protobuf.FieldInfo.forOneofMemberField; +import static com.google.protobuf.FieldInfo.forProto2OptionalField; +import static com.google.protobuf.FieldInfo.forProto2RequiredField; +import static com.google.protobuf.FieldInfo.forRepeatedMessageField; + +import com.google.protobuf.testing.Proto2TestingLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroup49; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroup69; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldGroupList51; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.FieldRequiredGroup88; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.RequiredNestedMessage; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite.TestEnum; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions; +import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps; +import java.lang.reflect.Field; + +/** A factory that generates a hard-coded info for {@link Proto2MessageLite}. */ +public final class Proto2MessageLiteInfoFactory implements MessageInfoFactory { + private static final Proto2MessageLiteInfoFactory instanceForRawMessageInfo = + new Proto2MessageLiteInfoFactory(true); + private static final Proto2MessageLiteInfoFactory instanceForStructuralMessageInfo = + new Proto2MessageLiteInfoFactory(false); + + public static Proto2MessageLiteInfoFactory getInstanceForRawMessageInfo() { + return instanceForRawMessageInfo; + } + + public static Proto2MessageLiteInfoFactory getInstanceForStructuralMessageInfo() { + return instanceForStructuralMessageInfo; + } + + private final boolean produceRawMessageInfo; + + private Proto2MessageLiteInfoFactory(boolean produceRawMessageInfo) { + this.produceRawMessageInfo = produceRawMessageInfo; + } + + @Override + public boolean isSupported(Class<?> clazz) { + return true; + } + + @Override + public MessageInfo messageInfoFor(Class<?> clazz) { + return produceRawMessageInfo ? rawMessageInfoFor(clazz) : structuralMessageInfoFor(clazz); + } + + private MessageInfo rawMessageInfoFor(Class<?> clazz) { + if (Proto2MessageLite.class.isAssignableFrom(clazz)) { + return newRawMessageInfoForProto2MessageLite(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + private MessageInfo newRawMessageInfoForProto2MessageLite() { + java.lang.Object[] objects = + new java.lang.Object[] { + "testOneof_", + "testOneofCase_", + "bitField0_", + "bitField1_", + "fieldDouble1_", + "fieldFloat2_", + "fieldInt643_", + "fieldUint644_", + "fieldInt325_", + "fieldFixed646_", + "fieldFixed327_", + "fieldBool8_", + "fieldString9_", + "fieldMessage10_", + "fieldBytes11_", + "fieldUint3212_", + "fieldEnum13_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed3214_", + "fieldSfixed6415_", + "fieldSint3216_", + "fieldSint6417_", + "fieldDoubleList18_", + "fieldFloatList19_", + "fieldInt64List20_", + "fieldUint64List21_", + "fieldInt32List22_", + "fieldFixed64List23_", + "fieldFixed32List24_", + "fieldBoolList25_", + "fieldStringList26_", + "fieldMessageList27_", + Proto2MessageLite.class, + "fieldBytesList28_", + "fieldUint32List29_", + "fieldEnumList30_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed32List31_", + "fieldSfixed64List32_", + "fieldSint32List33_", + "fieldSint64List34_", + "fieldDoubleListPacked35_", + "fieldFloatListPacked36_", + "fieldInt64ListPacked37_", + "fieldUint64ListPacked38_", + "fieldInt32ListPacked39_", + "fieldFixed64ListPacked40_", + "fieldFixed32ListPacked41_", + "fieldBoolListPacked42_", + "fieldUint32ListPacked43_", + "fieldEnumListPacked44_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldSfixed32ListPacked45_", + "fieldSfixed64ListPacked46_", + "fieldSint32ListPacked47_", + "fieldSint64ListPacked48_", + "fieldGroup49_", + "fieldGroupList51_", + Proto2MessageLite.FieldGroupList51.class, + Proto2MessageLite.class, + Proto2MessageLite.FieldGroup69.class, + "fieldRequiredDouble71_", + "fieldRequiredFloat72_", + "fieldRequiredInt6473_", + "fieldRequiredUint6474_", + "fieldRequiredInt3275_", + "fieldRequiredFixed6476_", + "fieldRequiredFixed3277_", + "fieldRequiredBool78_", + "fieldRequiredString79_", + "fieldRequiredMessage80_", + "fieldRequiredBytes81_", + "fieldRequiredUint3282_", + "fieldRequiredEnum83_", + Proto2MessageLite.TestEnum.internalGetVerifier(), + "fieldRequiredSfixed3284_", + "fieldRequiredSfixed6485_", + "fieldRequiredSint3286_", + "fieldRequiredSint6487_", + "fieldRequiredGroup88_", + }; + // To update this after a proto change, run protoc on proto2_message_lite.proto and copy over + // the content of the generated buildMessageInfo() method here. + java.lang.String info = + "\u0001U\u0001\u0002\u0001XU\u0000 \u0015\u0001\u0000\u0000\u0002\u0001\u0001\u0003" + + "\u0002\u0002\u0004\u0003\u0003\u0005\u0004\u0004\u0006\u0005\u0005\u0007\u0006\u0006" + + "\b\u0007\u0007\t\b\b\n\u0409\t\u000b\n\n\f\u000b\u000b\r\f\f\u000e\r\r\u000f\u000e" + + "\u000e\u0010\u000f\u000f\u0011\u0010\u0010\u0012\u0012\u0013\u0013\u0014\u0014\u0015" + + "\u0015\u0016\u0016\u0017\u0017\u0018\u0018\u0019\u0019\u001a\u001a\u001b\u041b\u001c" + + "\u001c\u001d\u001d\u001e\u001e\u001f\u001f !!\"\"##$$%%&&\'\'(())**++,,--..//00" + + "1\u0011\u00113153\u000064\u000075\u000086\u000097\u0000:8\u0000;9\u0000<:\u0000=" + + ";\u0000>\u043c\u0000?=\u0000@>\u0000A@\u0000BA\u0000CB\u0000DC\u0000ED\u0000G\u0500" + + "#H\u0501$I\u0502%J\u0503&K\u0504\'L\u0505(M\u0506)N\u0507*O\u0508+P\u0509,Q\u050a" + + "-R\u050b.S\u050c/T\u050d0U\u050e1V\u050f2W\u05103X\u05114"; + return new RawMessageInfo(Proto2MessageLite.getDefaultInstance(), info, objects); + } + + private MessageInfo structuralMessageInfoFor(Class<?> clazz) { + if (Proto2MessageLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLite(); + } else if (FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup49(); + } else if (FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroupList51(); + } else if (FieldGroup69.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldGroup69(); + } else if (FieldRequiredGroup88.class.isAssignableFrom(clazz)) { + return newMessageInfoForFieldRequiredGroup88(); + } else if (RequiredNestedMessage.class.isAssignableFrom(clazz)) { + return newMessageInfoForRequiredNestedMessage(); + } else if (Proto2EmptyLite.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2EmptyLite(); + } else if (Proto2MessageLiteWithExtensions.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLiteWithExtensions(); + } else if (Proto2TestingLite.FieldGroup49.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroup49(); + } else if (Proto2TestingLite.FieldGroupList51.class.isAssignableFrom(clazz)) { + return newMessageInfoForExtensionFieldGroupList51(); + } else if (Proto2TestingLite.Proto2MessageLiteWithMaps.class.isAssignableFrom(clazz)) { + return newMessageInfoForProto2MessageLiteWithMaps(); + } else { + throw new IllegalArgumentException("Unsupported class: " + clazz.getName()); + } + } + + /** + * Creates a new hard-coded info for {@link Proto2MessageLite}. Each time this is called, we + * manually go through the entire process of what a message would do if it self-registered its own + * info, including looking up each field by name. This is done for benchmarking purposes, so that + * we get a more accurate representation of the time it takes to perform this process. + */ + private static StructuralMessageInfo newMessageInfoForProto2MessageLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(48); + builder.withCheckInitialized( + new int[] { + 10, 27, 62, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + }); + lookupFieldsByName(builder); + return builder.build(); + } + + private static void lookupFieldsByName(StructuralMessageInfo.Builder builder) { + Field bitField0 = field(Proto2MessageLite.class, "bitField0_"); + + builder.withDefaultInstance(Proto2MessageLite.getDefaultInstance()); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldDouble1_"), + 1, + FieldType.DOUBLE, + bitField0, + 0x00000001, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFloat2_"), + 2, + FieldType.FLOAT, + bitField0, + 0x00000002, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldInt643_"), + 3, + FieldType.INT64, + bitField0, + 0x00000004, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldUint644_"), + 4, + FieldType.UINT64, + bitField0, + 0x00000008, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldInt325_"), + 5, + FieldType.INT32, + bitField0, + 0x00000010, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFixed646_"), + 6, + FieldType.FIXED64, + bitField0, + 0x00000020, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldFixed327_"), + 7, + FieldType.FIXED32, + bitField0, + 0x00000040, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldBool8_"), + 8, + FieldType.BOOL, + bitField0, + 0x00000080, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldString9_"), + 9, + FieldType.STRING, + bitField0, + 0x00000100, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldMessage10_"), + 10, + FieldType.MESSAGE, + bitField0, + 0x00000200, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldBytes11_"), + 11, + FieldType.BYTES, + bitField0, + 0x00000400, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldUint3212_"), + 12, + FieldType.UINT32, + bitField0, + 0x00000800, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldEnum13_"), + 13, + FieldType.ENUM, + bitField0, + 0x00001000, + false, + TestEnum.internalGetVerifier())); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSfixed3214_"), + 14, + FieldType.SFIXED32, + bitField0, + 0x00002000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSfixed6415_"), + 15, + FieldType.SFIXED64, + bitField0, + 0x00004000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSint3216_"), + 16, + FieldType.SINT32, + bitField0, + 0x00008000, + false, + null)); + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldSint6417_"), + 17, + FieldType.SINT64, + bitField0, + 0x00010000, + false, + null)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldDoubleList18_"), + 18, + FieldType.DOUBLE_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFloatList19_"), 19, FieldType.FLOAT_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt64List20_"), 20, FieldType.INT64_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint64List21_"), + 21, + FieldType.UINT64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt32List22_"), 22, FieldType.INT32_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed64List23_"), + 23, + FieldType.FIXED64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed32List24_"), + 24, + FieldType.FIXED32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBoolList25_"), 25, FieldType.BOOL_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldStringList26_"), + 26, + FieldType.STRING_LIST, + false)); + builder.withField( + forRepeatedMessageField( + field(Proto2MessageLite.class, "fieldMessageList27_"), + 27, + FieldType.MESSAGE_LIST, + Proto2MessageLite.class)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBytesList28_"), 28, FieldType.BYTES_LIST, false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint32List29_"), + 29, + FieldType.UINT32_LIST, + false)); + builder.withField( + forFieldWithEnumVerifier( + field(Proto2MessageLite.class, "fieldEnumList30_"), + 30, + FieldType.ENUM_LIST, + TestEnum.internalGetVerifier())); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed32List31_"), + 31, + FieldType.SFIXED32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed64List32_"), + 32, + FieldType.SFIXED64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint32List33_"), + 33, + FieldType.SINT32_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint64List34_"), + 34, + FieldType.SINT64_LIST, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldDoubleListPacked35_"), + 35, + FieldType.DOUBLE_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFloatListPacked36_"), + 36, + FieldType.FLOAT_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt64ListPacked37_"), + 37, + FieldType.INT64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint64ListPacked38_"), + 38, + FieldType.UINT64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldInt32ListPacked39_"), + 39, + FieldType.INT32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed64ListPacked40_"), + 40, + FieldType.FIXED64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldFixed32ListPacked41_"), + 41, + FieldType.FIXED32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldBoolListPacked42_"), + 42, + FieldType.BOOL_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldUint32ListPacked43_"), + 43, + FieldType.UINT32_LIST_PACKED, + false)); + builder.withField( + forFieldWithEnumVerifier( + field(Proto2MessageLite.class, "fieldEnumListPacked44_"), + 44, + FieldType.ENUM_LIST_PACKED, + TestEnum.internalGetVerifier())); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed32ListPacked45_"), + 45, + FieldType.SFIXED32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSfixed64ListPacked46_"), + 46, + FieldType.SFIXED64_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint32ListPacked47_"), + 47, + FieldType.SINT32_LIST_PACKED, + false)); + builder.withField( + forField( + field(Proto2MessageLite.class, "fieldSint64ListPacked48_"), + 48, + FieldType.SINT64_LIST_PACKED, + false)); + + builder.withField( + forProto2OptionalField( + field(Proto2MessageLite.class, "fieldGroup49_"), + 49, + FieldType.GROUP, + bitField0, + 0x00020000, + false, + null)); + builder.withField( + forRepeatedMessageField( + field(Proto2MessageLite.class, "fieldGroupList51_"), + 51, + FieldType.GROUP_LIST, + Proto2MessageLite.FieldGroupList51.class)); + + OneofInfo oneof = + new OneofInfo( + 0, + field(Proto2MessageLite.class, "testOneofCase_"), + field(Proto2MessageLite.class, "testOneof_")); + builder.withField(forOneofMemberField(53, FieldType.DOUBLE, oneof, Double.class, false, null)); + builder.withField(forOneofMemberField(54, FieldType.FLOAT, oneof, Float.class, false, null)); + builder.withField(forOneofMemberField(55, FieldType.INT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(56, FieldType.UINT64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(57, FieldType.INT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(58, FieldType.FIXED64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField(59, FieldType.FIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(60, FieldType.BOOL, oneof, Boolean.class, false, null)); + builder.withField(forOneofMemberField(61, FieldType.STRING, oneof, String.class, false, null)); + builder.withField( + forOneofMemberField(62, FieldType.MESSAGE, oneof, Proto2MessageLite.class, false, null)); + builder.withField( + forOneofMemberField(63, FieldType.BYTES, oneof, ByteString.class, false, null)); + builder.withField(forOneofMemberField(64, FieldType.UINT32, oneof, Integer.class, false, null)); + builder.withField( + forOneofMemberField(65, FieldType.SFIXED32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(66, FieldType.SFIXED64, oneof, Long.class, false, null)); + builder.withField(forOneofMemberField(67, FieldType.SINT32, oneof, Integer.class, false, null)); + builder.withField(forOneofMemberField(68, FieldType.SINT64, oneof, Long.class, false, null)); + builder.withField( + forOneofMemberField( + 69, FieldType.GROUP, oneof, Proto2MessageLite.FieldGroup69.class, false, null)); + + Field bitField1 = field(Proto2MessageLite.class, "bitField1_"); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredDouble71_"), + 71, + FieldType.DOUBLE, + bitField1, + 0x00000008, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFloat72_"), + 72, + FieldType.FLOAT, + bitField1, + 0x00000010, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredInt6473_"), + 73, + FieldType.INT64, + bitField1, + 0x00000020, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredUint6474_"), + 74, + FieldType.UINT64, + bitField1, + 0x00000040, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredInt3275_"), + 75, + FieldType.INT32, + bitField1, + 0x00000080, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFixed6476_"), + 76, + FieldType.FIXED64, + bitField1, + 0x00000100, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredFixed3277_"), + 77, + FieldType.FIXED32, + bitField1, + 0x00000200, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredBool78_"), + 78, + FieldType.BOOL, + bitField1, + 0x00000400, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredString79_"), + 79, + FieldType.STRING, + bitField1, + 0x00000800, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredMessage80_"), + 80, + FieldType.MESSAGE, + bitField1, + 0x00001000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredBytes81_"), + 81, + FieldType.BYTES, + bitField1, + 0x00002000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredUint3282_"), + 82, + FieldType.UINT32, + bitField1, + 0x00004000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredEnum83_"), + 83, + FieldType.ENUM, + bitField1, + 0x00008000, + false, + TestEnum.internalGetVerifier())); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSfixed3284_"), + 84, + FieldType.SFIXED32, + bitField1, + 0x00010000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSfixed6485_"), + 85, + FieldType.SFIXED64, + bitField1, + 0x00020000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSint3286_"), + 86, + FieldType.SINT32, + bitField1, + 0x00040000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredSint6487_"), + 87, + FieldType.SINT64, + bitField1, + 0x00080000, + false, + null)); + builder.withField( + forProto2RequiredField( + field(Proto2MessageLite.class, "fieldRequiredGroup88_"), + 88, + FieldType.GROUP, + bitField1, + 0x00100000, + false, + null)); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldGroup69() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldGroup69.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldGroup69.class, "fieldInt3270_"), + 70, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForRequiredNestedMessage() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(RequiredNestedMessage.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(RequiredNestedMessage.class, "value_"), + 1, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForFieldRequiredGroup88() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(FieldRequiredGroup88.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(FieldRequiredGroup88.class, "fieldInt3289_"), + 89, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2EmptyLite() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForProto2MessageLiteWithExtensions() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(0); + builder.withSyntax(ProtoSyntax.PROTO2); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroup49() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2TestingLite.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2TestingLite.FieldGroup49.class, "fieldInt3250_"), + 50, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + private static StructuralMessageInfo newMessageInfoForExtensionFieldGroupList51() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(1); + builder.withSyntax(ProtoSyntax.PROTO2); + Field bitField0 = field(Proto2TestingLite.FieldGroup49.class, "bitField0_"); + builder.withField( + forProto2OptionalField( + field(Proto2TestingLite.FieldGroupList51.class, "fieldInt3252_"), + 52, + FieldType.INT32, + bitField0, + 0x00000001, + false, + null)); + return builder.build(); + } + + + private static StructuralMessageInfo newMessageInfoForProto2MessageLiteWithMaps() { + StructuralMessageInfo.Builder builder = StructuralMessageInfo.newBuilder(); + builder.withCheckInitialized( + new int[] { + 10, 27, 44, 61, 78, 95, 112, 129, 146, 163, 180, 197, + }); + builder.withSyntax(ProtoSyntax.PROTO2); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_bool_1", 1)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_bytes_2", 2)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_double_3", 3)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_enum_4", 4)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_fixed32_5", 5)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_fixed64_6", 6)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_float_7", 7)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_int32_8", 8)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_int64_9", 9)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_message_10", 10)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sfixed32_11", 11)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sfixed64_12", 12)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sint32_13", 13)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_sint64_14", 14)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_string_15", 15)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_uint32_16", 16)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_bool_uint64_17", 17)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_bool_18", 18)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_bytes_19", 19)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_double_20", 20)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_enum_21", 21)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_fixed32_22", 22)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_fixed64_23", 23)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_float_24", 24)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_int32_25", 25)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_int64_26", 26)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_message_27", 27)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sfixed32_28", 28)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sfixed64_29", 29)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sint32_30", 30)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_sint64_31", 31)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_string_32", 32)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_uint32_33", 33)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed32_uint64_34", 34)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_bool_35", 35)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_bytes_36", 36)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_double_37", 37)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_enum_38", 38)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_fixed32_39", 39)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_fixed64_40", 40)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_float_41", 41)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_int32_42", 42)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_int64_43", 43)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_message_44", 44)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sfixed32_45", 45)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sfixed64_46", 46)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sint32_47", 47)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_sint64_48", 48)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_string_49", 49)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_uint32_50", 50)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_fixed64_uint64_51", 51)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_bool_52", 52)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_bytes_53", 53)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_double_54", 54)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_enum_55", 55)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_fixed32_56", 56)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_fixed64_57", 57)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_float_58", 58)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_int32_59", 59)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_int64_60", 60)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_message_61", 61)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sfixed32_62", 62)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sfixed64_63", 63)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sint32_64", 64)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_sint64_65", 65)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_string_66", 66)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_uint32_67", 67)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int32_uint64_68", 68)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_bool_69", 69)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_bytes_70", 70)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_double_71", 71)); + builder.withField(mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_enum_72", 72)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_fixed32_73", 73)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_fixed64_74", 74)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_float_75", 75)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_int32_76", 76)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_int64_77", 77)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_message_78", 78)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sfixed32_79", 79)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sfixed64_80", 80)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sint32_81", 81)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_sint64_82", 82)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_string_83", 83)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_uint32_84", 84)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_int64_uint64_85", 85)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_bool_86", 86)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_bytes_87", 87)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_double_88", 88)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_enum_89", 89)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_fixed32_90", 90)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_fixed64_91", 91)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_float_92", 92)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_int32_93", 93)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_int64_94", 94)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_message_95", 95)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sfixed32_96", 96)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sfixed64_97", 97)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sint32_98", 98)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_sint64_99", 99)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_string_100", 100)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_uint32_101", 101)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed32_uint64_102", 102)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_bool_103", 103)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_bytes_104", 104)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_double_105", 105)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_enum_106", 106)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_fixed32_107", 107)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_fixed64_108", 108)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_float_109", 109)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_int32_110", 110)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_int64_111", 111)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_message_112", 112)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sfixed32_113", 113)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sfixed64_114", 114)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sint32_115", 115)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_sint64_116", 116)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_string_117", 117)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_uint32_118", 118)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sfixed64_uint64_119", 119)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_bool_120", 120)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_bytes_121", 121)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_double_122", 122)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_enum_123", 123)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_fixed32_124", 124)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_fixed64_125", 125)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_float_126", 126)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_int32_127", 127)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_int64_128", 128)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_message_129", 129)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sfixed32_130", 130)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sfixed64_131", 131)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sint32_132", 132)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_sint64_133", 133)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_string_134", 134)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_uint32_135", 135)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint32_uint64_136", 136)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_bool_137", 137)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_bytes_138", 138)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_double_139", 139)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_enum_140", 140)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_fixed32_141", 141)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_fixed64_142", 142)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_float_143", 143)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_int32_144", 144)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_int64_145", 145)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_message_146", 146)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sfixed32_147", 147)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sfixed64_148", 148)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sint32_149", 149)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_sint64_150", 150)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_string_151", 151)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_uint32_152", 152)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_sint64_uint64_153", 153)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_bool_154", 154)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_bytes_155", 155)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_double_156", 156)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_enum_157", 157)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_fixed32_158", 158)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_fixed64_159", 159)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_float_160", 160)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_int32_161", 161)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_int64_162", 162)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_message_163", 163)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sfixed32_164", 164)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sfixed64_165", 165)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sint32_166", 166)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_sint64_167", 167)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_string_168", 168)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_uint32_169", 169)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_string_uint64_170", 170)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_bool_171", 171)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_bytes_172", 172)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_double_173", 173)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_enum_174", 174)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_fixed32_175", 175)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_fixed64_176", 176)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_float_177", 177)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_int32_178", 178)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_int64_179", 179)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_message_180", 180)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sfixed32_181", 181)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sfixed64_182", 182)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sint32_183", 183)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_sint64_184", 184)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_string_185", 185)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_uint32_186", 186)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint32_uint64_187", 187)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_bool_188", 188)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_bytes_189", 189)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_double_190", 190)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_enum_191", 191)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_fixed32_192", 192)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_fixed64_193", 193)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_float_194", 194)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_int32_195", 195)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_int64_196", 196)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_message_197", 197)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sfixed32_198", 198)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sfixed64_199", 199)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sint32_200", 200)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_sint64_201", 201)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_string_202", 202)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_uint32_203", 203)); + builder.withField( + mapFieldInfo(Proto2MessageLiteWithMaps.class, "field_map_uint64_uint64_204", 204)); + + return builder.build(); + } + + private static Field field(Class<?> clazz, String name) { + try { + return clazz.getDeclaredField(name); + } catch (NoSuchFieldException | SecurityException e) { + throw new RuntimeException(e); + } + } + + private static FieldInfo mapFieldInfo(Class<?> clazz, String fieldName, int fieldNumber) { + try { + return forMapField( + field(clazz, SchemaUtil.toCamelCase(fieldName, false) + "_"), + fieldNumber, + SchemaUtil.getMapDefaultEntry(clazz, fieldName), + fieldName.contains("_enum_") ? TestEnum.internalGetVerifier() : null); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } +} diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index b7b437ceac..75e2ae8a8e 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -464,6 +464,20 @@ public class JsonFormatTest extends TestCase { assertEquals(NullValue.NULL_VALUE, message.getOneofNullValue()); } + public void testNullMessageInDuplicateOneof() throws Exception { + // Succeeds if null is first. + TestOneof.Builder successBuilder = TestOneof.newBuilder(); + mergeFromJson("{\"oneofNestedMessage\": null, \"oneofInt32\": 1}", successBuilder); + + // Fails if null is last. + try { + TestOneof.Builder builder = TestOneof.newBuilder(); + mergeFromJson("{\"oneofInt32\": 1, \"oneofNestedMessage\": null}", builder); + fail(); + } catch (InvalidProtocolBufferException expected) { + } + } + public void testParserRejectDuplicatedFields() throws Exception { // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last // one if multiple entries have the same name. This is not the desired behavior but it can diff --git a/js/message.js b/js/message.js index 5ca0a73311..aa6e157798 100644 --- a/js/message.js +++ b/js/message.js @@ -172,7 +172,8 @@ jspb.Message = function() { * dead code eliminate fields used in protocol buffers that are never used * in an application. */ -goog.define('jspb.Message.GENERATE_TO_OBJECT', true); +jspb.Message.GENERATE_TO_OBJECT = + goog.define('jspb.Message.GENERATE_TO_OBJECT', true); /** @@ -184,7 +185,8 @@ goog.define('jspb.Message.GENERATE_TO_OBJECT', true); * used in an application. * By default this is enabled for test code only. */ -goog.define('jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); +jspb.Message.GENERATE_FROM_OBJECT = goog.define( + 'jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); /** @@ -192,7 +194,8 @@ goog.define('jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); * this off if you do not use toString in your project and want to trim it * from the compiled JS. */ -goog.define('jspb.Message.GENERATE_TO_STRING', true); +jspb.Message.GENERATE_TO_STRING = + goog.define('jspb.Message.GENERATE_TO_STRING', true); /** @@ -200,7 +203,8 @@ goog.define('jspb.Message.GENERATE_TO_STRING', true); * local (e.g. not from another iframe) and thus safely classified with * instanceof Array. */ -goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); +jspb.Message.ASSUME_LOCAL_ARRAYS = + goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); // TODO(jakubvrana): Turn this off by default. @@ -210,7 +214,8 @@ goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); * the proto before serialization. This is enabled by default to be * backwards compatible. Projects are advised to turn this flag always off. */ -goog.define('jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS', true); +jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS = + goog.define('jspb.Message.SERIALIZE_EMPTY_TRAILING_FIELDS', true); /** @@ -326,6 +331,10 @@ jspb.Message.getIndex_ = function(msg, fieldNumber) { return fieldNumber + msg.arrayIndexOffset_; }; +// This is only here to ensure we are not back sliding on ES6 requiements for +// protos in g3. +jspb.Message.hiddenES6Property_ = class {}; + /** * Returns the tag number based on the index in msg.array. diff --git a/python/google/protobuf/internal/api_implementation.cc b/python/google/protobuf/internal/api_implementation.cc index 6db12e8dc6..2a7f41d2d6 100644 --- a/python/google/protobuf/internal/api_implementation.cc +++ b/python/google/protobuf/internal/api_implementation.cc @@ -69,25 +69,24 @@ static const char* kImplVersionName = "api_version"; static const char* kModuleName = "_api_implementation"; static const char kModuleDocstring[] = -"_api_implementation is a module that exposes compile-time constants that\n" -"determine the default API implementation to use for Python proto2.\n" -"\n" -"It complements api_implementation.py by setting defaults using compile-time\n" -"constants defined in C, such that one can set defaults at compilation\n" -"(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; + "_api_implementation is a module that exposes compile-time constants that\n" + "determine the default API implementation to use for Python proto2.\n" + "\n" + "It complements api_implementation.py by setting defaults using " + "compile-time\n" + "constants defined in C, such that one can set defaults at compilation\n" + "(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; #if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef _module = { - PyModuleDef_HEAD_INIT, - kModuleName, - kModuleDocstring, - -1, - NULL, - NULL, - NULL, - NULL, - NULL -}; +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + kModuleName, + kModuleDocstring, + -1, + NULL, + NULL, + NULL, + NULL, + NULL}; #define INITFUNC PyInit__api_implementation #define INITFUNC_ERRORVAL NULL #else @@ -96,32 +95,31 @@ static struct PyModuleDef _module = { #endif extern "C" { - PyMODINIT_FUNC INITFUNC() { +PyMODINIT_FUNC INITFUNC() { #if PY_MAJOR_VERSION >= 3 - PyObject *module = PyModule_Create(&_module); + PyObject* module = PyModule_Create(&_module); #else - PyObject *module = Py_InitModule3( - const_cast<char*>(kModuleName), - NULL, - const_cast<char*>(kModuleDocstring)); + PyObject* module = Py_InitModule3(const_cast<char*>(kModuleName), NULL, + const_cast<char*>(kModuleDocstring)); #endif - if (module == NULL) { - return INITFUNC_ERRORVAL; - } + if (module == NULL) { + return INITFUNC_ERRORVAL; + } - // Adds the module variable "api_version". - if (PyModule_AddIntConstant( - module, - const_cast<char*>(kImplVersionName), - kImplVersion)) + // Adds the module variable "api_version". + if (PyModule_AddIntConstant(module, const_cast<char*>(kImplVersionName), + kImplVersion)) #if PY_MAJOR_VERSION < 3 - return; + return; #else - { Py_DECREF(module); return NULL; } + { + Py_DECREF(module); + return NULL; + } - return module; + return module; #endif - } +} } } // namespace python diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py index 800d54f324..a185612937 100644 --- a/python/google/protobuf/internal/descriptor_database_test.py +++ b/python/google/protobuf/internal/descriptor_database_test.py @@ -48,7 +48,8 @@ from google.protobuf.internal import testing_refleaks from google.protobuf import descriptor_database -class DescriptorDatabaseTest(testing_refleaks.BaseTestCase): +@testing_refleaks.TestCase +class DescriptorDatabaseTest(unittest.TestCase): def testAdd(self): db = descriptor_database.DescriptorDatabase() diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py index 4dc209475d..bf7e346a90 100644 --- a/python/google/protobuf/internal/descriptor_pool_test.py +++ b/python/google/protobuf/internal/descriptor_pool_test.py @@ -561,8 +561,8 @@ class DescriptorPoolTestBase(object): str(w[0].message)) -class DefaultDescriptorPoolTest(DescriptorPoolTestBase, - testing_refleaks.BaseTestCase): +@testing_refleaks.TestCase +class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): def setUp(self): self.pool = descriptor_pool.Default() @@ -597,8 +597,8 @@ class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest_pb2.DESCRIPTOR.services_by_name['TestService']) -class CreateDescriptorPoolTest(DescriptorPoolTestBase, - testing_refleaks.BaseTestCase): +@testing_refleaks.TestCase +class CreateDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): def setUp(self): self.pool = descriptor_pool.DescriptorPool() @@ -619,8 +619,9 @@ class CreateDescriptorPoolTest(DescriptorPoolTestBase, no_package_pb2.DESCRIPTOR.serialized_pb)) +@testing_refleaks.TestCase class SecondaryDescriptorFromDescriptorDB(DescriptorPoolTestBase, - testing_refleaks.BaseTestCase): + unittest.TestCase): def setUp(self): self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( @@ -812,7 +813,8 @@ class ExtensionField(object): test.assertEqual(file_desc, field_desc.file) -class AddDescriptorTest(testing_refleaks.BaseTestCase): +@testing_refleaks.TestCase +class AddDescriptorTest(unittest.TestCase): def _TestMessage(self, prefix): pool = descriptor_pool.DescriptorPool() diff --git a/python/google/protobuf/internal/extension_dict.py b/python/google/protobuf/internal/extension_dict.py index 4a1399d968..a8ce013d0a 100644 --- a/python/google/protobuf/internal/extension_dict.py +++ b/python/google/protobuf/internal/extension_dict.py @@ -183,3 +183,24 @@ class _ExtensionDict(object): Extension field descriptor. """ return self._extended_message._extensions_by_number.get(number, None) + + def __iter__(self): + # Return a generator over the populated extension fields + return (f[0] for f in self._extended_message.ListFields() + if f[0].is_extension) + + def __contains__(self, extension_handle): + _VerifyExtensionHandle(self._extended_message, extension_handle) + + if extension_handle not in self._extended_message._fields: + return False + + if extension_handle.label == FieldDescriptor.LABEL_REPEATED: + return bool(self._extended_message._fields.get(extension_handle)) + + if extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE: + value = self._extended_message._fields.get(extension_handle) + # pylint: disable=protected-access + return value is not None and value._is_present_in_parent + + return True diff --git a/python/google/protobuf/internal/keywords_test.py b/python/google/protobuf/internal/keywords_test.py new file mode 100755 index 0000000000..87449280a7 --- /dev/null +++ b/python/google/protobuf/internal/keywords_test.py @@ -0,0 +1,105 @@ +#! /usr/bin/env python +# +# Protocol Buffers - Google's data interchange format +# Copyright 2008 Google Inc. All rights reserved. +# https://developers.google.com/protocol-buffers/ +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +"""Tests for google.protobuf.internal.keywords.""" + +import unittest + + +from google.protobuf.internal import more_messages_pb2 +from google.protobuf import descriptor_pool + + +class KeywordsConflictTest(unittest.TestCase): + + def setUp(self): + super(KeywordsConflictTest, self).setUp() + self.pool = descriptor_pool.Default() + + def testMessage(self): + message = getattr(more_messages_pb2, 'class')() + message.int_field = 123 + self.assertEqual(message.int_field, 123) + des = self.pool.FindMessageTypeByName('google.protobuf.internal.class') + self.assertEqual(des.name, 'class') + + def testNestedMessage(self): + message = getattr(more_messages_pb2, 'class')() + message.nested_message.field = 234 + self.assertEqual(message.nested_message.field, 234) + des = self.pool.FindMessageTypeByName('google.protobuf.internal.class.try') + self.assertEqual(des.name, 'try') + + def testField(self): + message = getattr(more_messages_pb2, 'class')() + setattr(message, 'if', 123) + setattr(message, 'as', 1) + self.assertEqual(getattr(message, 'if'), 123) + self.assertEqual(getattr(message, 'as'), 1) + + def testEnum(self): + class_ = getattr(more_messages_pb2, 'class') + message = class_() + # Normal enum value. + message.enum_field = more_messages_pb2.default + self.assertEqual(message.enum_field, more_messages_pb2.default) + # Top level enum value. + message.enum_field = getattr(more_messages_pb2, 'else') + self.assertEqual(message.enum_field, 1) + # Nested enum value + message.nested_enum_field = getattr(class_, 'True') + self.assertEqual(message.nested_enum_field, 1) + + def testExtension(self): + message = getattr(more_messages_pb2, 'class')() + # Top level extension + extension1 = getattr(more_messages_pb2, 'continue') + message.Extensions[extension1] = 456 + self.assertEqual(message.Extensions[extension1], 456) + # None top level extension + extension2 = getattr(more_messages_pb2.ExtendClass, 'return') + message.Extensions[extension2] = 789 + self.assertEqual(message.Extensions[extension2], 789) + + def testExtensionForNestedMessage(self): + message = getattr(more_messages_pb2, 'class')() + extension = getattr(more_messages_pb2, 'with') + message.nested_message.Extensions[extension] = 999 + self.assertEqual(message.nested_message.Extensions[extension], 999) + + def TestFullKeywordUsed(self): + message = more_messages_pb2.TestFullKeyword() + message.field2.int_field = 123 + + +if __name__ == '__main__': + unittest.main() diff --git a/python/google/protobuf/internal/message_factory_test.py b/python/google/protobuf/internal/message_factory_test.py index 7a5a090134..24b79ecc9e 100644 --- a/python/google/protobuf/internal/message_factory_test.py +++ b/python/google/protobuf/internal/message_factory_test.py @@ -49,7 +49,8 @@ from google.protobuf import descriptor_pool from google.protobuf import message_factory -class MessageFactoryTest(testing_refleaks.BaseTestCase): +@testing_refleaks.TestCase +class MessageFactoryTest(unittest.TestCase): def setUp(self): self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index b66c1e0edb..3927ce1cdf 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -106,13 +106,11 @@ def IsNegInf(val): return isinf(val) and (val < 0) -BaseTestCase = testing_refleaks.BaseTestCase - - @_parameterized.named_parameters( ('_proto2', unittest_pb2), ('_proto3', unittest_proto3_arena_pb2)) -class MessageTest(BaseTestCase): +@testing_refleaks.TestCase +class MessageTest(unittest.TestCase): def testBadUtf8String(self, message_module): if api_implementation.Type() != 'python': @@ -779,28 +777,31 @@ class MessageTest(BaseTestCase): m1 = message_module.TestAllTypes() m2 = message_module.TestAllTypes() # Cpp extension will lazily create a sub message which is immutable. - self.assertEqual(0, m1.optional_nested_message.bb) + nested = m1.optional_nested_message + self.assertEqual(0, nested.bb) m2.optional_nested_message.bb = 1 # Make sure cmessage pointing to a mutable message after merge instead of # the lazily created message. m1.MergeFrom(m2) - self.assertEqual(1, m1.optional_nested_message.bb) + self.assertEqual(1, nested.bb) # Test more nested sub message. msg1 = message_module.NestedTestAllTypes() msg2 = message_module.NestedTestAllTypes() - self.assertEqual(0, msg1.child.payload.optional_nested_message.bb) + nested = msg1.child.payload.optional_nested_message + self.assertEqual(0, nested.bb) msg2.child.payload.optional_nested_message.bb = 1 msg1.MergeFrom(msg2) - self.assertEqual(1, msg1.child.payload.optional_nested_message.bb) + self.assertEqual(1, nested.bb) # Test repeated field. self.assertEqual(msg1.payload.repeated_nested_message, msg1.payload.repeated_nested_message) - msg2.payload.repeated_nested_message.add().bb = 1 + nested = msg2.payload.repeated_nested_message.add() + nested.bb = 1 msg1.MergeFrom(msg2) self.assertEqual(1, len(msg1.payload.repeated_nested_message)) - self.assertEqual(1, msg1.payload.repeated_nested_message[0].bb) + self.assertEqual(1, nested.bb) def testMergeFromString(self, message_module): m1 = message_module.TestAllTypes() @@ -1299,7 +1300,8 @@ class MessageTest(BaseTestCase): # Class to test proto2-only features (required, extensions, etc.) -class Proto2Test(BaseTestCase): +@testing_refleaks.TestCase +class Proto2Test(unittest.TestCase): def testFieldPresence(self): message = unittest_pb2.TestAllTypes() @@ -1573,7 +1575,8 @@ class Proto2Test(BaseTestCase): # Class to test proto3-only features/behavior (updated field presence & enums) -class Proto3Test(BaseTestCase): +@testing_refleaks.TestCase +class Proto3Test(unittest.TestCase): # Utility method for comparing equality with a map. def assertMapIterEquals(self, map_iter, dict_value): @@ -2444,7 +2447,8 @@ class Proto3Test(BaseTestCase): optional_string=u'\ud801\ud801') -class ValidTypeNamesTest(BaseTestCase): +@testing_refleaks.TestCase +class ValidTypeNamesTest(unittest.TestCase): def assertImportFromName(self, msg, base_name): # Parse <type 'module.class_name'> to extra 'some.name' as a string. @@ -2465,7 +2469,8 @@ class ValidTypeNamesTest(BaseTestCase): self.assertImportFromName(pb.repeated_int32, 'Scalar') self.assertImportFromName(pb.repeated_nested_message, 'Composite') -class PackedFieldTest(BaseTestCase): +@testing_refleaks.TestCase +class PackedFieldTest(unittest.TestCase): def setMessage(self, message): message.repeated_int32.append(1) @@ -2525,7 +2530,8 @@ class PackedFieldTest(BaseTestCase): @unittest.skipIf(api_implementation.Type() != 'cpp' or sys.version_info < (2, 7), 'explicit tests of the C++ implementation for PY27 and above') -class OversizeProtosTest(BaseTestCase): +@testing_refleaks.TestCase +class OversizeProtosTest(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/python/google/protobuf/internal/more_extensions.proto b/python/google/protobuf/internal/more_extensions.proto index 78f1467361..5038fd2447 100644 --- a/python/google/protobuf/internal/more_extensions.proto +++ b/python/google/protobuf/internal/more_extensions.proto @@ -41,7 +41,9 @@ message TopLevelMessage { message ExtendedMessage { - extensions 1 to max; + optional int32 optional_int32 = 1001; + repeated string repeated_string = 1002; + extensions 1 to 999; } diff --git a/python/google/protobuf/internal/more_messages.proto b/python/google/protobuf/internal/more_messages.proto index 612f0291c8..c5889c481f 100644 --- a/python/google/protobuf/internal/more_messages.proto +++ b/python/google/protobuf/internal/more_messages.proto @@ -30,6 +30,8 @@ // Author: robinson@google.com (Will Robinson) +// LINT: LEGACY_NAMES + syntax = "proto2"; package google.protobuf.internal; @@ -51,6 +53,53 @@ extend OutOfOrderFields { optional int64 optional_int64 = 2; } +enum is { // top level enum keyword + default = 0; + else = 1; // top level enum value keyword +} + +message class { // message keyword + optional int32 int_field = 1; + optional int32 if = 2; // field keyword + optional is as = 3; // enum field keyword + optional is enum_field = 4; + enum for { // nested enum keyword + default = 0; + True = 1; // nested enum value keyword + } + optional for nested_enum_field = 5; + message try { + optional int32 field = 1; + extensions 999 to 9999; + } + optional try nested_message = 6; + extensions 999 to 9999; +} + +extend class { + optional int32 continue = 1001; // top level extension keyword +} + +extend class.try { + optional int32 with = 1001; +} + +message ExtendClass { + extend class { + optional int32 return = 1002; // nested extension keyword + } +} + +message TestFullKeyword { + optional google.protobuf.internal.OutOfOrderFields field1 = 1; + optional google.protobuf.internal.class field2 = 2; +} + +// TODO(jieluo): Add keyword support for service. +// service False { +// rpc Bar(class) returns (class); +// } + message LotsNestedMessage { message B0 {} message B1 {} diff --git a/python/google/protobuf/internal/python_protobuf.cc b/python/google/protobuf/internal/python_protobuf.cc index f90cc4387e..e823bf228c 100644 --- a/python/google/protobuf/internal/python_protobuf.cc +++ b/python/google/protobuf/internal/python_protobuf.cc @@ -36,12 +36,8 @@ namespace google { namespace protobuf { namespace python { -static const Message* GetCProtoInsidePyProtoStub(PyObject* msg) { - return NULL; -} -static Message* MutableCProtoInsidePyProtoStub(PyObject* msg) { - return NULL; -} +static const Message* GetCProtoInsidePyProtoStub(PyObject* msg) { return NULL; } +static Message* MutableCProtoInsidePyProtoStub(PyObject* msg) { return NULL; } // This is initialized with a default, stub implementation. // If python-google.protobuf.cc is loaded, the function pointer is overridden diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py index b87eabaa9b..d486bb1ea9 100755 --- a/python/google/protobuf/internal/reflection_test.py +++ b/python/google/protobuf/internal/reflection_test.py @@ -68,9 +68,6 @@ if six.PY3: long = int # pylint: disable=redefined-builtin,invalid-name -BaseTestCase = testing_refleaks.BaseTestCase - - class _MiniDecoder(object): """Decodes a stream of values from a string. @@ -116,7 +113,8 @@ class _MiniDecoder(object): return self._pos == len(self._bytes) -class ReflectionTest(BaseTestCase): +@testing_refleaks.TestCase +class ReflectionTest(unittest.TestCase): def assertListsEqual(self, values, others): self.assertEqual(len(values), len(others)) @@ -1201,32 +1199,71 @@ class ReflectionTest(BaseTestCase): self.assertEqual(prius.price, new_prius.price) self.assertEqual(prius.owners, new_prius.owners) + def testExtensionIter(self): + extendee_proto = more_extensions_pb2.ExtendedMessage() + + extension_int32 = more_extensions_pb2.optional_int_extension + extendee_proto.Extensions[extension_int32] = 23 + + extension_repeated = more_extensions_pb2.repeated_int_extension + extendee_proto.Extensions[extension_repeated].append(11) + + extension_msg = more_extensions_pb2.optional_message_extension + extendee_proto.Extensions[extension_msg].foreign_message_int = 56 + + # Set some normal fields. + extendee_proto.optional_int32 = 1 + extendee_proto.repeated_string.append('hi') + + expected = (extension_int32, extension_msg, extension_repeated) + count = 0 + for item in extendee_proto.Extensions: + self.assertEqual(item.name, expected[count].name) + self.assertIn(item, extendee_proto.Extensions) + count += 1 + self.assertEqual(count, 3) + + def testExtensionContainsError(self): + extendee_proto = more_extensions_pb2.ExtendedMessage() + self.assertRaises(KeyError, extendee_proto.Extensions.__contains__, 0) + + field = more_extensions_pb2.ExtendedMessage.DESCRIPTOR.fields_by_name[ + 'optional_int32'] + self.assertRaises(KeyError, extendee_proto.Extensions.__contains__, field) + def testTopLevelExtensionsForOptionalScalar(self): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.optional_int32_extension self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertEqual(0, extendee_proto.Extensions[extension]) # As with normal scalar fields, just doing a read doesn't actually set the # "has" bit. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) # Actually set the thing. extendee_proto.Extensions[extension] = 23 self.assertEqual(23, extendee_proto.Extensions[extension]) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) # Ensure that clearing works as well. extendee_proto.ClearExtension(extension) self.assertEqual(0, extendee_proto.Extensions[extension]) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) def testTopLevelExtensionsForRepeatedScalar(self): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.repeated_string_extension self.assertEqual(0, len(extendee_proto.Extensions[extension])) + self.assertNotIn(extension, extendee_proto.Extensions) extendee_proto.Extensions[extension].append('foo') self.assertEqual(['foo'], extendee_proto.Extensions[extension]) + self.assertIn(extension, extendee_proto.Extensions) string_list = extendee_proto.Extensions[extension] extendee_proto.ClearExtension(extension) self.assertEqual(0, len(extendee_proto.Extensions[extension])) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertTrue(string_list is not extendee_proto.Extensions[extension]) # Shouldn't be allowed to do Extensions[extension] = 'a' self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions, @@ -1236,13 +1273,16 @@ class ReflectionTest(BaseTestCase): extendee_proto = unittest_pb2.TestAllExtensions() extension = unittest_pb2.optional_foreign_message_extension self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) self.assertEqual(0, extendee_proto.Extensions[extension].c) # As with normal (non-extension) fields, merely reading from the # thing shouldn't set the "has" bit. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) extendee_proto.Extensions[extension].c = 23 self.assertEqual(23, extendee_proto.Extensions[extension].c) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) # Save a reference here. foreign_message = extendee_proto.Extensions[extension] extendee_proto.ClearExtension(extension) @@ -1253,6 +1293,7 @@ class ReflectionTest(BaseTestCase): self.assertEqual(42, foreign_message.c) self.assertTrue(foreign_message.HasField('c')) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) # Shouldn't be allowed to do Extensions[extension] = 'a' self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions, extension, 'a') @@ -1280,15 +1321,19 @@ class ReflectionTest(BaseTestCase): # We just test the non-repeated case. self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) required = extendee_proto.Extensions[extension] self.assertEqual(0, required.a) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) required.a = 23 self.assertEqual(23, extendee_proto.Extensions[extension].a) self.assertTrue(extendee_proto.HasExtension(extension)) + self.assertIn(extension, extendee_proto.Extensions) extendee_proto.ClearExtension(extension) self.assertTrue(required is not extendee_proto.Extensions[extension]) self.assertTrue(not extendee_proto.HasExtension(extension)) + self.assertNotIn(extension, extendee_proto.Extensions) def testRegisteredExtensions(self): pool = unittest_pb2.DESCRIPTOR.pool @@ -1952,7 +1997,8 @@ class ReflectionTest(BaseTestCase): # into separate TestCase classes. -class TestAllTypesEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class TestAllTypesEqualityTest(unittest.TestCase): def setUp(self): self.first_proto = unittest_pb2.TestAllTypes() @@ -1968,7 +2014,8 @@ class TestAllTypesEqualityTest(BaseTestCase): self.assertEqual(self.first_proto, self.second_proto) -class FullProtosEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class FullProtosEqualityTest(unittest.TestCase): """Equality tests using completely-full protos as a starting point.""" @@ -2054,7 +2101,8 @@ class FullProtosEqualityTest(BaseTestCase): self.assertEqual(self.first_proto, self.second_proto) -class ExtensionEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class ExtensionEqualityTest(unittest.TestCase): def testExtensionEquality(self): first_proto = unittest_pb2.TestAllExtensions() @@ -2087,7 +2135,8 @@ class ExtensionEqualityTest(BaseTestCase): self.assertEqual(first_proto, second_proto) -class MutualRecursionEqualityTest(BaseTestCase): +@testing_refleaks.TestCase +class MutualRecursionEqualityTest(unittest.TestCase): def testEqualityWithMutualRecursion(self): first_proto = unittest_pb2.TestMutualRecursionA() @@ -2099,7 +2148,8 @@ class MutualRecursionEqualityTest(BaseTestCase): self.assertEqual(first_proto, second_proto) -class ByteSizeTest(BaseTestCase): +@testing_refleaks.TestCase +class ByteSizeTest(unittest.TestCase): def setUp(self): self.proto = unittest_pb2.TestAllTypes() @@ -2411,7 +2461,8 @@ class ByteSizeTest(BaseTestCase): # * Handling of empty submessages (with and without "has" # bits set). -class SerializationTest(BaseTestCase): +@testing_refleaks.TestCase +class SerializationTest(unittest.TestCase): def testSerializeEmtpyMessage(self): first_proto = unittest_pb2.TestAllTypes() @@ -3004,7 +3055,8 @@ class SerializationTest(BaseTestCase): self.assertEqual(3, proto.repeated_int32[2]) -class OptionsTest(BaseTestCase): +@testing_refleaks.TestCase +class OptionsTest(unittest.TestCase): def testMessageOptions(self): proto = message_set_extensions_pb2.TestMessageSet() @@ -3031,7 +3083,8 @@ class OptionsTest(BaseTestCase): -class ClassAPITest(BaseTestCase): +@testing_refleaks.TestCase +class ClassAPITest(unittest.TestCase): @unittest.skipIf( api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, diff --git a/python/google/protobuf/internal/testing_refleaks.py b/python/google/protobuf/internal/testing_refleaks.py index 8ce06519bc..e448fceef4 100644 --- a/python/google/protobuf/internal/testing_refleaks.py +++ b/python/google/protobuf/internal/testing_refleaks.py @@ -69,8 +69,8 @@ class LocalTestResult(unittest.TestResult): pass -class ReferenceLeakCheckerTestCase(unittest.TestCase): - """A TestCase which runs tests multiple times, collecting reference counts.""" +class ReferenceLeakCheckerMixin(object): + """A mixin class for TestCase, which checks reference counts.""" NB_RUNS = 3 @@ -81,8 +81,8 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): self._saved_pickle_registry = copyreg.dispatch_table.copy() # Run the test twice, to warm up the instance attributes. - super(ReferenceLeakCheckerTestCase, self).run(result=result) - super(ReferenceLeakCheckerTestCase, self).run(result=result) + super(ReferenceLeakCheckerMixin, self).run(result=result) + super(ReferenceLeakCheckerMixin, self).run(result=result) oldrefcount = 0 local_result = LocalTestResult(result) @@ -90,7 +90,7 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): refcount_deltas = [] for _ in range(self.NB_RUNS): oldrefcount = self._getRefcounts() - super(ReferenceLeakCheckerTestCase, self).run(result=local_result) + super(ReferenceLeakCheckerMixin, self).run(result=local_result) newrefcount = self._getRefcounts() refcount_deltas.append(newrefcount - oldrefcount) print(refcount_deltas, self) @@ -112,12 +112,19 @@ class ReferenceLeakCheckerTestCase(unittest.TestCase): if hasattr(sys, 'gettotalrefcount'): - BaseTestCase = ReferenceLeakCheckerTestCase + + def TestCase(test_class): + new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__ + new_class = type(test_class)( + test_class.__name__, new_bases, dict(test_class.__dict__)) + return new_class SkipReferenceLeakChecker = unittest.skip else: # When PyDEBUG is not enabled, run the tests normally. - BaseTestCase = unittest.TestCase + + def TestCase(test_class): + return test_class def SkipReferenceLeakChecker(reason): del reason # Don't skip, so don't need a reason. diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py index cdab1c6277..3b0adcb712 100755 --- a/python/google/protobuf/internal/text_format_test.py +++ b/python/google/protobuf/internal/text_format_test.py @@ -598,6 +598,12 @@ class TextFormatParserTests(TextFormatBase): six.assertRaisesRegex(self, text_format.ParseError, ( r'1:1 : Message type "\w+.TestAllTypes" has no field named ' r'"unknown_field".'), text_format.Parse, text, message) + text = ('optional_int32: 123\n' + 'unknown_field: 8\n' + 'optional_nested_message { bb: 45 }') + text_format.Parse(text, message, allow_unknown_field=True) + self.assertEqual(message.optional_nested_message.bb, 45) + self.assertEqual(message.optional_int32, 123) def testParseBadEnumValue(self, message_module): message = message_module.TestAllTypes() diff --git a/python/google/protobuf/internal/unknown_fields_test.py b/python/google/protobuf/internal/unknown_fields_test.py index ab0260e010..bcc4a46d6e 100755 --- a/python/google/protobuf/internal/unknown_fields_test.py +++ b/python/google/protobuf/internal/unknown_fields_test.py @@ -53,10 +53,8 @@ from google.protobuf.internal import type_checkers from google.protobuf import descriptor -BaseTestCase = testing_refleaks.BaseTestCase - - -class UnknownFieldsTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownFieldsTest(unittest.TestCase): def setUp(self): self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR @@ -152,7 +150,8 @@ class UnknownFieldsTest(BaseTestCase): msg.map_int32_all_types[1].optional_nested_message.SerializeToString()) -class UnknownFieldsAccessorsTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownFieldsAccessorsTest(unittest.TestCase): def setUp(self): self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR @@ -348,7 +347,8 @@ class UnknownFieldsAccessorsTest(BaseTestCase): self.assertEqual(message.SerializeToString(), self.all_fields_data) -class UnknownEnumValuesTest(BaseTestCase): +@testing_refleaks.TestCase +class UnknownEnumValuesTest(unittest.TestCase): def setUp(self): self.descriptor = missing_enum_values_pb2.TestEnumValues.DESCRIPTOR diff --git a/python/google/protobuf/message.py b/python/google/protobuf/message.py index 8854c86529..35ceccb58e 100755 --- a/python/google/protobuf/message.py +++ b/python/google/protobuf/message.py @@ -311,4 +311,9 @@ class Message(object): def __setstate__(self, state): """Support the pickle protocol.""" self.__init__() - self.ParseFromString(state['serialized']) + serialized = state['serialized'] + # On Python 3, using encoding='latin1' is required for unpickling + # protos pickled by Python 2. + if not isinstance(serialized, bytes): + serialized = serialized.encode('latin1') + self.ParseFromString(serialized) diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc index fb55511ca2..7102a941eb 100644 --- a/python/google/protobuf/pyext/descriptor.cc +++ b/python/google/protobuf/pyext/descriptor.cc @@ -394,11 +394,12 @@ PyObject* NewInternedDescriptor(PyTypeObject* type, return reinterpret_cast<PyObject*>(py_descriptor); } -static void Dealloc(PyBaseDescriptor* self) { +static void Dealloc(PyObject* pself) { + PyBaseDescriptor* self = reinterpret_cast<PyBaseDescriptor*>(pself); // Remove from interned dictionary interned_descriptors->erase(self->descriptor); Py_CLEAR(self->pool); - Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); + Py_TYPE(self)->tp_free(pself); } static int GcTraverse(PyObject* pself, visitproc visit, void* arg) { @@ -1323,7 +1324,7 @@ static const FileDescriptor* _GetDescriptor(PyFileDescriptor *self) { static void Dealloc(PyFileDescriptor* self) { Py_XDECREF(self->serialized_pb); - descriptor::Dealloc(&self->base); + descriptor::Dealloc(reinterpret_cast<PyObject*>(self)); } static PyObject* GetPool(PyFileDescriptor *self, void *closure) { diff --git a/python/google/protobuf/pyext/extension_dict.cc b/python/google/protobuf/pyext/extension_dict.cc index 7debe6f0a1..35b6ec6922 100644 --- a/python/google/protobuf/pyext/extension_dict.cc +++ b/python/google/protobuf/pyext/extension_dict.cc @@ -90,6 +90,47 @@ static Py_ssize_t len(ExtensionDict* self) { return size; } +struct ExtensionIterator { + PyObject_HEAD; + Py_ssize_t index; + std::vector<const FieldDescriptor*> fields; + + // Owned reference, to keep the FieldDescriptors alive. + ExtensionDict* extension_dict; +}; + +PyObject* GetIter(PyObject* _self) { + ExtensionDict* self = reinterpret_cast<ExtensionDict*>(_self); + + ScopedPyObjectPtr obj(PyType_GenericAlloc(&ExtensionIterator_Type, 0)); + if (obj == nullptr) { + return PyErr_Format(PyExc_MemoryError, + "Could not allocate extension iterator"); + } + + ExtensionIterator* iter = reinterpret_cast<ExtensionIterator*>(obj.get()); + + // Call "placement new" to initialize. So the constructor of + // std::vector<...> fields will be called. + new (iter) ExtensionIterator; + + self->parent->message->GetReflection()->ListFields(*self->parent->message, + &iter->fields); + iter->index = 0; + Py_INCREF(self); + iter->extension_dict = self; + + return obj.release(); +} + +static void DeallocExtensionIterator(PyObject* _self) { + ExtensionIterator* self = reinterpret_cast<ExtensionIterator*>(_self); + self->fields.clear(); + Py_XDECREF(self->extension_dict); + self->~ExtensionIterator(); + Py_TYPE(_self)->tp_free(_self); +} + PyObject* subscript(ExtensionDict* self, PyObject* key) { const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key); if (descriptor == NULL) { @@ -108,20 +149,19 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) { self->parent->composite_fields->find(descriptor); if (iterator != self->parent->composite_fields->end()) { Py_INCREF(iterator->second); - return iterator->second; + return iterator->second->AsPyObject(); } if (descriptor->label() != FieldDescriptor::LABEL_REPEATED && descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // TODO(plabatut): consider building the class on the fly! - PyObject* sub_message = cmessage::InternalGetSubMessage( + ContainerBase* sub_message = cmessage::InternalGetSubMessage( self->parent, descriptor); if (sub_message == NULL) { return NULL; } - Py_INCREF(sub_message); (*self->parent->composite_fields)[descriptor] = sub_message; - return sub_message; + return sub_message->AsPyObject(); } if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) { @@ -144,23 +184,21 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) { if (message_class == NULL) { return NULL; } - PyObject* py_container = repeated_composite_container::NewContainer( + ContainerBase* py_container = repeated_composite_container::NewContainer( self->parent, descriptor, message_class); if (py_container == NULL) { return NULL; } - Py_INCREF(py_container); (*self->parent->composite_fields)[descriptor] = py_container; - return py_container; + return py_container->AsPyObject(); } else { - PyObject* py_container = repeated_scalar_container::NewContainer( + ContainerBase* py_container = repeated_scalar_container::NewContainer( self->parent, descriptor); if (py_container == NULL) { return NULL; } - Py_INCREF(py_container); (*self->parent->composite_fields)[descriptor] = py_container; - return py_container; + return py_container->AsPyObject(); } } PyErr_SetString(PyExc_ValueError, "control reached unexpected line"); @@ -236,6 +274,35 @@ PyObject* _FindExtensionByNumber(ExtensionDict* self, PyObject* arg) { return PyFieldDescriptor_FromDescriptor(message_extension); } +static int Contains(PyObject* _self, PyObject* key) { + ExtensionDict* self = reinterpret_cast<ExtensionDict*>(_self); + const FieldDescriptor* field_descriptor = + cmessage::GetExtensionDescriptor(key); + if (field_descriptor == nullptr) { + return -1; + } + + if (!field_descriptor->is_extension()) { + PyErr_Format(PyExc_KeyError, "%s is not an extension", + field_descriptor->full_name().c_str()); + return -1; + } + + const Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + if (field_descriptor->is_repeated()) { + if (reflection->FieldSize(*message, field_descriptor) > 0) { + return 1; + } + } else { + if (reflection->HasField(*message, field_descriptor)) { + return 1; + } + } + + return 0; +} + ExtensionDict* NewExtensionDict(CMessage *parent) { ExtensionDict* self = reinterpret_cast<ExtensionDict*>( PyType_GenericAlloc(&ExtensionDict_Type, 0)); @@ -248,7 +315,8 @@ ExtensionDict* NewExtensionDict(CMessage *parent) { return self; } -void dealloc(ExtensionDict* self) { +void dealloc(PyObject* pself) { + ExtensionDict* self = reinterpret_cast<ExtensionDict*>(pself); Py_CLEAR(self->parent); Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); } @@ -269,6 +337,16 @@ static PyObject* RichCompare(ExtensionDict* self, PyObject* other, int opid) { Py_RETURN_TRUE; } } +static PySequenceMethods SeqMethods = { + (lenfunc)len, // sq_length + 0, // sq_concat + 0, // sq_repeat + 0, // sq_item + 0, // sq_slice + 0, // sq_ass_item + 0, // sq_ass_slice + (objobjproc)Contains, // sq_contains +}; static PyMappingMethods MpMethods = { (lenfunc)len, /* mp_length */ @@ -278,54 +356,119 @@ static PyMappingMethods MpMethods = { #define EDMETHOD(name, args, doc) { #name, (PyCFunction)name, args, doc } static PyMethodDef Methods[] = { - EDMETHOD(_FindExtensionByName, METH_O, - "Finds an extension by name."), - EDMETHOD(_FindExtensionByNumber, METH_O, - "Finds an extension by field number."), - { NULL, NULL } + EDMETHOD(_FindExtensionByName, METH_O, "Finds an extension by name."), + EDMETHOD(_FindExtensionByNumber, METH_O, + "Finds an extension by field number."), + {NULL, NULL}, }; } // namespace extension_dict PyTypeObject ExtensionDict_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - FULL_MODULE_NAME ".ExtensionDict", // tp_name - sizeof(ExtensionDict), // tp_basicsize - 0, // tp_itemsize - (destructor)extension_dict::dealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - &extension_dict::MpMethods, // tp_as_mapping - PyObject_HashNotImplemented, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "An extension dict", // tp_doc - 0, // tp_traverse - 0, // tp_clear - (richcmpfunc)extension_dict::RichCompare, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - extension_dict::Methods, // tp_methods - 0, // tp_members - 0, // tp_getset - 0, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init + PyVarObject_HEAD_INIT(&PyType_Type, 0) // + FULL_MODULE_NAME ".ExtensionDict", // tp_name + sizeof(ExtensionDict), // tp_basicsize + 0, // tp_itemsize + (destructor)extension_dict::dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + &extension_dict::SeqMethods, // tp_as_sequence + &extension_dict::MpMethods, // tp_as_mapping + PyObject_HashNotImplemented, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "An extension dict", // tp_doc + 0, // tp_traverse + 0, // tp_clear + (richcmpfunc)extension_dict::RichCompare, // tp_richcompare + 0, // tp_weaklistoffset + extension_dict::GetIter, // tp_iter + 0, // tp_iternext + extension_dict::Methods, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init }; +PyObject* IterNext(PyObject* _self) { + extension_dict::ExtensionIterator* self = + reinterpret_cast<extension_dict::ExtensionIterator*>(_self); + Py_ssize_t total_size = self->fields.size(); + Py_ssize_t index = self->index; + while (self->index < total_size) { + index = self->index; + ++self->index; + if (self->fields[index]->is_extension()) { + // With C++ descriptors, the field can always be retrieved, but for + // unknown extensions which have not been imported in Python code, there + // is no message class and we cannot retrieve the value. + // ListFields() has the same behavior. + if (self->fields[index]->message_type() != nullptr && + message_factory::GetMessageClass( + cmessage::GetFactoryForMessage(self->extension_dict->parent), + self->fields[index]->message_type()) == nullptr) { + PyErr_Clear(); + continue; + } + + return PyFieldDescriptor_FromDescriptor(self->fields[index]); + } + } + + return nullptr; +} + +PyTypeObject ExtensionIterator_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) // + FULL_MODULE_NAME ".ExtensionIterator", // tp_name + sizeof(extension_dict::ExtensionIterator), // tp_basicsize + 0, // tp_itemsize + extension_dict::DeallocExtensionIterator, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "A scalar map iterator", // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + PyObject_SelfIter, // tp_iter + IterNext, // tp_iternext + 0, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init +}; } // namespace python } // namespace protobuf } // namespace google diff --git a/python/google/protobuf/pyext/extension_dict.h b/python/google/protobuf/pyext/extension_dict.h index a7d6bb7b3e..c9da443161 100644 --- a/python/google/protobuf/pyext/extension_dict.h +++ b/python/google/protobuf/pyext/extension_dict.h @@ -56,6 +56,7 @@ typedef struct ExtensionDict { } ExtensionDict; extern PyTypeObject ExtensionDict_Type; +extern PyTypeObject ExtensionIterator_Type; namespace extension_dict { diff --git a/python/google/protobuf/pyext/map_container.cc b/python/google/protobuf/pyext/map_container.cc index d85840219d..bc59ae8a1e 100644 --- a/python/google/protobuf/pyext/map_container.cc +++ b/python/google/protobuf/pyext/map_container.cc @@ -43,6 +43,7 @@ #include <google/protobuf/pyext/message.h> #include <google/protobuf/pyext/repeated_composite_container.h> #include <google/protobuf/pyext/scoped_pyobject_ptr.h> +#include <google/protobuf/stubs/map_util.h> #if PY_MAJOR_VERSION >= 3 #define PyInt_FromLong PyLong_FromLong @@ -82,26 +83,23 @@ struct MapIterator { // We own a ref on this. MapContainer* container; - // We need to keep a ref on the Message* too, because + // We need to keep a ref on the parent Message too, because // MapIterator::~MapIterator() accesses it. Normally this would be ok because // the ref on container (above) would guarantee outlive semantics. However in - // the case of ClearField(), InitializeAndCopyToParentContainer() resets the - // message pointer (and the owner) to a different message, a copy of the - // original. But our iterator still points to the original, which could now - // get deleted before us. + // the case of ClearField(), the MapContainer points to a different message, + // a copy of the original. But our iterator still points to the original, + // which could now get deleted before us. // // To prevent this, we ensure that the Message will always stay alive as long // as this iterator does. This is solely for the benefit of the MapIterator // destructor -- we should never actually access the iterator in this state // except to delete it. - CMessage::OwnerRef owner; - + CMessage* parent; // The version of the map when we took the iterator to it. // // We store this so that if the map is modified during iteration we can throw // an error. uint64 version; - // True if the container is empty. We signal this separately to avoid calling // any of the iteration methods, which are non-const. bool empty; @@ -109,7 +107,7 @@ struct MapIterator { Message* MapContainer::GetMutableMessage() { cmessage::AssureWritable(parent); - return const_cast<Message*>(message); + return parent->message; } // Consumes a reference on the Python string object. @@ -315,7 +313,7 @@ static MapContainer* GetMap(PyObject* obj) { Py_ssize_t MapReflectionFriend::Length(PyObject* _self) { MapContainer* self = GetMap(_self); - const google::protobuf::Message* message = self->message; + const google::protobuf::Message* message = self->parent->message; return message->GetReflection()->MapSize(*message, self->parent_field_descriptor); } @@ -343,7 +341,7 @@ PyObject* MapReflectionFriend::MergeFrom(PyObject* _self, PyObject* arg) { MapContainer* self = GetMap(_self); MapContainer* other_map = GetMap(arg); Message* message = self->GetMutableMessage(); - const Message* other_message = other_map->message; + const Message* other_message = other_map->parent->message; const Reflection* reflection = message->GetReflection(); const Reflection* other_reflection = other_message->GetReflection(); internal::MapFieldBase* field = reflection->MutableMapData( @@ -359,7 +357,7 @@ PyObject* MapReflectionFriend::MergeFrom(PyObject* _self, PyObject* arg) { PyObject* MapReflectionFriend::Contains(PyObject* _self, PyObject* key) { MapContainer* self = GetMap(_self); - const Message* message = self->message; + const Message* message = self->parent->message; const Reflection* reflection = message->GetReflection(); MapKey map_key; @@ -375,71 +373,26 @@ PyObject* MapReflectionFriend::Contains(PyObject* _self, PyObject* key) { } } -// Initializes the underlying Message object of "to" so it becomes a new parent -// map container, and copies all the values from "from" to it. A child map -// container can be released by passing it as both from and to (e.g. making it -// the recipient of the new parent message and copying the values from itself). -// In fact, this is the only supported use at the moment. -static int InitializeAndCopyToParentContainer(MapContainer* from, - MapContainer* to) { - // For now we require from == to, re-evaluate if we want to support deep copy - // as in repeated_scalar_container.cc. - GOOGLE_DCHECK(from == to); - Message* new_message = from->message->New(); - - if (MapReflectionFriend::Length(reinterpret_cast<PyObject*>(from)) > 0) { - // A somewhat roundabout way of copying just one field from old_message to - // new_message. This is the best we can do with what Reflection gives us. - Message* mutable_old = from->GetMutableMessage(); - std::vector<const FieldDescriptor*> fields; - fields.push_back(from->parent_field_descriptor); - - // Move the map field into the new message. - mutable_old->GetReflection()->SwapFields(mutable_old, new_message, fields); - - // If/when we support from != to, this will be required also to copy the - // map field back into the existing message: - // mutable_old->MergeFrom(*new_message); - } - - // If from == to this could delete old_message. - to->owner.reset(new_message); - - to->parent = NULL; - to->parent_field_descriptor = from->parent_field_descriptor; - to->message = new_message; - - // Invalidate iterators, since they point to the old copy of the field. - to->version++; - - return 0; -} - -int MapContainer::Release() { - return InitializeAndCopyToParentContainer(this, this); -} - - // ScalarMap /////////////////////////////////////////////////////////////////// -PyObject *NewScalarMapContainer( +MapContainer* NewScalarMapContainer( CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; } - ScopedPyObjectPtr obj(PyType_GenericAlloc(ScalarMapContainer_Type, 0)); - if (obj.get() == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate new container."); + PyObject* obj(PyType_GenericAlloc(ScalarMapContainer_Type, 0)); + if (obj == NULL) { + PyErr_Format(PyExc_RuntimeError, + "Could not allocate new container."); + return NULL; } - MapContainer* self = GetMap(obj.get()); + MapContainer* self = GetMap(obj); - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; self->version = 0; self->key_field_descriptor = @@ -449,11 +402,12 @@ PyObject *NewScalarMapContainer( if (self->key_field_descriptor == NULL || self->value_field_descriptor == NULL) { - return PyErr_Format(PyExc_KeyError, - "Map entry descriptor did not have key/value fields"); + PyErr_Format(PyExc_KeyError, + "Map entry descriptor did not have key/value fields"); + return NULL; } - return obj.release(); + return self; } PyObject* MapReflectionFriend::ScalarMapGetItem(PyObject* _self, @@ -571,7 +525,7 @@ PyObject* MapReflectionFriend::ScalarMapToStr(PyObject* _self) { static void ScalarMapDealloc(PyObject* _self) { MapContainer* self = GetMap(_self); - self->owner.reset(); + self->RemoveFromParentCache(); PyTypeObject *type = Py_TYPE(_self); type->tp_free(_self); if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { @@ -674,36 +628,15 @@ static MessageMapContainer* GetMessageMap(PyObject* obj) { return reinterpret_cast<MessageMapContainer*>(obj); } -static PyObject* GetCMessage(MessageMapContainer* self, Message* message, - bool insert_message_dict) { +static PyObject* GetCMessage(MessageMapContainer* self, Message* message) { // Get or create the CMessage object corresponding to this message. - ScopedPyObjectPtr key(PyLong_FromVoidPtr(message)); - PyObject* ret = PyDict_GetItem(self->message_dict, key.get()); - - if (ret == NULL) { - CMessage* cmsg = cmessage::NewEmptyMessage(self->message_class); - ret = reinterpret_cast<PyObject*>(cmsg); - - if (cmsg == NULL) { - return NULL; - } - cmsg->owner = self->owner; - cmsg->message = message; - cmsg->parent = self->parent; - if (insert_message_dict) { - if (PyDict_SetItem(self->message_dict, key.get(), ret) < 0) { - Py_DECREF(ret); - return NULL; - } - } - } else { - Py_INCREF(ret); - } - - return ret; + return self->parent + ->BuildSubMessageFromPointer(self->parent_field_descriptor, message, + self->message_class) + ->AsPyObject(); } -PyObject* NewMessageMapContainer( +MessageMapContainer* NewMessageMapContainer( CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor, CMessageClass* message_class) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { @@ -712,16 +645,15 @@ PyObject* NewMessageMapContainer( PyObject* obj = PyType_GenericAlloc(MessageMapContainer_Type, 0); if (obj == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate new container."); + PyErr_SetString(PyExc_RuntimeError, "Could not allocate new container."); + return NULL; } MessageMapContainer* self = GetMessageMap(obj); - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; self->version = 0; self->key_field_descriptor = @@ -729,23 +661,18 @@ PyObject* NewMessageMapContainer( self->value_field_descriptor = parent_field_descriptor->message_type()->FindFieldByName("value"); - self->message_dict = PyDict_New(); - if (self->message_dict == NULL) { - return PyErr_Format(PyExc_RuntimeError, - "Could not allocate message dict."); - } - Py_INCREF(message_class); self->message_class = message_class; if (self->key_field_descriptor == NULL || self->value_field_descriptor == NULL) { - Py_DECREF(obj); - return PyErr_Format(PyExc_KeyError, - "Map entry descriptor did not have key/value fields"); + Py_DECREF(self); + PyErr_SetString(PyExc_KeyError, + "Map entry descriptor did not have key/value fields"); + return NULL; } - return obj; + return self; } int MapReflectionFriend::MessageMapSetItem(PyObject* _self, PyObject* key, @@ -777,22 +704,14 @@ int MapReflectionFriend::MessageMapSetItem(PyObject* _self, PyObject* key, MapValueRef value; reflection->InsertOrLookupMapValue(message, self->parent_field_descriptor, map_key, &value); - ScopedPyObjectPtr key(PyLong_FromVoidPtr(value.MutableMessageValue())); - - PyObject* cmsg_value = PyDict_GetItem(self->message_dict, key.get()); - if (cmsg_value) { - // Need to keep CMessage stay alive if it is still referenced after - // deletion. Makes a new message and swaps values into CMessage - // instead of just removing. - CMessage* cmsg = reinterpret_cast<CMessage*>(cmsg_value); - Message* msg = cmsg->message; - cmsg->owner.reset(msg->New()); - cmsg->message = cmsg->owner.get(); - cmsg->parent = NULL; - msg->GetReflection()->Swap(msg, cmsg->message); - if (PyDict_DelItem(self->message_dict, key.get()) < 0) { - return -1; - } + Message* sub_message = value.MutableMessageValue(); + // If there is a living weak reference to an item, we "Release" it, + // otherwise we just discard the C++ value. + if (CMessage* released = + self->parent->MaybeReleaseSubMessage(sub_message)) { + Message* msg = released->message; + released->message = msg->New(); + msg->GetReflection()->Swap(msg, released->message); } // Delete key from map. @@ -823,7 +742,7 @@ PyObject* MapReflectionFriend::MessageMapGetItem(PyObject* _self, self->version++; } - return GetCMessage(self, value.MutableMessageValue(), true); + return GetCMessage(self, value.MutableMessageValue()); } PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { @@ -846,10 +765,7 @@ PyObject* MapReflectionFriend::MessageMapToStr(PyObject* _self) { if (key == NULL) { return NULL; } - // Do not insert the cmessage to self->message_dict because - // the returned CMessage will not escape this function. - value.reset(GetCMessage( - self, it.MutableValueRef()->MutableMessageValue(), false)); + value.reset(GetCMessage(self, it.MutableValueRef()->MutableMessageValue())); if (value == NULL) { return NULL; } @@ -886,8 +802,7 @@ PyObject* MessageMapGet(PyObject* self, PyObject* args) { static void MessageMapDealloc(PyObject* _self) { MessageMapContainer* self = GetMessageMap(_self); - self->owner.reset(); - Py_DECREF(self->message_dict); + self->RemoveFromParentCache(); Py_DECREF(self->message_class); PyTypeObject *type = Py_TYPE(_self); type->tp_free(_self); @@ -1005,7 +920,8 @@ PyObject* MapReflectionFriend::GetIterator(PyObject *_self) { Py_INCREF(self); iter->container = self; iter->version = self->version; - iter->owner = self->owner; + Py_INCREF(self->parent); + iter->parent = self->parent; if (MapReflectionFriend::Length(_self) > 0) { Message* message = self->GetMutableMessage(); @@ -1027,6 +943,10 @@ PyObject* MapReflectionFriend::IterNext(PyObject* _self) { return PyErr_Format(PyExc_RuntimeError, "Map modified during iteration."); } + if (self->parent != self->container->parent) { + return PyErr_Format(PyExc_RuntimeError, + "Map cleared during iteration."); + } if (self->iter.get() == NULL) { return NULL; @@ -1051,8 +971,8 @@ PyObject* MapReflectionFriend::IterNext(PyObject* _self) { static void DeallocMapIterator(PyObject* _self) { MapIterator* self = GetIter(_self); self->iter.reset(); - self->owner.reset(); - Py_XDECREF(self->container); + Py_CLEAR(self->container); + Py_CLEAR(self->parent); Py_TYPE(_self)->tp_free(_self); } diff --git a/python/google/protobuf/pyext/map_container.h b/python/google/protobuf/pyext/map_container.h index 7e77b027c3..2c9b323efa 100644 --- a/python/google/protobuf/pyext/map_container.h +++ b/python/google/protobuf/pyext/map_container.h @@ -50,55 +50,21 @@ struct CMessageClass; // This struct is used directly for ScalarMap, and is the base class of // MessageMapContainer, which is used for MessageMap. -struct MapContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python MapContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Pointer to the C++ Message that contains this container. The - // MapContainer does not own this pointer. - const Message* message; - +struct MapContainer : public ContainerBase { // Use to get a mutable message when necessary. Message* GetMutableMessage(); - // Weak reference to a parent CMessage object (i.e. may be NULL.) - // - // Used to make sure all ancestors are also mutable when first - // modifying the container. - CMessage* parent; - - // Pointer to the parent's descriptor that describes this - // field. Used together with the parent's message when making a - // default message instance mutable. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; + // Cache some descriptors, used to convert keys and values. const FieldDescriptor* key_field_descriptor; const FieldDescriptor* value_field_descriptor; - // We bump this whenever we perform a mutation, to invalidate existing // iterators. uint64 version; - - // Releases the messages in the container to a new message. - // - // Returns 0 on success, -1 on failure. - int Release(); - - // Set the owner field of self and any children of self. - void SetOwner(const CMessage::OwnerRef& new_owner) { owner = new_owner; } }; struct MessageMapContainer : public MapContainer { // The type used to create new child messages. CMessageClass* message_class; - - // A dict mapping Message* -> CMessage. - PyObject* message_dict; }; bool InitMapContainers(); @@ -109,12 +75,12 @@ extern PyTypeObject MapIterator_Type; // Both map types use the same iterator. // Builds a MapContainer object, from a parent message and a // field descriptor. -extern PyObject* NewScalarMapContainer( +extern MapContainer* NewScalarMapContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor); // Builds a MessageMap object, from a parent message and a // field descriptor. -extern PyObject* NewMessageMapContainer( +extern MessageMapContainer* NewMessageMapContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, CMessageClass* message_class); diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 9a3caa41e3..ae0971da3f 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -68,6 +68,7 @@ #include <google/protobuf/pyext/scoped_pyobject_ptr.h> #include <google/protobuf/util/message_differencer.h> #include <google/protobuf/stubs/strutil.h> +#include <google/protobuf/stubs/map_util.h> #include <google/protobuf/port_def.inc> @@ -531,100 +532,9 @@ static const Descriptor* GetMessageDescriptor(PyTypeObject* cls) { namespace cmessage { int InternalReleaseFieldByDescriptor( CMessage* self, - const FieldDescriptor* field_descriptor, - PyObject* composite_field); + const FieldDescriptor* field_descriptor); } // namespace cmessage -// --------------------------------------------------------------------- -// Visiting the composite children of a CMessage - -struct ChildVisitor { - // Returns 0 on success, -1 on failure. - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitMapContainer(MapContainer* container) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return 0; - } - - // Returns 0 on success, -1 on failure. - int VisitUnknownFieldSet(PyUnknownFields* unknown_field_set) { - return 0; - } -}; - -// Apply a function to a composite field. Does nothing if child is of -// non-composite type. -template<class Visitor> -static int VisitCompositeField(const FieldDescriptor* descriptor, - PyObject* child, - Visitor visitor) { - if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) { - if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - if (descriptor->is_map()) { - MapContainer* container = reinterpret_cast<MapContainer*>(child); - if (visitor.VisitMapContainer(container) == -1) { - return -1; - } - } else { - RepeatedCompositeContainer* container = - reinterpret_cast<RepeatedCompositeContainer*>(child); - if (visitor.VisitRepeatedCompositeContainer(container) == -1) - return -1; - } - } else { - RepeatedScalarContainer* container = - reinterpret_cast<RepeatedScalarContainer*>(child); - if (visitor.VisitRepeatedScalarContainer(container) == -1) - return -1; - } - } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - CMessage* cmsg = reinterpret_cast<CMessage*>(child); - if (visitor.VisitCMessage(cmsg, descriptor) == -1) - return -1; - } - // The ExtensionDict might contain non-composite fields, which we - // skip here. - return 0; -} - -// Visit each composite field and extension field of this CMessage. -// Returns -1 on error and 0 on success. -template<class Visitor> -int ForEachCompositeField(CMessage* self, Visitor visitor) { - // Visit normal fields. - if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - const FieldDescriptor* descriptor = it->first; - PyObject* field = it->second; - if (VisitCompositeField(descriptor, field, visitor) == -1) return -1; - } - } - - if (self->unknown_field_set) { - PyUnknownFields* unknown_field_set = - reinterpret_cast<PyUnknownFields*>(self->unknown_field_set); - visitor.VisitUnknownFieldSet(unknown_field_set); - } - - return 0; -} - // --------------------------------------------------------------------- PyObject* EncodeError_class; @@ -941,138 +851,78 @@ static int MaybeReleaseOverlappingOneofField( // Non-message fields don't need to be released. return 0; } - if (cmessage->composite_fields) { - CMessage::CompositeFieldsMap::iterator iterator = - cmessage->composite_fields->find(existing_field); - if (iterator != cmessage->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(cmessage, existing_field, - iterator->second) < 0) { - return -1; - } - Py_DECREF(iterator->second); - cmessage->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(cmessage, existing_field) < 0) { + return -1; } #endif return 0; } -// --------------------------------------------------------------------- -// Making a message writable - -static Message* GetMutableMessage( - CMessage* parent, - const FieldDescriptor* parent_field) { - Message* parent_message = parent->message; - const Reflection* reflection = parent_message->GetReflection(); - if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) { - return NULL; - } - return reflection->MutableMessage( - parent_message, parent_field, - GetFactoryForMessage(parent)->message_factory); -} - -struct FixupMessageReference : public ChildVisitor { - // message must outlive this object. - explicit FixupMessageReference(Message* message) : - message_(message) {} - - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - container->message = message_; - return 0; - } - - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - container->message = message_; - return 0; - } - - int VisitMapContainer(MapContainer* container) { - container->message = message_; - return 0; - } - - int VisitUnknownFieldSet(PyUnknownFields* unknown_field_set) { - const Reflection* reflection = message_->GetReflection(); - unknown_field_set->fields = &reflection->GetUnknownFields(*message_); - return 0; - } - - private: - Message* message_; -}; - // After a Merge, visit every sub-message that was read-only, and // eventually update their pointer if the Merge operation modified them. -struct FixupMessageAfterMerge : public FixupMessageReference { - explicit FixupMessageAfterMerge(CMessage* parent) : - FixupMessageReference(parent->message), - parent_cmessage(parent), message(parent->message) {} - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - if (cmessage->read_only == false) { - return 0; - } - if (message->GetReflection()->HasField(*message, field_descriptor)) { - Message* mutable_message = GetMutableMessage( - parent_cmessage, field_descriptor); - if (mutable_message == NULL) { - return -1; +int FixupMessageAfterMerge(CMessage* self) { + if (!self->composite_fields) { + return 0; + } + for (const auto& item : *self->composite_fields) { + const FieldDescriptor* descriptor = item.first; + if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + !descriptor->is_repeated()) { + CMessage* cmsg = reinterpret_cast<CMessage*>(item.second); + if (cmsg->read_only == false) { + return 0; } - cmessage->message = mutable_message; - cmessage->read_only = false; - if (ForEachCompositeField( - cmessage, FixupMessageAfterMerge(cmessage)) == -1) { - return -1; + Message* message = self->message; + const Reflection* reflection = message->GetReflection(); + if (reflection->HasField(*message, descriptor)) { + // Message used to be read_only, but is no longer. Get the new pointer + // and record it. + Message* mutable_message = + reflection->MutableMessage(message, descriptor, nullptr); + cmsg->message = mutable_message; + cmsg->read_only = false; + if (FixupMessageAfterMerge(cmsg) < 0) { + return -1; + } } } - return 0; } - private: - CMessage* parent_cmessage; - Message* message; -}; + return 0; +} + +// --------------------------------------------------------------------- +// Making a message writable int AssureWritable(CMessage* self) { if (self == NULL || !self->read_only) { return 0; } - if (self->parent == NULL) { - // If parent is NULL but we are trying to modify a read-only message, this - // is a reference to a constant default instance that needs to be replaced - // with a mutable top-level message. - self->message = self->message->New(); - self->owner.reset(self->message); - // Cascade the new owner to eventual children: even if this message is - // empty, some submessages or repeated containers might exist already. - SetOwner(self, self->owner); - } else { - // Otherwise, we need a mutable child message. - if (AssureWritable(self->parent) == -1) - return -1; + // Toplevel messages are always mutable. + GOOGLE_DCHECK(self->parent); - // Make self->message writable. - Message* mutable_message = GetMutableMessage( - self->parent, - self->parent_field_descriptor); - if (mutable_message == NULL) { - return -1; - } - self->message = mutable_message; + if (AssureWritable(self->parent) == -1) + return -1; + + // If this message is part of a oneof, there might be a field to release in + // the parent. + if (MaybeReleaseOverlappingOneofField(self->parent, + self->parent_field_descriptor) < 0) { + return -1; } - self->read_only = false; - // When a CMessage is made writable its Message pointer is updated - // to point to a new mutable Message. When that happens we need to - // update any references to the old, read-only CMessage. There are - // three places such references occur: RepeatedScalarContainer, - // RepeatedCompositeContainer, and MapContainer. - if (ForEachCompositeField(self, FixupMessageReference(self->message)) == -1) + // Make self->message writable. + Message* parent_message = self->parent->message; + const Reflection* reflection = parent_message->GetReflection(); + Message* mutable_message = reflection->MutableMessage( + parent_message, self->parent_field_descriptor, + GetFactoryForMessage(self->parent)->message_factory); + if (mutable_message == NULL) { return -1; + } + self->message = mutable_message; + self->read_only = false; return 0; } @@ -1119,16 +969,16 @@ static PyObject* GetIntegerEnumValue(const FieldDescriptor& descriptor, return value; } -// If cmessage_list is not NULL, this function releases values into the -// container CMessages instead of just removing. Repeated composite container -// needs to do this to make sure CMessages stay alive if they're still -// referenced after deletion. Repeated scalar container doesn't need to worry. -int InternalDeleteRepeatedField( - Message* message, +// Delete a slice from a repeated field. +// The only way to remove items in C++ protos is to delete the last one, +// so we swap items to move the deleted ones at the end, and then strip the +// sequence. +int DeleteRepeatedField( + CMessage* self, const FieldDescriptor* field_descriptor, - PyObject* slice, - PyObject* cmessage_list) { + PyObject* slice) { Py_ssize_t length, from, to, step, slice_length; + Message* message = self->message; const Reflection* reflection = message->GetReflection(); int min, max; length = reflection->FieldSize(*message, field_descriptor); @@ -1178,39 +1028,31 @@ int InternalDeleteRepeatedField( i += step; } + // Swap elements so that items to delete are at the end. to = 0; for (i = 0; i < length; ++i) { if (!to_delete[i]) { if (i != to) { reflection->SwapElements(message, field_descriptor, i, to); - if (cmessage_list != NULL) { - // If a list of cmessages is passed in (i.e. from a repeated - // composite container), swap those as well to correspond to the - // swaps in the underlying message so they're in the right order - // when we start releasing. - PyObject* tmp = PyList_GET_ITEM(cmessage_list, i); - PyList_SET_ITEM(cmessage_list, i, - PyList_GET_ITEM(cmessage_list, to)); - PyList_SET_ITEM(cmessage_list, to, tmp); - } } ++to; } } - while (i > to) { - if (cmessage_list == NULL) { - reflection->RemoveLast(message, field_descriptor); - } else { - CMessage* last_cmessage = reinterpret_cast<CMessage*>( - PyList_GET_ITEM(cmessage_list, PyList_GET_SIZE(cmessage_list) - 1)); - repeated_composite_container::ReleaseLastTo( - message, field_descriptor, last_cmessage); - if (PySequence_DelItem(cmessage_list, -1) < 0) { - return -1; + // Remove items, starting from the end. + for (; length > to; length--) { + // If there is a living weak reference to a deleted item, we "Release" it, + if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && + self->child_submessages) { + Message* sub_message = reflection->MutableRepeatedMessage( + message, field_descriptor, length - 1); + if (CMessage* released = self->MaybeReleaseSubMessage(sub_message)) { + released->message = reflection->ReleaseLast(message, field_descriptor); + continue; } } - --i; + // No Python object refers to this item, discard the value. + reflection->RemoveLast(message, field_descriptor); } return 0; @@ -1384,8 +1226,8 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { return 0; } -// Allocates an incomplete Python Message: the caller must fill self->message, -// self->owner and eventually self->parent. +// Allocates an incomplete Python Message: the caller must fill self->message +// and eventually self->parent. CMessage* NewEmptyMessage(CMessageClass* type) { CMessage* self = reinterpret_cast<CMessage*>( PyType_GenericAlloc(&type->super.ht_type, 0)); @@ -1393,14 +1235,13 @@ CMessage* NewEmptyMessage(CMessageClass* type) { return NULL; } - // Use "placement new" syntax to initialize the C++ object. - new (&self->owner) CMessage::OwnerRef(NULL); self->message = NULL; self->parent = NULL; self->parent_field_descriptor = NULL; self->read_only = false; self->composite_fields = NULL; + self->child_submessages = NULL; self->unknown_field_set = NULL; @@ -1420,9 +1261,10 @@ static PyObject* New(PyTypeObject* cls, if (message_descriptor == NULL) { return NULL; } - const Message* default_message = type->py_message_factory->message_factory - ->GetPrototype(message_descriptor); - if (default_message == NULL) { + const Message* prototype = + type->py_message_factory->message_factory->GetPrototype( + message_descriptor); + if (prototype == NULL) { PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str()); return NULL; } @@ -1431,8 +1273,8 @@ static PyObject* New(PyTypeObject* cls, if (self == NULL) { return NULL; } - self->message = default_message->New(); - self->owner.reset(self->message); + self->message = prototype->New(); + self->parent = nullptr; // This message owns its data. return reinterpret_cast<PyObject*>(self); } @@ -1444,63 +1286,39 @@ static int Init(CMessage* self, PyObject* args, PyObject* kwargs) { // --------------------------------------------------------------------- // Deallocating a CMessage -// -// Deallocating a CMessage requires that we clear any weak references -// from children to the message being deallocated. - -// Clear the weak reference from the child to the parent. -struct ClearWeakReferences : public ChildVisitor { - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - container->parent = NULL; - // The elements in the container have the same parent as the - // container itself, so NULL out that pointer as well. - const Py_ssize_t n = PyList_GET_SIZE(container->child_messages); - for (Py_ssize_t i = 0; i < n; ++i) { - CMessage* child_cmessage = reinterpret_cast<CMessage*>( - PyList_GET_ITEM(container->child_messages, i)); - child_cmessage->parent = NULL; - } - return 0; - } - - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - container->parent = NULL; - return 0; - } - - int VisitMapContainer(MapContainer* container) { - container->parent = NULL; - return 0; - } - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - cmessage->parent = NULL; - return 0; - } -}; static void Dealloc(CMessage* self) { if (self->weakreflist) { PyObject_ClearWeakRefs(reinterpret_cast<PyObject*>(self)); } - // Null out all weak references from children to this message. - GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences())); - - if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - Py_DECREF(it->second); - } - delete self->composite_fields; - } + // At this point all dependent objects have been removed. + GOOGLE_DCHECK(!self->child_submessages || self->child_submessages->empty()); + GOOGLE_DCHECK(!self->composite_fields || self->composite_fields->empty()); + delete self->child_submessages; + delete self->composite_fields; if (self->unknown_field_set) { unknown_fields::Clear( reinterpret_cast<PyUnknownFields*>(self->unknown_field_set)); - Py_CLEAR(self->unknown_field_set); } - self->owner.~ThreadUnsafeSharedPtr<Message>(); + + CMessage* parent = self->parent; + if (!parent) { + // No parent, we own the message. + delete self->message; + } else if (parent->AsPyObject() == Py_None) { + // Message owned externally: Nothing to dealloc + Py_CLEAR(self->parent); + } else { + // Clear this message from its parent's map. + if (self->parent_field_descriptor->is_repeated()) { + if (parent->child_submessages) + parent->child_submessages->erase(self->message); + } else { + if (parent->composite_fields) + parent->composite_fields->erase(self->parent_field_descriptor); + } + Py_CLEAR(self->parent); + } Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); } @@ -1652,17 +1470,8 @@ PyObject* ClearExtension(CMessage* self, PyObject* extension) { if (descriptor == NULL) { return NULL; } - if (self->composite_fields != NULL) { - CMessage::CompositeFieldsMap::iterator iterator = - self->composite_fields->find(descriptor); - if (iterator != self->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(self, descriptor, - iterator->second) < 0) { - return NULL; - } - Py_DECREF(iterator->second); - self->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(self, descriptor) < 0) { + return NULL; } return ClearFieldByDescriptor(self, descriptor); } @@ -1680,141 +1489,107 @@ PyObject* HasExtension(CMessage* self, PyObject* extension) { // // The Python API's ClearField() and Clear() methods behave // differently than their C++ counterparts. While the C++ versions -// clears the children the Python versions detaches the children, +// clears the children, the Python versions detaches the children, // without touching their content. This impedance mismatch causes // some complexity in the implementation, which is captured in this // section. // -// When a CMessage field is cleared we need to: -// -// * Release the Message used as the backing store for the CMessage -// from its parent. +// When one or multiple fields are cleared we need to: // -// * Change the owner field of the released CMessage and all of its -// children to point to the newly released Message. +// * Gather all child objects that need to be detached from the message. +// In composite_fields and child_submessages. // -// * Clear the weak references from the released CMessage to the -// parent. +// * Create a new Python message of the same kind. Use SwapFields() to move +// data from the original message. // -// When a RepeatedCompositeContainer field is cleared we need to: -// -// * Release all the Message used as the backing store for the -// CMessages stored in the container. -// -// * Change the owner field of all the released CMessage and all of -// their children to point to the newly released Messages. -// -// * Clear the weak references from the released container to the -// parent. +// * Change the parent of all child objects: update their strong reference +// to their parent, and move their presence in composite_fields and +// child_submessages. -class SetOwnerVisitor : public ChildVisitor { - public: - // new_owner must outlive this object. - explicit SetOwnerVisitor(const CMessage::OwnerRef& new_owner) - : new_owner_(new_owner) {} +// --------------------------------------------------------------------- +// Release a composite child of a CMessage - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - repeated_composite_container::SetOwner(container, new_owner_); +static int InternalReparentFields( + CMessage* self, const std::vector<CMessage*>& messages_to_release, + const std::vector<ContainerBase*>& containers_to_release) { + if (messages_to_release.empty() && containers_to_release.empty()) { return 0; } - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - repeated_scalar_container::SetOwner(container, new_owner_); - return 0; + // Move all the passed sub_messages to another message. + CMessage* new_message = cmessage::NewEmptyMessage(self->GetMessageClass()); + if (new_message == nullptr) { + return -1; } + new_message->message = self->message->New(); + ScopedPyObjectPtr holder(reinterpret_cast<PyObject*>(new_message)); + new_message->child_submessages = new CMessage::SubMessagesMap(); + new_message->composite_fields = new CMessage::CompositeFieldsMap(); + std::set<const FieldDescriptor*> fields_to_swap; - int VisitMapContainer(MapContainer* container) { - container->SetOwner(new_owner_); - return 0; - } + // In case this the removed fields are the last reference to a message, keep + // a reference. + Py_INCREF(self); - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return SetOwner(cmessage, new_owner_); + for (const auto& to_release : messages_to_release) { + fields_to_swap.insert(to_release->parent_field_descriptor); + // Reparent + Py_INCREF(new_message); + Py_DECREF(to_release->parent); + to_release->parent = new_message; + self->child_submessages->erase(to_release->message); + new_message->child_submessages->emplace(to_release->message, to_release); } - private: - const CMessage::OwnerRef& new_owner_; -}; - -// Change the owner of this CMessage and all its children, recursively. -int SetOwner(CMessage* self, const CMessage::OwnerRef& new_owner) { - self->owner = new_owner; - if (ForEachCompositeField(self, SetOwnerVisitor(new_owner)) == -1) - return -1; - return 0; -} - -// Releases the message specified by 'field' and returns the -// pointer. If the field does not exist a new message is created using -// 'descriptor'. The caller takes ownership of the returned pointer. -Message* ReleaseMessage(CMessage* self, - const Descriptor* descriptor, - const FieldDescriptor* field_descriptor) { - MessageFactory* message_factory = GetFactoryForMessage(self)->message_factory; - Message* released_message = self->message->GetReflection()->ReleaseMessage( - self->message, field_descriptor, message_factory); - // ReleaseMessage will return NULL which differs from - // child_cmessage->message, if the field does not exist. In this case, - // the latter points to the default instance via a const_cast<>, so we - // have to reset it to a new mutable object since we are taking ownership. - if (released_message == NULL) { - const Message* prototype = message_factory->GetPrototype(descriptor); - GOOGLE_DCHECK(prototype != NULL); - released_message = prototype->New(); + for (const auto& to_release : containers_to_release) { + fields_to_swap.insert(to_release->parent_field_descriptor); + Py_INCREF(new_message); + Py_DECREF(to_release->parent); + to_release->parent = new_message; + self->composite_fields->erase(to_release->parent_field_descriptor); + new_message->composite_fields->emplace(to_release->parent_field_descriptor, + to_release); } - return released_message; -} - -int ReleaseSubMessage(CMessage* self, - const FieldDescriptor* field_descriptor, - CMessage* child_cmessage) { - // Release the Message - CMessage::OwnerRef released_message(ReleaseMessage( - self, child_cmessage->message->GetDescriptor(), field_descriptor)); - child_cmessage->message = released_message.get(); - child_cmessage->owner.swap(released_message); - child_cmessage->parent = NULL; - child_cmessage->parent_field_descriptor = NULL; - child_cmessage->read_only = false; - return ForEachCompositeField(child_cmessage, - SetOwnerVisitor(child_cmessage->owner)); -} + self->message->GetReflection()->SwapFields( + self->message, new_message->message, + std::vector<const FieldDescriptor*>(fields_to_swap.begin(), + fields_to_swap.end())); -struct ReleaseChild : public ChildVisitor { - // message must outlive this object. - explicit ReleaseChild(CMessage* parent) : - parent_(parent) {} + // This might delete the Python message completely if all children were moved. + Py_DECREF(self); - int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { - return repeated_composite_container::Release(container); - } + return 0; +} - int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { - return repeated_scalar_container::Release(container); +int InternalReleaseFieldByDescriptor( + CMessage* self, + const FieldDescriptor* field_descriptor) { + if (!field_descriptor->is_repeated() && + field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { + // Single scalars are not in any cache. + return 0; } - - int VisitMapContainer(MapContainer* container) { - return container->Release(); + std::vector<CMessage*> messages_to_release; + std::vector<ContainerBase*> containers_to_release; + if (self->child_submessages && field_descriptor->is_repeated() && + field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + for (const auto& child_item : *self->child_submessages) { + if (child_item.second->parent_field_descriptor == field_descriptor) { + messages_to_release.push_back(child_item.second); + } + } } - - int VisitCMessage(CMessage* cmessage, - const FieldDescriptor* field_descriptor) { - return ReleaseSubMessage(parent_, field_descriptor, cmessage); + if (self->composite_fields) { + CMessage::CompositeFieldsMap::iterator it = + self->composite_fields->find(field_descriptor); + if (it != self->composite_fields->end()) { + containers_to_release.push_back(it->second); + } } - CMessage* parent_; -}; - -int InternalReleaseFieldByDescriptor( - CMessage* self, - const FieldDescriptor* field_descriptor, - PyObject* composite_field) { - return VisitCompositeField( - field_descriptor, - composite_field, - ReleaseChild(self)); + return InternalReparentFields(self, messages_to_release, + containers_to_release); } PyObject* ClearFieldByDescriptor( @@ -1864,37 +1639,35 @@ PyObject* ClearField(CMessage* self, PyObject* arg) { arg = arg_in_oneof.get(); } - if (self->composite_fields) { - CMessage::CompositeFieldsMap::iterator iterator = - self->composite_fields->find(field_descriptor); - if (iterator != self->composite_fields->end()) { - if (InternalReleaseFieldByDescriptor(self, field_descriptor, - iterator->second) < 0) { - return NULL; - } - Py_DECREF(iterator->second); - self->composite_fields->erase(iterator); - } + if (InternalReleaseFieldByDescriptor(self, field_descriptor) < 0) { + return NULL; } return ClearFieldByDescriptor(self, field_descriptor); } PyObject* Clear(CMessage* self) { AssureWritable(self); - if (ForEachCompositeField(self, ReleaseChild(self)) == -1) - return NULL; + // Detach all current fields of this message + std::vector<CMessage*> messages_to_release; + std::vector<ContainerBase*> containers_to_release; + if (self->child_submessages) { + for (const auto& item : *self->child_submessages) { + messages_to_release.push_back(item.second); + } + } if (self->composite_fields) { - for (CMessage::CompositeFieldsMap::iterator it = - self->composite_fields->begin(); - it != self->composite_fields->end(); it++) { - Py_DECREF(it->second); + for (const auto& item : *self->composite_fields) { + containers_to_release.push_back(item.second); } - self->composite_fields->clear(); + } + if (InternalReparentFields(self, messages_to_release, containers_to_release) < + 0) { + return NULL; } if (self->unknown_field_set) { unknown_fields::Clear( reinterpret_cast<PyUnknownFields*>(self->unknown_field_set)); - Py_CLEAR(self->unknown_field_set); + self->unknown_field_set = nullptr; } self->message->Clear(); Py_RETURN_NONE; @@ -2075,7 +1848,7 @@ PyObject* MergeFrom(CMessage* self, PyObject* arg) { self->message->MergeFrom(*other_message->message); // Child message might be lazily created before MergeFrom. Make sure they // are mutable at this point if child messages are really created. - if (ForEachCompositeField(self, FixupMessageAfterMerge(self)) == -1) { + if (FixupMessageAfterMerge(self) < 0) { return NULL; } @@ -2168,7 +1941,7 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) { bool success = self->message->MergePartialFromCodedStream(&input); // Child message might be lazily created before MergeFrom. Make sure they // are mutable at this point if child messages are really created. - if (ForEachCompositeField(self, FixupMessageAfterMerge(self)) == -1) { + if (FixupMessageAfterMerge(self) < 0) { return NULL; } @@ -2458,7 +2231,7 @@ PyObject* InternalGetScalar(const Message* message, return result; } -PyObject* InternalGetSubMessage( +CMessage* InternalGetSubMessage( CMessage* self, const FieldDescriptor* field_descriptor) { const Reflection* reflection = self->message->GetReflection(); PyMessageFactory* factory = GetFactoryForMessage(self); @@ -2467,7 +2240,7 @@ PyObject* InternalGetSubMessage( CMessageClass* message_class = message_factory::GetOrCreateMessageClass( factory, field_descriptor->message_type()); - ScopedPyObjectPtr message_class_handler( + ScopedPyObjectPtr message_class_owner( reinterpret_cast<PyObject*>(message_class)); if (message_class == NULL) { return NULL; @@ -2478,13 +2251,12 @@ PyObject* InternalGetSubMessage( return NULL; } - cmsg->owner = self->owner; + Py_INCREF(self); cmsg->parent = self; cmsg->parent_field_descriptor = field_descriptor; cmsg->read_only = !reflection->HasField(*self->message, field_descriptor); cmsg->message = const_cast<Message*>(&sub_message); - - return reinterpret_cast<PyObject*>(cmsg); + return cmsg; } int InternalSetNonOneofScalar( @@ -2680,6 +2452,13 @@ PyObject* SetState(CMessage* self, PyObject* state) { if (serialized == NULL) { return NULL; } +#if PY_MAJOR_VERSION >= 3 + // On Python 3, using encoding='latin1' is required for unpickling + // protos pickled by Python 2. + if (!PyBytes_Check(serialized)) { + serialized = PyUnicode_AsEncodedString(serialized, "latin1", NULL); + } +#endif if (ScopedPyObjectPtr(ParseFromString(self, serialized)) == NULL) { return NULL; } @@ -2719,8 +2498,9 @@ static PyObject* GetExtensionDict(CMessage* self, void *closure) { static PyObject* UnknownFieldSet(CMessage* self) { if (self->unknown_field_set == NULL) { self->unknown_field_set = unknown_fields::NewPyUnknownFields(self); + } else { + Py_INCREF(self->unknown_field_set); } - Py_INCREF(self->unknown_field_set); return self->unknown_field_set; } @@ -2805,17 +2585,23 @@ static PyMethodDef Methods[] = { { NULL, NULL} }; -static bool SetCompositeField(CMessage* self, const FieldDescriptor* field, - PyObject* value) { +bool SetCompositeField(CMessage* self, const FieldDescriptor* field, + ContainerBase* value) { if (self->composite_fields == NULL) { self->composite_fields = new CMessage::CompositeFieldsMap(); } - Py_INCREF(value); - Py_XDECREF((*self->composite_fields)[field]); (*self->composite_fields)[field] = value; return true; } +bool SetSubmessage(CMessage* self, CMessage* submessage) { + if (self->child_submessages == NULL) { + self->child_submessages = new CMessage::SubMessagesMap(); + } + (*self->child_submessages)[submessage->message] = submessage; + return true; +} + PyObject* GetAttr(PyObject* pself, PyObject* name) { CMessage* self = reinterpret_cast<CMessage*>(pself); PyObject* result = PyObject_GenericGetAttr( @@ -2838,9 +2624,9 @@ PyObject* GetFieldValue(CMessage* self, CMessage::CompositeFieldsMap::iterator it = self->composite_fields->find(field_descriptor); if (it != self->composite_fields->end()) { - PyObject* value = it->second; + ContainerBase* value = it->second; Py_INCREF(value); - return value; + return value->AsPyObject(); } } @@ -2852,8 +2638,13 @@ PyObject* GetFieldValue(CMessage* self, return NULL; } + if (!field_descriptor->is_repeated() && + field_descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { + return InternalGetScalar(self->message, field_descriptor); + } + + ContainerBase* py_container = nullptr; if (field_descriptor->is_map()) { - PyObject* py_container = NULL; const Descriptor* entry_type = field_descriptor->message_type(); const FieldDescriptor* value_type = entry_type->FindFieldByName("value"); if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { @@ -2867,18 +2658,7 @@ PyObject* GetFieldValue(CMessage* self, } else { py_container = NewScalarMapContainer(self, field_descriptor); } - if (py_container == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, py_container)) { - Py_DECREF(py_container); - return NULL; - } - return py_container; - } - - if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { - PyObject* py_container = NULL; + } else if (field_descriptor->is_repeated()) { if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { CMessageClass* message_class = message_factory::GetMessageClass( GetFactoryForMessage(self), field_descriptor->message_type()); @@ -2888,32 +2668,24 @@ PyObject* GetFieldValue(CMessage* self, py_container = repeated_composite_container::NewContainer( self, field_descriptor, message_class); } else { - py_container = repeated_scalar_container::NewContainer( - self, field_descriptor); - } - if (py_container == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, py_container)) { - Py_DECREF(py_container); - return NULL; + py_container = + repeated_scalar_container::NewContainer(self, field_descriptor); } - return py_container; + } else if (field_descriptor->cpp_type() == + FieldDescriptor::CPPTYPE_MESSAGE) { + py_container = InternalGetSubMessage(self, field_descriptor); + } else { + PyErr_SetString(PyExc_SystemError, "Should never happen"); } - if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - PyObject* sub_message = InternalGetSubMessage(self, field_descriptor); - if (sub_message == NULL) { - return NULL; - } - if (!SetCompositeField(self, field_descriptor, sub_message)) { - Py_DECREF(sub_message); - return NULL; - } - return sub_message; + if (py_container == NULL) { + return NULL; } - - return InternalGetScalar(self->message, field_descriptor); + if (!SetCompositeField(self, field_descriptor, py_container)) { + Py_DECREF(py_container); + return NULL; + } + return py_container->AsPyObject(); } int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, @@ -2941,8 +2713,82 @@ int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, return InternalSetScalar(self, field_descriptor, value); } } + } // namespace cmessage +// All containers which are not messages: +// - Make a new parent message +// - Copy the field +// - return the field. +PyObject* ContainerBase::DeepCopy() { + CMessage* new_parent = + cmessage::NewEmptyMessage(this->parent->GetMessageClass()); + new_parent->message = this->parent->message->New(); + + // Copy the map field into the new message. + this->parent->message->GetReflection()->SwapFields( + this->parent->message, new_parent->message, + {this->parent_field_descriptor}); + this->parent->message->MergeFrom(*new_parent->message); + + PyObject* result = + cmessage::GetFieldValue(new_parent, this->parent_field_descriptor); + Py_DECREF(new_parent); + return result; +} + +void ContainerBase::RemoveFromParentCache() { + CMessage* parent = this->parent; + if (parent) { + if (parent->composite_fields) + parent->composite_fields->erase(this->parent_field_descriptor); + Py_CLEAR(parent); + } +} + +CMessage* CMessage::BuildSubMessageFromPointer( + const FieldDescriptor* field_descriptor, Message* sub_message, + CMessageClass* message_class) { + if (!this->child_submessages) { + this->child_submessages = new CMessage::SubMessagesMap(); + } + CMessage* cmsg = FindPtrOrNull( + *this->child_submessages, sub_message); + if (cmsg) { + Py_INCREF(cmsg); + } else { + cmsg = cmessage::NewEmptyMessage(message_class); + + if (cmsg == NULL) { + return NULL; + } + cmsg->message = sub_message; + Py_INCREF(this); + cmsg->parent = this; + cmsg->parent_field_descriptor = field_descriptor; + cmessage::SetSubmessage(this, cmsg); + } + return cmsg; +} + +CMessage* CMessage::MaybeReleaseSubMessage(Message* sub_message) { + if (!this->child_submessages) { + return nullptr; + } + CMessage* released = FindPtrOrNull( + *this->child_submessages, sub_message); + if (!released) { + return nullptr; + } + // The target message will now own its content. + Py_CLEAR(released->parent); + released->parent_field_descriptor = nullptr; + released->read_only = false; + // Delete it from the cache. + this->child_submessages->erase(sub_message); + return released; +} + static CMessageClass _CMessage_Type = { { { PyVarObject_HEAD_INIT(&_CMessageClass_Type, 0) FULL_MODULE_NAME ".CMessage", // tp_name @@ -3023,10 +2869,11 @@ Message* PyMessage_GetMutableMessagePointer(PyObject* msg) { PyErr_SetString(PyExc_TypeError, "Not a Message instance"); return NULL; } + CMessage* cmsg = reinterpret_cast<CMessage*>(msg); - CMessage* cmsg = reinterpret_cast<CMessage*>(msg); - if (cmsg->composite_fields && !cmsg->composite_fields->empty()) { + if ((cmsg->composite_fields && !cmsg->composite_fields->empty()) || + (cmsg->child_submessages && !cmsg->child_submessages->empty())) { // There is currently no way of accurately syncing arbitrary changes to // the underlying C++ message back to the CMessage (e.g. removed repeated // composite containers). We only allow direct mutation of the underlying @@ -3040,6 +2887,34 @@ Message* PyMessage_GetMutableMessagePointer(PyObject* msg) { return cmsg->message; } +PyObject* PyMessage_NewMessageOwnedExternally(Message* message, + PyObject* message_factory) { + if (message_factory) { + PyErr_SetString(PyExc_NotImplementedError, + "Default message_factory=NULL is the only supported value"); + return NULL; + } + if (message->GetReflection()->GetMessageFactory() != + MessageFactory::generated_factory()) { + PyErr_SetString(PyExc_TypeError, + "Message pointer was not created from the default factory"); + return NULL; + } + + CMessageClass* message_class = message_factory::GetOrCreateMessageClass( + GetDefaultDescriptorPool()->py_message_factory, message->GetDescriptor()); + + CMessage* self = cmessage::NewEmptyMessage(message_class); + if (self == NULL) { + return NULL; + } + Py_DECREF(message_class); + self->message = message; + Py_INCREF(Py_None); + self->parent = reinterpret_cast<CMessage*>(Py_None); + return self->AsPyObject(); +} + void InitGlobals() { // TODO(gps): Check all return values in this function for NULL and propagate // the error (MemoryError) on up to result in an import failure. These should @@ -3168,6 +3043,11 @@ bool InitProto2MessageModule(PyObject *m) { PyModule_AddObject( m, "ExtensionDict", reinterpret_cast<PyObject*>(&ExtensionDict_Type)); + if (PyType_Ready(&ExtensionIterator_Type) < 0) { + return false; + } + PyModule_AddObject(m, "ExtensionIterator", + reinterpret_cast<PyObject*>(&ExtensionIterator_Type)); // Expose the DescriptorPool used to hold all descriptors added from generated // pb2.py files. diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h index 64aafaf05a..55be297fce 100644 --- a/python/google/protobuf/pyext/message.h +++ b/python/google/protobuf/pyext/message.h @@ -41,7 +41,6 @@ #include <unordered_map> #include <google/protobuf/stubs/common.h> -#include <google/protobuf/pyext/thread_unsafe_shared_ptr.h> namespace google { namespace protobuf { @@ -57,39 +56,51 @@ namespace python { struct ExtensionDict; struct PyMessageFactory; +struct CMessageClass; -typedef struct CMessage { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python CMessage holds a reference to it in - // order to keep it alive as long as there's a Python object that - // references any part of the tree. +// Most of the complexity of the Message class comes from the "Release" +// behavior: +// +// When a field is cleared, it is only detached from its message. Existing +// references to submessages, to repeated container etc. won't see any change, +// as if the data was effectively managed by these containers. +// +// ExtensionDicts and UnknownFields containers do NOT follow this rule. They +// don't store any data, and always refer to their parent message. - typedef ThreadUnsafeSharedPtr<Message> OwnerRef; - OwnerRef owner; +struct ContainerBase { + PyObject_HEAD; - // Weak reference to a parent CMessage object. This is NULL for any top-level - // message and is set for any child message (i.e. a child submessage or a - // part of a repeated composite field). + // Strong reference to a parent message object. For a CMessage there are three + // cases: + // - For a top-level message, this pointer is NULL. + // - For a sub-message, this points to the parent message. + // - For a message managed externally, this is a owned reference to Py_None. // - // Used to make sure all ancestors are also mutable when first modifying - // a child submessage (in other words, turning a default message instance - // into a mutable one). - // - // If a submessage is released (becomes a new top-level message), this field - // MUST be set to NULL. The parent may get deallocated and further attempts - // to use this pointer will result in a crash. + // For all other types: repeated containers, maps, it always point to a + // valid parent CMessage. struct CMessage* parent; - // Pointer to the parent's descriptor that describes this submessage. - // Used together with the parent's message when making a default message - // instance mutable. - // The pointer is owned by the global DescriptorPool. + // If this object belongs to a parent message, describes which field it comes + // from. + // The pointer is owned by the DescriptorPool (which is kept alive + // through the message's Python class) const FieldDescriptor* parent_field_descriptor; - // Pointer to the C++ Message object for this CMessage. The - // CMessage does not own this pointer. + PyObject* AsPyObject() { return reinterpret_cast<PyObject*>(this); } + + // The Three methods below are only used by Repeated containers, and Maps. + + // This implementation works for all containers which have a parent. + PyObject* DeepCopy(); + // Delete this container object from its parent. Does not work for messages. + void RemoveFromParentCache(); +}; + +typedef struct CMessage : public ContainerBase { + // Pointer to the C++ Message object for this CMessage. + // - If this object has no parent, we own this pointer. + // - If this object has a parent message, the parent owns this pointer. Message* message; // Indicates this submessage is pointing to a default instance of a message. @@ -97,23 +108,37 @@ typedef struct CMessage { // made writable, at which point this field is set to false. bool read_only; - // A mapping indexed by field, containing CMessage, - // RepeatedCompositeContainer, and RepeatedScalarContainer - // objects. Used as a cache to make sure we don't have to make a - // Python wrapper for the C++ Message objects on every access, or - // deal with the synchronization nightmare that could create. - // Also cache extension fields. - // The FieldDescriptor is owned by the message's pool; PyObject references - // are owned. - typedef std::unordered_map<const FieldDescriptor*, PyObject*> + // A mapping indexed by field, containing weak references to contained objects + // which need to implement the "Release" mechanism: + // direct submessages, RepeatedCompositeContainer, RepeatedScalarContainer + // and MapContainer. + typedef std::unordered_map<const FieldDescriptor*, ContainerBase*> CompositeFieldsMap; CompositeFieldsMap* composite_fields; + // A mapping containing weak references to indirect child messages, accessed + // through containers: repeated messages, and values of message maps. + // This avoid the creation of similar maps in each of those containers. + typedef std::unordered_map<const Message*, CMessage*> SubMessagesMap; + SubMessagesMap* child_submessages; + // A reference to PyUnknownFields. PyObject* unknown_field_set; // Implements the "weakref" protocol for this object. PyObject* weakreflist; + + // Return a *borrowed* reference to the message class. + CMessageClass* GetMessageClass() { + return reinterpret_cast<CMessageClass*>(Py_TYPE(this)); + } + + // For container containing messages, return a Python object for the given + // pointer to a message. + CMessage* BuildSubMessageFromPointer(const FieldDescriptor* field_descriptor, + Message* sub_message, + CMessageClass* message_class); + CMessage* MaybeReleaseSubMessage(Message* sub_message); } CMessage; // The (meta) type of all Messages classes. @@ -161,21 +186,16 @@ const FieldDescriptor* GetExtensionDescriptor(PyObject* extension); // submessage as the result is cached in composite_fields. // // Corresponds to reflection api method GetMessage. -PyObject* InternalGetSubMessage( +CMessage* InternalGetSubMessage( CMessage* self, const FieldDescriptor* field_descriptor); -// Deletes a range of C++ submessages in a repeated field (following a +// Deletes a range of items in a repeated field (following a // removal in a RepeatedCompositeContainer). // -// Releases submessages to the provided cmessage_list if it is not NULL rather -// than just removing them from the underlying proto. This cmessage_list must -// have a CMessage for each underlying submessage. The CMessages referred to -// by slice will be removed from cmessage_list by this function. -// // Corresponds to reflection api method RemoveLast. -int InternalDeleteRepeatedField(Message* message, - const FieldDescriptor* field_descriptor, - PyObject* slice, PyObject* cmessage_list); +int DeleteRepeatedField(CMessage* self, + const FieldDescriptor* field_descriptor, + PyObject* slice); // Sets the specified scalar value to the message. int InternalSetScalar(CMessage* self, @@ -193,6 +213,11 @@ int InternalSetNonOneofScalar(Message* message, PyObject* InternalGetScalar(const Message* message, const FieldDescriptor* field_descriptor); +bool SetCompositeField(CMessage* self, const FieldDescriptor* field, + ContainerBase* value); + +bool SetSubmessage(CMessage* self, CMessage* submessage); + // Clears the message, removing all contained data. Extension dictionary and // submessages are released first if there are remaining external references. // @@ -248,11 +273,6 @@ int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor, PyObject* FindInitializationErrors(CMessage* self); -// Set the owner field of self and any children of self, recursively. -// Used when self is being released and thus has a new owner (the -// released Message.) -int SetOwner(CMessage* self, const CMessage::OwnerRef& new_owner); - int AssureWritable(CMessage* self); // Returns the message factory for the given message. @@ -344,6 +364,8 @@ extern PyObject* PickleError_class; const Message* PyMessage_GetMessagePointer(PyObject* msg); Message* PyMessage_GetMutableMessagePointer(PyObject* msg); +PyObject* PyMessage_NewMessageOwnedExternally(Message* message, + PyObject* message_factory); bool InitProto2MessageModule(PyObject *m); diff --git a/python/google/protobuf/pyext/repeated_composite_container.cc b/python/google/protobuf/pyext/repeated_composite_container.cc index d8088a1eb7..4188b5f4a1 100644 --- a/python/google/protobuf/pyext/repeated_composite_container.cc +++ b/python/google/protobuf/pyext/repeated_composite_container.cc @@ -46,6 +46,7 @@ #include <google/protobuf/pyext/message_factory.h> #include <google/protobuf/pyext/scoped_pyobject_ptr.h> #include <google/protobuf/reflection.h> +#include <google/protobuf/stubs/map_util.h> #if PY_MAJOR_VERSION >= 3 #define PyInt_Check PyLong_Check @@ -59,22 +60,6 @@ namespace python { namespace repeated_composite_container { -// TODO(tibell): We might also want to check: -// GOOGLE_CHECK_NOTNULL((self)->owner.get()); -#define GOOGLE_CHECK_ATTACHED(self) \ - do { \ - GOOGLE_CHECK_NOTNULL((self)->message); \ - GOOGLE_CHECK_NOTNULL((self)->parent_field_descriptor); \ - } while (0); - -#define GOOGLE_CHECK_RELEASED(self) \ - do { \ - GOOGLE_CHECK((self)->owner.get() == NULL); \ - GOOGLE_CHECK((self)->message == NULL); \ - GOOGLE_CHECK((self)->parent_field_descriptor == NULL); \ - GOOGLE_CHECK((self)->parent == NULL); \ - } while (0); - // --------------------------------------------------------------------- // len() @@ -82,75 +67,28 @@ static Py_ssize_t Length(PyObject* pself) { RepeatedCompositeContainer* self = reinterpret_cast<RepeatedCompositeContainer*>(pself); - Message* message = self->message; - if (message != NULL) { - return message->GetReflection()->FieldSize(*message, - self->parent_field_descriptor); - } else { - // The container has been released (i.e. by a call to Clear() or - // ClearField() on the parent) and thus there's no message. - return PyList_GET_SIZE(self->child_messages); - } -} - -// Returns 0 if successful; returns -1 and sets an exception if -// unsuccessful. -static int UpdateChildMessages(RepeatedCompositeContainer* self) { - if (self->message == NULL) - return 0; - - // A MergeFrom on a parent message could have caused extra messages to be - // added in the underlying protobuf so add them to our list. They can never - // be removed in such a way so there's no need to worry about that. - Py_ssize_t message_length = Length(reinterpret_cast<PyObject*>(self)); - Py_ssize_t child_length = PyList_GET_SIZE(self->child_messages); - Message* message = self->message; - const Reflection* reflection = message->GetReflection(); - for (Py_ssize_t i = child_length; i < message_length; ++i) { - const Message& sub_message = reflection->GetRepeatedMessage( - *(self->message), self->parent_field_descriptor, i); - CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class); - ScopedPyObjectPtr py_cmsg(reinterpret_cast<PyObject*>(cmsg)); - if (cmsg == NULL) { - return -1; - } - cmsg->owner = self->owner; - cmsg->message = const_cast<Message*>(&sub_message); - cmsg->parent = self->parent; - if (PyList_Append(self->child_messages, py_cmsg.get()) < 0) { - return -1; - } - } - return 0; + Message* message = self->parent->message; + return message->GetReflection()->FieldSize(*message, + self->parent_field_descriptor); } // --------------------------------------------------------------------- // add() -static PyObject* AddToAttached(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - GOOGLE_CHECK_ATTACHED(self); - - if (UpdateChildMessages(self) < 0) { - return NULL; - } +PyObject* Add(RepeatedCompositeContainer* self, PyObject* args, + PyObject* kwargs) { if (cmessage::AssureWritable(self->parent) == -1) return NULL; - Message* message = self->message; + Message* message = self->parent->message; Message* sub_message = message->GetReflection()->AddMessage( message, self->parent_field_descriptor, self->child_message_class->py_message_factory->message_factory); - CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class); - if (cmsg == NULL) - return NULL; + CMessage* cmsg = self->parent->BuildSubMessageFromPointer( + self->parent_field_descriptor, sub_message, self->child_message_class); - cmsg->owner = self->owner; - cmsg->message = sub_message; - cmsg->parent = self->parent; if (cmessage::InitAttributes(cmsg, args, kwargs) < 0) { message->GetReflection()->RemoveLast( message, self->parent_field_descriptor); @@ -158,38 +96,7 @@ static PyObject* AddToAttached(RepeatedCompositeContainer* self, return NULL; } - PyObject* py_cmsg = reinterpret_cast<PyObject*>(cmsg); - return py_cmsg; -} - -static PyObject* AddToReleased(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - GOOGLE_CHECK_RELEASED(self); - - // Create a new Message detached from the rest. - PyObject* py_cmsg = PyEval_CallObjectWithKeywords( - self->child_message_class->AsPyObject(), args, kwargs); - return py_cmsg; -} - -PyObject* Add(RepeatedCompositeContainer* self, - PyObject* args, - PyObject* kwargs) { - PyObject* py_cmsg; - if (self->message == nullptr) - py_cmsg = AddToReleased(self, args, kwargs); - else - py_cmsg = AddToAttached(self, args, kwargs); - - if (py_cmsg == NULL) - return NULL; - - if (PyList_Append(self->child_messages, py_cmsg) < 0) { - Py_DECREF(py_cmsg); - return NULL; - } - return py_cmsg; + return cmsg->AsPyObject(); } static PyObject* AddMethod(PyObject* self, PyObject* args, PyObject* kwargs) { @@ -201,31 +108,17 @@ static PyObject* AddMethod(PyObject* self, PyObject* args, PyObject* kwargs) { static PyObject* AddMessage(RepeatedCompositeContainer* self, PyObject* value) { cmessage::AssureWritable(self->parent); - if (UpdateChildMessages(self) < 0) { - return nullptr; - } - PyObject* py_cmsg; - if (self->message == nullptr) { - py_cmsg = AddToReleased(self, nullptr, nullptr); - if (py_cmsg == nullptr) return nullptr; - CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg); - if (ScopedPyObjectPtr(cmessage::MergeFrom(cmsg, value)) == nullptr) { - Py_DECREF(cmsg); - return nullptr; - } - } else { - Message* message = self->message; - const Reflection* reflection = message->GetReflection(); - py_cmsg = AddToAttached(self, nullptr, nullptr); - if (py_cmsg == nullptr) return nullptr; - CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg); - if (ScopedPyObjectPtr(cmessage::MergeFrom(cmsg, value)) == nullptr) { - reflection->RemoveLast( - message, self->parent_field_descriptor); - Py_DECREF(cmsg); - return nullptr; - } + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + py_cmsg = Add(self, nullptr, nullptr); + if (py_cmsg == nullptr) return nullptr; + CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg); + if (ScopedPyObjectPtr(cmessage::MergeFrom(cmsg, value)) == nullptr) { + reflection->RemoveLast( + message, self->parent_field_descriptor); + Py_DECREF(cmsg); + return nullptr; } return py_cmsg; } @@ -238,10 +131,6 @@ static PyObject* AppendMethod(PyObject* pself, PyObject* value) { return nullptr; } - if (PyList_Append(self->child_messages, py_cmsg.get()) < 0) { - return nullptr; - } - Py_RETURN_NONE; } @@ -262,23 +151,18 @@ static PyObject* Insert(PyObject* pself, PyObject* args) { return nullptr; } - if (self->message != nullptr) { - // Swap the element to right position. - Message* message = self->message; - const Reflection* reflection = message->GetReflection(); - const FieldDescriptor* field_descriptor = self->parent_field_descriptor; - Py_ssize_t length = reflection->FieldSize(*message, field_descriptor) - 1; - Py_ssize_t end_index = index; - if (end_index < 0) end_index += length; - if (end_index < 0) end_index = 0; - for (Py_ssize_t i = length; i > end_index; i --) { - reflection->SwapElements(message, field_descriptor, i, i - 1); - } + // Swap the element to right position. + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + const FieldDescriptor* field_descriptor = self->parent_field_descriptor; + Py_ssize_t length = reflection->FieldSize(*message, field_descriptor) - 1; + Py_ssize_t end_index = index; + if (end_index < 0) end_index += length; + if (end_index < 0) end_index = 0; + for (Py_ssize_t i = length; i > end_index; i --) { + reflection->SwapElements(message, field_descriptor, i, i - 1); } - if (PyList_Insert(self->child_messages, index, py_cmsg.get()) < 0) { - return nullptr; - } Py_RETURN_NONE; } @@ -287,9 +171,6 @@ static PyObject* Insert(PyObject* pself, PyObject* args) { PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value) { cmessage::AssureWritable(self->parent); - if (UpdateChildMessages(self) < 0) { - return NULL; - } ScopedPyObjectPtr iter(PyObject_GetIter(value)); if (iter == NULL) { PyErr_SetString(PyExc_TypeError, "Value must be iterable"); @@ -322,9 +203,6 @@ static PyObject* ExtendMethod(PyObject* self, PyObject* value) { } PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other) { - if (UpdateChildMessages(self) < 0) { - return NULL; - } return Extend(self, other); } @@ -332,13 +210,70 @@ static PyObject* MergeFromMethod(PyObject* self, PyObject* other) { return MergeFrom(reinterpret_cast<RepeatedCompositeContainer*>(self), other); } -PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice) { - if (UpdateChildMessages(self) < 0) { +// This function does not check the bounds. +static PyObject* GetItem(RepeatedCompositeContainer* self, Py_ssize_t index, + Py_ssize_t length = -1) { + if (length == -1) { + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + length = reflection->FieldSize(*message, self->parent_field_descriptor); + } + if (index < 0 || index >= length) { + PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index); + return NULL; + } + Message* message = self->parent->message; + Message* sub_message = message->GetReflection()->MutableRepeatedMessage( + message, self->parent_field_descriptor, index); + return self->parent + ->BuildSubMessageFromPointer(self->parent_field_descriptor, sub_message, + self->child_message_class) + ->AsPyObject(); +} + +PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* item) { + Message* message = self->parent->message; + const Reflection* reflection = message->GetReflection(); + Py_ssize_t length = + reflection->FieldSize(*message, self->parent_field_descriptor); + + if (PyIndex_Check(item)) { + Py_ssize_t index; + index = PyNumber_AsSsize_t(item, PyExc_IndexError); + if (index == -1 && PyErr_Occurred()) return NULL; + if (index < 0) index += length; + return GetItem(self, index, length); + } else if (PySlice_Check(item)) { + Py_ssize_t from, to, step, slicelength, cur, i; + PyObject* result; + +#if PY_MAJOR_VERSION >= 3 + if (PySlice_GetIndicesEx(item, + length, &from, &to, &step, &slicelength) == -1) { +#else + if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(item), + length, &from, &to, &step, &slicelength) == -1) { +#endif + return NULL; + } + + if (slicelength <= 0) { + return PyList_New(0); + } else { + result = PyList_New(slicelength); + if (!result) return NULL; + + for (cur = from, i = 0; i < slicelength; cur += step, i++) { + PyList_SET_ITEM(result, i, GetItem(self, cur, length)); + } + + return result; + } + } else { + PyErr_Format(PyExc_TypeError, "indices must be integers, not %.200s", + item->ob_type->tp_name); return NULL; } - // Just forward the call to the subscript-handling function of the - // list containing the child messages. - return PyObject_GetItem(self->child_messages, slice); } static PyObject* SubscriptMethod(PyObject* self, PyObject* slice) { @@ -348,49 +283,13 @@ static PyObject* SubscriptMethod(PyObject* self, PyObject* slice) { int AssignSubscript(RepeatedCompositeContainer* self, PyObject* slice, PyObject* value) { - if (UpdateChildMessages(self) < 0) { - return -1; - } if (value != NULL) { PyErr_SetString(PyExc_TypeError, "does not support assignment"); return -1; } - // Delete from the underlying Message, if any. - if (self->message != nullptr) { - if (cmessage::InternalDeleteRepeatedField(self->message, - self->parent_field_descriptor, - slice, - self->child_messages) < 0) { - return -1; - } - } else { - Py_ssize_t from; - Py_ssize_t to; - Py_ssize_t step; - Py_ssize_t length = Length(reinterpret_cast<PyObject*>(self)); - Py_ssize_t slicelength; - if (PySlice_Check(slice)) { -#if PY_MAJOR_VERSION >= 3 - if (PySlice_GetIndicesEx(slice, - length, &from, &to, &step, &slicelength) == -1) { -#else - if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(slice), - length, &from, &to, &step, &slicelength) == -1) { -#endif - return -1; - } - return PySequence_DelSlice(self->child_messages, from, to); - } else if (PyInt_Check(slice) || PyLong_Check(slice)) { - from = to = PyLong_AsLong(slice); - if (from < 0) { - from = to = length + from; - } - return PySequence_DelItem(self->child_messages, from); - } - } - - return 0; + return cmessage::DeleteRepeatedField(self->parent, + self->parent_field_descriptor, slice); } static int AssignSubscriptMethod(PyObject* self, PyObject* slice, @@ -402,28 +301,33 @@ static int AssignSubscriptMethod(PyObject* self, PyObject* slice, static PyObject* Remove(PyObject* pself, PyObject* value) { RepeatedCompositeContainer* self = reinterpret_cast<RepeatedCompositeContainer*>(pself); + Py_ssize_t len = Length(reinterpret_cast<PyObject*>(self)); - if (UpdateChildMessages(self) < 0) { - return NULL; - } - Py_ssize_t index = PySequence_Index(self->child_messages, value); - if (index == -1) { - return NULL; - } - ScopedPyObjectPtr py_index(PyLong_FromLong(index)); - if (AssignSubscript(self, py_index.get(), NULL) < 0) { - return NULL; + for (Py_ssize_t i = 0; i < len; i++) { + ScopedPyObjectPtr item(GetItem(self, i, len)); + if (item == NULL) { + return NULL; + } + int result = PyObject_RichCompareBool(item.get(), value, Py_EQ); + if (result < 0) { + return NULL; + } + if (result) { + ScopedPyObjectPtr py_index(PyLong_FromSsize_t(i)); + if (AssignSubscript(self, py_index.get(), NULL) < 0) { + return NULL; + } + Py_RETURN_NONE; + } } - Py_RETURN_NONE; + PyErr_SetString(PyExc_ValueError, "Item to delete not in list"); + return NULL; } static PyObject* RichCompare(PyObject* pself, PyObject* other, int opid) { RepeatedCompositeContainer* self = reinterpret_cast<RepeatedCompositeContainer*>(pself); - if (UpdateChildMessages(self) < 0) { - return NULL; - } if (!PyObject_TypeCheck(other, &RepeatedCompositeContainer_Type)) { PyErr_SetString(PyExc_TypeError, "Can only compare repeated composite fields " @@ -469,8 +373,9 @@ static PyObject* ToStr(PyObject* pself) { // --------------------------------------------------------------------- // sort() -static void ReorderAttached(RepeatedCompositeContainer* self) { - Message* message = self->message; +static void ReorderAttached(RepeatedCompositeContainer* self, + PyObject* child_list) { + Message* message = self->parent->message; const Reflection* reflection = message->GetReflection(); const FieldDescriptor* descriptor = self->parent_field_descriptor; const Py_ssize_t length = Length(reinterpret_cast<PyObject*>(self)); @@ -483,7 +388,7 @@ static void ReorderAttached(RepeatedCompositeContainer* self) { for (Py_ssize_t i = 0; i < length; ++i) { CMessage* py_cmsg = reinterpret_cast<CMessage*>( - PyList_GET_ITEM(self->child_messages, i)); + PyList_GET_ITEM(child_list, i)); reflection->AddAllocatedMessage(message, descriptor, py_cmsg->message); } } @@ -493,14 +398,17 @@ static void ReorderAttached(RepeatedCompositeContainer* self) { static int SortPythonMessages(RepeatedCompositeContainer* self, PyObject* args, PyObject* kwds) { - ScopedPyObjectPtr m(PyObject_GetAttrString(self->child_messages, "sort")); + ScopedPyObjectPtr child_list( + PySequence_List(reinterpret_cast<PyObject*>(self))); + if (child_list == NULL) { + return -1; + } + ScopedPyObjectPtr m(PyObject_GetAttrString(child_list.get(), "sort")); if (m == NULL) return -1; if (ScopedPyObjectPtr(PyObject_Call(m.get(), args, kwds)) == NULL) return -1; - if (self->message != NULL) { - ReorderAttached(self); - } + ReorderAttached(self, child_list.get()); return 0; } @@ -520,9 +428,6 @@ static PyObject* Sort(PyObject* pself, PyObject* args, PyObject* kwds) { } } - if (UpdateChildMessages(self) < 0) { - return NULL; - } if (SortPythonMessages(self, args, kwds) < 0) { return NULL; } @@ -534,20 +439,7 @@ static PyObject* Sort(PyObject* pself, PyObject* args, PyObject* kwds) { static PyObject* Item(PyObject* pself, Py_ssize_t index) { RepeatedCompositeContainer* self = reinterpret_cast<RepeatedCompositeContainer*>(pself); - - if (UpdateChildMessages(self) < 0) { - return NULL; - } - Py_ssize_t length = Length(pself); - if (index < 0) { - index = length + index; - } - PyObject* item = PyList_GetItem(self->child_messages, index); - if (item == NULL) { - return NULL; - } - Py_INCREF(item); - return item; + return GetItem(self, index); } static PyObject* Pop(PyObject* pself, PyObject* args) { @@ -558,9 +450,10 @@ static PyObject* Pop(PyObject* pself, PyObject* args) { if (!PyArg_ParseTuple(args, "|n", &index)) { return NULL; } - PyObject* item = Item(pself, index); + Py_ssize_t length = Length(pself); + if (index < 0) index += length; + PyObject* item = GetItem(self, index, length); if (item == NULL) { - PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index); return NULL; } ScopedPyObjectPtr py_index(PyLong_FromSsize_t(index)); @@ -570,106 +463,15 @@ static PyObject* Pop(PyObject* pself, PyObject* args) { return item; } -// Release field of parent message and transfer the ownership to target. -void ReleaseLastTo(Message* message, - const FieldDescriptor* field, - CMessage* target) { - GOOGLE_CHECK(message != nullptr); - GOOGLE_CHECK(field != nullptr); - GOOGLE_CHECK(target != nullptr); - - CMessage::OwnerRef released_message( - message->GetReflection()->ReleaseLast(message, field)); - // TODO(tibell): Deal with proto1. - - target->parent = NULL; - target->parent_field_descriptor = NULL; - target->message = released_message.get(); - target->read_only = false; - cmessage::SetOwner(target, released_message); -} - -// Called to release a container using -// ClearField('container_field_name') on the parent. -int Release(RepeatedCompositeContainer* self) { - if (UpdateChildMessages(self) < 0) { - PyErr_WriteUnraisable(PyBytes_FromString("Failed to update released " - "messages")); - return -1; - } - - Message* message = self->message; - const FieldDescriptor* field = self->parent_field_descriptor; - - // The reflection API only lets us release the last message in a - // repeated field. Therefore we iterate through the children - // starting with the last one. - const Py_ssize_t size = PyList_GET_SIZE(self->child_messages); - GOOGLE_DCHECK_EQ(size, message->GetReflection()->FieldSize(*message, field)); - for (Py_ssize_t i = size - 1; i >= 0; --i) { - CMessage* child_cmessage = reinterpret_cast<CMessage*>( - PyList_GET_ITEM(self->child_messages, i)); - ReleaseLastTo(message, field, child_cmessage); - } - - // Detach from containing message. - self->parent = NULL; - self->parent_field_descriptor = NULL; - self->message = NULL; - self->owner.reset(); - - return 0; -} - PyObject* DeepCopy(PyObject* pself, PyObject* arg) { - RepeatedCompositeContainer* self = - reinterpret_cast<RepeatedCompositeContainer*>(pself); - - ScopedPyObjectPtr cloneObj( - PyType_GenericAlloc(&RepeatedCompositeContainer_Type, 0)); - if (cloneObj == NULL) { - return NULL; - } - RepeatedCompositeContainer* clone = - reinterpret_cast<RepeatedCompositeContainer*>(cloneObj.get()); - - Message* new_message = self->message->New(); - clone->parent = NULL; - clone->parent_field_descriptor = self->parent_field_descriptor; - clone->message = new_message; - clone->owner.reset(new_message); - Py_INCREF(self->child_message_class); - clone->child_message_class = self->child_message_class; - clone->child_messages = PyList_New(0); - - new_message->GetReflection() - ->GetMutableRepeatedFieldRef<Message>(new_message, - self->parent_field_descriptor) - .MergeFrom(self->message->GetReflection()->GetRepeatedFieldRef<Message>( - *self->message, self->parent_field_descriptor)); - return cloneObj.release(); -} - -int SetOwner(RepeatedCompositeContainer* self, - const CMessage::OwnerRef& new_owner) { - GOOGLE_CHECK_ATTACHED(self); - - self->owner = new_owner; - const Py_ssize_t n = PyList_GET_SIZE(self->child_messages); - for (Py_ssize_t i = 0; i < n; ++i) { - PyObject* msg = PyList_GET_ITEM(self->child_messages, i); - if (cmessage::SetOwner(reinterpret_cast<CMessage*>(msg), new_owner) == -1) { - return -1; - } - } - return 0; + return reinterpret_cast<RepeatedCompositeContainer*>(pself)->DeepCopy(); } // The private constructor of RepeatedCompositeContainer objects. -PyObject *NewContainer( +RepeatedCompositeContainer *NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, - CMessageClass* concrete_class) { + CMessageClass* child_message_class) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; } @@ -681,27 +483,19 @@ PyObject *NewContainer( return NULL; } - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; - Py_INCREF(concrete_class); - self->child_message_class = concrete_class; - self->child_messages = PyList_New(0); - - return reinterpret_cast<PyObject*>(self); + Py_INCREF(child_message_class); + self->child_message_class = child_message_class; + return self; } static void Dealloc(PyObject* pself) { RepeatedCompositeContainer* self = reinterpret_cast<RepeatedCompositeContainer*>(pself); - - Py_CLEAR(self->child_messages); + self->RemoveFromParentCache(); Py_CLEAR(self->child_message_class); - // TODO(tibell): Do we need to call delete on these objects to make - // sure their destructors are called? - self->owner.reset(); - Py_TYPE(self)->tp_free(pself); } diff --git a/python/google/protobuf/pyext/repeated_composite_container.h b/python/google/protobuf/pyext/repeated_composite_container.h index d075577145..e241827ef5 100644 --- a/python/google/protobuf/pyext/repeated_composite_container.h +++ b/python/google/protobuf/pyext/repeated_composite_container.h @@ -52,46 +52,11 @@ namespace python { struct CMessageClass; -// A RepeatedCompositeContainer can be in one of two states: attached -// or released. -// -// When in the attached state all modifications to the container are -// done both on the 'message' and on the 'child_messages' -// list. In this state all Messages referred to by the children in -// 'child_messages' are owner by the 'owner'. -// -// When in the released state 'message', 'owner', 'parent', and -// 'parent_field_descriptor' are NULL. -typedef struct RepeatedCompositeContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python RepeatedCompositeContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Weak reference to parent object. May be NULL. Used to make sure - // the parent is writable before modifying the - // RepeatedCompositeContainer. - CMessage* parent; - - // A descriptor used to modify the underlying 'message'. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; - - // Pointer to the C++ Message that contains this container. The - // RepeatedCompositeContainer does not own this pointer. - // - // If NULL, this message has been released from its parent (by - // calling Clear() or ClearField() on the parent. - Message* message; - +// A RepeatedCompositeContainer always has a parent message. +// The parent message also caches reference to items of the container. +typedef struct RepeatedCompositeContainer : public ContainerBase { // The type used to create new child messages. CMessageClass* child_message_class; - - // A list of child messages. - PyObject* child_messages; } RepeatedCompositeContainer; extern PyTypeObject RepeatedCompositeContainer_Type; @@ -100,7 +65,7 @@ namespace repeated_composite_container { // Builds a RepeatedCompositeContainer object, from a parent message and a // field descriptor. -PyObject *NewContainer( +RepeatedCompositeContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor, CMessageClass *child_message_class); @@ -139,25 +104,6 @@ PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice); int AssignSubscript(RepeatedCompositeContainer* self, PyObject* slice, PyObject* value); - -// Releases the messages in the container to a new message. -// -// Returns 0 on success, -1 on failure. -int Release(RepeatedCompositeContainer* self); - -// Returns 0 on success, -1 on failure. -int SetOwner(RepeatedCompositeContainer* self, - const CMessage::OwnerRef& new_owner); - -// Removes the last element of the repeated message field 'field' on -// the Message 'parent', and transfers the ownership of the released -// Message to 'target'. -// -// Corresponds to reflection api method ReleaseMessage. -void ReleaseLastTo(Message* message, - const FieldDescriptor* field, - CMessage* target); - } // namespace repeated_composite_container } // namespace python } // namespace protobuf diff --git a/python/google/protobuf/pyext/repeated_scalar_container.cc b/python/google/protobuf/pyext/repeated_scalar_container.cc index ac06cff3aa..07ac602832 100644 --- a/python/google/protobuf/pyext/repeated_scalar_container.cc +++ b/python/google/protobuf/pyext/repeated_scalar_container.cc @@ -63,8 +63,8 @@ namespace repeated_scalar_container { static int InternalAssignRepeatedField( RepeatedScalarContainer* self, PyObject* list) { - self->message->GetReflection()->ClearField(self->message, - self->parent_field_descriptor); + Message* message = self->parent->message; + message->GetReflection()->ClearField(message, self->parent_field_descriptor); for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); ++i) { PyObject* value = PyList_GET_ITEM(list, i); if (ScopedPyObjectPtr(Append(self, value)) == NULL) { @@ -77,7 +77,7 @@ static int InternalAssignRepeatedField( static Py_ssize_t Len(PyObject* pself) { RepeatedScalarContainer* self = reinterpret_cast<RepeatedScalarContainer*>(pself); - Message* message = self->message; + Message* message = self->parent->message; return message->GetReflection()->FieldSize(*message, self->parent_field_descriptor); } @@ -87,7 +87,7 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) { reinterpret_cast<RepeatedScalarContainer*>(pself); cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -104,9 +104,8 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) { if (arg == NULL) { ScopedPyObjectPtr py_index(PyLong_FromLong(index)); - return cmessage::InternalDeleteRepeatedField(self->message, - field_descriptor, - py_index.get(), NULL); + return cmessage::DeleteRepeatedField(self->parent, field_descriptor, + py_index.get()); } if (PySequence_Check(arg) && !(PyBytes_Check(arg) || PyUnicode_Check(arg))) { @@ -193,7 +192,7 @@ static PyObject* Item(PyObject* pself, Py_ssize_t index) { RepeatedScalarContainer* self = reinterpret_cast<RepeatedScalarContainer*>(pself); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -343,7 +342,7 @@ static PyObject* Subscript(PyObject* pself, PyObject* slice) { PyObject* Append(RepeatedScalarContainer* self, PyObject* item) { cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; const Reflection* reflection = message->GetReflection(); @@ -437,7 +436,7 @@ static int AssSubscript(PyObject* pself, PyObject* slice, PyObject* value) { bool create_list = false; cmessage::AssureWritable(self->parent); - Message* message = self->message; + Message* message = self->parent->message; const FieldDescriptor* field_descriptor = self->parent_field_descriptor; @@ -467,8 +466,7 @@ static int AssSubscript(PyObject* pself, PyObject* slice, PyObject* value) { } if (value == NULL) { - return cmessage::InternalDeleteRepeatedField( - self->message, field_descriptor, slice, nullptr); + return cmessage::DeleteRepeatedField(self->parent, field_descriptor, slice); } if (!create_list) { @@ -669,7 +667,7 @@ static PyObject* MergeFrom(PyObject* pself, PyObject* arg) { } // The private constructor of RepeatedScalarContainer objects. -PyObject *NewContainer( +RepeatedScalarContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor) { if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) { return NULL; @@ -681,72 +679,20 @@ PyObject *NewContainer( return NULL; } - self->message = parent->message; + Py_INCREF(parent); self->parent = parent; self->parent_field_descriptor = parent_field_descriptor; - self->owner = parent->owner; - - return reinterpret_cast<PyObject*>(self); -} - -// Initializes the underlying Message object of "to" so it becomes a new parent -// repeated scalar, and copies all the values from "from" to it. A child scalar -// container can be released by passing it as both from and to (e.g. making it -// the recipient of the new parent message and copying the values from itself). -static int InitializeAndCopyToParentContainer( - RepeatedScalarContainer* from, - RepeatedScalarContainer* to) { - ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL)); - if (full_slice == NULL) { - return -1; - } - ScopedPyObjectPtr values( - Subscript(reinterpret_cast<PyObject*>(from), full_slice.get())); - if (values == NULL) { - return -1; - } - Message* new_message = from->message->New(); - to->parent = NULL; - to->parent_field_descriptor = from->parent_field_descriptor; - to->message = new_message; - to->owner.reset(new_message); - if (InternalAssignRepeatedField(to, values.get()) < 0) { - return -1; - } - return 0; -} -int Release(RepeatedScalarContainer* self) { - return InitializeAndCopyToParentContainer(self, self); + return self; } PyObject* DeepCopy(PyObject* pself, PyObject* arg) { - RepeatedScalarContainer* self = - reinterpret_cast<RepeatedScalarContainer*>(pself); - - RepeatedScalarContainer* clone = reinterpret_cast<RepeatedScalarContainer*>( - PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0)); - if (clone == NULL) { - return NULL; - } - - if (InitializeAndCopyToParentContainer(self, clone) < 0) { - Py_DECREF(clone); - return NULL; - } - return reinterpret_cast<PyObject*>(clone); + return reinterpret_cast<RepeatedScalarContainer*>(pself)->DeepCopy(); } static void Dealloc(PyObject* pself) { - RepeatedScalarContainer* self = - reinterpret_cast<RepeatedScalarContainer*>(pself); - self->owner.reset(); - Py_TYPE(self)->tp_free(pself); -} - -void SetOwner(RepeatedScalarContainer* self, - const CMessage::OwnerRef& new_owner) { - self->owner = new_owner; + reinterpret_cast<RepeatedScalarContainer*>(pself)->RemoveFromParentCache(); + Py_TYPE(pself)->tp_free(pself); } static PySequenceMethods SqMethods = { diff --git a/python/google/protobuf/pyext/repeated_scalar_container.h b/python/google/protobuf/pyext/repeated_scalar_container.h index 4dcecbac98..f9f0ea8f31 100644 --- a/python/google/protobuf/pyext/repeated_scalar_container.h +++ b/python/google/protobuf/pyext/repeated_scalar_container.h @@ -45,30 +45,7 @@ namespace google { namespace protobuf { namespace python { -typedef struct RepeatedScalarContainer { - PyObject_HEAD; - - // This is the top-level C++ Message object that owns the whole - // proto tree. Every Python RepeatedScalarContainer holds a - // reference to it in order to keep it alive as long as there's a - // Python object that references any part of the tree. - CMessage::OwnerRef owner; - - // Pointer to the C++ Message that contains this container. The - // RepeatedScalarContainer does not own this pointer. - Message* message; - - // Weak reference to a parent CMessage object (i.e. may be NULL.) - // - // Used to make sure all ancestors are also mutable when first - // modifying the container. - CMessage* parent; - - // Pointer to the parent's descriptor that describes this - // field. Used together with the parent's message when making a - // default message instance mutable. - // The pointer is owned by the global DescriptorPool. - const FieldDescriptor* parent_field_descriptor; +typedef struct RepeatedScalarContainer : public ContainerBase { } RepeatedScalarContainer; extern PyTypeObject RepeatedScalarContainer_Type; @@ -77,7 +54,7 @@ namespace repeated_scalar_container { // Builds a RepeatedScalarContainer object, from a parent message and a // field descriptor. -extern PyObject *NewContainer( +extern RepeatedScalarContainer* NewContainer( CMessage* parent, const FieldDescriptor* parent_field_descriptor); // Appends the scalar 'item' to the end of the container 'self'. @@ -86,21 +63,12 @@ extern PyObject *NewContainer( // unsuccessful. PyObject* Append(RepeatedScalarContainer* self, PyObject* item); -// Releases the messages in the container to a new message. -// -// Returns 0 on success, -1 on failure. -int Release(RepeatedScalarContainer* self); - // Appends all the elements in the input iterator to the container. // // Returns None if successful; returns NULL and sets an exception if // unsuccessful. PyObject* Extend(RepeatedScalarContainer* self, PyObject* value); -// Set the owner field of self and any children of self. -void SetOwner(RepeatedScalarContainer* self, - const CMessage::OwnerRef& new_owner); - } // namespace repeated_scalar_container } // namespace python } // namespace protobuf diff --git a/python/google/protobuf/pyext/thread_unsafe_shared_ptr.h b/python/google/protobuf/pyext/thread_unsafe_shared_ptr.h deleted file mode 100644 index 79fa9e3d7f..0000000000 --- a/python/google/protobuf/pyext/thread_unsafe_shared_ptr.h +++ /dev/null @@ -1,104 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// ThreadUnsafeSharedPtr<T> is the same as shared_ptr<T> without the locking -// overhread (and thread-safety). -#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ -#define GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ - -#include <algorithm> -#include <utility> - -#include <google/protobuf/stubs/logging.h> -#include <google/protobuf/stubs/common.h> - -namespace google { -namespace protobuf { -namespace python { - -template <typename T> -class ThreadUnsafeSharedPtr { - public: - // Takes ownership. - explicit ThreadUnsafeSharedPtr(T* ptr) - : ptr_(ptr), refcount_(ptr ? new RefcountT(1) : nullptr) { - } - - ThreadUnsafeSharedPtr(const ThreadUnsafeSharedPtr& other) - : ThreadUnsafeSharedPtr(nullptr) { - *this = other; - } - - ThreadUnsafeSharedPtr& operator=(const ThreadUnsafeSharedPtr& other) { - if (other.refcount_ == refcount_) { - return *this; - } - this->~ThreadUnsafeSharedPtr(); - ptr_ = other.ptr_; - refcount_ = other.refcount_; - if (refcount_) { - ++*refcount_; - } - return *this; - } - - ~ThreadUnsafeSharedPtr() { - if (refcount_ == nullptr) { - GOOGLE_DCHECK(ptr_ == nullptr); - return; - } - if (--*refcount_ == 0) { - delete refcount_; - delete ptr_; - } - } - - void reset(T* ptr = nullptr) { *this = ThreadUnsafeSharedPtr(ptr); } - - T* get() { return ptr_; } - const T* get() const { return ptr_; } - - void swap(ThreadUnsafeSharedPtr& other) { - using std::swap; - swap(ptr_, other.ptr_); - swap(refcount_, other.refcount_); - } - - private: - typedef int RefcountT; - T* ptr_; - RefcountT* refcount_; -}; - -} // namespace python -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_PYTHON_CPP_THREAD_UNSAFE_SHARED_PTR_H__ diff --git a/python/google/protobuf/pyext/unknown_fields.cc b/python/google/protobuf/pyext/unknown_fields.cc index 760452f2fe..c3679c0d36 100755 --- a/python/google/protobuf/pyext/unknown_fields.cc +++ b/python/google/protobuf/pyext/unknown_fields.cc @@ -137,6 +137,8 @@ static void Dealloc(PyObject* pself) { if (PyObject_TypeCheck(self->parent, &PyUnknownFields_Type)) { reinterpret_cast<PyUnknownFields*>( self->parent)->sub_unknown_fields.erase(self); + } else { + reinterpret_cast<CMessage*>(self->parent)->unknown_field_set = nullptr; } Py_CLEAR(self->parent); self->~PyUnknownFields(); diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py index 557c7e2852..72116622e0 100755 --- a/python/google/protobuf/text_format.py +++ b/python/google/protobuf/text_format.py @@ -584,7 +584,8 @@ def Parse(text, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. NOTE: for historical reasons this function does not clear the input @@ -610,6 +611,9 @@ def Parse(text, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -621,14 +625,16 @@ def Parse(text, message, allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) def Merge(text, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Like Parse(), but allows repeated values for a non-repeated field, and uses @@ -641,6 +647,9 @@ def Merge(text, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -653,14 +662,16 @@ def Merge(text, message, allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) def ParseLines(lines, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Args: @@ -670,6 +681,9 @@ def ParseLines(lines, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -679,7 +693,8 @@ def ParseLines(lines, """ parser = _Parser(allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) return parser.ParseLines(lines, message) @@ -687,7 +702,8 @@ def MergeLines(lines, message, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): """Parses a text representation of a protocol message into a message. Like ParseLines(), but allows repeated values for a non-repeated field, and @@ -700,6 +716,9 @@ def MergeLines(lines, parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. + allow_unknown_field: if True, skip over unknown field and keep + parsing. Avoid to use this option if possible. It may hide some + errors (e.g. spelling error on field name) Returns: The same message passed as argument. @@ -709,7 +728,8 @@ def MergeLines(lines, """ parser = _Parser(allow_unknown_extension, allow_field_number, - descriptor_pool=descriptor_pool) + descriptor_pool=descriptor_pool, + allow_unknown_field=allow_unknown_field) return parser.MergeLines(lines, message) @@ -719,10 +739,12 @@ class _Parser(object): def __init__(self, allow_unknown_extension=False, allow_field_number=False, - descriptor_pool=None): + descriptor_pool=None, + allow_unknown_field=False): self.allow_unknown_extension = allow_unknown_extension self.allow_field_number = allow_field_number self.descriptor_pool = descriptor_pool + self.allow_unknown_field = allow_unknown_field def ParseLines(self, lines, message): """Parses a text representation of a protocol message into a message.""" @@ -844,7 +866,7 @@ class _Parser(object): field.message_type.name != name): field = None - if not field: + if not field and not self.allow_unknown_field: raise tokenizer.ParseErrorPreviousToken( 'Message type "%s" has no field named "%s".' % (message_descriptor.full_name, name)) @@ -883,7 +905,7 @@ class _Parser(object): merger(tokenizer, message, field) else: # Proto field is unknown. - assert self.allow_unknown_extension + assert (self.allow_unknown_extension or self.allow_unknown_field) _SkipFieldContents(tokenizer) # For historical reasons, fields may optionally be separated by commas or diff --git a/src/Makefile.am b/src/Makefile.am index 6f64b96e27..fabc23a3c2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -182,8 +182,8 @@ libprotobuf_lite_la_SOURCES = \ google/protobuf/stubs/hash.h \ google/protobuf/stubs/int128.cc \ google/protobuf/stubs/int128.h \ - google/protobuf/stubs/io_win32.cc \ - google/protobuf/stubs/io_win32.h \ + google/protobuf/io/io_win32.cc \ + google/protobuf/io/io_win32.h \ google/protobuf/stubs/map_util.h \ google/protobuf/stubs/mathutil.h \ google/protobuf/stubs/status.cc \ @@ -730,7 +730,7 @@ protobuf_test_SOURCES = \ google/protobuf/stubs/bytestream_unittest.cc \ google/protobuf/stubs/common_unittest.cc \ google/protobuf/stubs/int128_unittest.cc \ - google/protobuf/stubs/io_win32_unittest.cc \ + google/protobuf/io/io_win32_unittest.cc \ google/protobuf/stubs/statusor_test.cc \ google/protobuf/stubs/status_test.cc \ google/protobuf/stubs/stringpiece_unittest.cc \ diff --git a/src/google/protobuf/any.cc b/src/google/protobuf/any.cc index 4b3cce622c..a79214b750 100644 --- a/src/google/protobuf/any.cc +++ b/src/google/protobuf/any.cc @@ -64,16 +64,16 @@ bool AnyMetadata::UnpackTo(Message* message) const { bool GetAnyFieldDescriptors(const Message& message, const FieldDescriptor** type_url_field, const FieldDescriptor** value_field) { - const Descriptor* descriptor = message.GetDescriptor(); - if (descriptor->full_name() != kAnyFullTypeName) { - return false; - } - *type_url_field = descriptor->FindFieldByNumber(1); - *value_field = descriptor->FindFieldByNumber(2); - return (*type_url_field != NULL && - (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && - *value_field != NULL && - (*value_field)->type() == FieldDescriptor::TYPE_BYTES); + const Descriptor* descriptor = message.GetDescriptor(); + if (descriptor->full_name() != kAnyFullTypeName) { + return false; + } + *type_url_field = descriptor->FindFieldByNumber(1); + *value_field = descriptor->FindFieldByNumber(2); + return (*type_url_field != NULL && + (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && + *value_field != NULL && + (*value_field)->type() == FieldDescriptor::TYPE_BYTES); } } // namespace internal diff --git a/src/google/protobuf/any.pb.cc b/src/google/protobuf/any.pb.cc index 3515411f4e..404de6fc7b 100644 --- a/src/google/protobuf/any.pb.cc +++ b/src/google/protobuf/any.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsAny_google_2fprotobuf_2fany_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Any_google_2fprotobuf_2fany_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsAny_google_2fprotobuf_2fany_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fany_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Any_google_2fprotobuf_2fany_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fany_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fany_2eproto = nullptr; @@ -76,8 +72,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fany_2eproto[] = "roto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fany_2eproto = { - false, InitDefaults_google_2fprotobuf_2fany_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fany_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fany_2eproto, "google/protobuf/any.proto", &assign_descriptors_table_google_2fprotobuf_2fany_2eproto, 205, }; @@ -85,6 +80,7 @@ void AddDescriptors_google_2fprotobuf_2fany_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Any_google_2fprotobuf_2fany_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fany_2eproto, deps, 0); } @@ -190,38 +186,44 @@ void Any::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Any::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string type_url = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_type_url(), ptr, ctx, "google.protobuf.Any.type_url"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // bytes value = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(mutable_value(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Any::MergePartialFromCodedStream( diff --git a/src/google/protobuf/any_test.cc b/src/google/protobuf/any_test.cc index 72bd5e0d19..4fbf0b630e 100644 --- a/src/google/protobuf/any_test.cc +++ b/src/google/protobuf/any_test.cc @@ -166,4 +166,3 @@ TEST(AnyTest, MoveAssignment) { } // namespace } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/api.pb.cc b/src/google/protobuf/api.pb.cc index 1b56305bee..c9e2123698 100644 --- a/src/google/protobuf/api.pb.cc +++ b/src/google/protobuf/api.pb.cc @@ -81,12 +81,6 @@ static void InitDefaultsMixin_google_2fprotobuf_2fapi_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Mixin_google_2fprotobuf_2fapi_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsMixin_google_2fprotobuf_2fapi_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fapi_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fapi_2eproto[3]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fapi_2eproto = nullptr; @@ -164,8 +158,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto[] = "rotobuf.WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fapi_2eproto = { - false, InitDefaults_google_2fprotobuf_2fapi_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fapi_2eproto, "google/protobuf/api.proto", &assign_descriptors_table_google_2fprotobuf_2fapi_2eproto, 750, }; @@ -175,6 +168,9 @@ void AddDescriptors_google_2fprotobuf_2fapi_2eproto() { ::AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, ::AddDescriptors_google_2fprotobuf_2ftype_2eproto, }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Api_google_2fprotobuf_2fapi_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Method_google_2fprotobuf_2fapi_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Mixin_google_2fprotobuf_2fapi_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fapi_2eproto, deps, 2); } @@ -296,67 +292,74 @@ void Api::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Api::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Api.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Method methods = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_methods(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // repeated .google.protobuf.Option options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } // string version = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_version(), ptr, ctx, "google.protobuf.Api.version"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.SourceContext source_context = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; ptr = ctx->ParseMessage(mutable_source_context(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Mixin mixins = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_mixins(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 50 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 50) break; + ptr += 1; + } break; } // .google.protobuf.Syntax syntax = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 56) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_syntax(static_cast<PROTOBUF_NAMESPACE_ID::Syntax>(val)); break; } @@ -364,15 +367,20 @@ const char* Api::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::intern handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Api::MergePartialFromCodedStream( @@ -898,61 +906,64 @@ void Method::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Method::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Method.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // string request_type_url = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_request_type_url(), ptr, ctx, "google.protobuf.Method.request_type_url"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // bool request_streaming = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_request_streaming(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + request_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // string response_type_url = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_response_type_url(), ptr, ctx, "google.protobuf.Method.response_type_url"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // bool response_streaming = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; - set_response_streaming(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + response_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Option options = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 50 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 50) break; + ptr += 1; + } break; } // .google.protobuf.Syntax syntax = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 56) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_syntax(static_cast<PROTOBUF_NAMESPACE_ID::Syntax>(val)); break; } @@ -960,15 +971,20 @@ const char* Method::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Method::MergePartialFromCodedStream( @@ -1464,38 +1480,44 @@ void Mixin::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Mixin::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Mixin.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // string root = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_root(), ptr, ctx, "google.protobuf.Mixin.root"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Mixin::MergePartialFromCodedStream( diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index eb3a1dda44..0171c56168 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -31,6 +31,7 @@ #include <google/protobuf/arena.h> #include <algorithm> +#include <atomic> #include <limits> #include <google/protobuf/stubs/mutex.h> @@ -49,7 +50,7 @@ namespace protobuf { namespace internal { -std::atomic<int64> ArenaImpl::lifecycle_id_generator_; +std::atomic<LifecycleId> ArenaImpl::lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { static internal::ThreadLocalStorage<ThreadCache>* thread_cache_ = @@ -58,7 +59,7 @@ ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { } #elif defined(PROTOBUF_USE_DLLS) ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { - static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL }; + static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = {-1, NULL}; return thread_cache_; } #else @@ -242,7 +243,7 @@ uint64 ArenaImpl::SpaceAllocated() const { uint64 ArenaImpl::SpaceUsed() const { SerialArena* serial = threads_.load(std::memory_order_acquire); uint64 space_used = 0; - for ( ; serial; serial = serial->next()) { + for (; serial; serial = serial->next()) { space_used += serial->SpaceUsed(); } return space_used; @@ -286,7 +287,7 @@ uint64 ArenaImpl::SerialArena::Free(ArenaImpl::SerialArena* serial, // We have to be careful in this function, since we will be freeing the Block // that contains this SerialArena. Be careful about accessing |serial|. - for (Block* b = serial->head_; b; ) { + for (Block* b = serial->head_; b;) { // This is inside the block we are freeing, so we need to read it now. Block* next_block = b->next(); space_allocated += (b->size()); @@ -312,7 +313,7 @@ void ArenaImpl::CleanupList() { // properly synchronize Reset() or the destructor will throw a TSAN warning. SerialArena* serial = threads_.load(std::memory_order_relaxed); - for ( ; serial; serial = serial->next()) { + for (; serial; serial = serial->next()) { serial->CleanupList(); } } @@ -365,7 +366,7 @@ PROTOBUF_NOINLINE ArenaImpl::SerialArena* ArenaImpl::GetSerialArenaFallback(void* me) { // Look for this SerialArena in our linked list. SerialArena* serial = threads_.load(std::memory_order_acquire); - for ( ; serial; serial = serial->next()) { + for (; serial; serial = serial->next()) { if (serial->owner() == me) { break; } diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index 3a9feb5b34..52f3fb9505 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -72,8 +72,8 @@ struct ArenaOptions; // defined below namespace google { namespace protobuf { -class Arena; // defined below -class Message; // defined in message.h +class Arena; // defined below +class Message; // defined in message.h class MessageLite; namespace arena_metrics { @@ -84,8 +84,8 @@ void EnableArenaMetrics(ArenaOptions* options); namespace internal { -struct ArenaStringPtr; // defined in arenastring.h -class LazyField; // defined in lazy_field.h +struct ArenaStringPtr; // defined in arenastring.h +class LazyField; // defined in lazy_field.h template <typename Type> class GenericTypeHandler; // defined in repeated_field.h @@ -402,8 +402,8 @@ class PROTOBUF_EXPORT Arena final { // will be manually called when the arena is destroyed or reset. This differs // from OwnDestructor() in that any member function may be specified, not only // the class destructor. - PROTOBUF_NOINLINE void OwnCustomDestructor( - void* object, void (*destruct)(void*)) { + PROTOBUF_NOINLINE void OwnCustomDestructor(void* object, + void (*destruct)(void*)) { impl_.AddCleanup(object, destruct); } diff --git a/src/google/protobuf/arena_impl.h b/src/google/protobuf/arena_impl.h index 4ba595417a..0beec171f7 100644 --- a/src/google/protobuf/arena_impl.h +++ b/src/google/protobuf/arena_impl.h @@ -55,6 +55,8 @@ inline size_t AlignUpTo8(size_t n) { return (n + 7) & static_cast<size_t>(-8); } +using LifecycleId = int64_t; + // This class provides the core Arena memory allocation library. Different // implementations only need to implement the public interface below. // Arena is not a template type as that would only be useful if all protos @@ -73,12 +75,12 @@ class PROTOBUF_EXPORT ArenaImpl { template <typename O> explicit Options(const O& options) - : start_block_size(options.start_block_size), - max_block_size(options.max_block_size), - initial_block(options.initial_block), - initial_block_size(options.initial_block_size), - block_alloc(options.block_alloc), - block_dealloc(options.block_dealloc) {} + : start_block_size(options.start_block_size), + max_block_size(options.max_block_size), + initial_block(options.initial_block), + initial_block_size(options.initial_block_size), + block_alloc(options.block_alloc), + block_dealloc(options.block_dealloc) {} }; template <typename O> @@ -193,11 +195,11 @@ class PROTOBUF_EXPORT ArenaImpl { void AddCleanupFallback(void* elem, void (*cleanup)(void*)); void CleanupListFallback(); - ArenaImpl* arena_; // Containing arena. - void* owner_; // &ThreadCache of this thread; - Block* head_; // Head of linked list of blocks. - CleanupChunk* cleanup_; // Head of cleanup list. - SerialArena* next_; // Next SerialArena in this linked list. + ArenaImpl* arena_; // Containing arena. + void* owner_; // &ThreadCache of this thread; + Block* head_; // Head of linked list of blocks. + CleanupChunk* cleanup_; // Head of cleanup list. + SerialArena* next_; // Next SerialArena in this linked list. // Next pointer to allocate from. Always 8-byte aligned. Points inside // head_ (and head_->pos will always be non-canonical). We keep these @@ -227,7 +229,7 @@ class PROTOBUF_EXPORT ArenaImpl { void set_pos(size_t pos) { pos_ = pos; } private: - Block* next_; // Next block for this thread. + Block* next_; // Next block for this thread. size_t pos_; size_t size_; // data follows @@ -243,10 +245,10 @@ class PROTOBUF_EXPORT ArenaImpl { // The ThreadCache is considered valid as long as this matches the // lifecycle_id of the arena being used. - int64 last_lifecycle_id_seen; + LifecycleId last_lifecycle_id_seen; SerialArena* last_serial_arena; }; - static std::atomic<int64> lifecycle_id_generator_; + static std::atomic<LifecycleId> lifecycle_id_generator_; #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) // Android ndk does not support GOOGLE_THREAD_LOCAL keyword so we use a custom thread // local storage class we implemented. @@ -279,21 +281,20 @@ class PROTOBUF_EXPORT ArenaImpl { hint_.store(serial, std::memory_order_release); } - std::atomic<SerialArena*> threads_; // Pointer to a linked list of SerialArena. std::atomic<SerialArena*> hint_; // Fast thread-local block access std::atomic<size_t> space_allocated_; // Total size of all allocated blocks. - Block *initial_block_; // If non-NULL, points to the block that came from - // user data. + Block* initial_block_; // If non-NULL, points to the block that came from + // user data. Block* NewBlock(Block* last_block, size_t min_bytes); SerialArena* GetSerialArena(); bool GetSerialArenaFast(SerialArena** arena); SerialArena* GetSerialArenaFallback(void* me); - int64 lifecycle_id_; // Unique for each arena. Changes on Reset(). + LifecycleId lifecycle_id_; // Unique for each arena. Changes on Reset(). Options options_; diff --git a/src/google/protobuf/arena_test_util.cc b/src/google/protobuf/arena_test_util.cc index df9c5bd6b4..da32960ce1 100644 --- a/src/google/protobuf/arena_test_util.cc +++ b/src/google/protobuf/arena_test_util.cc @@ -28,9 +28,9 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include <google/protobuf/arena_test_util.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> -#include <google/protobuf/arena_test_util.h> #define EXPECT_EQ GOOGLE_CHECK_EQ diff --git a/src/google/protobuf/arena_test_util.h b/src/google/protobuf/arena_test_util.h index fdbaca068a..84df34989c 100644 --- a/src/google/protobuf/arena_test_util.h +++ b/src/google/protobuf/arena_test_util.h @@ -78,10 +78,9 @@ namespace internal { class NoHeapChecker { public: - NoHeapChecker() { - capture_alloc.Hook(); - } + NoHeapChecker() { capture_alloc.Hook(); } ~NoHeapChecker(); + private: class NewDeleteCapture { public: diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index a682a9c323..6ef3094ee5 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -71,12 +71,8 @@ namespace protobuf { class Notifier { public: Notifier() : count_(0) {} - void Notify() { - count_++; - } - int GetCount() { - return count_; - } + void Notify() { count_++; } + int GetCount() { return count_; } private: int count_; @@ -85,14 +81,13 @@ class Notifier { class SimpleDataType { public: SimpleDataType() : notifier_(NULL) {} - void SetNotifier(Notifier* notifier) { - notifier_ = notifier; - } + void SetNotifier(Notifier* notifier) { notifier_ = notifier; } virtual ~SimpleDataType() { if (notifier_ != NULL) { notifier_->Notify(); } }; + private: Notifier* notifier_; }; @@ -227,8 +222,8 @@ TEST(ArenaTest, CreateWithFourConstructorArguments) { const std::string three("3"); const PleaseDontCopyMe four(4); const MustBeConstructedWithOneThroughFour* new_object = - Arena::Create<MustBeConstructedWithOneThroughFour>( - &arena, 1, "2", three, &four); + Arena::Create<MustBeConstructedWithOneThroughFour>(&arena, 1, "2", three, + &four); EXPECT_TRUE(new_object != NULL); ASSERT_EQ(1, new_object->one_); ASSERT_STREQ("2", new_object->two_); @@ -462,7 +457,7 @@ TEST(ArenaTest, ReflectionSwapFields) { TEST(ArenaTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena); + TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); @@ -620,8 +615,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenas) { heap_submessage->set_bb(42); arena1_message->set_allocated_optional_nested_message(heap_submessage); // Should keep same object and add to arena's Own()-list. - EXPECT_EQ(heap_submessage, - arena1_message->mutable_optional_nested_message()); + EXPECT_EQ(heap_submessage, arena1_message->mutable_optional_nested_message()); { Arena arena2; TestAllTypes::NestedMessage* arena2_submessage = @@ -637,8 +631,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenas) { arena1_submessage->set_bb(42); TestAllTypes* heap_message = new TestAllTypes; heap_message->set_allocated_optional_nested_message(arena1_submessage); - EXPECT_NE(arena1_submessage, - heap_message->mutable_optional_nested_message()); + EXPECT_NE(arena1_submessage, heap_message->mutable_optional_nested_message()); delete heap_message; } @@ -648,15 +641,14 @@ TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) { TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1); const Reflection* r = arena1_message->GetReflection(); const Descriptor* d = arena1_message->GetDescriptor(); - const FieldDescriptor* msg_field = d->FindFieldByName( - "optional_nested_message"); + const FieldDescriptor* msg_field = + d->FindFieldByName("optional_nested_message"); TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage(); heap_submessage->set_bb(42); r->SetAllocatedMessage(arena1_message, heap_submessage, msg_field); // Should keep same object and add to arena's Own()-list. - EXPECT_EQ(heap_submessage, - arena1_message->mutable_optional_nested_message()); + EXPECT_EQ(heap_submessage, arena1_message->mutable_optional_nested_message()); { Arena arena2; TestAllTypes::NestedMessage* arena2_submessage = @@ -672,8 +664,7 @@ TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) { arena1_submessage->set_bb(42); TestAllTypes* heap_message = new TestAllTypes; r->SetAllocatedMessage(heap_message, arena1_submessage, msg_field); - EXPECT_NE(arena1_submessage, - heap_message->mutable_optional_nested_message()); + EXPECT_NE(arena1_submessage, heap_message->mutable_optional_nested_message()); delete heap_message; } @@ -728,11 +719,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage(); heap_submessage->set_bb(42); - arena1_message->mutable_repeated_nested_message()-> - AddAllocated(heap_submessage); + arena1_message->mutable_repeated_nested_message()->AddAllocated( + heap_submessage); // Should not copy object -- will use arena_->Own(). - EXPECT_EQ(heap_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_EQ(heap_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -743,11 +733,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* arena2_submessage = Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2); arena2_submessage->set_bb(42); - arena1_message->mutable_repeated_nested_message()-> - AddAllocated(arena2_submessage); + arena1_message->mutable_repeated_nested_message()->AddAllocated( + arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -758,11 +747,10 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { TestAllTypes::NestedMessage* arena2_submessage = Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2); arena2_submessage->set_bb(42); - heap_message->mutable_repeated_nested_message()-> - AddAllocated(arena2_submessage); + heap_message->mutable_repeated_nested_message()->AddAllocated( + arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &heap_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &heap_message->repeated_nested_message(i)); EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb()); } delete heap_message; @@ -771,8 +759,7 @@ TEST(ArenaTest, AddAllocatedToRepeatedField) { arena1_message->Clear(); for (int i = 0; i < 10; i++) { std::string* s = new std::string("Test"); - arena1_message->mutable_repeated_string()-> - AddAllocated(s); + arena1_message->mutable_repeated_string()->AddAllocated(s); // Should not copy. EXPECT_EQ(s, &arena1_message->repeated_string(i)); EXPECT_EQ("Test", arena1_message->repeated_string(i)); @@ -785,16 +772,14 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1); const Reflection* r = arena1_message->GetReflection(); const Descriptor* d = arena1_message->GetDescriptor(); - const FieldDescriptor* fd = - d->FindFieldByName("repeated_nested_message"); + const FieldDescriptor* fd = d->FindFieldByName("repeated_nested_message"); for (int i = 0; i < 10; i++) { TestAllTypes::NestedMessage* heap_submessage = new TestAllTypes::NestedMessage; heap_submessage->set_bb(42); r->AddAllocatedMessage(arena1_message, fd, heap_submessage); // Should not copy object -- will use arena_->Own(). - EXPECT_EQ(heap_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_EQ(heap_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -807,8 +792,7 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { arena2_submessage->set_bb(42); r->AddAllocatedMessage(arena1_message, fd, arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &arena1_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &arena1_message->repeated_nested_message(i)); EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb()); } @@ -821,8 +805,7 @@ TEST(ArenaTest, AddAllocatedToRepeatedFieldViaReflection) { arena2_submessage->set_bb(42); r->AddAllocatedMessage(heap_message, fd, arena2_submessage); // Should copy object. - EXPECT_NE(arena2_submessage, - &heap_message->repeated_nested_message(i)); + EXPECT_NE(arena2_submessage, &heap_message->repeated_nested_message(i)); EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb()); } delete heap_message; @@ -841,9 +824,9 @@ TEST(ArenaTest, ReleaseLastRepeatedField) { } for (int i = 0; i < 10; i++) { - const TestAllTypes::NestedMessage *orig_submessage = + const TestAllTypes::NestedMessage* orig_submessage = &arena_message->repeated_nested_message(10 - 1 - i); // last element - TestAllTypes::NestedMessage *released = + TestAllTypes::NestedMessage* released = arena_message->mutable_repeated_nested_message()->ReleaseLast(); EXPECT_NE(released, orig_submessage); EXPECT_EQ(42, released->bb()); @@ -859,11 +842,11 @@ TEST(ArenaTest, ReleaseLastRepeatedField) { } for (int i = 0; i < 10; i++) { - const TestAllTypes::NestedMessage *orig_submessage = + const TestAllTypes::NestedMessage* orig_submessage = &arena_message->repeated_nested_message(10 - 1 - i); // last element - TestAllTypes::NestedMessage *released = - arena_message->mutable_repeated_nested_message()-> - UnsafeArenaReleaseLast(); + TestAllTypes::NestedMessage* released = + arena_message->mutable_repeated_nested_message() + ->UnsafeArenaReleaseLast(); EXPECT_EQ(released, orig_submessage); EXPECT_EQ(42, released->bb()); // no delete -- |released| is on the arena. @@ -962,12 +945,10 @@ TEST(ArenaTest, ArenaOneofReflection) { const Descriptor* desc = message->GetDescriptor(); const Reflection* refl = message->GetReflection(); - const FieldDescriptor* string_field = desc->FindFieldByName( - "oneof_string"); - const FieldDescriptor* msg_field = desc->FindFieldByName( - "oneof_nested_message"); - const OneofDescriptor* oneof = desc->FindOneofByName( - "oneof_field"); + const FieldDescriptor* string_field = desc->FindFieldByName("oneof_string"); + const FieldDescriptor* msg_field = + desc->FindFieldByName("oneof_nested_message"); + const OneofDescriptor* oneof = desc->FindOneofByName("oneof_field"); refl->SetString(message, string_field, "Test value"); EXPECT_TRUE(refl->HasOneof(*message, oneof)); @@ -1051,12 +1032,12 @@ TEST(ArenaTest, ExtensionsOnArena) { // Ensure no leaks. TestAllExtensions* message_ext = Arena::CreateMessage<TestAllExtensions>(&arena); - message_ext->SetExtension( - protobuf_unittest::optional_int32_extension, 42); + message_ext->SetExtension(protobuf_unittest::optional_int32_extension, 42); message_ext->SetExtension(protobuf_unittest::optional_string_extension, std::string("test")); - message_ext->MutableExtension( - protobuf_unittest::optional_nested_message_extension)->set_bb(42); + message_ext + ->MutableExtension(protobuf_unittest::optional_nested_message_extension) + ->set_bb(42); } TEST(ArenaTest, RepeatedFieldOnArena) { @@ -1119,7 +1100,7 @@ TEST(ArenaTest, RepeatedFieldOnArena) { // code that may allocate messages or repeated fields of messages on an arena. { RepeatedPtrField<TestAllTypes>* repeated_ptr_on_arena = - Arena::CreateMessage< RepeatedPtrField<TestAllTypes> >(&arena); + Arena::CreateMessage<RepeatedPtrField<TestAllTypes> >(&arena); for (int i = 0; i < 10; i++) { // Add some elements and let the leak-checker ensure that everything is // freed. @@ -1127,7 +1108,7 @@ TEST(ArenaTest, RepeatedFieldOnArena) { } RepeatedField<int>* repeated_int_on_arena = - Arena::CreateMessage< RepeatedField<int> >(&arena); + Arena::CreateMessage<RepeatedField<int> >(&arena); for (int i = 0; i < 100; i++) { repeated_int_on_arena->Add(i); } @@ -1154,7 +1135,8 @@ TEST(ArenaTest, MutableMessageReflection) { EXPECT_EQ(submessage_expected, submessage); EXPECT_EQ(&arena, submessage->GetArena()); - const FieldDescriptor* oneof_field = d->FindFieldByName("oneof_nested_message"); + const FieldDescriptor* oneof_field = + d->FindFieldByName("oneof_nested_message"); submessage = static_cast<TestAllTypes::NestedMessage*>( r->MutableMessage(message, oneof_field)); submessage_expected = message->mutable_oneof_nested_message(); diff --git a/src/google/protobuf/arenastring_unittest.cc b/src/google/protobuf/arenastring_unittest.cc index edb6f6cd61..70a3f76722 100644 --- a/src/google/protobuf/arenastring_unittest.cc +++ b/src/google/protobuf/arenastring_unittest.cc @@ -40,10 +40,9 @@ #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> -#include <gtest/gtest.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/zero_copy_stream_impl.h> - +#include <gtest/gtest.h> namespace google { diff --git a/src/google/protobuf/compiler/code_generator.cc b/src/google/protobuf/compiler/code_generator.cc index 3319d5c503..428ec46983 100644 --- a/src/google/protobuf/compiler/code_generator.cc +++ b/src/google/protobuf/compiler/code_generator.cc @@ -57,8 +57,9 @@ bool CodeGenerator::GenerateAll(const std::vector<const FileDescriptor*>& files, const FileDescriptor* file = files[i]; succeeded = Generate(file, parameter, generator_context, error); if (!succeeded && error && error->empty()) { - *error = "Code generator returned false but provided no error " - "description."; + *error = + "Code generator returned false but provided no error " + "description."; } if (error && !error->empty()) { *error = file->name() + ": " + *error; diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index b1eb61accb..528dc76428 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -38,17 +38,19 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ -#include <google/protobuf/stubs/common.h> #include <string> -#include <vector> #include <utility> +#include <vector> +#include <google/protobuf/stubs/common.h> #include <google/protobuf/port_def.inc> namespace google { namespace protobuf { -namespace io { class ZeroCopyOutputStream; } +namespace io { +class ZeroCopyOutputStream; +} class FileDescriptor; namespace compiler { diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 126c433b64..02987e3b20 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -45,17 +45,17 @@ #ifdef minor #undef minor #endif -#include <sys/stat.h> #include <fcntl.h> +#include <sys/stat.h> #ifndef _MSC_VER #include <unistd.h> #endif +#include <ctype.h> #include <errno.h> #include <fstream> #include <iostream> -#include <ctype.h> -#include <limits.h> //For PATH_MAX +#include <limits.h> //For PATH_MAX #include <memory> @@ -71,6 +71,7 @@ #include <google/protobuf/compiler/plugin.pb.h> #include <google/protobuf/compiler/code_generator.h> #include <google/protobuf/compiler/importer.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/io/zero_copy_stream_impl.h> @@ -81,7 +82,6 @@ #include <google/protobuf/stubs/substitute.h> #include <google/protobuf/stubs/map_util.h> #include <google/protobuf/stubs/stl_util.h> -#include <google/protobuf/stubs/io_win32.h> #include <google/protobuf/port_def.inc> @@ -94,7 +94,7 @@ namespace compiler { #ifdef _O_BINARY #define O_BINARY _O_BINARY #else -#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. +#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. #endif #endif @@ -102,12 +102,12 @@ namespace { #if defined(_WIN32) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::setmode; -using google::protobuf::internal::win32::write; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; +using google::protobuf::io::win32::setmode; +using google::protobuf::io::win32::write; #endif static const char* kDefaultDirectDependenciesViolationMsg = @@ -118,10 +118,8 @@ static const char* kDefaultDirectDependenciesViolationMsg = // copy in importer.cc? static bool IsWindowsAbsolutePath(const std::string& text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && - isalpha(text[0]) && - (text[2] == '/' || text[2] == '\\') && - text.find_last_of(':') == 1; + return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; #endif @@ -281,8 +279,8 @@ class CommandLineInterface::ErrorPrinter public io::ErrorCollector, public DescriptorPool::ErrorCollector { public: - ErrorPrinter(ErrorFormat format, DiskSourceTree *tree = NULL) - : format_(format), tree_(tree), found_errors_(false) {} + ErrorPrinter(ErrorFormat format, DiskSourceTree* tree = NULL) + : format_(format), tree_(tree), found_errors_(false) {} ~ErrorPrinter() {} // implements MultiFileErrorCollector ------------------------------ @@ -327,8 +325,7 @@ class CommandLineInterface::ErrorPrinter std::ostream& out) { // Print full path when running under MSVS std::string dfile; - if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS && - tree_ != NULL && + if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS && tree_ != NULL && tree_->VirtualFileToDiskFile(filename, &dfile)) { out << dfile; } else { @@ -344,8 +341,8 @@ class CommandLineInterface::ErrorPrinter out << ":" << (line + 1) << ":" << (column + 1); break; case CommandLineInterface::ERROR_FORMAT_MSVS: - out << "(" << (line + 1) << ") : " - << type << " in column=" << (column + 1); + out << "(" << (line + 1) << ") : " << type + << " in column=" << (column + 1); break; } } @@ -358,7 +355,7 @@ class CommandLineInterface::ErrorPrinter } const ErrorFormat format_; - DiskSourceTree *tree_; + DiskSourceTree* tree_; bool found_errors_; }; @@ -417,8 +414,8 @@ class CommandLineInterface::MemoryOutputStream // implements ZeroCopyOutputStream --------------------------------- virtual bool Next(void** data, int* size) { return inner_->Next(data, size); } - virtual void BackUp(int count) { inner_->BackUp(count); } - virtual int64 ByteCount() const { return inner_->ByteCount(); } + virtual void BackUp(int count) { inner_->BackUp(count); } + virtual int64 ByteCount() const { return inner_->ByteCount(); } private: // Checks to see if "filename_.meta" exists in directory_; if so, fixes the @@ -447,8 +444,7 @@ class CommandLineInterface::MemoryOutputStream CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl( const std::vector<const FileDescriptor*>& parsed_files) - : parsed_files_(parsed_files), - had_error_(false) {} + : parsed_files_(parsed_files), had_error_(false) {} CommandLineInterface::GeneratorContextImpl::~GeneratorContextImpl() { STLDeleteValues(&files_); @@ -480,7 +476,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk( int file_descriptor; do { file_descriptor = - open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); + open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); } while (file_descriptor < 0 && errno == EINTR); if (file_descriptor < 0) { @@ -540,7 +536,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip( int file_descriptor; do { file_descriptor = - open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); + open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); } while (file_descriptor < 0 && errno == EINTR); if (file_descriptor < 0) { @@ -794,7 +790,8 @@ CommandLineInterface::CommandLineInterface() kDefaultDirectDependenciesViolationMsg), imports_in_descriptor_set_(false), source_info_in_descriptor_set_(false), - disallow_services_(false) {} + disallow_services_(false) { +} CommandLineInterface::~CommandLineInterface() {} void CommandLineInterface::RegisterGenerator(const std::string& flag_name, @@ -986,10 +983,10 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { break; case PRINT_NONE: GOOGLE_LOG(ERROR) << "If the code reaches here, it usually means a bug of " - "flag parsing in the CommandLineInterface."; + "flag parsing in the CommandLineInterface."; return 1; - // Do not add a default case. + // Do not add a default case. } } @@ -1021,16 +1018,15 @@ bool CommandLineInterface::PopulateSimpleDescriptorDatabase( fd = open(descriptor_set_in_names_[i].c_str(), O_RDONLY | O_BINARY); } while (fd < 0 && errno == EINTR); if (fd < 0) { - std::cerr << descriptor_set_in_names_[i] << ": " - << strerror(ENOENT) << std::endl; + std::cerr << descriptor_set_in_names_[i] << ": " << strerror(ENOENT) + << std::endl; return false; } FileDescriptorSet file_descriptor_set; bool parsed = file_descriptor_set.ParseFromFileDescriptor(fd); if (close(fd) != 0) { - std::cerr << descriptor_set_in_names_[i] << ": close: " - << strerror(errno) + std::cerr << descriptor_set_in_names_[i] << ": close: " << strerror(errno) << std::endl; return false; } @@ -1095,8 +1091,10 @@ bool CommandLineInterface::ParseInputFiles( // Enforce --disallow_services. if (disallow_services_ && parsed_file->service_count() > 0) { - std::cerr << parsed_file->name() << ": This file contains services, but " - "--disallow_services was used." << std::endl; + std::cerr << parsed_file->name() + << ": This file contains services, but " + "--disallow_services was used." + << std::endl; return false; } @@ -1168,18 +1166,18 @@ bool CommandLineInterface::MakeProtoProtoPathRelative( } std::string virtual_file, shadowing_disk_file; - switch (source_tree->DiskFileToVirtualFile( - *proto, &virtual_file, &shadowing_disk_file)) { + switch (source_tree->DiskFileToVirtualFile(*proto, &virtual_file, + &shadowing_disk_file)) { case DiskSourceTree::SUCCESS: *proto = virtual_file; break; case DiskSourceTree::SHADOWED: - std::cerr << *proto - << ": Input is shadowed in the --proto_path by \"" + std::cerr << *proto << ": Input is shadowed in the --proto_path by \"" << shadowing_disk_file << "\". Either use the latter file as your input or reorder " "the --proto_path so that the former file's location " - "comes first." << std::endl; + "comes first." + << std::endl; return false; case DiskSourceTree::CANNOT_OPEN: if (in_fallback_database) { @@ -1242,8 +1240,8 @@ bool CommandLineInterface::ExpandArgumentFile( return true; } -CommandLineInterface::ParseArgumentStatus -CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { +CommandLineInterface::ParseArgumentStatus CommandLineInterface::ParseArguments( + int argc, const char* const argv[]) { executable_name_ = argv[0]; std::vector<std::string> arguments; @@ -1285,8 +1283,7 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { } ParseArgumentStatus status = InterpretArgument(name, value); - if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE) - return status; + if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE) return status; } // Make sure each plugin option has a matching plugin output. @@ -1298,7 +1295,8 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { continue; } bool foundImplicitPlugin = false; - for (std::vector<OutputDirective>::const_iterator j = output_directives_.begin(); + for (std::vector<OutputDirective>::const_iterator j = + output_directives_.begin(); j != output_directives_.end(); ++j) { if (j->generator == NULL) { std::string plugin_name = PluginName(plugin_prefix_, j->name); @@ -1357,11 +1355,13 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { } if (imports_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_imports only makes sense when combined with " - "--descriptor_set_out." << std::endl; + "--descriptor_set_out." + << std::endl; } if (source_info_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_source_info only makes sense when combined with " - "--descriptor_set_out." << std::endl; + "--descriptor_set_out." + << std::endl; } return PARSE_ARGUMENT_DONE_AND_CONTINUE; @@ -1411,12 +1411,9 @@ bool CommandLineInterface::ParseArgument(const char* arg, std::string* name, return false; } - if (*name == "-h" || *name == "--help" || - *name == "--disallow_services" || - *name == "--include_imports" || - *name == "--include_source_info" || - *name == "--version" || - *name == "--decode_raw" || + if (*name == "-h" || *name == "--help" || *name == "--disallow_services" || + *name == "--include_imports" || *name == "--include_source_info" || + *name == "--version" || *name == "--decode_raw" || *name == "--print_free_field_numbers") { // HACK: These are the only flags that don't take a value. // They probably should not be hard-coded like this but for now it's @@ -1436,10 +1433,12 @@ CommandLineInterface::InterpretArgument(const std::string& name, if (value.empty()) { std::cerr << "You seem to have passed an empty string as one of the " - "arguments to " << executable_name_ + "arguments to " + << executable_name_ << ". This is actually " "sort of hard to do. Congrats. Unfortunately it is not valid " - "input so the program is going to die now." << std::endl; + "input so the program is going to die now." + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1469,7 +1468,8 @@ CommandLineInterface::InterpretArgument(const std::string& name, if (disk_path.empty()) { std::cerr << "--proto_path passed empty directory name. (Use \".\" for " - "current directory.)" << std::endl; + "current directory.)" + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1494,9 +1494,10 @@ CommandLineInterface::InterpretArgument(const std::string& name, } else if (name == "--direct_dependencies") { if (direct_dependencies_explicitly_set_) { - std::cerr << name << " may only be passed once. To specify multiple " - "direct dependencies, pass them all as a single " - "parameter separated by ':'." + std::cerr << name + << " may only be passed once. To specify multiple " + "direct dependencies, pass them all as a single " + "parameter separated by ':'." << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1512,11 +1513,11 @@ CommandLineInterface::InterpretArgument(const std::string& name, } else if (name == "--descriptor_set_in") { if (!descriptor_set_in_names_.empty()) { - std::cerr << name << " may only be passed once. To specify multiple " - "descriptor sets, pass them all as a single " - "parameter separated by '" - << CommandLineInterface::kPathSeparator << "'." - << std::endl; + std::cerr << name + << " may only be passed once. To specify multiple " + "descriptor sets, pass them all as a single " + "parameter separated by '" + << CommandLineInterface::kPathSeparator << "'." << std::endl; return PARSE_ARGUMENT_FAIL; } if (value.empty()) { @@ -1545,7 +1546,8 @@ CommandLineInterface::InterpretArgument(const std::string& name, if (mode_ != MODE_COMPILE) { std::cerr << "Cannot use --encode or --decode and generate descriptors at the " - "same time." << std::endl; + "same time." + << std::endl; return PARSE_ARGUMENT_FAIL; } descriptor_set_out_name_ = value; @@ -1588,9 +1590,8 @@ CommandLineInterface::InterpretArgument(const std::string& name, if (!version_info_.empty()) { std::cout << version_info_ << std::endl; } - std::cout << "libprotoc " - << internal::VersionString(PROTOBUF_VERSION) - << std::endl; + std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION) + << std::endl; return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler. } else if (name == "--disallow_services") { @@ -1707,7 +1708,8 @@ CommandLineInterface::InterpretArgument(const std::string& name, // It's an output flag. Add it to the output directives. if (mode_ != MODE_COMPILE) { std::cerr << "Cannot use --encode, --decode or print .proto info and " - "generate code at the same time." << std::endl; + "generate code at the same time." + << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1743,71 +1745,120 @@ void CommandLineInterface::PrintHelpText() { << "Usage: " << executable_name_ << " [OPTION] PROTO_FILES\n" -"Parse PROTO_FILES and generate output based on the options given:\n" -" -IPATH, --proto_path=PATH Specify the directory in which to search for\n" -" imports. May be specified multiple times;\n" -" directories will be searched in order. If not\n" -" given, the current working directory is used.\n" -" If not found in any of the these directories,\n" -" the --descriptor_set_in descriptors will be\n" -" checked for required proto file.\n" -" --version Show version info and exit.\n" -" -h, --help Show this text and exit.\n" -" --encode=MESSAGE_TYPE Read a text-format message of the given type\n" -" from standard input and write it in binary\n" -" to standard output. The message type must\n" -" be defined in PROTO_FILES or their imports.\n" -" --decode=MESSAGE_TYPE Read a binary message of the given type from\n" -" standard input and write it in text format\n" -" to standard output. The message type must\n" -" be defined in PROTO_FILES or their imports.\n" -" --decode_raw Read an arbitrary protocol message from\n" -" standard input and write the raw tag/value\n" -" pairs in text format to standard output. No\n" -" PROTO_FILES should be given when using this\n" -" flag.\n" -" --descriptor_set_in=FILES Specifies a delimited list of FILES\n" -" each containing a FileDescriptorSet (a\n" -" protocol buffer defined in descriptor.proto).\n" -" The FileDescriptor for each of the PROTO_FILES\n" -" provided will be loaded from these\n" -" FileDescriptorSets. If a FileDescriptor\n" -" appears multiple times, the first occurrence\n" -" will be used.\n" -" -oFILE, Writes a FileDescriptorSet (a protocol buffer,\n" -" --descriptor_set_out=FILE defined in descriptor.proto) containing all of\n" -" the input files to FILE.\n" -" --include_imports When using --descriptor_set_out, also include\n" -" all dependencies of the input files in the\n" -" set, so that the set is self-contained.\n" -" --include_source_info When using --descriptor_set_out, do not strip\n" -" SourceCodeInfo from the FileDescriptorProto.\n" -" This results in vastly larger descriptors that\n" -" include information about the original\n" -" location of each decl in the source file as\n" -" well as surrounding comments.\n" -" --dependency_out=FILE Write a dependency output file in the format\n" -" expected by make. This writes the transitive\n" -" set of input file paths to FILE\n" -" --error_format=FORMAT Set the format in which to print errors.\n" -" FORMAT may be 'gcc' (the default) or 'msvs'\n" -" (Microsoft Visual Studio format).\n" -" --print_free_field_numbers Print the free field numbers of the messages\n" -" defined in the given proto files. Groups share\n" -" the same field number space with the parent \n" -" message. Extension ranges are counted as \n" -" occupied fields numbers.\n" + "Parse PROTO_FILES and generate output based on the options given:\n" + " -IPATH, --proto_path=PATH Specify the directory in which to " + "search for\n" + " imports. May be specified multiple " + "times;\n" + " directories will be searched in order. " + " If not\n" + " given, the current working directory " + "is used.\n" + " If not found in any of the these " + "directories,\n" + " the --descriptor_set_in descriptors " + "will be\n" + " checked for required proto file.\n" + " --version Show version info and exit.\n" + " -h, --help Show this text and exit.\n" + " --encode=MESSAGE_TYPE Read a text-format message of the " + "given type\n" + " from standard input and write it in " + "binary\n" + " to standard output. The message type " + "must\n" + " be defined in PROTO_FILES or their " + "imports.\n" + " --decode=MESSAGE_TYPE Read a binary message of the given " + "type from\n" + " standard input and write it in text " + "format\n" + " to standard output. The message type " + "must\n" + " be defined in PROTO_FILES or their " + "imports.\n" + " --decode_raw Read an arbitrary protocol message " + "from\n" + " standard input and write the raw " + "tag/value\n" + " pairs in text format to standard " + "output. No\n" + " PROTO_FILES should be given when using " + "this\n" + " flag.\n" + " --descriptor_set_in=FILES Specifies a delimited list of FILES\n" + " each containing a FileDescriptorSet " + "(a\n" + " protocol buffer defined in " + "descriptor.proto).\n" + " The FileDescriptor for each of the " + "PROTO_FILES\n" + " provided will be loaded from these\n" + " FileDescriptorSets. If a " + "FileDescriptor\n" + " appears multiple times, the first " + "occurrence\n" + " will be used.\n" + " -oFILE, Writes a FileDescriptorSet (a protocol " + "buffer,\n" + " --descriptor_set_out=FILE defined in descriptor.proto) " + "containing all of\n" + " the input files to FILE.\n" + " --include_imports When using --descriptor_set_out, also " + "include\n" + " all dependencies of the input files in " + "the\n" + " set, so that the set is " + "self-contained.\n" + " --include_source_info When using --descriptor_set_out, do " + "not strip\n" + " SourceCodeInfo from the " + "FileDescriptorProto.\n" + " This results in vastly larger " + "descriptors that\n" + " include information about the " + "original\n" + " location of each decl in the source " + "file as\n" + " well as surrounding comments.\n" + " --dependency_out=FILE Write a dependency output file in the " + "format\n" + " expected by make. This writes the " + "transitive\n" + " set of input file paths to FILE\n" + " --error_format=FORMAT Set the format in which to print " + "errors.\n" + " FORMAT may be 'gcc' (the default) or " + "'msvs'\n" + " (Microsoft Visual Studio format).\n" + " --print_free_field_numbers Print the free field numbers of the " + "messages\n" + " defined in the given proto files. " + "Groups share\n" + " the same field number space with the " + "parent \n" + " message. Extension ranges are counted " + "as \n" + " occupied fields numbers.\n" << std::endl; if (!plugin_prefix_.empty()) { - std::cout << -" --plugin=EXECUTABLE Specifies a plugin executable to use.\n" -" Normally, protoc searches the PATH for\n" -" plugins, but you may specify additional\n" -" executables not in the path using this flag.\n" -" Additionally, EXECUTABLE may be of the form\n" -" NAME=PATH, in which case the given plugin name\n" -" is mapped to the given executable even if\n" -" the executable's own name differs." << std::endl; + std::cout + << " --plugin=EXECUTABLE Specifies a plugin executable to " + "use.\n" + " Normally, protoc searches the PATH " + "for\n" + " plugins, but you may specify " + "additional\n" + " executables not in the path using " + "this flag.\n" + " Additionally, EXECUTABLE may be of " + "the form\n" + " NAME=PATH, in which case the given " + "plugin name\n" + " is mapped to the given executable " + "even if\n" + " the executable's own name differs." + << std::endl; } for (GeneratorMap::iterator iter = generators_by_flag_name_.begin(); @@ -1820,20 +1871,30 @@ void CommandLineInterface::PrintHelpText() { ' ') // Spaces for alignment. << iter->second.help_text << std::endl; } - std::cout << -" @<filename> Read options and filenames from file. If a\n" -" relative file path is specified, the file\n" -" will be searched in the working directory.\n" -" The --proto_path option will not affect how\n" -" this argument file is searched. Content of\n" -" the file will be expanded in the position of\n" -" @<filename> as in the argument list. Note\n" -" that shell expansion is not applied to the\n" -" content of the file (i.e., you cannot use\n" -" quotes, wildcards, escapes, commands, etc.).\n" -" Each line corresponds to a single argument,\n" -" even if it contains spaces." - << std::endl; + std::cout << " @<filename> Read options and filenames from " + "file. If a\n" + " relative file path is specified, " + "the file\n" + " will be searched in the working " + "directory.\n" + " The --proto_path option will not " + "affect how\n" + " this argument file is searched. " + "Content of\n" + " the file will be expanded in the " + "position of\n" + " @<filename> as in the argument " + "list. Note\n" + " that shell expansion is not " + "applied to the\n" + " content of the file (i.e., you " + "cannot use\n" + " quotes, wildcards, escapes, " + "commands, etc.).\n" + " Each line corresponds to a " + "single argument,\n" + " even if it contains spaces." + << std::endl; } bool CommandLineInterface::GenerateOutput( @@ -1856,8 +1917,7 @@ bool CommandLineInterface::GenerateOutput( } parameters.append(plugin_parameters_[plugin_name]); } - if (!GeneratePluginOutput(parsed_files, plugin_name, - parameters, + if (!GeneratePluginOutput(parsed_files, plugin_name, parameters, generator_context, &error)) { std::cerr << output_directive.name << ": " << error << std::endl; return false; @@ -1871,8 +1931,8 @@ bool CommandLineInterface::GenerateOutput( } parameters.append(generator_parameters_[output_directive.name]); } - if (!output_directive.generator->GenerateAll( - parsed_files, parameters, generator_context, &error)) { + if (!output_directive.generator->GenerateAll(parsed_files, parameters, + generator_context, &error)) { // Generator returned an error. std::cerr << output_directive.name << ": " << error << std::endl; return false; @@ -1890,10 +1950,7 @@ bool CommandLineInterface::GenerateDependencyManifestFile( std::set<const FileDescriptor*> already_seen; for (int i = 0; i < parsed_files.size(); i++) { - GetTransitiveDependencies(parsed_files[i], - false, - false, - &already_seen, + GetTransitiveDependencies(parsed_files[i], false, false, &already_seen, file_set.mutable_file()); } @@ -2022,8 +2079,9 @@ bool CommandLineInterface::GeneratePluginOutput( current_output.reset(generator_context->Open(output_file.name())); } else if (current_output == NULL) { *error = strings::Substitute( - "$0: First file chunk returned by plugin did not specify a file name.", - plugin_name); + "$0: First file chunk returned by plugin did not specify a file " + "name.", + plugin_name); return false; } @@ -2132,8 +2190,8 @@ bool CommandLineInterface::WriteDescriptorSet( for (int i = 0; i < parsed_files.size(); i++) { GetTransitiveDependencies(parsed_files[i], true, // Include json_name - source_info_in_descriptor_set_, - &already_seen, file_set.mutable_file()); + source_info_in_descriptor_set_, &already_seen, + file_set.mutable_file()); } int fd; @@ -2164,8 +2222,7 @@ bool CommandLineInterface::WriteDescriptorSet( } void CommandLineInterface::GetTransitiveDependencies( - const FileDescriptor* file, - bool include_json_name, + const FileDescriptor* file, bool include_json_name, bool include_source_code_info, std::set<const FileDescriptor*>* already_seen, RepeatedPtrField<FileDescriptorProto>* output) { @@ -2176,10 +2233,8 @@ void CommandLineInterface::GetTransitiveDependencies( // Add all dependencies. for (int i = 0; i < file->dependency_count(); i++) { - GetTransitiveDependencies(file->dependency(i), - include_json_name, - include_source_code_info, - already_seen, output); + GetTransitiveDependencies(file->dependency(i), include_json_name, + include_source_code_info, already_seen, output); } // Add this file. @@ -2291,8 +2346,7 @@ void FormatFreeFieldNumbers(const std::string& name, } // namespace -void CommandLineInterface::PrintFreeFieldNumbers( - const Descriptor* descriptor) { +void CommandLineInterface::PrintFreeFieldNumbers(const Descriptor* descriptor) { std::set<FieldRange> ranges; std::vector<const Descriptor*> nested_messages; GatherOccupiedFieldRanges(descriptor, &ranges, &nested_messages); @@ -2304,7 +2358,6 @@ void CommandLineInterface::PrintFreeFieldNumbers( } - } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h index 520585ca29..6c9893fa28 100644 --- a/src/google/protobuf/compiler/command_line_interface.h +++ b/src/google/protobuf/compiler/command_line_interface.h @@ -52,14 +52,15 @@ namespace google { namespace protobuf { -class Descriptor; // descriptor.h -class DescriptorDatabase; // descriptor_database.h -class DescriptorPool; // descriptor.h -class FileDescriptor; // descriptor.h -class FileDescriptorSet; // descriptor.h -class FileDescriptorProto; // descriptor.pb.h -template<typename T> class RepeatedPtrField; // repeated_field.h -class SimpleDescriptorDatabase; // descriptor_database.h +class Descriptor; // descriptor.h +class DescriptorDatabase; // descriptor_database.h +class DescriptorPool; // descriptor.h +class FileDescriptor; // descriptor.h +class FileDescriptorSet; // descriptor.h +class FileDescriptorProto; // descriptor.pb.h +template <typename T> +class RepeatedPtrField; // repeated_field.h +class SimpleDescriptorDatabase; // descriptor_database.h namespace compiler { @@ -307,8 +308,7 @@ class PROTOC_EXPORT CommandLineInterface { // If include_json_name is true, populate the json_name field of // FieldDescriptorProto for all fields. static void GetTransitiveDependencies( - const FileDescriptor* file, - bool include_json_name, + const FileDescriptor* file, bool include_json_name, bool include_source_code_info, std::set<const FileDescriptor*>* already_seen, RepeatedPtrField<FileDescriptorProto>* output); @@ -374,15 +374,15 @@ class PROTOC_EXPORT CommandLineInterface { Mode mode_; enum PrintMode { - PRINT_NONE, // Not in MODE_PRINT - PRINT_FREE_FIELDS, // --print_free_fields + PRINT_NONE, // Not in MODE_PRINT + PRINT_FREE_FIELDS, // --print_free_fields }; PrintMode print_mode_; enum ErrorFormat { - ERROR_FORMAT_GCC, // GCC error output format (default). - ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). + ERROR_FORMAT_GCC, // GCC error output format (default). + ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). }; ErrorFormat error_format_; @@ -403,8 +403,8 @@ class PROTOC_EXPORT CommandLineInterface { // output_directives_ lists all the files we are supposed to output and what // generator to use for each. struct OutputDirective { - std::string name; // E.g. "--foo_out" - CodeGenerator* generator; // NULL for plugins + std::string name; // E.g. "--foo_out" + CodeGenerator* generator; // NULL for plugins std::string parameter; std::string output_location; }; diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index e4b393c616..c6fad5fac8 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -51,6 +51,7 @@ #include <google/protobuf/compiler/command_line_interface.h> #include <google/protobuf/test_util2.h> #include <google/protobuf/unittest.pb.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/descriptor.pb.h> @@ -62,7 +63,6 @@ #include <gtest/gtest.h> #include <google/protobuf/stubs/strutil.h> -#include <google/protobuf/stubs/io_win32.h> namespace google { namespace protobuf { @@ -71,12 +71,12 @@ namespace compiler { #if defined(_WIN32) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::dup; -using google::protobuf::internal::win32::dup2; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::write; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::dup; +using google::protobuf::io::win32::dup2; +using google::protobuf::io::win32::open; +using google::protobuf::io::win32::write; #endif // Disable the whole test when we use tcmalloc for "draconian" heap checks, in @@ -122,7 +122,7 @@ class CommandLineInterfaceTest : public testing::Test { void SwitchToTempDirectory() { File::ChangeWorkingDirectory(temp_directory_); } -#else // !PROTOBUF_OPENSOURCE +#else // !PROTOBUF_OPENSOURCE // TODO(teboring): Figure out how to change and get working directory in // google3. #endif // !PROTOBUF_OPENSOURCE @@ -303,21 +303,24 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector<std::string> args) { plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH; #else const char* possible_paths[] = { - // When building with shared libraries, libtool hides the real executable - // in .libs and puts a fake wrapper in the current directory. - // Unfortunately, due to an apparent bug on Cygwin/MinGW, if one program - // wrapped in this way (e.g. protobuf-tests.exe) tries to execute another - // program wrapped in this way (e.g. test_plugin.exe), the latter fails - // with error code 127 and no explanation message. Presumably the problem - // is that the wrapper for protobuf-tests.exe set some environment - // variables that confuse the wrapper for test_plugin.exe. Luckily, it - // turns out that if we simply invoke the wrapped test_plugin.exe - // directly, it works -- I guess the environment variables set by the - // protobuf-tests.exe wrapper happen to be correct for it too. So we do - // that. - ".libs/test_plugin.exe", // Win32 w/autotool (Cygwin / MinGW) - "test_plugin.exe", // Other Win32 (MSVC) - "test_plugin", // Unix + // When building with shared libraries, libtool hides the real + // executable + // in .libs and puts a fake wrapper in the current directory. + // Unfortunately, due to an apparent bug on Cygwin/MinGW, if one program + // wrapped in this way (e.g. protobuf-tests.exe) tries to execute + // another + // program wrapped in this way (e.g. test_plugin.exe), the latter fails + // with error code 127 and no explanation message. Presumably the + // problem + // is that the wrapper for protobuf-tests.exe set some environment + // variables that confuse the wrapper for test_plugin.exe. Luckily, it + // turns out that if we simply invoke the wrapped test_plugin.exe + // directly, it works -- I guess the environment variables set by the + // protobuf-tests.exe wrapper happen to be correct for it too. So we do + // that. + ".libs/test_plugin.exe", // Win32 w/autotool (Cygwin / MinGW) + "test_plugin.exe", // Other Win32 (MSVC) + "test_plugin", // Unix }; for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) { if (access(possible_paths[i], F_OK) == 0) { @@ -335,7 +338,7 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector<std::string> args) { } } - std::unique_ptr<const char * []> argv(new const char* [args.size()]); + std::unique_ptr<const char*[]> argv(new const char*[args.size()]); for (int i = 0; i < args.size(); i++) { args[i] = StringReplace(args[i], "$tmpdir", temp_directory_, true); @@ -419,36 +422,34 @@ bool CommandLineInterfaceTest::HasAlternateErrorSubstring( void CommandLineInterfaceTest::ExpectGenerated( const std::string& generator_name, const std::string& parameter, const std::string& proto_name, const std::string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, "", proto_name, message_name, proto_name, - temp_directory_); + MockCodeGenerator::ExpectGenerated(generator_name, parameter, "", proto_name, + message_name, proto_name, temp_directory_); } void CommandLineInterfaceTest::ExpectGenerated( const std::string& generator_name, const std::string& parameter, const std::string& proto_name, const std::string& message_name, const std::string& output_directory) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, "", proto_name, message_name, proto_name, - temp_directory_ + "/" + output_directory); + MockCodeGenerator::ExpectGenerated(generator_name, parameter, "", proto_name, + message_name, proto_name, + temp_directory_ + "/" + output_directory); } void CommandLineInterfaceTest::ExpectGeneratedWithMultipleInputs( const std::string& generator_name, const std::string& all_proto_names, const std::string& proto_name, const std::string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, "", "", proto_name, message_name, - all_proto_names, - temp_directory_); + MockCodeGenerator::ExpectGenerated(generator_name, "", "", proto_name, + message_name, all_proto_names, + temp_directory_); } void CommandLineInterfaceTest::ExpectGeneratedWithInsertions( const std::string& generator_name, const std::string& parameter, const std::string& insertions, const std::string& proto_name, const std::string& message_name) { - MockCodeGenerator::ExpectGenerated( - generator_name, parameter, insertions, proto_name, message_name, - proto_name, temp_directory_); + MockCodeGenerator::ExpectGenerated(generator_name, parameter, insertions, + proto_name, message_name, proto_name, + temp_directory_); } void CommandLineInterfaceTest::CheckGeneratedAnnotations( @@ -489,8 +490,8 @@ void CommandLineInterfaceTest::ExpectCapturedStdout( void CommandLineInterfaceTest::ExpectCapturedStdoutSubstringWithZeroReturnCode( const std::string& expected_substring) { EXPECT_EQ(0, return_code_); - EXPECT_PRED_FORMAT2( - testing::IsSubstring, expected_substring, captured_stdout_); + EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, + captured_stdout_); } void CommandLineInterfaceTest::ExpectFileContent(const std::string& filename, @@ -509,8 +510,8 @@ TEST_F(CommandLineInterfaceTest, BasicOutput) { // Test that the common case works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -539,8 +540,8 @@ TEST_F(CommandLineInterfaceTest, BasicPlugin) { // Test that basic plugins work. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -570,8 +571,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin) { // Invoke a generator and a plugin at the same time. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -603,11 +604,11 @@ TEST_F(CommandLineInterfaceTest, MultipleInputs) { // Test parsing multiple input files. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto bar.proto"); @@ -655,17 +656,17 @@ TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport) { // Test parsing multiple input files with an import of a separate file. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"baz.proto\";\n" - "message Bar {\n" - " optional Baz a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"baz.proto\";\n" + "message Bar {\n" + " optional Baz a = 1;\n" + "}\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "message Baz {}\n"); + "syntax = \"proto2\";\n" + "message Baz {}\n"); Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " "--proto_path=$tmpdir foo.proto bar.proto"); @@ -841,8 +842,8 @@ TEST_F(CommandLineInterfaceTest, CreateDirectory) { // Test that when we output to a sub-directory, it is created. CreateTempFile("bar/baz/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempDir("out"); CreateTempDir("plugout"); @@ -858,8 +859,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorParameters) { // Test that generator parameters are correctly parsed from the command line. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=TestParameter:$tmpdir " "--plug_out=TestPluginParameter:$tmpdir " @@ -875,8 +876,8 @@ TEST_F(CommandLineInterfaceTest, ExtraGeneratorParameters) { // correctly passed to the code generator. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -890,10 +891,10 @@ TEST_F(CommandLineInterfaceTest, ExtraGeneratorParameters) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGenerated( - "test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); - ExpectGenerated( - "test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); + ExpectGenerated("test_generator", "bar,foo1,foo2,foo3", "foo.proto", "Foo", + "a"); + ExpectGenerated("test_generator", "baz,foo1,foo2,foo3", "foo.proto", "Foo", + "b"); } TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { @@ -901,8 +902,8 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { // correctly passed to the code generator. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -916,16 +917,14 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParameters) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGenerated( - "test_plugin", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); - ExpectGenerated( - "test_plugin", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); + ExpectGenerated("test_plugin", "bar,foo1,foo2,foo3", "foo.proto", "Foo", "a"); + ExpectGenerated("test_plugin", "baz,foo1,foo2,foo3", "foo.proto", "Foo", "b"); } TEST_F(CommandLineInterfaceTest, UnrecognizedExtraParameters) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--unknown_plug_a_opt=Foo " @@ -948,8 +947,8 @@ TEST_F(CommandLineInterfaceTest, ExtraPluginParametersForOutParameters) { // directives appear, they both are reported. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--xyz_opt=foo=bar --xyz_out=$tmpdir " @@ -964,8 +963,8 @@ TEST_F(CommandLineInterfaceTest, Insert) { // Test running a generator that inserts code into another's output. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler " "--test_out=TestParameter:$tmpdir " @@ -975,12 +974,12 @@ TEST_F(CommandLineInterfaceTest, Insert) { "--proto_path=$tmpdir foo.proto"); ExpectNoErrors(); - ExpectGeneratedWithInsertions( - "test_generator", "TestParameter", "test_generator,test_plugin", - "foo.proto", "Foo"); - ExpectGeneratedWithInsertions( - "test_plugin", "TestPluginParameter", "test_generator,test_plugin", - "foo.proto", "Foo"); + ExpectGeneratedWithInsertions("test_generator", "TestParameter", + "test_generator,test_plugin", "foo.proto", + "Foo"); + ExpectGeneratedWithInsertions("test_plugin", "TestPluginParameter", + "test_generator,test_plugin", "foo.proto", + "Foo"); } TEST_F(CommandLineInterfaceTest, InsertWithAnnotationFixup) { @@ -1007,8 +1006,7 @@ TEST_F(CommandLineInterfaceTest, InsertWithAnnotationFixup) { TEST_F(CommandLineInterfaceTest, WindowsOutputPath) { // Test that the output path can be a Windows-style path. - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --null_out=C:\\ " "--proto_path=$tmpdir foo.proto"); @@ -1020,8 +1018,7 @@ TEST_F(CommandLineInterfaceTest, WindowsOutputPath) { TEST_F(CommandLineInterfaceTest, WindowsOutputPathAndParameter) { // Test that we can have a windows-style output path and a parameter. - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --null_out=bar:C:\\ " "--proto_path=$tmpdir foo.proto"); @@ -1035,8 +1032,8 @@ TEST_F(CommandLineInterfaceTest, TrailingBackslash) { // doesn't work on their system. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir\\ " "--proto_path=$tmpdir\\ foo.proto"); @@ -1056,14 +1053,14 @@ TEST_F(CommandLineInterfaceTest, PathLookup) { // Test that specifying multiple directories in the proto search path works. CreateTempFile("b/bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("a/foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar a = 1;\n" + "}\n"); CreateTempFile("b/foo.proto", "this should not be parsed\n"); Run("protocol_compiler --test_out=$tmpdir " @@ -1077,14 +1074,14 @@ TEST_F(CommandLineInterfaceTest, ColonDelimitedPath) { // Same as PathLookup, but we provide the proto_path in a single flag. CreateTempFile("b/bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("a/foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar a = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar a = 1;\n" + "}\n"); CreateTempFile("b/foo.proto", "this should not be parsed\n"); Run(strings::Substitute( @@ -1100,8 +1097,8 @@ TEST_F(CommandLineInterfaceTest, NonRootMapping) { // Test setting up a search path mapping a directory to a non-root location. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=bar=$tmpdir bar/foo.proto"); @@ -1115,8 +1112,8 @@ TEST_F(CommandLineInterfaceTest, PathWithEqualsSign) { CreateTempDir("with=sign"); CreateTempFile("with=sign/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/with=sign foo.proto"); @@ -1130,8 +1127,8 @@ TEST_F(CommandLineInterfaceTest, MultipleGenerators) { // each with a different output directory. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create the "a" and "b" sub-directories. CreateTempDir("a"); CreateTempDir("b"); @@ -1151,8 +1148,8 @@ TEST_F(CommandLineInterfaceTest, DisallowServicesNoServices) { // services. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --disallow_services --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1165,9 +1162,9 @@ TEST_F(CommandLineInterfaceTest, DisallowServicesHasService) { // Test that --disallow_services produces an error when there are services. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n" - "service Bar {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n" + "service Bar {}\n"); Run("protocol_compiler --disallow_services --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1179,9 +1176,9 @@ TEST_F(CommandLineInterfaceTest, AllowServicesHasService) { // Test that services work fine as long as --disallow_services is not used. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n" - "service Bar {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n" + "service Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1263,8 +1260,7 @@ TEST_F(CommandLineInterfaceTest, DirectDependencies_NoViolation_MultiImports) { } TEST_F(CommandLineInterfaceTest, DirectDependencies_ProvidedMultipleTimes) { - CreateTempFile("foo.proto", - "syntax = \"proto2\";\n"); + CreateTempFile("foo.proto", "syntax = \"proto2\";\n"); Run("protocol_compiler --test_out=$tmpdir --proto_path=$tmpdir " "--direct_dependencies=bar.proto --direct_dependencies=bla.proto " @@ -1301,8 +1297,8 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { // Test that we can accept working-directory-relative input files. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir $tmpdir/foo.proto"); @@ -1313,14 +1309,14 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSet) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--proto_path=$tmpdir bar.proto"); @@ -1342,20 +1338,20 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSet) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithDuplicates) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Baz {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Baz {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--proto_path=$tmpdir bar.proto foo.proto bar.proto baz.proto"); @@ -1381,14 +1377,14 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithDuplicates) { TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithSourceInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_source_info --proto_path=$tmpdir bar.proto"); @@ -1406,14 +1402,14 @@ TEST_F(CommandLineInterfaceTest, WriteDescriptorSetWithSourceInfo) { TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSet) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_imports --proto_path=$tmpdir bar.proto"); @@ -1437,14 +1433,14 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSet) { TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --descriptor_set_out=$tmpdir/descriptor_set " "--include_imports --include_source_info --proto_path=$tmpdir bar.proto"); @@ -1471,14 +1467,14 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) { #else TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --dependency_out=$tmpdir/manifest " "--test_out=$tmpdir --proto_path=$tmpdir bar.proto foo.proto"); @@ -1490,14 +1486,14 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) { #ifdef PROTOBUF_OPENSOURCE TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); std::string current_working_directory = getcwd(NULL, 0); SwitchToTempDirectory(); @@ -1520,14 +1516,14 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForAbsolutePath) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n" - "message Bar {\n" - " optional Foo foo = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n" + "message Bar {\n" + " optional Foo foo = 1;\n" + "}\n"); Run("protocol_compiler --dependency_out=$tmpdir/manifest " "--test_out=$tmpdir --proto_path=$tmpdir bar.proto"); @@ -1544,11 +1540,11 @@ TEST_F(CommandLineInterfaceTest, TestArgumentFile) { // Test parsing multiple input files using an argument file. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); CreateTempFile("arguments.txt", "--test_out=$tmpdir\n" "--plug_out=$tmpdir\n" @@ -1577,14 +1573,14 @@ TEST_F(CommandLineInterfaceTest, ParseErrors) { // Test that parse errors are reported. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorText( - "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); + "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(CommandLineInterfaceTest, ParseErrors_DescriptorSetIn) { @@ -1594,8 +1590,7 @@ TEST_F(CommandLineInterfaceTest, ParseErrors_DescriptorSetIn) { Run("protocol_compiler --test_out=$tmpdir " "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); - ExpectErrorText( - "$tmpdir/foo.bin: Unable to parse.\n"); + ExpectErrorText("$tmpdir/foo.bin: Unable to parse.\n"); } TEST_F(CommandLineInterfaceTest, ParseErrorsMultipleFiles) { @@ -1605,37 +1600,38 @@ TEST_F(CommandLineInterfaceTest, ParseErrorsMultipleFiles) { // two ways: Directly and through baz.proto. bar.proto's errors should // only be reported once. CreateTempFile("bar.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); CreateTempFile("baz.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "import \"baz.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "import \"baz.proto\";\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorText( - "bar.proto:2:1: Expected top-level statement (e.g. \"message\").\n" - "baz.proto: Import \"bar.proto\" was not found or had errors.\n" - "foo.proto: Import \"bar.proto\" was not found or had errors.\n" - "foo.proto: Import \"baz.proto\" was not found or had errors.\n"); + "bar.proto:2:1: Expected top-level statement (e.g. \"message\").\n" + "baz.proto:2:1: Import \"bar.proto\" was not found or had errors.\n" + "foo.proto:2:1: Import \"bar.proto\" was not found or had errors.\n" + "foo.proto:3:1: Import \"baz.proto\" was not found or had errors.\n"); } TEST_F(CommandLineInterfaceTest, RecursiveImportFails) { // Create a proto file that imports itself. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"foo.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"foo.proto\";\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); ExpectErrorSubstring( - "foo.proto: File recursively imports itself: foo.proto -> foo.proto\n"); + "foo.proto:2:1: File recursively imports itself: " + "foo.proto -> foo.proto\n"); } TEST_F(CommandLineInterfaceTest, InputNotFoundError) { @@ -1653,8 +1649,7 @@ TEST_F(CommandLineInterfaceTest, InputNotFoundError_DescriptorSetIn) { Run("protocol_compiler --test_out=$tmpdir " "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); - ExpectErrorText( - "$tmpdir/foo.bin: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.bin: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundError) { @@ -1664,8 +1659,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundError) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir $tmpdir/foo.proto"); - ExpectErrorText( - "$tmpdir/foo.proto: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.proto: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { @@ -1673,8 +1667,8 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { // mapped to a virtual path. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); // Create a directory called "bar" so that we can point --proto_path at it. CreateTempFile("bar/dummy", ""); @@ -1683,7 +1677,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { "--proto_path=$tmpdir/bar $tmpdir/foo.proto"); ExpectErrorText( - "$tmpdir/foo.proto: File does not reside within any path " + "$tmpdir/foo.proto: File does not reside within any path " "specified using --proto_path (or -I). You must specify a " "--proto_path which encompasses this file. Note that the " "proto_path must be an exact prefix of the .proto file " @@ -1702,8 +1696,7 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundAndNotMappedError) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/bar $tmpdir/foo.proto"); - ExpectErrorText( - "$tmpdir/foo.proto: No such file or directory\n"); + ExpectErrorText("$tmpdir/foo.proto: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputShadowedError) { @@ -1711,21 +1704,21 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputShadowedError) { // by another file in the virtual path. CreateTempFile("foo/foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); CreateTempFile("bar/foo.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir/foo --proto_path=$tmpdir/bar " "$tmpdir/bar/foo.proto"); ExpectErrorText( - "$tmpdir/bar/foo.proto: Input is shadowed in the --proto_path " - "by \"$tmpdir/foo/foo.proto\". Either use the latter " - "file as your input or reorder the --proto_path so that the " - "former file's location comes first.\n"); + "$tmpdir/bar/foo.proto: Input is shadowed in the --proto_path " + "by \"$tmpdir/foo/foo.proto\". Either use the latter " + "file as your input or reorder the --proto_path so that the " + "former file's location comes first.\n"); } TEST_F(CommandLineInterfaceTest, ProtoPathNotFoundError) { @@ -1812,8 +1805,8 @@ TEST_F(CommandLineInterfaceTest, MissingInputError) { TEST_F(CommandLineInterfaceTest, MissingOutputError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --proto_path=$tmpdir foo.proto"); @@ -1822,8 +1815,8 @@ TEST_F(CommandLineInterfaceTest, MissingOutputError) { TEST_F(CommandLineInterfaceTest, OutputWriteError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); std::string output_file = MockCodeGenerator::GetOutputFileName("test_generator", "foo.proto"); @@ -1851,8 +1844,8 @@ TEST_F(CommandLineInterfaceTest, OutputWriteError) { TEST_F(CommandLineInterfaceTest, PluginOutputWriteError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); std::string output_file = MockCodeGenerator::GetOutputFileName("test_plugin", "foo.proto"); @@ -1875,8 +1868,8 @@ TEST_F(CommandLineInterfaceTest, PluginOutputWriteError) { TEST_F(CommandLineInterfaceTest, OutputDirectoryNotFoundError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir/nosuchdir " "--proto_path=$tmpdir foo.proto"); @@ -1886,8 +1879,8 @@ TEST_F(CommandLineInterfaceTest, OutputDirectoryNotFoundError) { TEST_F(CommandLineInterfaceTest, PluginOutputDirectoryNotFoundError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --plug_out=$tmpdir/nosuchdir " "--proto_path=$tmpdir foo.proto"); @@ -1897,8 +1890,8 @@ TEST_F(CommandLineInterfaceTest, PluginOutputDirectoryNotFoundError) { TEST_F(CommandLineInterfaceTest, OutputDirectoryIsFileError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out=$tmpdir/foo.proto " "--proto_path=$tmpdir foo.proto"); @@ -1915,8 +1908,8 @@ TEST_F(CommandLineInterfaceTest, OutputDirectoryIsFileError) { TEST_F(CommandLineInterfaceTest, GeneratorError) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Error {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Error {}\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1929,8 +1922,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginError) { // Test a generator plugin that returns an error. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Error {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Error {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1943,8 +1936,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginFail) { // Test a generator plugin that exits with an error code. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Exit {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Exit {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1958,8 +1951,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginCrash) { // Test a generator plugin that crashes. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_Abort {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Abort {}\n"); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -1973,15 +1966,14 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginCrash) { "--plug_out: prefix-gen-plug: Plugin failed with status code"); #else // Don't depend on the exact signal number. - ExpectErrorSubstring( - "--plug_out: prefix-gen-plug: Plugin killed by signal"); + ExpectErrorSubstring("--plug_out: prefix-gen-plug: Plugin killed by signal"); #endif } TEST_F(CommandLineInterfaceTest, PluginReceivesSourceCodeInfo) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_HasSourceCodeInfo {}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_HasSourceCodeInfo {}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); @@ -1991,10 +1983,10 @@ TEST_F(CommandLineInterfaceTest, PluginReceivesSourceCodeInfo) { TEST_F(CommandLineInterfaceTest, PluginReceivesJsonName) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_HasJsonName {\n" - " optional int32 value = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_HasJsonName {\n" + " optional int32 value = 1;\n" + "}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); @@ -2003,25 +1995,24 @@ TEST_F(CommandLineInterfaceTest, PluginReceivesJsonName) { TEST_F(CommandLineInterfaceTest, PluginReceivesCompilerVersion) { CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message MockCodeGenerator_ShowVersionNumber {\n" - " optional int32 value = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "message MockCodeGenerator_ShowVersionNumber {\n" + " optional int32 value = 1;\n" + "}\n"); Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto"); - ExpectErrorSubstring( - StringPrintf("Saw compiler_version: %d %s", - GOOGLE_PROTOBUF_VERSION, - GOOGLE_PROTOBUF_VERSION_SUFFIX)); + ExpectErrorSubstring(StringPrintf("Saw compiler_version: %d %s", + GOOGLE_PROTOBUF_VERSION, + GOOGLE_PROTOBUF_VERSION_SUFFIX)); } TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) { // Test what happens if the plugin isn't found. CreateTempFile("error.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --badplug_out=TestParameter:$tmpdir " "--plugin=prefix-gen-badplug=no_such_file " @@ -2029,11 +2020,10 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) { #ifdef _WIN32 ExpectErrorSubstring("--badplug_out: prefix-gen-badplug: " + - Subprocess::Win32ErrorMessage(ERROR_FILE_NOT_FOUND)); + Subprocess::Win32ErrorMessage(ERROR_FILE_NOT_FOUND)); #else // Error written to stdout by child process after exec() fails. - ExpectErrorSubstring( - "no_such_file: program not found or is not executable"); + ExpectErrorSubstring("no_such_file: program not found or is not executable"); // Error written by parent process when child fails. ExpectErrorSubstring( @@ -2045,8 +2035,8 @@ TEST_F(CommandLineInterfaceTest, GeneratorPluginNotAllowed) { // Test what happens if plugins aren't allowed. CreateTempFile("error.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); DisallowPlugins(); Run("protocol_compiler --plug_out=TestParameter:$tmpdir " @@ -2070,28 +2060,28 @@ TEST_F(CommandLineInterfaceTest, GccFormatErrors) { // that it can be set explicitly). CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=gcc foo.proto"); ExpectErrorText( - "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); + "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(CommandLineInterfaceTest, MsvsFormatErrors) { // Test --error_format=msvs CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=msvs foo.proto"); ExpectErrorText( - "$tmpdir/foo.proto(2) : error in column=1: Expected top-level statement " + "$tmpdir/foo.proto(2) : error in column=1: Expected top-level statement " "(e.g. \"message\").\n"); } @@ -2099,14 +2089,13 @@ TEST_F(CommandLineInterfaceTest, InvalidErrorFormat) { // Test --error_format=msvs CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "badsyntax\n"); + "syntax = \"proto2\";\n" + "badsyntax\n"); Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir --error_format=invalid foo.proto"); - ExpectErrorText( - "Unknown error format: invalid\n"); + ExpectErrorText("Unknown error format: invalid\n"); } // ------------------------------------------------------------------- @@ -2116,8 +2105,8 @@ TEST_F(CommandLineInterfaceTest, ParseSingleCharacterFlag) { // Test that a single-character flag works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler -t$tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2130,8 +2119,8 @@ TEST_F(CommandLineInterfaceTest, ParseSpaceDelimitedValue) { // Test that separating the flag value with a space works. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler --test_out $tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2145,8 +2134,8 @@ TEST_F(CommandLineInterfaceTest, ParseSingleCharacterSpaceDelimitedValue) { // single-character flags. CreateTempFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); Run("protocol_compiler -t $tmpdir " "--proto_path=$tmpdir foo.proto"); @@ -2173,38 +2162,35 @@ TEST_F(CommandLineInterfaceTest, MissingValueAtEndError) { } TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { - CreateTempFile( - "foo.proto", - "syntax = \"proto2\";\n" - "package foo;\n" - "message Foo {\n" - " optional int32 a = 2;\n" - " optional string b = 4;\n" - " optional string c = 5;\n" - " optional int64 d = 8;\n" - " optional double e = 10;\n" - "}\n"); - CreateTempFile( - "bar.proto", - "syntax = \"proto2\";\n" - "message Bar {\n" - " optional int32 a = 2;\n" - " extensions 4 to 5;\n" - " optional int64 d = 8;\n" - " extensions 10;\n" - "}\n"); - CreateTempFile( - "baz.proto", - "syntax = \"proto2\";\n" - "message Baz {\n" - " optional int32 a = 2;\n" - " optional int64 d = 8;\n" - " extensions 15 to max;\n" // unordered. - " extensions 13;\n" - " extensions 10 to 12;\n" - " extensions 5;\n" - " extensions 4;\n" - "}\n"); + CreateTempFile("foo.proto", + "syntax = \"proto2\";\n" + "package foo;\n" + "message Foo {\n" + " optional int32 a = 2;\n" + " optional string b = 4;\n" + " optional string c = 5;\n" + " optional int64 d = 8;\n" + " optional double e = 10;\n" + "}\n"); + CreateTempFile("bar.proto", + "syntax = \"proto2\";\n" + "message Bar {\n" + " optional int32 a = 2;\n" + " extensions 4 to 5;\n" + " optional int64 d = 8;\n" + " extensions 10;\n" + "}\n"); + CreateTempFile("baz.proto", + "syntax = \"proto2\";\n" + "message Baz {\n" + " optional int32 a = 2;\n" + " optional int64 d = 8;\n" + " extensions 15 to max;\n" // unordered. + " extensions 13;\n" + " extensions 10 to 12;\n" + " extensions 5;\n" + " extensions 4;\n" + "}\n"); CreateTempFile( "quz.proto", "syntax = \"proto2\";\n" @@ -2249,10 +2235,7 @@ TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { // platforms that don't have a Bourne-compatible shell available (especially // Windows/MSVC). -enum EncodeDecodeTestMode { - PROTO_PATH, - DESCRIPTOR_SET_IN -}; +enum EncodeDecodeTestMode { PROTO_PATH, DESCRIPTOR_SET_IN }; class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> { protected: @@ -2312,7 +2295,7 @@ class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> { ADD_FAILURE() << "unexpected EncodeDecodeTestMode: " << GetParam(); } - std::unique_ptr<const char * []> argv(new const char* [args.size()]); + std::unique_ptr<const char*[]> argv(new const char*[args.size()]); for (int i = 0; i < args.size(); i++) { argv[i] = args[i].c_str(); } @@ -2365,7 +2348,6 @@ class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> { protobuf_unittest_import::ImportMessage import_message; import_message.descriptor()->file()->CopyTo(file_descriptor_set.add_file()); - protobuf_unittest_import::PublicImportMessage public_import_message; public_import_message.descriptor()->file()->CopyTo( file_descriptor_set.add_file()); @@ -2373,10 +2355,8 @@ class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> { std::string binary_proto; GOOGLE_CHECK(file_descriptor_set.SerializeToString(&binary_proto)); - GOOGLE_CHECK_OK(File::SetContents( - unittest_proto_descriptor_set_filename_, - binary_proto, - true)); + GOOGLE_CHECK_OK(File::SetContents(unittest_proto_descriptor_set_filename_, + binary_proto, true)); } int duped_stdin_; @@ -2416,7 +2396,7 @@ TEST_P(EncodeDecodeTest, Partial) { " --encode=protobuf_unittest.TestRequired")); ExpectStdoutMatchesText(""); ExpectStderrMatchesText( - "warning: Input message is missing required fields: a, b, c\n"); + "warning: Input message is missing required fields: a, b, c\n"); } TEST_P(EncodeDecodeTest, DecodeRaw) { @@ -2428,8 +2408,9 @@ TEST_P(EncodeDecodeTest, DecodeRaw) { RedirectStdinFromText(data); EXPECT_TRUE(Run("--decode_raw")); - ExpectStdoutMatchesText("1: 123\n" - "14: \"foo\"\n"); + ExpectStdoutMatchesText( + "1: 123\n" + "14: \"foo\"\n"); ExpectStderrMatchesText(""); } diff --git a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc index c197dae94f..d73cfb72a4 100644 --- a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc @@ -77,8 +77,8 @@ class MockErrorCollector : public MultiFileErrorCollector { // implements ErrorCollector --------------------------------------- void AddError(const std::string& filename, int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", - filename, line, column, message); + strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, + column, message); } }; @@ -92,7 +92,7 @@ class MockGeneratorContext : public GeneratorContext { std::string* expected_contents = FindPtrOrNull(files_, virtual_filename); ASSERT_TRUE(expected_contents != NULL) - << "Generator failed to generate file: " << virtual_filename; + << "Generator failed to generate file: " << virtual_filename; std::string actual_contents; GOOGLE_CHECK_OK( @@ -101,10 +101,11 @@ class MockGeneratorContext : public GeneratorContext { << physical_filename; CleanStringLineEndings(&actual_contents, false); -#ifdef WRITE_FILES // Define to debug mismatched files. +#ifdef WRITE_FILES // Define to debug mismatched files. + GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", *expected_contents, + true)); GOOGLE_CHECK_OK( - File::SetContents("/tmp/expected.cc", *expected_contents, true)); - GOOGLE_CHECK_OK(File::SetContents("/tmp/actual.cc", actual_contents, true)); + File::SetContents("/tmp/actual.cc", actual_contents, true)); #endif ASSERT_EQ(*expected_contents, actual_contents) @@ -142,10 +143,14 @@ TEST(BootstrapTest, GeneratedFilesMatch) { // of the data to compare to. std::map<std::string, std::string> vpath_map; std::map<std::string, std::string> rpath_map; - rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto2"] = - "net/proto2/z_generated_example/test_messages_proto2"; - rpath_map["third_party/protobuf/src/google/protobuf/test_messages_proto3"] = - "net/proto2/z_generated_example/test_messages_proto3"; + rpath_map + ["third_party/protobuf_legacy_opensource/src/google/protobuf/" + "test_messages_proto2"] = + "net/proto2/z_generated_example/test_messages_proto2"; + rpath_map + ["third_party/protobuf_legacy_opensource/src/google/protobuf/" + "test_messages_proto3"] = + "net/proto2/z_generated_example/test_messages_proto3"; rpath_map["net/proto2/internal/proto2_weak"] = "net/proto2/z_generated_example/proto2_weak"; diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc index 9220ddb1c5..0164108dc7 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc @@ -145,28 +145,18 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { "$dllexport_decl $const ::$proto_ns$::EnumDescriptor* " "$classname$_descriptor();\n"); // The _Name and _Parse methods - if (options_.opensource_runtime) { - // TODO(haberman): consider removing this in favor of the stricter - // version below. Would this break our compatibility guarantees? - format( - "inline const std::string& $classname$_Name($classname$ value) {\n" - " return ::$proto_ns$::internal::NameOfEnum(\n" - " $classname$_descriptor(), value);\n" - "}\n"); - } else { - // Support a stricter, type-checked enum-to-string method that - // statically checks whether the parameter is the exact enum type or is - // an integral type. - format( - "template<typename T>\n" - "inline const std::string& $classname$_Name(T enum_t_value) {\n" - " static_assert(::std::is_same<T, $classname$>::value ||\n" - " ::std::is_integral<T>::value,\n" - " \"Incorrect type passed to function $classname$_Name.\");\n" - " return ::$proto_ns$::internal::NameOfEnum(\n" - " $classname$_descriptor(), enum_t_value);\n" - "}\n"); - } + // Support a stricter, type-checked enum-to-string method that + // statically checks whether the parameter is the exact enum type or is + // an integral type. + format( + "template<typename T>\n" + "inline const std::string& $classname$_Name(T enum_t_value) {\n" + " static_assert(::std::is_same<T, $classname$>::value ||\n" + " ::std::is_integral<T>::value,\n" + " \"Incorrect type passed to function $classname$_Name.\");\n" + " return ::$proto_ns$::internal::NameOfEnum(\n" + " $classname$_descriptor(), enum_t_value);\n" + "}\n"); format( "inline bool $classname$_Parse(\n" " const std::string& name, $classname$* value) {\n" @@ -227,29 +217,18 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) const { "$nested_name$_descriptor() {\n" " return $classname$_descriptor();\n" "}\n"); - if (options_.opensource_runtime) { - // TODO(haberman): consider removing this in favor of the stricter - // version below. Would this break our compatibility guarantees? - format( - "static inline const std::string& " - "$nested_name$_Name($resolved_name$ value) {" - "\n" - " return $classname$_Name(value);\n" - "}\n"); - } else { - // Support a stricter, type-checked enum-to-string method that - // statically checks whether the parameter is the exact enum type or is - // an integral type. - format( - "template<typename T>\n" - "static inline const std::string& $nested_name$_Name(T enum_t_value) " - "{\n" - " static_assert(::std::is_same<T, $resolved_name$>::value ||\n" - " ::std::is_integral<T>::value,\n" - " \"Incorrect type passed to function $nested_name$_Name.\");\n" - " return $classname$_Name(enum_t_value);\n" - "}\n"); - } + // Support a stricter, type-checked enum-to-string method that + // statically checks whether the parameter is the exact enum type or is + // an integral type. + format( + "template<typename T>\n" + "static inline const std::string& $nested_name$_Name(T enum_t_value) " + "{\n" + " static_assert(::std::is_same<T, $resolved_name$>::value ||\n" + " ::std::is_integral<T>::value,\n" + " \"Incorrect type passed to function $nested_name$_Name.\");\n" + " return $classname$_Name(enum_t_value);\n" + "}\n"); format( "static inline bool $nested_name$_Parse(const std::string& name,\n" " $resolved_name$* value) {\n" @@ -301,8 +280,9 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { // Before C++17, we must define the static constants which were // declared in the header, to give the linker a place to put them. // But pre-2015 MSVC++ insists that we not. - format("#if (__cplusplus < 201703) && " - "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); + format( + "#if (__cplusplus < 201703) && " + "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); for (int i = 0; i < descriptor_->value_count(); i++) { format("constexpr $classname$ $1$::$2$;\n", parent, @@ -316,8 +296,9 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* printer) { format("constexpr int $1$::$nested_name$_ARRAYSIZE;\n", parent); } - format("#endif // (__cplusplus < 201703) && " - "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); + format( + "#endif // (__cplusplus < 201703) && " + "(!defined(_MSC_VER) || _MSC_VER >= 1900)\n"); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.h b/src/google/protobuf/compiler/cpp/cpp_enum.h index cce8cf200f..3687f04c27 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.h +++ b/src/google/protobuf/compiler/cpp/cpp_enum.h @@ -43,9 +43,9 @@ namespace google { namespace protobuf { - namespace io { - class Printer; // printer.h - } +namespace io { +class Printer; // printer.h +} } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc index b03ca837bc..97be5e6426 100644 --- a/src/google/protobuf/compiler/cpp/cpp_extension.cc +++ b/src/google/protobuf/compiler/cpp/cpp_extension.cc @@ -60,8 +60,7 @@ std::string ExtendeeClassName(const FieldDescriptor* descriptor) { ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, const Options& options) - : descriptor_(descriptor), - options_(options) { + : descriptor_(descriptor), options_(options) { // Construct type_traits_. if (descriptor_->is_repeated()) { type_traits_ = "Repeated"; diff --git a/src/google/protobuf/compiler/cpp/cpp_field.cc b/src/google/protobuf/compiler/cpp/cpp_field.cc index 48e6cec1fc..7aae57dc58 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_field.cc @@ -77,7 +77,7 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, (*variables)["clear_hasbit"] = ""; if (HasFieldPresence(descriptor->file())) { (*variables)["set_hasbit_io"] = - "HasBitSetters::set_has_" + FieldName(descriptor) + "(this);"; + "HasBitSetters::set_has_" + FieldName(descriptor) + "(&_has_bits_);"; } else { (*variables)["set_hasbit_io"] = ""; } @@ -113,8 +113,8 @@ void SetCommonOneofFieldVariables( FieldGenerator::~FieldGenerator() {} -void FieldGenerator:: -GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { +void FieldGenerator::GenerateMergeFromCodedStreamWithPacking( + io::Printer* printer) const { // Reaching here indicates a bug. Cases are: // - This FieldGenerator should support packing, but this method should be // overridden. @@ -122,7 +122,6 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { // never have been called. GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() " << "called on field generator that does not support packing."; - } FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index d9a7bde45d..7164bd754d 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -75,7 +75,6 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) variables_["add_descriptors"] = UniqueName("AddDescriptors", file_, options_); variables_["filename"] = file_->name(); variables_["package_ns"] = Namespace(file_, options); - variables_["init_defaults"] = UniqueName("InitDefaults", file_, options_); std::vector<const Descriptor*> msgs = FlattenMessagesInFile(file); for (int i = 0; i < msgs.size(); i++) { @@ -150,11 +149,6 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { format("#define $1$$ dllexport_decl$\n", UniqueName("PROTOBUF_INTERNAL_EXPORT", file_, options_)); GenerateMacroUndefs(printer); - if (IsProto2MessageSetFile(file_, options_)) { - // Proto2 MessageSet overrides GetMapper() so we forward declare TagMapper - // to avoid inclusion of "tagmapper.h". - format("class TagMapper;\n"); - } // For Any support with lite protos, we need to friend AnyMetadata, so we // forward-declare it here. @@ -165,18 +159,6 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { "} // namespace internal\n" "PROTOBUF_NAMESPACE_CLOSE\n"); - if (!options_.opensource_runtime) { - // EmbeddedMessageHolder is a proxy class to provide access into arena - // constructors for proto1 message objects. - // See net/proto/proto_arena_internal.h - format( - "namespace proto {\n" - "namespace internal {\n" - "template <typename T> struct EmbeddedMessageHolder;\n" - "} // namespace internal\n" - "} // namespace proto\n"); - } - GenerateGlobalStateFunctionDeclarations(printer); GenerateForwardDeclarations(printer); @@ -249,14 +231,7 @@ void FileGenerator::GenerateProtoHeader(io::Printer* printer, for (int i = 0; i < file_->public_dependency_count(); i++) { const FileDescriptor* dep = file_->public_dependency(i); - const char* extension = ".proto.h"; - // The proto1 compiler only generates .pb.h files, so even if we are - // running in proto-h mode, we can only use the .pb.h. - if (IsProto1(dep, options_)) { - extension = ".pb.h"; - } - std::string dependency = StripProto(dep->name()) + extension; - format("#include \"$1$\"\n", dependency); + format("#include \"$1$.proto.h\"\n", StripProto(dep->name())); } format("// @@protoc_insertion_point(includes)\n"); @@ -413,20 +388,13 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* printer) { // Use the smaller .proto.h files. for (int i = 0; i < file_->dependency_count(); i++) { const FileDescriptor* dep = file_->dependency(i); - const char* extension = ".proto.h"; - std::string basename = StripProto(dep->name()); // Do not import weak deps. if (!options_.opensource_runtime && IsDepWeak(dep)) continue; - // The proto1 compiler only generates .pb.h files, so even if we are - // running in proto-h mode, we can only use the .pb.h. - if (IsProto1(dep, options_)) { - extension = ".pb.h"; - } + std::string basename = StripProto(dep->name()); if (IsBootstrapProto(options_, file_)) { GetBootstrapBasename(options_, basename, &basename); } - std::string dependency = basename + extension; - format("#include \"$1$\"\n", dependency); + format("#include \"$1$.proto.h\"\n", basename); } } @@ -479,7 +447,7 @@ void FileGenerator::GenerateInternalForwardDeclarations( } std::string dllexport = UniqueName("PROTOBUF_INTERNAL_EXPORT", msg, options_); - if (IsProto1(msg->file(), options_) || IsWeak(field, options_)) { + if (IsWeak(field, options_)) { dllexport = ""; } auto scc = scc_analyzer->GetSCC(msg); @@ -626,19 +594,6 @@ void FileGenerator::GenerateSource(io::Printer* printer) { } if (HasDescriptorMethods(file_, options_)) { - // TODO(gerbens) This is for proto1 interoperability. Remove when proto1 - // is gone. - format("void $init_defaults$() {\n"); - for (int i = 0; i < message_generators_.size(); i++) { - if (!IsSCCRepresentative(message_generators_[i]->descriptor_)) continue; - std::string scc_name = - UniqueName(ClassName(message_generators_[i]->descriptor_), - message_generators_[i]->descriptor_, options_); - format(" ::$proto_ns$::internal::InitSCC(&scc_info_$1$.base);\n", - scc_name); - } - format("}\n\n"); - // Define the code to initialize reflection. This code uses a global // constructor to register reflection data with the runtime pre-main. GenerateReflectionInitializationCode(printer); @@ -781,8 +736,8 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format( "reinterpret_cast<const " "::$proto_ns$::Message*>(&$1$::_$2$_default_instance_),\n", - Namespace(descriptor, options_), // 1 - ClassName(descriptor)); // 2 + Namespace(descriptor, options_), // 1 + ClassName(descriptor)); // 2 } format.Outdent(); format( @@ -823,7 +778,7 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { // built into real descriptors at initialization time. const std::string protodef_name = UniqueName("descriptor_table_protodef", file_, options_); - format( "const char $1$[] =\n", protodef_name); + format("const char $1$[] =\n", protodef_name); format.Indent(); FileDescriptorProto file_proto; file_->CopyTo(&file_proto); @@ -861,10 +816,8 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { format( "static " "::$proto_ns$::internal::DescriptorTable $1$ = {\n" - " false, $init_defaults$, \n" - " $2$,\n", - UniqueName("descriptor_table", file_, options_), - protodef_name); + " false, $2$,\n", + UniqueName("descriptor_table", file_, options_), protodef_name); const int num_deps = file_->dependency_count(); format( @@ -878,8 +831,18 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* printer) { const FileDescriptor* dependency = file_->dependency(i); format(" ::$1$,\n", UniqueName("AddDescriptors", dependency, options_)); } + format(" };\n"); + // Reflection referes to the default instances so make sure they are + // initialized. + for (int i = 0; i < message_generators_.size(); i++) { + if (!IsSCCRepresentative(message_generators_[i]->descriptor_)) continue; + std::string scc_name = + UniqueName(ClassName(message_generators_[i]->descriptor_), + message_generators_[i]->descriptor_, options_); + format(" ::$proto_ns$::internal::InitSCC(&scc_info_$1$.base);\n", + scc_name); + } format( - " };\n" " ::$proto_ns$::internal::AddDescriptors(&$1$, deps, $2$);\n" "}\n\n", UniqueName("descriptor_table", file_, options_), // 1 @@ -904,15 +867,6 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { format.Indent(); - if (!options_.opensource_runtime) { - for (int i = 0; i < scc->children.size(); i++) { - const SCC* child_scc = scc->children[i]; - const FileDescriptor* dependency = child_scc->GetRepresentative()->file(); - if (!IsProto1(dependency, options_)) continue; - format("$1$();\n", UniqueName("InitDefaults", dependency, options_)); - } - } - // First construct all the necessary default instances. for (int i = 0; i < message_generators_.size(); i++) { if (scc_analyzer_.GetSCC(message_generators_[i]->descriptor_) != scc) { @@ -961,11 +915,6 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { options_)); for (const SCC* child : scc->children) { auto repr = child->GetRepresentative(); - if (IsProto1(repr->file(), options_)) { - GOOGLE_CHECK(!options_.opensource_runtime); - format("\n nullptr,"); - continue; - } format("\n &scc_info_$1$.base,", UniqueName(ClassName(repr), repr, options_)); } @@ -1243,7 +1192,7 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { "#endif\n" "\n", PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC, // 1 - PROTOBUF_VERSION); // 2 + PROTOBUF_VERSION); // 2 IncludeFile("net/proto2/public/port_undef.inc", printer); } @@ -1381,11 +1330,6 @@ void FileGenerator::GenerateGlobalStateFunctionDeclarations( std::max(size_t(1), message_generators_.size())); if (HasDescriptorMethods(file_, options_)) { format("void $dllexport_decl $$add_descriptors$();\n"); - if (!options_.opensource_runtime) { - // TODO(gerbens) This is for proto1 interoperability. Remove when proto1 - // is gone. - format("void $dllexport_decl $$init_defaults$();\n"); - } } } diff --git a/src/google/protobuf/compiler/cpp/cpp_file.h b/src/google/protobuf/compiler/cpp/cpp_file.h index a7308ab995..ca9067297c 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.h +++ b/src/google/protobuf/compiler/cpp/cpp_file.h @@ -163,9 +163,7 @@ class FileGenerator { const Descriptor* GetSCCRepresentative(const Descriptor* d) { return GetSCC(d)->GetRepresentative(); } - const SCC* GetSCC(const Descriptor* d) { - return scc_analyzer_.GetSCC(d); - } + const SCC* GetSCC(const Descriptor* d) { return scc_analyzer_.GetSCC(d); } bool IsDepWeak(const FileDescriptor* dep) const { if (weak_deps_.count(dep) != 0) { diff --git a/src/google/protobuf/compiler/cpp/cpp_generator.cc b/src/google/protobuf/compiler/cpp/cpp_generator.cc index fe7131fe80..0deaf6b532 100644 --- a/src/google/protobuf/compiler/cpp/cpp_generator.cc +++ b/src/google/protobuf/compiler/cpp/cpp_generator.cc @@ -34,9 +34,9 @@ #include <google/protobuf/compiler/cpp/cpp_generator.h> -#include <vector> #include <memory> #include <utility> +#include <vector> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/compiler/cpp/cpp_file.h> @@ -101,8 +101,8 @@ bool CppGenerator::Generate(const FileDescriptor* file, file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime; file_options.lite_implicit_weak_fields = true; if (!options[i].second.empty()) { - file_options.num_cc_files = strto32(options[i].second.c_str(), - NULL, 10); + file_options.num_cc_files = + strto32(options[i].second.c_str(), NULL, 10); } } else if (options[i].first == "table_driven_parsing") { file_options.table_driven_parsing = true; @@ -142,9 +142,9 @@ bool CppGenerator::Generate(const FileDescriptor* file, io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( &annotations); std::string info_path = basename + ".proto.h.meta"; - io::Printer printer(output.get(), '$', file_options.annotate_headers - ? &annotation_collector - : NULL); + io::Printer printer( + output.get(), '$', + file_options.annotate_headers ? &annotation_collector : NULL); file_generator.GenerateProtoHeader( &printer, file_options.annotate_headers ? info_path : ""); if (file_options.annotate_headers) { @@ -161,9 +161,9 @@ bool CppGenerator::Generate(const FileDescriptor* file, io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( &annotations); std::string info_path = basename + ".pb.h.meta"; - io::Printer printer(output.get(), '$', file_options.annotate_headers - ? &annotation_collector - : NULL); + io::Printer printer( + output.get(), '$', + file_options.annotate_headers ? &annotation_collector : NULL); file_generator.GeneratePBHeader( &printer, file_options.annotate_headers ? info_path : ""); if (file_options.annotate_headers) { diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc index 21af61f11d..036ffa85d2 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc @@ -55,7 +55,6 @@ #include <google/protobuf/stubs/hash.h> - #include <google/protobuf/port_def.inc> namespace google { @@ -358,8 +357,10 @@ std::string Namespace(const FileDescriptor* d, const Options& options) { if (IsWellKnownMessage(d) && options.opensource_runtime) { // Written with string concatenation to prevent rewriting of // ::google::protobuf. - ret = StringReplace(ret, "::google::" "protobuf", "PROTOBUF_NAMESPACE_ID", - false); + ret = StringReplace(ret, + "::google::" + "protobuf", + "PROTOBUF_NAMESPACE_ID", false); } return ret; } @@ -1336,21 +1337,27 @@ bool MaybeBootstrap(const Options& options, GeneratorContext* generator_context, bool ShouldRepeat(const FieldDescriptor* descriptor, internal::WireFormatLite::WireType wiretype) { - return descriptor->is_repeated() && + constexpr int kMaxTwoByteFieldNumber = 16 * 128; + return descriptor->number() < kMaxTwoByteFieldNumber && + descriptor->is_repeated() && (!descriptor->is_packable() || wiretype != internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); } class ParseLoopGenerator { public: - ParseLoopGenerator(const Options& options, MessageSCCAnalyzer* scc_analyzer, - io::Printer* printer) - : scc_analyzer_(scc_analyzer), options_(options), format_(printer) {} + ParseLoopGenerator(int num_hasbits, const Options& options, + MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) + : scc_analyzer_(scc_analyzer), + options_(options), + format_(printer), + num_hasbits_(num_hasbits) {} void GenerateParserLoop(const Descriptor* descriptor) { format_.Set("classname", ClassName(descriptor)); format_.Set("p_ns", "::" + ProtobufNamespace(options_)); - format_.Set("pi_ns", StrCat("::", ProtobufNamespace(options_), "::internal")); + format_.Set("pi_ns", + StrCat("::", ProtobufNamespace(options_), "::internal")); format_.Set("GOOGLE_PROTOBUF", MacroPrefix(options_)); std::map<std::string, std::string> vars; SetCommonVars(options_, &vars); @@ -1358,9 +1365,6 @@ class ParseLoopGenerator { std::vector<const FieldDescriptor*> ordered_fields; for (auto field : FieldRange(descriptor)) { - if (IsProto1(descriptor->file(), options_)) { - if (field->number() >= (1 << 14)) continue; - } ordered_fields.push_back(field); } std::sort(ordered_fields.begin(), ordered_fields.end(), @@ -1370,20 +1374,43 @@ class ParseLoopGenerator { format_( "const char* $classname$::_InternalParse(const char* ptr, " - "$pi_ns$::ParseContext* ctx) {\n"); + "$pi_ns$::ParseContext* ctx) {\n" + "#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure\n"); format_.Indent(); + int hasbits_size = 0; + if (HasFieldPresence(descriptor->file())) { + hasbits_size = (num_hasbits_ + 31) / 32; + } + // For now only optimize small hasbits. + if (hasbits_size != 1) hasbits_size = 0; + if (hasbits_size) { + format_("HasBitSetters::HasBits has_bits{};\n"); + format_.Set("has_bits", "has_bits"); + } else { + format_.Set("has_bits", "_has_bits_"); + } + if (descriptor->file()->options().cc_enable_arenas()) { format_("$p_ns$::Arena* arena = GetArenaNoVirtual(); (void)arena;\n"); } GenerateParseLoop(descriptor, ordered_fields); format_.Outdent(); - format_("}\n"); + format_("success:\n"); + if (hasbits_size) format_(" _has_bits_.Or(has_bits);\n"); + format_( + " return ptr;\n" + "failure:\n" + " ptr = nullptr;\n" + " goto success;\n" + "#undef CHK_\n" + "}\n"); } private: MessageSCCAnalyzer* scc_analyzer_; const Options& options_; Formatter format_; + int num_hasbits_; using WireFormat = internal::WireFormat; using WireFormatLite = internal::WireFormatLite; @@ -1396,7 +1423,7 @@ class ParseLoopGenerator { field_name = ", kFieldName"; } if (HasFieldPresence(field->file())) { - format_("HasBitSetters::set_has_$1$(this);\n", FieldName(field)); + format_("HasBitSetters::set_has_$1$(&$has_bits$);\n", FieldName(field)); } string default_string = field->default_value_string().empty() @@ -1432,18 +1459,12 @@ class ParseLoopGenerator { if (!options_.opensource_runtime) { // Open source doesn't support other ctypes; ctype = field->options().ctype(); - if (IsProto1(field->file(), options_) && - ctype == FieldOptions::STRING_PIECE) { - // proto1 doesn't support STRING_PIECE - ctype = FieldOptions::STRING; - } } if (field->file()->options().cc_enable_arenas() && !field->is_repeated() && !options_.opensource_runtime && GetOptimizeFor(field->file(), options_) != FileOptions::LITE_RUNTIME && // For now only use arena string for strings with empty defaults. field->default_value_string().empty() && - !IsProto1(field->file(), options_) && !IsStringInlined(field, options_) && field->containing_oneof() == nullptr && ctype == FieldOptions::STRING) { GenerateArenaString(field, utf8, field_name); @@ -1461,14 +1482,13 @@ class ParseLoopGenerator { name = "StringPieceParser" + utf8; break; } - format_( - "ptr = $pi_ns$::Inline$1$($2$_$3$(), ptr, ctx$4$);\n", - name, field->is_repeated() && !field->is_packable() ? "add" : "mutable", - FieldName(field), field_name); + format_("ptr = $pi_ns$::Inline$1$($2$_$3$(), ptr, ctx$4$);\n", name, + field->is_repeated() && !field->is_packable() ? "add" : "mutable", + FieldName(field), field_name); } void GenerateLengthDelim(const FieldDescriptor* field) { - if (!IsProto1(field->file(), options_) && field->is_packable()) { + if (field->is_packable()) { std::string enum_validator; if (field->type() == FieldDescriptor::TYPE_ENUM && !HasPreservingUnknownEnumSemantics(field)) { @@ -1476,22 +1496,11 @@ class ParseLoopGenerator { ", ", QualifiedClassName(field->enum_type(), options_), "_IsValid, mutable_unknown_fields(), ", field->number()); } - format_( - "ptr = $pi_ns$::Packed$1$Parser(mutable_$2$(), ptr, ctx$3$);\n", - DeclaredTypeMethodName(field->type()), FieldName(field), - enum_validator); + format_("ptr = $pi_ns$::Packed$1$Parser(mutable_$2$(), ptr, ctx$3$);\n", + DeclaredTypeMethodName(field->type()), FieldName(field), + enum_validator); } else { auto field_type = field->type(); - if (IsProto1(field->file(), options_)) { - if (field->is_packable()) { - // Sigh ... packed fields endup as a string in proto1 - field_type = FieldDescriptor::TYPE_BYTES; - } - if (field_type == FieldDescriptor::TYPE_STRING) { - // In proto1 strings are treated as bytes - field_type = FieldDescriptor::TYPE_BYTES; - } - } switch (field_type) { case FieldDescriptor::TYPE_STRING: GenerateStrings(field, true /* utf8 */); @@ -1500,7 +1509,7 @@ class ParseLoopGenerator { GenerateStrings(field, false /* utf8 */); break; case FieldDescriptor::TYPE_MESSAGE: { - if (!IsProto1(field->file(), options_) && field->is_map()) { + if (field->is_map()) { const FieldDescriptor* val = field->message_type()->FindFieldByName("value"); GOOGLE_CHECK(val); @@ -1516,8 +1525,7 @@ class ParseLoopGenerator { format_("ptr = ctx->ParseMessage(&$1$_, ptr);\n", FieldName(field)); } - } else if (!IsProto1(field->file(), options_) && - IsLazy(field, options_)) { + } else if (IsLazy(field, options_)) { if (field->containing_oneof() != nullptr) { format_( "if (!has_$1$()) {\n" @@ -1531,12 +1539,12 @@ class ParseLoopGenerator { FieldName(field), field->containing_oneof()->name()); } else if (HasFieldPresence(field->file())) { format_( - "HasBitSetters::set_has_$1$(this);\n" + "HasBitSetters::set_has_$1$(&$has_bits$);\n" "ptr = ctx->ParseMessage(&$1$_, ptr);\n", FieldName(field)); } else { - format_( - "ptr = ctx->ParseMessage(&$1$_, ptr);\n", FieldName(field)); + format_("ptr = ctx->ParseMessage(&$1$_, ptr);\n", + FieldName(field)); } } else if (IsImplicitWeakField(field, options_, scc_analyzer_)) { if (!field->is_repeated()) { @@ -1554,18 +1562,10 @@ class ParseLoopGenerator { ClassName(field->message_type())); } } else if (IsWeak(field, options_)) { - if (IsProto1(field->file(), options_)) { - format_( - "ptr = ctx->ParseMessage(" - "reinterpret_cast<$p_ns$::MessageLite*>(internal_mutable_$1$(" - ")), ptr);\n", - FieldName(field)); - } else { - format_( - "ptr = ctx->ParseMessage(_weak_field_map_.MutableMessage($1$," - " _$classname$_default_instance_.$2$_), ptr);\n", - field->number(), FieldName(field)); - } + format_( + "ptr = ctx->ParseMessage(_weak_field_map_.MutableMessage($1$," + " _$classname$_default_instance_.$2$_), ptr);\n", + field->number(), FieldName(field)); } else { format_("ptr = ctx->ParseMessage($1$_$2$(), ptr);\n", field->is_repeated() ? "add" : "mutable", FieldName(field)); @@ -1582,18 +1582,17 @@ class ParseLoopGenerator { void GenerateCaseBody(internal::WireFormatLite::WireType wiretype, const FieldDescriptor* field) { if (ShouldRepeat(field, wiretype)) { - format_("do {\n"); + format_("while (true) {\n"); format_.Indent(); } switch (wiretype) { case WireFormatLite::WIRETYPE_VARINT: { std::string type = PrimitiveTypeName(options_, field->cpp_type()); std::string prefix = field->is_repeated() ? "add" : "set"; - if (field->type() == FieldDescriptor::TYPE_ENUM && - !IsProto1(field->file(), options_)) { + if (field->type() == FieldDescriptor::TYPE_ENUM) { format_( "$uint64$ val = $pi_ns$::ReadVarint(&ptr);\n" - "$GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr);\n"); + "CHK_(ptr);\n"); if (!HasPreservingUnknownEnumSemantics(field)) { format_( "if (!$1$_IsValid(val)) {\n" @@ -1610,36 +1609,58 @@ class ParseLoopGenerator { int size = field->type() == FieldDescriptor::TYPE_SINT32 ? 32 : 64; std::string zigzag; if ((field->type() == FieldDescriptor::TYPE_SINT32 || - field->type() == FieldDescriptor::TYPE_SINT64) && - !IsProto1(field->file(), options_)) { + field->type() == FieldDescriptor::TYPE_SINT64)) { zigzag = StrCat("ZigZag", size); } - format_( - "$1$_$2$($pi_ns$::ReadVarint$3$(&ptr));\n" - "$GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr);\n", - prefix, FieldName(field), zigzag); + if (field->is_repeated() || field->containing_oneof()) { + string prefix = field->is_repeated() ? "add" : "set"; + format_( + "$1$_$2$($pi_ns$::ReadVarint$3$(&ptr));\n" + "CHK_(ptr);\n", + prefix, FieldName(field), zigzag); + } else { + if (HasFieldPresence(field->file())) { + format_("HasBitSetters::set_has_$1$(&$has_bits$);\n", + FieldName(field)); + } + format_( + "$1$_ = $pi_ns$::ReadVarint$2$(&ptr);\n" + "CHK_(ptr);\n", + FieldName(field), zigzag); + } } break; } case WireFormatLite::WIRETYPE_FIXED32: case WireFormatLite::WIRETYPE_FIXED64: { - std::string prefix = field->is_repeated() ? "add" : "set"; std::string type = PrimitiveTypeName(options_, field->cpp_type()); - format_( - "$1$_$2$($pi_ns$::UnalignedLoad<$3$>(ptr));\n" - "ptr += sizeof($3$);\n", - prefix, FieldName(field), type); + if (field->is_repeated() || field->containing_oneof()) { + string prefix = field->is_repeated() ? "add" : "set"; + format_( + "$1$_$2$($pi_ns$::UnalignedLoad<$3$>(ptr));\n" + "ptr += sizeof($3$);\n", + prefix, FieldName(field), type); + } else { + if (HasFieldPresence(field->file())) { + format_("HasBitSetters::set_has_$1$(&$has_bits$);\n", + FieldName(field)); + } + format_( + "$1$_ = $pi_ns$::UnalignedLoad<$2$>(ptr);\n" + "ptr += sizeof($2$);\n", + FieldName(field), type); + } break; } case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: { GenerateLengthDelim(field); - format_("$GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr);\n"); + format_("CHK_(ptr);\n"); break; } case WireFormatLite::WIRETYPE_START_GROUP: { format_( "ptr = ctx->ParseGroup($1$_$2$(), ptr, tag);\n" - "$GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr);\n", + "CHK_(ptr);\n", field->is_repeated() ? "add" : "mutable", FieldName(field)); break; } @@ -1651,26 +1672,26 @@ class ParseLoopGenerator { if (ShouldRepeat(field, wiretype)) { uint32 x = field->number() * 8 + wiretype; - uint64 y = 0; - int cnt = 0; - do { - y += static_cast<uint64>((x & 0x7F) + (x >= 128 ? 128 : 0)) - << (cnt++ * 8); - x >>= 7; - } while (x); - uint64 mask = (1ull << (cnt * 8)) - 1; - format_("if (ctx->Done(&ptr)) return ptr;\n"); - format_.Outdent(); + int cnt = 1; + string type = "uint8"; + if (x >= 128) { + x += (x & 0xFF80) + 128; + cnt = 2; + type = "uint16"; + } format_( - "} while (($pi_ns$::UnalignedLoad<$uint64$>(ptr)" - " & $1$) == $2$ && (ptr += $3$));\n", - mask, y, cnt); + "if (!ctx->DataAvailable(ptr)) break;\n" + "if ($pi_ns$::UnalignedLoad<$1$>(ptr) != $2$) break;\n" + "ptr += $3$;\n", + IntTypeName(options_, type), x, cnt); + format_.Outdent(); + format_("}\n"); } format_("break;\n"); } void GenerateCaseBody(const FieldDescriptor* field) { - if (!IsProto1(field->file(), options_) && field->is_packable()) { + if (field->is_packable()) { auto expected_wiretype = WireFormat::WireTypeForFieldType(field->type()); GOOGLE_CHECK(expected_wiretype != WireFormatLite::WIRETYPE_LENGTH_DELIMITED); uint32 expected_tag = @@ -1707,7 +1728,7 @@ class ParseLoopGenerator { "while (!ctx->Done(&ptr)) {\n" " $uint32$ tag;\n" " ptr = $pi_ns$::ReadTag(ptr, &tag);\n" - " $GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr);\n" + " CHK_(ptr);\n" " switch (tag >> 3) {\n"); format_.Indent(); @@ -1741,7 +1762,7 @@ class ParseLoopGenerator { format_( " if ((tag & 7) == 4 || tag == 0) {\n" " ctx->SetLastTag(tag);\n" - " return ptr;\n" + " goto success;\n" " }\n"); if (IsMapEntryMessage(descriptor)) { format_(" break;\n"); @@ -1769,13 +1790,13 @@ class ParseLoopGenerator { " ptr = _extensions_.ParseField(tag, ptr, \n" " internal_default_instance(), &_internal_metadata_, " "ctx);\n" - " $GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr != nullptr);\n" + " CHK_(ptr != nullptr);\n" " break;\n" "}\n"); } format_( " ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx);\n" - " $GOOGLE_PROTOBUF$_PARSER_ASSERT(ptr != nullptr);\n" + " CHK_(ptr != nullptr);\n" " break;\n"); } format_("}\n"); // default case @@ -1783,15 +1804,15 @@ class ParseLoopGenerator { format_.Outdent(); format_( " } // switch\n" - "} // while\n" - "return ptr;\n"); + "} // while\n"); } }; -void GenerateParserLoop(const Descriptor* descriptor, const Options& options, +void GenerateParserLoop(const Descriptor* descriptor, int num_hasbits, + const Options& options, MessageSCCAnalyzer* scc_analyzer, io::Printer* printer) { - ParseLoopGenerator generator(options, scc_analyzer, printer); + ParseLoopGenerator generator(num_hasbits, options, scc_analyzer, printer); generator.GenerateParserLoop(descriptor); } diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.h b/src/google/protobuf/compiler/cpp/cpp_helpers.h index 895749db01..f4cf005cd3 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.h +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.h @@ -75,10 +75,6 @@ inline std::string DeprecatedAttribute(const Options& options, extern const char kThickSeparator[]; extern const char kThinSeparator[]; -inline bool IsProto1(const FileDescriptor* file, const Options& options) { - return false; -} - void SetCommonVars(const Options& options, std::map<std::string, std::string>* variables); @@ -168,8 +164,8 @@ std::string FieldConstantName(const FieldDescriptor* field); // Returns the scope where the field was defined (for extensions, this is // different from the message type to which the field applies). inline const Descriptor* FieldScope(const FieldDescriptor* field) { - return field->is_extension() ? - field->extension_scope() : field->containing_type(); + return field->is_extension() ? field->extension_scope() + : field->containing_type(); } // Returns the fully-qualified type name field->message_type(). Usually this @@ -776,16 +772,15 @@ struct OneOfRangeImpl { }; Iterator begin() const { return {0, descriptor}; } - Iterator end() const { - return {descriptor->oneof_decl_count(), descriptor}; - } + Iterator end() const { return {descriptor->oneof_decl_count(), descriptor}; } const Descriptor* descriptor; }; inline OneOfRangeImpl OneOfRange(const Descriptor* desc) { return {desc}; } -void GenerateParserLoop(const Descriptor* descriptor, const Options& options, +void GenerateParserLoop(const Descriptor* descriptor, int num_hasbits, + const Options& options, MessageSCCAnalyzer* scc_analyzer, io::Printer* printer); } // namespace cpp diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc index 7ed55307ed..0738b91e63 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc @@ -89,8 +89,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, (*variables)["lite"] = "Lite"; } - if (!IsProto3Field(descriptor) && - val->type() == FieldDescriptor::TYPE_ENUM) { + if (!IsProto3Field(descriptor) && val->type() == FieldDescriptor::TYPE_ENUM) { const EnumValueDescriptor* default_value = val->default_value_enum(); (*variables)["default_enum_value"] = Int32ToString(default_value->number()); } else { @@ -106,8 +105,7 @@ MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor, MapFieldGenerator::~MapFieldGenerator() {} -void MapFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void MapFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); format( "::$proto_ns$::internal::MapField$lite$<\n" @@ -118,8 +116,8 @@ GeneratePrivateMembers(io::Printer* printer) const { " $default_enum_value$ > $name$_;\n"); } -void MapFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void MapFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$const ::$proto_ns$::Map< $key_cpp$, $val_cpp$ >&\n" @@ -129,8 +127,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void MapFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MapFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline const ::$proto_ns$::Map< $key_cpp$, $val_cpp$ >&\n" @@ -145,32 +143,29 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void MapFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void MapFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void MapFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Swap(&other->$name$_);\n"); } -void MapFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void MapFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { GenerateConstructorCode(printer); GenerateMergingCode(printer); } -void MapFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void MapFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); const FieldDescriptor* key_field = descriptor_->message_type()->FindFieldByName("key"); @@ -278,13 +273,13 @@ static void GenerateSerializationLoop(const Formatter& format, bool string_key, format("}\n"); } -void MapFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void MapFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { GenerateSerializeWithCachedSizes(printer, false); } -void MapFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void MapFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { GenerateSerializeWithCachedSizes(printer, true); } @@ -367,8 +362,7 @@ void MapFieldGenerator::GenerateSerializeWithCachedSizes(io::Printer* printer, format("}\n"); } -void MapFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void MapFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ *\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index bd17215d0d..c56cf5bc6e 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -154,8 +154,7 @@ bool CanConstructByZeroing(const FieldDescriptor* field, // Non-repeated, non-lazy message fields are simply raw pointers, so we can // use memset to initialize these in SharedCtor. We cannot use this in // Clear, as we need to potentially delete the existing value. - ret = ret || (!field->is_repeated() && - !IsLazy(field, options) && + ret = ret || (!field->is_repeated() && !IsLazy(field, options) && field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE); return ret; } @@ -270,16 +269,6 @@ bool ShouldMarkIsInitializedAsFinal(const Descriptor* descriptor, options.opensource_runtime; } -bool ShouldMarkMergePartialAsFinal(const Descriptor* descriptor, - const Options& options) { - static std::set<std::string> exclusions{ - }; - - const std::string name = ClassName(descriptor, true); - return exclusions.find(name) == exclusions.end() || - options.opensource_runtime; -} - bool ShouldMarkNewAsFinal(const Descriptor* descriptor, const Options& options) { static std::set<std::string> exclusions{ @@ -437,10 +426,10 @@ class MatchRepeatedAndHasByteAndZeroInits : public MatchRepeatedAndHasByte { // Collects neighboring fields based on a given criteria (equivalent predicate). template <typename Predicate> -std::vector<std::vector<const FieldDescriptor*> > CollectFields( +std::vector<std::vector<const FieldDescriptor*>> CollectFields( const std::vector<const FieldDescriptor*>& fields, const Predicate& equivalent) { - std::vector<std::vector<const FieldDescriptor*> > chunks; + std::vector<std::vector<const FieldDescriptor*>> chunks; if (fields.empty()) { return chunks; } @@ -520,7 +509,7 @@ class ColdChunkSkipper { return has_bit_indices_[chunks_[chunk][offset]->index()] / 32; } - const std::vector<std::vector<const FieldDescriptor*> >& chunks_; + const std::vector<std::vector<const FieldDescriptor*>>& chunks_; const std::vector<int>& has_bit_indices_; const AccessInfoMap* access_info_map_; const double cold_threshold_; @@ -848,8 +837,7 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field, } void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field, - bool is_inline, - Formatter format) { + bool is_inline, Formatter format) { // Generate clear_$name$(). if (is_inline) { format("inline "); @@ -933,7 +921,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* printer) { void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { Formatter format(printer, variables_); format.Set("class_final", - ShouldMarkClassAsFinal(descriptor_, options_) ? "final": ""); + ShouldMarkClassAsFinal(descriptor_, options_) ? "final" : ""); if (IsMapEntryMessage(descriptor_)) { std::map<std::string, std::string> vars; @@ -965,19 +953,19 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { !suffix.empty()) { if (suffix == "UTF8") { format( - " bool ValidateKey() const {\n" + " static bool ValidateKey(std::string* s) {\n" " return ::$proto_ns$::internal::WireFormatLite::" - "VerifyUtf8String(key().data(), key().size(), " + "VerifyUtf8String(s->data(), s->size(), " "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n" " }\n", descriptor_->field(0)->full_name()); } else { GOOGLE_CHECK(suffix == "UTF8Verify"); format( - " bool ValidateKey() const {\n" + " static bool ValidateKey(std::string* s) {\n" "#ifndef NDEBUG\n" " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n" - " key().data(), key().size(), ::$proto_ns$::internal::" + " s->data(), s->size(), ::$proto_ns$::internal::" "WireFormatLite::PARSE, \"$1$\");\n" "#endif\n" " return true;\n" @@ -985,25 +973,25 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { descriptor_->field(0)->full_name()); } } else { - format(" bool ValidateKey() const { return true; }\n"); + format(" static bool ValidateKey(void*) { return true; }\n"); } if (descriptor_->field(1)->type() == FieldDescriptor::TYPE_STRING && !suffix.empty()) { if (suffix == "UTF8") { format( - " bool ValidateValue() const {\n" + " static bool ValidateValue(std::string* s) {\n" " return ::$proto_ns$::internal::WireFormatLite::" - "VerifyUtf8String(value().data(), value().size(), " + "VerifyUtf8String(s->data(), s->size(), " "::$proto_ns$::internal::WireFormatLite::PARSE, \"$1$\");\n" " }\n", descriptor_->field(1)->full_name()); } else { GOOGLE_CHECK(suffix == "UTF8Verify"); format( - " bool ValidateValue() const {\n" + " static bool ValidateValue(std::string* s) {\n" "#ifndef NDEBUG\n" " ::$proto_ns$::internal::WireFormatLite::VerifyUtf8String(\n" - " value().data(), value().size(), ::$proto_ns$::internal::" + " s->data(), s->size(), ::$proto_ns$::internal::" "WireFormatLite::PARSE, \"$1$\");\n" "#endif\n" " return true;\n" @@ -1011,7 +999,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { descriptor_->field(1)->full_name()); } } else { - format(" bool ValidateValue() const { return true; }\n"); + format(" static bool ValidateValue(void*) { return true; }\n"); } if (HasDescriptorMethods(descriptor_->file(), options_)) { format( @@ -1133,7 +1121,8 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "}\n" "static constexpr int kIndexInFileMessages =\n" " $1$;\n" - "\n", index_in_file_messages_); + "\n", + index_in_file_messages_); if (SupportsArenas(descriptor_)) { format("void UnsafeArenaSwap($classname$* other);\n"); @@ -1218,9 +1207,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { format.Set( "is_initialized_final", ShouldMarkIsInitializedAsFinal(descriptor_, options_) ? "final" : ""); - format.Set( - "merge_partial_final", - ShouldMarkMergePartialAsFinal(descriptor_, options_) ? "final" : ""); format( "void CopyFrom(const $classname$& from);\n" @@ -1234,8 +1220,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { "::$proto_ns$::internal::ParseContext* ctx) final;\n" "#else\n" "bool MergePartialFromCodedStream(\n" - " ::$proto_ns$::io::CodedInputStream* input)$ " - "merge_partial_final$;\n" + " ::$proto_ns$::io::CodedInputStream* input) final;\n" "#endif // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); if (!options_.table_driven_serialization || @@ -1432,11 +1417,6 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* printer) { } if (SupportsArenas(descriptor_)) { - if (!options_.opensource_runtime) { - format( - "template <typename T> friend struct " - "::proto::internal::EmbeddedMessageHolder;\n"); - } format( "template <typename T> friend class " "::$proto_ns$::Arena::InternalHelper;\n" @@ -1702,13 +1682,10 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { vars["ptr"] = "nullptr"; if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { GOOGLE_CHECK(!IsMapEntryMessage(field->message_type())); - if (!IsProto1(field->message_type()->file(), options_)) { - vars["ptr"] = - "::" + - UniqueName("TableStruct", field->message_type(), options_) + - "::serialization_table + " + - StrCat(FindMessageIndexInFile(field->message_type())); - } + vars["ptr"] = + "::" + UniqueName("TableStruct", field->message_type(), options_) + + "::serialization_table + " + + StrCat(FindMessageIndexInFile(field->message_type())); } Formatter::SaveState saver(&format); format.AddMap(vars); @@ -1777,9 +1754,8 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { tag, FindMessageIndexInFile(field->message_type()), QualifiedClassName(field->message_type(), options_)); continue; - } else if (!IsProto1(field->message_type()->file(), options_) && - !field->message_type()->options().message_set_wire_format()) { - // Proto1 or message_set don't have the usual table and we need to + } else if (!field->message_type()->options().message_set_wire_format()) { + // message_set doesn't have the usual table and we need to // dispatch to generated serializer, hence ptr stays zero. ptr = "::" + UniqueName("TableStruct", field->message_type(), options_) + @@ -1869,8 +1845,7 @@ void MessageGenerator::GenerateDefaultInstanceInitializer( for (auto field : FieldRange(descriptor_)) { Formatter::SaveState saver(&format); - if (!field->is_repeated() && - !IsLazy(field, options_) && + if (!field->is_repeated() && !IsLazy(field, options_) && field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && (field->containing_oneof() == NULL || HasDescriptorMethods(descriptor_->file(), options_))) { @@ -1977,21 +1952,21 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) { "class $classname$::HasBitSetters {\n" " public:\n"); format.Indent(); + if (HasFieldPresence(descriptor_->file()) && HasBitsSize() != 0) { + format( + "using HasBits = decltype(std::declval<$classname$>()._has_bits_);\n"); + } for (auto field : FieldRange(descriptor_)) { field_generators_.get(field).GenerateInternalAccessorDeclarations(printer); if (HasFieldPresence(descriptor_->file()) && !field->is_repeated() && !field->options().weak() && !field->containing_oneof()) { int has_bit_index = has_bit_indices_[field->index()]; GOOGLE_CHECK_GE(has_bit_index, 0); - - format.Set("has_array_index", has_bit_index / 32); - format.Set("has_mask", - strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); format( - "static void set_has_$1$($classname$* msg) {\n" - " msg->_has_bits_[$has_array_index$] |= 0x$has_mask$u;\n" + "static void set_has_$1$(HasBits* has_bits) {\n" + " (*has_bits)[$2$] |= $3$u;\n" "}\n", - FieldName(field)); + FieldName(field), has_bit_index / 32, (1u << (has_bit_index % 32))); } } format.Outdent(); @@ -2259,7 +2234,8 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { default_val = field->default_value_string().empty() ? "&::" + variables_["proto_ns"] + "::internal::fixed_address_empty_string" - : "&" + QualifiedClassName(descriptor_, options_) + + : "&" + + QualifiedClassName(descriptor_, options_) + "::" + MakeDefaultName(field); break; case FieldOptions::CORD: @@ -2355,8 +2331,7 @@ void MessageGenerator::GenerateSharedConstructorCode(io::Printer* printer) { Formatter format(printer, variables_); format("void $classname$::SharedCtor() {\n"); - if (scc_analyzer_ - ->GetSCCAnalysis(scc_analyzer_->GetSCC(descriptor_)) + if (scc_analyzer_->GetSCCAnalysis(scc_analyzer_->GetSCC(descriptor_)) .constructor_requires_initialization) { format( " ::$proto_ns$::internal::InitSCC(\n" @@ -2784,14 +2759,14 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { unconditional_budget -= EstimateAlignmentSize(field); } - std::vector<std::vector<const FieldDescriptor*> > chunks_frag = CollectFields( + std::vector<std::vector<const FieldDescriptor*>> chunks_frag = CollectFields( optimized_order_, MatchRepeatedAndHasByteAndZeroInits( &has_bit_indices_, HasFieldPresence(descriptor_->file()))); // Merge next non-zero initializable chunk if it has the same has_byte index // and not meeting unconditional clear condition. - std::vector<std::vector<const FieldDescriptor*> > chunks; + std::vector<std::vector<const FieldDescriptor*>> chunks; if (!HasFieldPresence(descriptor_->file())) { // Don't bother with merging without has_bit field. chunks = chunks_frag; @@ -3068,8 +3043,7 @@ void MessageGenerator::GenerateSwap(io::Printer* printer) { } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - format( - "swap(_oneof_case_[$1$], other->_oneof_case_[$1$]);\n", i); + format("swap(_oneof_case_[$1$], other->_oneof_case_[$1$]);\n", i); } if (num_weak_fields_) { @@ -3387,7 +3361,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { return; } format("#if $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); - GenerateParserLoop(descriptor_, options_, scc_analyzer_, printer); + GenerateParserLoop(descriptor_, max_has_bit_index_, options_, scc_analyzer_, + printer); format("#else // $GOOGLE_PROTOBUF$_ENABLE_EXPERIMENTAL_PARSER\n"); std::vector<const FieldDescriptor*> ordered_fields = SortFieldsByNumber(descriptor_); @@ -3539,9 +3514,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { // Emit code to parse the common, expected case. // MSVC is warning about truncating constant in the static_cast so // we truncate the tag explicitly. - format( - "if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - WireFormat::MakeTag(field)); + format("if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + WireFormat::MakeTag(field)); format.Indent(); if (field->is_packed()) { @@ -3557,9 +3531,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { WireFormat::WireTypeForFieldType(field->type()); const uint32 tag = internal::WireFormatLite::MakeTag(field->number(), wiretype); - format( - "} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - tag); + format("} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + tag); format.Indent(); field_generator.GenerateMergeFromCodedStream(printer); @@ -3569,9 +3542,8 @@ void MessageGenerator::GenerateMergeFromCodedStream(io::Printer* printer) { internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED; const uint32 tag = internal::WireFormatLite::MakeTag(field->number(), wiretype); - format( - "} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", - tag); + format("} else if (static_cast< $uint8$>(tag) == ($1$ & 0xFF)) {\n", + tag); format.Indent(); field_generator.GenerateMergeFromCodedStreamWithPacking(printer); format.Outdent(); @@ -4178,7 +4150,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) { } } - std::vector<std::vector<const FieldDescriptor*> > chunks = CollectFields( + std::vector<std::vector<const FieldDescriptor*>> chunks = CollectFields( optimized_order_, MatchRepeatedAndHasByteAndRequired( &has_bit_indices_, HasFieldPresence(descriptor_->file()))); diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.cc b/src/google/protobuf/compiler/cpp/cpp_message_field.cc index 32653bb973..053e4fc8ef 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.cc @@ -91,8 +91,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, // NOTE: Escaped here to unblock proto1->proto2 migration. // TODO(liujisi): Extend this to apply for other conflicting methods. (*variables)["release_name"] = - SafeFunctionName(descriptor->containing_type(), - descriptor, "release_"); + SafeFunctionName(descriptor->containing_type(), descriptor, "release_"); (*variables)["full_name"] = descriptor->full_name(); } @@ -111,8 +110,7 @@ MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, MessageFieldGenerator::~MessageFieldGenerator() {} -void MessageFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void MessageFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format("::$proto_ns$::MessageLite* $name$_;\n"); @@ -121,8 +119,8 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void MessageFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$const $type$& ${1$$name$$}$() const;\n" @@ -165,8 +163,8 @@ void MessageFieldGenerator::GenerateNonInlineAccessorDefinitions( } } -void MessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MessageFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline const $type$& $classname$::$name$() const {\n" @@ -271,8 +269,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void MessageFieldGenerator:: -GenerateInternalAccessorDeclarations(io::Printer* printer) const { +void MessageFieldGenerator::GenerateInternalAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -285,8 +283,8 @@ GenerateInternalAccessorDeclarations(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateInternalAccessorDefinitions(io::Printer* printer) const { +void MessageFieldGenerator::GenerateInternalAccessorDefinitions( + io::Printer* printer) const { // In theory, these accessors could be inline in HasBitSetters. However, in // practice, the linker is then not able to throw them out making implicit // weak dependencies not work at all. @@ -363,8 +361,7 @@ GenerateInternalAccessorDefinitions(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (!HasFieldPresence(descriptor_->file())) { // If we don't have has-bits, message presence is indicated only by ptr != @@ -379,8 +376,8 @@ GenerateClearingCode(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (!HasFieldPresence(descriptor_->file())) { // If we don't have has-bits, message presence is indicated only by ptr != @@ -397,8 +394,7 @@ GenerateMessageClearingCode(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -409,14 +405,12 @@ GenerateMergingCode(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("swap($name$_, other->$name$_);\n"); } -void MessageFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateDestructorCode(io::Printer* printer) const { Formatter format(printer, variables_); if (options_.opensource_runtime) { // TODO(gerbens) Remove this when we don't need to destruct default @@ -429,14 +423,14 @@ GenerateDestructorCode(io::Printer* printer) const { format("delete $name$_;\n"); } -void MessageFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = nullptr;\n"); } -void MessageFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void MessageFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "if (from.has_$name$()) {\n" @@ -446,8 +440,8 @@ GenerateCopyConstructorCode(io::Printer* printer) const { "}\n"); } -void MessageFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void MessageFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -464,16 +458,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void MessageFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void MessageFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); format( "::$proto_ns$::internal::WireFormatLite::Write$stream_writer$(\n" " $number$, HasBitSetters::$name$(this), output);\n"); } -void MessageFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void MessageFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format( "target = ::$proto_ns$::internal::WireFormatLite::\n" @@ -481,8 +475,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { " $number$, HasBitSetters::$name$(this), target);\n"); } -void MessageFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void MessageFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ +\n" @@ -536,8 +529,8 @@ void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions( "}\n"); } -void MessageOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$* $classname$::$release_name$() {\n" @@ -609,8 +602,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void MessageOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( @@ -622,24 +615,24 @@ GenerateClearingCode(io::Printer* printer) const { } } -void MessageOneofFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { GenerateClearingCode(printer); } -void MessageOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void MessageOneofFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateDestructorCode( + io::Printer* printer) const { // We inherit from MessageFieldGenerator, so we need to override the default // behavior. } -void MessageOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void MessageOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Don't print any constructor code. The field is in a union. We allocate // space only when this field is used. } @@ -657,14 +650,14 @@ RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {} -void RepeatedMessageFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("::$proto_ns$::RepeatedPtrField< $type$ > $name$_;\n"); } -void RepeatedMessageFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$* ${1$mutable_$name$$}$(int index);\n" @@ -677,8 +670,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void RepeatedMessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$* $classname$::mutable_$name$(int index) {\n" @@ -725,8 +718,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void RepeatedMessageFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -737,8 +730,8 @@ GenerateClearingCode(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (implicit_weak_field_) { format( @@ -750,19 +743,19 @@ GenerateMergingCode(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("CastToBase(&$name$_)->InternalSwap(CastToBase(&other->$name$_));\n"); } -void RepeatedMessageFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedMessageFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { if (implicit_weak_field_) { @@ -784,8 +777,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void RepeatedMessageFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); format( "for (unsigned int i = 0,\n" @@ -805,8 +798,8 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { "}\n"); } -void RepeatedMessageFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format( "for (unsigned int i = 0,\n" @@ -817,8 +810,8 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "}\n"); } -void RepeatedMessageFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedMessageFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format( "{\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_options.h b/src/google/protobuf/compiler/cpp/cpp_options.h index 4056eccf52..6616e4ba04 100644 --- a/src/google/protobuf/compiler/cpp/cpp_options.h +++ b/src/google/protobuf/compiler/cpp/cpp_options.h @@ -44,7 +44,7 @@ namespace cpp { enum class EnforceOptimizeMode { kNoEnforcement, // Use the runtime specified by the file specific options. - kSpeed, // This is the full runtime. + kSpeed, // This is the full runtime. kLiteRuntime, }; @@ -73,5 +73,4 @@ struct Options { } // namespace protobuf } // namespace google - #endif // GOOGLE_PROTOBUF_COMPILER_CPP_OPTIONS_H__ diff --git a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc index 96b5a8bfba..10e468b0b9 100644 --- a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc @@ -38,8 +38,8 @@ #include <google/protobuf/compiler/cpp/cpp_generator.h> #include <google/protobuf/compiler/command_line_interface.h> -#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/printer.h> +#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/testing/file.h> #include <google/protobuf/testing/file.h> @@ -128,7 +128,8 @@ class TestGenerator : public CodeGenerator { // Check field accessors for a message inside oneof{}: TryInsert("test.pb.h", "field_get:foo.Bar.oneOfMessage", context); TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfMessage", context); - TryInsert("test.pb.cc", "field_set_allocated:foo.Bar.oneOfMessage", context); + TryInsert("test.pb.cc", "field_set_allocated:foo.Bar.oneOfMessage", + context); // Check field accessors for an optional enum: TryInsert("test.pb.h", "field_get:foo.Bar.optEnum", context); @@ -231,13 +232,8 @@ TEST(CppPluginTest, PluginTest) { std::string cpp_out = "--cpp_out=" + TestTempDir(); std::string test_out = "--test_out=" + TestTempDir(); - const char* argv[] = { - "protoc", - proto_path.c_str(), - cpp_out.c_str(), - test_out.c_str(), - "test.proto" - }; + const char* argv[] = {"protoc", proto_path.c_str(), cpp_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc index 9ab20c1a7e..164bb1c51f 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -52,29 +52,47 @@ namespace { // returns -1. int FixedSize(FieldDescriptor::Type type) { switch (type) { - case FieldDescriptor::TYPE_INT32 : return -1; - case FieldDescriptor::TYPE_INT64 : return -1; - case FieldDescriptor::TYPE_UINT32 : return -1; - case FieldDescriptor::TYPE_UINT64 : return -1; - case FieldDescriptor::TYPE_SINT32 : return -1; - case FieldDescriptor::TYPE_SINT64 : return -1; - case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size; - case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size; - case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size; - case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size; - case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize; - case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize; - - case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize; - case FieldDescriptor::TYPE_ENUM : return -1; - - case FieldDescriptor::TYPE_STRING : return -1; - case FieldDescriptor::TYPE_BYTES : return -1; - case FieldDescriptor::TYPE_GROUP : return -1; - case FieldDescriptor::TYPE_MESSAGE : return -1; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return -1; + case FieldDescriptor::TYPE_INT64: + return -1; + case FieldDescriptor::TYPE_UINT32: + return -1; + case FieldDescriptor::TYPE_UINT64: + return -1; + case FieldDescriptor::TYPE_SINT32: + return -1; + case FieldDescriptor::TYPE_SINT64: + return -1; + case FieldDescriptor::TYPE_FIXED32: + return WireFormatLite::kFixed32Size; + case FieldDescriptor::TYPE_FIXED64: + return WireFormatLite::kFixed64Size; + case FieldDescriptor::TYPE_SFIXED32: + return WireFormatLite::kSFixed32Size; + case FieldDescriptor::TYPE_SFIXED64: + return WireFormatLite::kSFixed64Size; + case FieldDescriptor::TYPE_FLOAT: + return WireFormatLite::kFloatSize; + case FieldDescriptor::TYPE_DOUBLE: + return WireFormatLite::kDoubleSize; + + case FieldDescriptor::TYPE_BOOL: + return WireFormatLite::kBoolSize; + case FieldDescriptor::TYPE_ENUM: + return -1; + + case FieldDescriptor::TYPE_STRING: + return -1; + case FieldDescriptor::TYPE_BYTES: + return -1; + case FieldDescriptor::TYPE_GROUP: + return -1; + case FieldDescriptor::TYPE_MESSAGE: + return -1; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return -1; @@ -108,14 +126,14 @@ PrimitiveFieldGenerator::PrimitiveFieldGenerator( PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {} -void PrimitiveFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void PrimitiveFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("$type$ $name$_;\n"); } -void PrimitiveFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$ ${1$$name$$}$() const;\n" @@ -123,8 +141,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void PrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" @@ -138,38 +156,35 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void PrimitiveFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = $default$;\n"); } -void PrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("set_$name$(from.$name$());\n"); } -void PrimitiveFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); format("swap($name$_, other->$name$_);\n"); } -void PrimitiveFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = $default$;\n"); } -void PrimitiveFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_ = from.$name$_;\n"); } -void PrimitiveFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$set_hasbit_io$\n" @@ -179,16 +194,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { " input, &$name$_)));\n"); } -void PrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); format( "::$proto_ns$::internal::WireFormatLite::Write$declared_type$(" "$number$, this->$name$(), output);\n"); } -void PrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format( "target = " @@ -196,8 +211,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "$number$, this->$name$(), target);\n"); } -void PrimitiveFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void PrimitiveFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); int fixed_size = FixedSize(descriptor_->type()); if (fixed_size == -1) { @@ -212,17 +226,16 @@ GenerateByteSize(io::Printer* printer) const { // =================================================================== -PrimitiveOneofFieldGenerator:: -PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, - const Options& options) - : PrimitiveFieldGenerator(descriptor, options) { +PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator( + const FieldDescriptor* descriptor, const Options& options) + : PrimitiveFieldGenerator(descriptor, options) { SetCommonOneofFieldVariables(descriptor, &variables_); } PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {} -void PrimitiveOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$() const {\n" @@ -242,25 +255,25 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$field_member$ = $default$;\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void PrimitiveOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$ns$::_$classname$_default_instance_.$name$_ = $default$;\n"); } -void PrimitiveOneofFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void PrimitiveOneofFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "clear_$oneof_name$();\n" @@ -289,8 +302,8 @@ RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator( RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {} -void RepeatedPrimitiveFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("::$proto_ns$::RepeatedField< $type$ > $name$_;\n"); if (descriptor_->is_packed() && @@ -299,8 +312,8 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void RepeatedPrimitiveFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$deprecated_attr$$type$ ${1$$name$$}$(int index) const;\n" @@ -313,8 +326,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { descriptor_); } -void RepeatedPrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); format( "inline $type$ $classname$::$name$(int index) const {\n" @@ -341,37 +354,37 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.InternalSwap(&other->$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedPrimitiveFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.CopyFrom(from.$name$_);\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_((::$proto_ns$::internal::WireFormatLite::$repeated_reader$<\n" @@ -380,8 +393,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { " $tag_size$, $tag$u, input, this->mutable_$name$())));\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateMergeFromCodedStreamWithPacking( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_((::$proto_ns$::internal::WireFormatLite::$packed_reader$<\n" @@ -390,8 +403,8 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { " input, this->mutable_$name$())));\n"); } -void RepeatedPrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); bool array_written = false; if (descriptor_->is_packed()) { @@ -433,8 +446,8 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { } } -void RepeatedPrimitiveFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->is_packed()) { // Write the tag and the size. @@ -459,8 +472,8 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { } } -void RepeatedPrimitiveFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedPrimitiveFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format("{\n"); format.Indent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index a5a0a60dc7..bd25d7a84e 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -68,8 +68,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, // NOTE: Escaped here to unblock proto1->proto2 migration. // TODO(liujisi): Extend this to apply for other conflicting methods. (*variables)["release_name"] = - SafeFunctionName(descriptor->containing_type(), - descriptor, "release_"); + SafeFunctionName(descriptor->containing_type(), descriptor, "release_"); (*variables)["full_name"] = descriptor->full_name(); if (options.opensource_runtime) { @@ -96,8 +95,7 @@ StringFieldGenerator::StringFieldGenerator(const FieldDescriptor* descriptor, StringFieldGenerator::~StringFieldGenerator() {} -void StringFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void StringFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { format("::$proto_ns$::internal::InlinedStringField $name$_;\n"); @@ -118,8 +116,7 @@ GeneratePrivateMembers(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateStaticMembers(io::Printer* printer) const { +void StringFieldGenerator::GenerateStaticMembers(io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // We make the default instance public, so it can be initialized by @@ -132,8 +129,8 @@ GenerateStaticMembers(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void StringFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); // If we're using StringFieldGenerator for a field with a ctype, it's // because that ctype isn't actually implemented. In particular, this is @@ -208,8 +205,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void StringFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( @@ -389,8 +386,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { +void StringFieldGenerator::GenerateNonInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // Initialized in GenerateDefaultInstanceAllocator. @@ -400,8 +397,7 @@ GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateClearingCode(io::Printer* printer) const { Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas or not, and default // value is the empty string or not. Complexity here ensures the minimal @@ -424,8 +420,8 @@ GenerateClearingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas, field presence, or // not, and default value is the empty string or not. Complexity here ensures @@ -435,8 +431,7 @@ GenerateMessageClearingCode(io::Printer* printer) const { // If we have field presence, then the Clear() method of the protocol buffer // will have checked that this field is set. If so, we can avoid redundant // checks against default_variable. - const bool must_be_present = - HasFieldPresence(descriptor_->file()); + const bool must_be_present = HasFieldPresence(descriptor_->file()); if (inlined_ && must_be_present) { // Calling mutable_$name$() gives us a string reference and sets the has bit @@ -481,8 +476,7 @@ GenerateMessageClearingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateMergingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { // TODO(gpike): improve this @@ -494,8 +488,7 @@ GenerateMergingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateSwappingCode(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { format("$name$_.Swap(&other->$name$_);\n"); @@ -506,8 +499,7 @@ GenerateSwappingCode(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateConstructorCode(io::Printer* printer) const { Formatter format(printer, variables_); // TODO(ckennelly): Construct non-empty strings as part of the initializer // list. @@ -519,8 +511,8 @@ GenerateConstructorCode(io::Printer* printer) const { format("$name$_.UnsafeSetDefault($default_variable$);\n"); } -void StringFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); GenerateConstructorCode(printer); @@ -545,8 +537,7 @@ GenerateCopyConstructorCode(io::Printer* printer) const { format("}\n"); } -void StringFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void StringFieldGenerator::GenerateDestructorCode(io::Printer* printer) const { Formatter format(printer, variables_); if (inlined_) { // The destructor is automatically invoked. @@ -567,8 +558,8 @@ bool StringFieldGenerator::GenerateArenaDestructorCode( return true; } -void StringFieldGenerator:: -GenerateDefaultInstanceAllocator(io::Printer* printer) const { +void StringFieldGenerator::GenerateDefaultInstanceAllocator( + io::Printer* printer) const { Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { format( @@ -580,8 +571,8 @@ GenerateDefaultInstanceAllocator(io::Printer* printer) const { } } -void StringFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void StringFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); // The google3 version of proto2 has ArenaStrings and parses into them // directly, but for the open-source release, we always parse into std::string @@ -617,13 +608,12 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -bool StringFieldGenerator:: -MergeFromCodedStreamNeedsArena() const { +bool StringFieldGenerator::MergeFromCodedStreamNeedsArena() const { return !lite_ && !inlined_ && !options_.opensource_runtime; } -void StringFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void StringFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( @@ -637,8 +627,8 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { " $number$, this->$name$(), output);\n"); } -void StringFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void StringFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( @@ -652,8 +642,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { " $number$, this->$name$(), target);\n"); } -void StringFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void StringFieldGenerator::GenerateByteSize(io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ +\n" @@ -677,8 +666,8 @@ StringOneofFieldGenerator::StringOneofFieldGenerator( StringOneofFieldGenerator::~StringOneofFieldGenerator() {} -void StringOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( @@ -906,8 +895,8 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { } } -void StringOneofFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { format( @@ -918,26 +907,26 @@ GenerateClearingCode(io::Printer* printer) const { } } -void StringOneofFieldGenerator:: -GenerateMessageClearingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateMessageClearingCode( + io::Printer* printer) const { return GenerateClearingCode(printer); } -void StringOneofFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { // Don't print any swapping code. Swapping the union will swap this field. } -void StringOneofFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "$ns$::_$classname$_default_instance_.$name$_.UnsafeSetDefault(\n" " $default_variable$);\n"); } -void StringOneofFieldGenerator:: -GenerateDestructorCode(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateDestructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format( "if (has_$name$()) {\n" @@ -945,8 +934,8 @@ GenerateDestructorCode(io::Printer* printer) const { "}\n"); } -void StringOneofFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void StringOneofFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); // See above: ArenaString is not included in the open-source release. if (!options_.opensource_runtime && SupportsArenas(descriptor_) && !lite_) { @@ -982,7 +971,6 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } - // =================================================================== RepeatedStringFieldGenerator::RepeatedStringFieldGenerator( @@ -993,14 +981,14 @@ RepeatedStringFieldGenerator::RepeatedStringFieldGenerator( RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} -void RepeatedStringFieldGenerator:: -GeneratePrivateMembers(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GeneratePrivateMembers( + io::Printer* printer) const { Formatter format(printer, variables_); format("::$proto_ns$::RepeatedPtrField<std::string> $name$_;\n"); } -void RepeatedStringFieldGenerator:: -GenerateAccessorDeclarations(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateAccessorDeclarations( + io::Printer* printer) const { Formatter format(printer, variables_); // See comment above about unknown ctypes. bool unknown_ctype = descriptor_->options().ctype() != @@ -1062,8 +1050,8 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } } -void RepeatedStringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateInlineAccessorDefinitions( + io::Printer* printer) const { Formatter format(printer, variables_); if (options_.safe_boundary_check) { format( @@ -1157,37 +1145,37 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "}\n"); } -void RepeatedStringFieldGenerator:: -GenerateClearingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateClearingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.Clear();\n"); } -void RepeatedStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.MergeFrom(from.$name$_);\n"); } -void RepeatedStringFieldGenerator:: -GenerateSwappingCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateSwappingCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.InternalSwap(CastToBase(&other->$name$_));\n"); } -void RepeatedStringFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateConstructorCode( + io::Printer* printer) const { // Not needed for repeated fields. } -void RepeatedStringFieldGenerator:: -GenerateCopyConstructorCode(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateCopyConstructorCode( + io::Printer* printer) const { Formatter format(printer, variables_); format("$name$_.CopyFrom(from.$name$_);"); } -void RepeatedStringFieldGenerator:: -GenerateMergeFromCodedStream(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateMergeFromCodedStream( + io::Printer* printer) const { Formatter format(printer, variables_); format( "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" @@ -1201,8 +1189,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } } -void RepeatedStringFieldGenerator:: -GenerateSerializeWithCachedSizes(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateSerializeWithCachedSizes( + io::Printer* printer) const { Formatter format(printer, variables_); format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); format.Indent(); @@ -1219,8 +1207,8 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { "}\n"); } -void RepeatedStringFieldGenerator:: -GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateSerializeWithCachedSizesToArray( + io::Printer* printer) const { Formatter format(printer, variables_); format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); format.Indent(); @@ -1237,8 +1225,8 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "}\n"); } -void RepeatedStringFieldGenerator:: -GenerateByteSize(io::Printer* printer) const { +void RepeatedStringFieldGenerator::GenerateByteSize( + io::Printer* printer) const { Formatter format(printer, variables_); format( "total_size += $tag_size$ *\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_unittest.cc index 4a52460d3f..74310a7fd7 100644 --- a/src/google/protobuf/compiler/cpp/cpp_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_unittest.cc @@ -47,8 +47,8 @@ #include <google/protobuf/compiler/cpp/cpp_unittest.h> #include <google/protobuf/unittest.pb.h> -#include <google/protobuf/unittest_optimize_for.pb.h> #include <google/protobuf/unittest_embed_optimize_for.pb.h> +#include <google/protobuf/unittest_optimize_for.pb.h> #include <google/protobuf/test_util.h> @@ -96,8 +96,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) { // parameter names. typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage; message.AddExtension(ExtensionMessage::repeated_int32_ext, 123); - EXPECT_EQ(123, - message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); + EXPECT_EQ(123, message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); } TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) { @@ -128,7 +127,6 @@ TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingExtension) { EXPECT_EQ(123, message.GetExtension(protobuf_unittest::void_)); } - } // namespace cpp_unittest } // namespace cpp } // namespace compiler diff --git a/src/google/protobuf/compiler/cpp/metadata_test.cc b/src/google/protobuf/compiler/cpp/metadata_test.cc index 3cdda93f14..045c6a3d29 100644 --- a/src/google/protobuf/compiler/cpp/metadata_test.cc +++ b/src/google/protobuf/compiler/cpp/metadata_test.cc @@ -69,8 +69,7 @@ class CppMetadataTest : public ::testing::Test { "annotation_guard_name=guard_name:" + TestTempDir(); - const bool result = - atu::RunProtoCompiler(filename, cpp_out, &cli, file); + const bool result = atu::RunProtoCompiler(filename, cpp_out, &cli, file); if (!result) { return result; diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc index 54ddfb83c6..715d999e7e 100644 --- a/src/google/protobuf/compiler/importer.cc +++ b/src/google/protobuf/compiler/importer.cc @@ -37,10 +37,10 @@ #else #include <unistd.h> #endif -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> #include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> #include <algorithm> #include <memory> @@ -48,12 +48,12 @@ #include <google/protobuf/compiler/importer.h> #include <google/protobuf/compiler/parser.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/io/tokenizer.h> #include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/stubs/strutil.h> -#include <google/protobuf/stubs/io_win32.h> #ifdef _WIN32 #include <ctype.h> @@ -66,8 +66,8 @@ namespace compiler { #ifdef _WIN32 // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::open; #endif // Returns true if the text looks like a Windows-style absolute path, starting @@ -75,10 +75,8 @@ using google::protobuf::internal::win32::open; // copy in command_line_interface.cc? static bool IsWindowsAbsolutePath(const std::string& text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && - isalpha(text[0]) && - (text[2] == '/' || text[2] == '\\') && - text.find_last_of(':') == 1; + return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; #endif @@ -165,8 +163,7 @@ bool SourceTreeDescriptorDatabase::FindFileByName(const std::string& filename, // Parse it. output->set_name(filename); - return parser.Parse(&tokenizer, output) && - !file_error_collector.had_errors(); + return parser.Parse(&tokenizer, output) && !file_error_collector.had_errors(); } bool SourceTreeDescriptorDatabase::FindFileContainingSymbol( @@ -183,11 +180,11 @@ bool SourceTreeDescriptorDatabase::FindFileContainingExtension( // ------------------------------------------------------------------- SourceTreeDescriptorDatabase::ValidationErrorCollector:: -ValidationErrorCollector(SourceTreeDescriptorDatabase* owner) - : owner_(owner) {} + ValidationErrorCollector(SourceTreeDescriptorDatabase* owner) + : owner_(owner) {} SourceTreeDescriptorDatabase::ValidationErrorCollector:: -~ValidationErrorCollector() {} + ~ValidationErrorCollector() {} void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError( const std::string& filename, const std::string& element_name, @@ -196,7 +193,12 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError( if (owner_->error_collector_ == NULL) return; int line, column; - owner_->source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + owner_->source_locations_.FindImport(descriptor, element_name, &line, + &column); + } else { + owner_->source_locations_.Find(descriptor, location, &line, &column); + } owner_->error_collector_->AddError(filename, line, column, message); } @@ -207,7 +209,12 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning( if (owner_->error_collector_ == NULL) return; int line, column; - owner_->source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + owner_->source_locations_.FindImport(descriptor, element_name, &line, + &column); + } else { + owner_->source_locations_.Find(descriptor, location, &line, &column); + } owner_->error_collector_->AddWarning(filename, line, column, message); } @@ -215,8 +222,8 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning( Importer::Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector) - : database_(source_tree), - pool_(&database_, database_.GetValidationErrorCollector()) { + : database_(source_tree), + pool_(&database_, database_.GetValidationErrorCollector()) { pool_.EnforceWeakDependencies(true); database_.RecordErrorsTo(error_collector); } @@ -297,8 +304,8 @@ static std::string CanonicalizePath(std::string path) { // Restore leading slash. result = '/' + result; } - if (!path.empty() && LastChar(path) == '/' && - !result.empty() && LastChar(result) != '/') { + if (!path.empty() && LastChar(path) == '/' && !result.empty() && + LastChar(result) != '/') { // Restore trailing slash. result += '/'; } @@ -453,8 +460,9 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile( // We do not allow importing of paths containing things like ".." or // consecutive slashes since the compiler expects files to be uniquely // identified by file name. - last_error_message_ = "Backslashes, consecutive slashes, \".\", or \"..\" " - "are not allowed in the virtual path"; + last_error_message_ = + "Backslashes, consecutive slashes, \".\", or \"..\" " + "are not allowed in the virtual path"; return NULL; } @@ -472,8 +480,8 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile( if (errno == EACCES) { // The file exists but is not readable. - last_error_message_ = "Read access is denied for file: " + - temp_disk_file; + last_error_message_ = + "Read access is denied for file: " + temp_disk_file; return NULL; } } diff --git a/src/google/protobuf/compiler/importer.h b/src/google/protobuf/compiler/importer.h index 5f7ed3961f..1a0b47ae2f 100644 --- a/src/google/protobuf/compiler/importer.h +++ b/src/google/protobuf/compiler/importer.h @@ -37,20 +37,22 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__ -#include <string> -#include <vector> #include <set> +#include <string> #include <utility> +#include <vector> +#include <google/protobuf/compiler/parser.h> #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor_database.h> -#include <google/protobuf/compiler/parser.h> #include <google/protobuf/port_def.inc> namespace google { namespace protobuf { -namespace io { class ZeroCopyInputStream; } +namespace io { +class ZeroCopyInputStream; +} namespace compiler { @@ -154,8 +156,7 @@ class PROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase { // TODO(kenton): I feel like this class is not well-named. class PROTOBUF_EXPORT Importer { public: - Importer(SourceTree* source_tree, - MultiFileErrorCollector* error_collector); + Importer(SourceTree* source_tree, MultiFileErrorCollector* error_collector); ~Importer(); // Import the given file and build a FileDescriptor representing it. If @@ -175,9 +176,7 @@ class PROTOBUF_EXPORT Importer { // The DescriptorPool in which all imported FileDescriptors and their // contents are stored. - inline const DescriptorPool* pool() const { - return &pool_; - } + inline const DescriptorPool* pool() const { return &pool_; } void AddUnusedImportTrackFile(const std::string& file_name); void ClearUnusedImportTrackFiles(); diff --git a/src/google/protobuf/compiler/importer_unittest.cc b/src/google/protobuf/compiler/importer_unittest.cc index 355fccd30d..6d22e85886 100644 --- a/src/google/protobuf/compiler/importer_unittest.cc +++ b/src/google/protobuf/compiler/importer_unittest.cc @@ -74,14 +74,14 @@ class MockErrorCollector : public MultiFileErrorCollector { // implements ErrorCollector --------------------------------------- void AddError(const std::string& filename, int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", - filename, line, column, message); + strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, + column, message); } void AddWarning(const std::string& filename, int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&warning_text_, "$0:$1:$2: $3\n", - filename, line, column, message); + strings::SubstituteAndAppend(&warning_text_, "$0:$1:$2: $3\n", filename, + line, column, message); } }; @@ -117,8 +117,7 @@ class MockSourceTree : public SourceTree { class ImporterTest : public testing::Test { protected: - ImporterTest() - : importer_(&source_tree_, &error_collector_) {} + ImporterTest() : importer_(&source_tree_, &error_collector_) {} void AddFile(const std::string& filename, const char* text) { source_tree_.AddFile(filename, text); @@ -136,8 +135,8 @@ class ImporterTest : public testing::Test { TEST_F(ImporterTest, Import) { // Test normal importing. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "message Foo {}\n"); + "syntax = \"proto2\";\n" + "message Foo {}\n"); const FileDescriptor* file = importer_.Import("foo.proto"); EXPECT_EQ("", error_collector_.text_); @@ -153,14 +152,14 @@ TEST_F(ImporterTest, Import) { TEST_F(ImporterTest, ImportNested) { // Test that importing a file which imports another file works. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n" - "message Foo {\n" - " optional Bar bar = 1;\n" - "}\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n" + "message Foo {\n" + " optional Bar bar = 1;\n" + "}\n"); AddFile("bar.proto", - "syntax = \"proto2\";\n" - "message Bar {}\n"); + "syntax = \"proto2\";\n" + "message Bar {}\n"); // Note that both files are actually parsed by the first call to Import() // here, since foo.proto imports bar.proto. The second call just returns @@ -190,42 +189,73 @@ TEST_F(ImporterTest, ImportNested) { TEST_F(ImporterTest, FileNotFound) { // Error: Parsing a file that doesn't exist. EXPECT_TRUE(importer_.Import("foo.proto") == NULL); - EXPECT_EQ( - "foo.proto:-1:0: File not found.\n", - error_collector_.text_); + EXPECT_EQ("foo.proto:-1:0: File not found.\n", error_collector_.text_); } TEST_F(ImporterTest, ImportNotFound) { // Error: Importing a file that doesn't exist. AddFile("foo.proto", - "syntax = \"proto2\";\n" - "import \"bar.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); EXPECT_TRUE(importer_.Import("foo.proto") == NULL); EXPECT_EQ( - "bar.proto:-1:0: File not found.\n" - "foo.proto:-1:0: Import \"bar.proto\" was not found or had errors.\n", - error_collector_.text_); + "bar.proto:-1:0: File not found.\n" + "foo.proto:1:0: Import \"bar.proto\" was not found or had errors.\n", + error_collector_.text_); } TEST_F(ImporterTest, RecursiveImport) { // Error: Recursive import. AddFile("recursive1.proto", - "syntax = \"proto2\";\n" - "import \"recursive2.proto\";\n"); + "syntax = \"proto2\";\n" + "\n" + "import \"recursive2.proto\";\n"); AddFile("recursive2.proto", - "syntax = \"proto2\";\n" - "import \"recursive1.proto\";\n"); + "syntax = \"proto2\";\n" + "import \"recursive1.proto\";\n"); EXPECT_TRUE(importer_.Import("recursive1.proto") == NULL); EXPECT_EQ( - "recursive1.proto:-1:0: File recursively imports itself: recursive1.proto " + "recursive1.proto:2:0: File recursively imports itself: " + "recursive1.proto " "-> recursive2.proto -> recursive1.proto\n" - "recursive2.proto:-1:0: Import \"recursive1.proto\" was not found " + "recursive2.proto:1:0: Import \"recursive1.proto\" was not found " "or had errors.\n" - "recursive1.proto:-1:0: Import \"recursive2.proto\" was not found " + "recursive1.proto:2:0: Import \"recursive2.proto\" was not found " "or had errors.\n", - error_collector_.text_); + error_collector_.text_); +} + +TEST_F(ImporterTest, RecursiveImportSelf) { + // Error: Recursive import. + AddFile("recursive.proto", + "syntax = \"proto2\";\n" + "\n" + "import \"recursive.proto\";\n"); + + EXPECT_TRUE(importer_.Import("recursive.proto") == nullptr); + EXPECT_EQ( + "recursive.proto:2:0: File recursively imports itself: " + "recursive.proto -> recursive.proto\n", + error_collector_.text_); +} + +TEST_F(ImporterTest, LiteRuntimeImport) { + // Error: Recursive import. + AddFile("bar.proto", + "syntax = \"proto2\";\n" + "option optimize_for = LITE_RUNTIME;\n"); + AddFile("foo.proto", + "syntax = \"proto2\";\n" + "import \"bar.proto\";\n"); + + EXPECT_TRUE(importer_.Import("foo.proto") == nullptr); + EXPECT_EQ( + "foo.proto:1:0: Files that do not use optimize_for = LITE_RUNTIME " + "cannot import files which do use this option. This file is not " + "lite, but it imports \"bar.proto\" which is.\n", + error_collector_.text_); } @@ -396,23 +426,23 @@ TEST_F(DiskSourceTreeTest, DiskFileToVirtualFile) { std::string shadowing_disk_file; EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/foo", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ(DiskSourceTree::SHADOWED, - source_tree_.DiskFileToVirtualFile( - dirnames_[1] + "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[1] + "/foo", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/foo", virtual_file); EXPECT_EQ(dirnames_[0] + "/foo", shadowing_disk_file); EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - dirnames_[1] + "/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[1] + "/baz", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/baz", virtual_file); EXPECT_EQ(DiskSourceTree::SUCCESS, - source_tree_.DiskFileToVirtualFile( - dirnames_[0] + "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile( + dirnames_[0] + "/foo", &virtual_file, &shadowing_disk_file)); EXPECT_EQ("bar/foo", virtual_file); } @@ -431,55 +461,55 @@ TEST_F(DiskSourceTreeTest, DiskFileToVirtualFileCanonicalization) { // "../.." should not be considered to be under "..". EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "../../baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../../baz", &virtual_file, + &shadowing_disk_file)); // "/foo" is not mapped (it should not be misintepreted as being under "."). EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "/foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/foo", &virtual_file, + &shadowing_disk_file)); #ifdef WIN32 // "C:\foo" is not mapped (it should not be misintepreted as being under "."). EXPECT_EQ(DiskSourceTree::NO_MAPPING, - source_tree_.DiskFileToVirtualFile( - "C:\\foo", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("C:\\foo", &virtual_file, + &shadowing_disk_file)); #endif // WIN32 // But "../baz" should be. EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "../baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir1/baz", virtual_file); // "../../foo/baz" is under "../../foo". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "../../foo/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("../../foo/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir2/baz", virtual_file); // "foo/./bar/baz" is under "./foo/bar/.". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "foo/bar/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("foo/bar/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir3/baz", virtual_file); // "bar" is under ".". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "bar", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("bar", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir4/bar", virtual_file); // "/qux/baz" is under "/qux". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "/qux/baz", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/qux/baz", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("baz", virtual_file); // "/quux/bar" is under "/quux". EXPECT_EQ(DiskSourceTree::CANNOT_OPEN, - source_tree_.DiskFileToVirtualFile( - "/quux/bar", &virtual_file, &shadowing_disk_file)); + source_tree_.DiskFileToVirtualFile("/quux/bar", &virtual_file, + &shadowing_disk_file)); EXPECT_EQ("dir5/bar", virtual_file); } diff --git a/src/google/protobuf/compiler/java/java_context.cc b/src/google/protobuf/compiler/java/java_context.cc index 8c8ab83088..778d3ba1e3 100644 --- a/src/google/protobuf/compiler/java/java_context.cc +++ b/src/google/protobuf/compiler/java/java_context.cc @@ -48,8 +48,7 @@ Context::Context(const FileDescriptor* file, const Options& options) InitializeFieldGeneratorInfo(file); } -Context::~Context() { -} +Context::~Context() {} ClassNameResolver* Context::GetNameResolver() const { return name_resolver_.get(); diff --git a/src/google/protobuf/compiler/java/java_context.h b/src/google/protobuf/compiler/java/java_context.h index fd41a27054..3fa6af1778 100644 --- a/src/google/protobuf/compiler/java/java_context.h +++ b/src/google/protobuf/compiler/java/java_context.h @@ -40,16 +40,16 @@ namespace google { namespace protobuf { - class FileDescriptor; - class FieldDescriptor; - class OneofDescriptor; - class Descriptor; - class EnumDescriptor; - namespace compiler { - namespace java { - class ClassNameResolver; // name_resolver.h - } - } +class FileDescriptor; +class FieldDescriptor; +class OneofDescriptor; +class Descriptor; +class EnumDescriptor; +namespace compiler { +namespace java { +class ClassNameResolver; // name_resolver.h +} +} // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_doc_comment.cc b/src/google/protobuf/compiler/java/java_doc_comment.cc index 280b052dd6..29447ac005 100644 --- a/src/google/protobuf/compiler/java/java_doc_comment.cc +++ b/src/google/protobuf/compiler/java/java_doc_comment.cc @@ -102,8 +102,8 @@ std::string EscapeJavadoc(const std::string& input) { return result; } -static void WriteDocCommentBodyForLocation( - io::Printer* printer, const SourceLocation& location) { +static void WriteDocCommentBodyForLocation(io::Printer* printer, + const SourceLocation& location) { std::string comments = location.leading_comments.empty() ? location.trailing_comments : location.leading_comments; @@ -139,8 +139,8 @@ static void WriteDocCommentBodyForLocation( } template <typename DescriptorType> -static void WriteDocCommentBody( - io::Printer* printer, const DescriptorType* descriptor) { +static void WriteDocCommentBody(io::Printer* printer, + const DescriptorType* descriptor) { SourceLocation location; if (descriptor->GetSourceLocation(&location)) { WriteDocCommentBodyForLocation(printer, location); @@ -167,9 +167,9 @@ void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) { printer->Print("/**\n"); WriteDocCommentBody(printer, message); printer->Print( - " * Protobuf type {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(message->full_name())); + " * Protobuf type {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(message->full_name())); } void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) { @@ -183,9 +183,8 @@ void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) { // If the field is a group, the debug string might end with {. printer->Print("/**\n"); WriteDocCommentBody(printer, field); - printer->Print( - " * <code>$def$</code>\n", - "def", EscapeJavadoc(FirstLineOf(field->DebugString()))); + printer->Print(" * <code>$def$</code>\n", "def", + EscapeJavadoc(FirstLineOf(field->DebugString()))); printer->Print(" */\n"); } @@ -193,9 +192,9 @@ void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_) { printer->Print("/**\n"); WriteDocCommentBody(printer, enum_); printer->Print( - " * Protobuf enum {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(enum_->full_name())); + " * Protobuf enum {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(enum_->full_name())); } void WriteEnumValueDocComment(io::Printer* printer, @@ -203,9 +202,9 @@ void WriteEnumValueDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, value); printer->Print( - " * <code>$def$</code>\n" - " */\n", - "def", EscapeJavadoc(FirstLineOf(value->DebugString()))); + " * <code>$def$</code>\n" + " */\n", + "def", EscapeJavadoc(FirstLineOf(value->DebugString()))); } void WriteServiceDocComment(io::Printer* printer, @@ -213,9 +212,9 @@ void WriteServiceDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, service); printer->Print( - " * Protobuf service {@code $fullname$}\n" - " */\n", - "fullname", EscapeJavadoc(service->full_name())); + " * Protobuf service {@code $fullname$}\n" + " */\n", + "fullname", EscapeJavadoc(service->full_name())); } void WriteMethodDocComment(io::Printer* printer, @@ -223,9 +222,9 @@ void WriteMethodDocComment(io::Printer* printer, printer->Print("/**\n"); WriteDocCommentBody(printer, method); printer->Print( - " * <code>$def$</code>\n" - " */\n", - "def", EscapeJavadoc(FirstLineOf(method->DebugString()))); + " * <code>$def$</code>\n" + " */\n", + "def", EscapeJavadoc(FirstLineOf(method->DebugString()))); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_enum.cc b/src/google/protobuf/compiler/java/java_enum.cc index cab34cac54..0dade1c354 100644 --- a/src/google/protobuf/compiler/java/java_enum.cc +++ b/src/google/protobuf/compiler/java/java_enum.cc @@ -51,15 +51,15 @@ namespace compiler { namespace java { EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, - bool immutable_api, - Context* context) - : descriptor_(descriptor), immutable_api_(immutable_api), - context_(context), - name_resolver_(context->GetNameResolver()) { + bool immutable_api, Context* context) + : descriptor_(descriptor), + immutable_api_(immutable_api), + context_(context), + name_resolver_(context->GetNameResolver()) { for (int i = 0; i < descriptor_->value_count(); i++) { const EnumValueDescriptor* value = descriptor_->value(i); const EnumValueDescriptor* canonical_value = - descriptor_->FindValueByNumber(value->number()); + descriptor_->FindValueByNumber(value->number()); if (value == canonical_value) { canonical_values_.push_back(value); @@ -104,11 +104,9 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("@java.lang.Deprecated\n"); } if (ordinal_is_index) { - printer->Print(vars, - "$name$($number$),\n"); + printer->Print(vars, "$name$($number$),\n"); } else { - printer->Print(vars, - "$name$($index$, $number$),\n"); + printer->Print(vars, "$name$($index$, $number$),\n"); } printer->Annotate("name", canonical_values_[i]); } @@ -123,8 +121,8 @@ void EnumGenerator::Generate(io::Printer* printer) { } printer->Print( - ";\n" - "\n"); + ";\n" + "\n"); // ----------------------------------------------------------------- @@ -134,8 +132,8 @@ void EnumGenerator::Generate(io::Printer* printer) { vars["name"] = aliases_[i].value->name(); vars["canonical_name"] = aliases_[i].canonical_value->name(); WriteEnumValueDocComment(printer, aliases_[i].value); - printer->Print(vars, - "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Print( + vars, "public static final $classname$ $name$ = $canonical_name$;\n"); printer->Annotate("name", aliases_[i].value); } @@ -147,7 +145,7 @@ void EnumGenerator::Generate(io::Printer* printer) { vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); @@ -155,38 +153,38 @@ void EnumGenerator::Generate(io::Printer* printer) { // ----------------------------------------------------------------- printer->Print( - "\n" - "public final int getNumber() {\n"); + "\n" + "public final int getNumber() {\n"); if (SupportUnknownEnumValue(descriptor_->file())) { if (ordinal_is_index) { printer->Print( - " if (this == UNRECOGNIZED) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Can't get the number of an unknown enum value.\");\n" - " }\n"); + " if (this == UNRECOGNIZED) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Can't get the number of an unknown enum value.\");\n" + " }\n"); } else { printer->Print( - " if (index == -1) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Can't get the number of an unknown enum value.\");\n" - " }\n"); + " if (index == -1) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Can't get the number of an unknown enum value.\");\n" + " }\n"); } } printer->Print( - " return value;\n" - "}\n" - "\n" - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $classname$ valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $classname$ forNumber(int value) {\n" - " switch (value) {\n", - "classname", descriptor_->name()); + " return value;\n" + "}\n" + "\n" + "/**\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $classname$ valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $classname$ forNumber(int value) {\n" + " switch (value) {\n", + "classname", descriptor_->name()); printer->Indent(); printer->Indent(); @@ -199,40 +197,40 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Outdent(); printer->Print( - " default: return null;\n" - " }\n" - "}\n" - "\n" - "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n" - " internalGetValueMap() {\n" - " return internalValueMap;\n" - "}\n" - "private static final com.google.protobuf.Internal.EnumLiteMap<\n" - " $classname$> internalValueMap =\n" - " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n" - " public $classname$ findValueByNumber(int number) {\n" - " return $classname$.forNumber(number);\n" - " }\n" - " };\n" - "\n", - "classname", descriptor_->name()); + " default: return null;\n" + " }\n" + "}\n" + "\n" + "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n" + " internalGetValueMap() {\n" + " return internalValueMap;\n" + "}\n" + "private static final com.google.protobuf.Internal.EnumLiteMap<\n" + " $classname$> internalValueMap =\n" + " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n" + " public $classname$ findValueByNumber(int number) {\n" + " return $classname$.forNumber(number);\n" + " }\n" + " };\n" + "\n", + "classname", descriptor_->name()); // ----------------------------------------------------------------- // Reflection if (HasDescriptorMethods(descriptor_, context_->EnforceLite())) { printer->Print( - "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n" - " getValueDescriptor() {\n" - " return getDescriptor().getValues().get($index_text$);\n" - "}\n" - "public final com.google.protobuf.Descriptors.EnumDescriptor\n" - " getDescriptorForType() {\n" - " return getDescriptor();\n" - "}\n" - "public static final com.google.protobuf.Descriptors.EnumDescriptor\n" - " getDescriptor() {\n", - "index_text", index_text); + "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n" + " getValueDescriptor() {\n" + " return getDescriptor().getValues().get($index_text$);\n" + "}\n" + "public final com.google.protobuf.Descriptors.EnumDescriptor\n" + " getDescriptorForType() {\n" + " return getDescriptor();\n" + "}\n" + "public static final com.google.protobuf.Descriptors.EnumDescriptor\n" + " getDescriptor() {\n", + "index_text", index_text); // TODO(kenton): Cache statically? Note that we can't access descriptors // at module init time because it wouldn't work with descriptor.proto, but @@ -262,10 +260,10 @@ void EnumGenerator::Generate(io::Printer* printer) { } printer->Print( - "}\n" - "\n" - "private static final $classname$[] VALUES = ", - "classname", descriptor_->name()); + "}\n" + "\n" + "private static final $classname$[] VALUES = ", + "classname", descriptor_->name()); if (CanUseEnumValues()) { // If the constants we are going to output are exactly the ones we @@ -275,11 +273,10 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("values();\n"); } else { printer->Print( - "{\n" - " "); + "{\n" + " "); for (int i = 0; i < descriptor_->value_count(); i++) { - printer->Print("$name$, ", - "name", descriptor_->value(i)->name()); + printer->Print("$name$, ", "name", descriptor_->value(i)->name()); } printer->Print( "\n" @@ -287,24 +284,24 @@ void EnumGenerator::Generate(io::Printer* printer) { } printer->Print( - "\n" - "public static $classname$ valueOf(\n" - " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n" - " if (desc.getType() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"EnumValueDescriptor is not for this type.\");\n" - " }\n", - "classname", descriptor_->name()); + "\n" + "public static $classname$ valueOf(\n" + " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n" + " if (desc.getType() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"EnumValueDescriptor is not for this type.\");\n" + " }\n", + "classname", descriptor_->name()); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( - " if (desc.getIndex() == -1) {\n" - " return UNRECOGNIZED;\n" - " }\n"); + " if (desc.getIndex() == -1) {\n" + " return UNRECOGNIZED;\n" + " }\n"); } printer->Print( - " return VALUES[desc.getIndex()];\n" - "}\n" - "\n"); + " return VALUES[desc.getIndex()];\n" + "}\n" + "\n"); if (!ordinal_is_index) { printer->Print("private final int index;\n"); @@ -313,30 +310,27 @@ void EnumGenerator::Generate(io::Printer* printer) { // ----------------------------------------------------------------- - printer->Print( - "private final int value;\n\n"); + printer->Print("private final int value;\n\n"); if (ordinal_is_index) { - printer->Print( - "private $classname$(int value) {\n", - "classname", descriptor_->name()); + printer->Print("private $classname$(int value) {\n", "classname", + descriptor_->name()); } else { - printer->Print( - "private $classname$(int index, int value) {\n", - "classname", descriptor_->name()); + printer->Print("private $classname$(int index, int value) {\n", "classname", + descriptor_->name()); } if (HasDescriptorMethods(descriptor_, context_->EnforceLite()) && !ordinal_is_index) { printer->Print(" this.index = index;\n"); } printer->Print( - " this.value = value;\n" - "}\n"); + " this.value = value;\n" + "}\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(enum_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(enum_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); diff --git a/src/google/protobuf/compiler/java/java_enum.h b/src/google/protobuf/compiler/java/java_enum.h index d650924c98..0a2c363b53 100644 --- a/src/google/protobuf/compiler/java/java_enum.h +++ b/src/google/protobuf/compiler/java/java_enum.h @@ -42,13 +42,13 @@ namespace google { namespace protobuf { namespace compiler { - namespace java { - class Context; // context.h - class ClassNameResolver; // name_resolver.h - } -} +namespace java { +class Context; // context.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler namespace io { - class Printer; // printer.h +class Printer; // printer.h } } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_enum_field.cc b/src/google/protobuf/compiler/java/java_enum_field.cc index c30a82346b..b602bba28c 100644 --- a/src/google/protobuf/compiler/java/java_enum_field.cc +++ b/src/google/protobuf/compiler/java/java_enum_field.cc @@ -73,8 +73,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; // Use deprecated valueOf() method to be compatible with old generated code // for v2.5.0/v2.6.1. @@ -102,8 +102,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["clear_has_field_bit_builder"] = ""; (*variables)["is_field_present_message"] = - (*variables)["name"] + "_ != " + - (*variables)["default"] + ".getNumber()"; + (*variables)["name"] + "_ != " + (*variables)["default"] + + ".getNumber()"; } // For repated builders, one bit is used for whether the array is immutable. @@ -134,17 +134,16 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, // =================================================================== -ImmutableEnumFieldGenerator:: -ImmutableEnumFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), - name_resolver_(context->GetNameResolver()) { +ImmutableEnumFieldGenerator::ImmutableEnumFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); + context->GetFieldGeneratorInfo(descriptor), name_resolver_, + &variables_); } ImmutableEnumFieldGenerator::~ImmutableEnumFieldGenerator() {} @@ -157,214 +156,214 @@ int ImmutableEnumFieldGenerator::GetNumBitsForBuilder() const { return GetNumBitsForMessage(); } -void ImmutableEnumFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value();\n"); + "$deprecation$int get$capitalized_name$Value();\n"); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableEnumFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_;\n"); +void ImmutableEnumFieldGenerator::GenerateMembers(io::Printer* printer) const { + printer->Print(variables_, "private int $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_ = $default_number$;\n"); +void ImmutableEnumFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, "private int $name$_ = $default_number$;\n"); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_has_field_bit_builder$\n" - " $name$_ = value.getNumber();\n" - " $on_changed$\n" - " return this;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_has_field_bit_builder$\n" + " $name$_ = value.getNumber();\n" + " $on_changed$\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n" - " $name$_ = $default_number$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n" + " $name$_ = $default_number$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for enums } -void ImmutableEnumFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $default_number$;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default_number$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default_number$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutableEnumFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } else if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "if (other.$name$_ != $default_number$) {\n" - " set$capitalized_name$Value(other.get$capitalized_name$Value());\n" - "}\n"); + printer->Print( + variables_, + "if (other.$name$_ != $default_number$) {\n" + " set$capitalized_name$Value(other.get$capitalized_name$Value());\n" + "}\n"); } else { GOOGLE_LOG(FATAL) << "Can't reach here."; } } -void ImmutableEnumFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if ($get_has_field_bit_from_local$) {\n" - " $set_has_field_bit_to_local$;\n" - "}\n"); + "if ($get_has_field_bit_from_local$) {\n" + " $set_has_field_bit_to_local$;\n" + "}\n"); } - printer->Print(variables_, - "result.$name$_ = $name$_;\n"); + printer->Print(variables_, "result.$name$_ = $name$_;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_has_field_bit_message$\n" - "$name$_ = rawValue;\n"); + "int rawValue = input.readEnum();\n" + "$set_has_field_bit_message$\n" + "$name$_ = rawValue;\n"); } else { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - " @SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_has_field_bit_message$\n" - " $name$_ = rawValue;\n" - "}\n"); + "int rawValue = input.readEnum();\n" + " @SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " $set_has_field_bit_message$\n" + " $name$_ = rawValue;\n" + "}\n"); } } -void ImmutableEnumFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for enums } -void ImmutableEnumFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeEnum($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " output.writeEnum($number$, $name$_);\n" + "}\n"); } -void ImmutableEnumFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSize($number$, $name$_);\n" + "}\n"); } -void ImmutableEnumFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($name$_ != other.$name$_) return false;\n"); +void ImmutableEnumFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print(variables_, "if ($name$_ != other.$name$_) return false;\n"); } -void ImmutableEnumFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableEnumFieldGenerator::GenerateHashCode(io::Printer* printer) const { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + $name$_;\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + $name$_;\n"); } std::string ImmutableEnumFieldGenerator::GetBoxedType() const { @@ -373,221 +372,230 @@ std::string ImmutableEnumFieldGenerator::GetBoxedType() const { // =================================================================== -ImmutableEnumOneofFieldGenerator:: -ImmutableEnumOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableEnumFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableEnumOneofFieldGenerator::ImmutableEnumOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableEnumFieldGenerator(descriptor, messageBitIndex, builderBitIndex, + context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableEnumOneofFieldGenerator:: -~ImmutableEnumOneofFieldGenerator() {} +ImmutableEnumOneofFieldGenerator::~ImmutableEnumOneofFieldGenerator() {} -void ImmutableEnumOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return (java.lang.Integer) $oneof_name$_;\n" - " }\n" - " return $default_number$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return (java.lang.Integer) $oneof_name$_;\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(\n" - " (java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(\n" + " (java.lang.Integer) $oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ((java.lang.Integer) $oneof_name$_).intValue();\n" - " }\n" - " return $default_number$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ((java.lang.Integer) $oneof_name$_).intValue();\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(\n" - " (java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(\n" + " (java.lang.Integer) $oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.getNumber();\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.getNumber();\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "set$capitalized_name$Value(other.get$capitalized_name$Value());\n"); + printer->Print( + variables_, + "set$capitalized_name$Value(other.get$capitalized_name$Value());\n"); } else { printer->Print(variables_, - "set$capitalized_name$(other.get$capitalized_name$());\n"); + "set$capitalized_name$(other.get$capitalized_name$());\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = rawValue;\n"); + "int rawValue = input.readEnum();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = rawValue;\n"); } else { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "@SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = rawValue;\n" - "}\n"); + "int rawValue = input.readEnum();\n" + "@SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = rawValue;\n" + "}\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); +void ImmutableEnumOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); +void ImmutableEnumOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" + "}\n"); } -void ImmutableEnumOneofFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "if (get$capitalized_name$Value()\n" - " != other.get$capitalized_name$Value()) return false;\n"); + printer->Print( + variables_, + "if (get$capitalized_name$Value()\n" + " != other.get$capitalized_name$Value()) return false;\n"); } else { - printer->Print(variables_, - "if (!get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$())) return false;\n"); + printer->Print( + variables_, + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } } -void ImmutableEnumOneofFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableEnumOneofFieldGenerator::GenerateHashCode( + io::Printer* printer) const { if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$Value();\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$Value();\n"); } else { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); + printer->Print( + variables_, + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); } } // =================================================================== -RepeatedImmutableEnumFieldGenerator:: -RepeatedImmutableEnumFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableEnumFieldGenerator::RepeatedImmutableEnumFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); + context->GetFieldGeneratorInfo(descriptor), name_resolver_, + &variables_); } RepeatedImmutableEnumFieldGenerator::~RepeatedImmutableEnumFieldGenerator() {} @@ -600,398 +608,413 @@ int RepeatedImmutableEnumFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableEnumFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); + printer->Print( + variables_, + "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList();\n"); + "$deprecation$java.util.List<java.lang.Integer>\n" + "get$capitalized_name$ValueList();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value(int index);\n"); + "$deprecation$int get$capitalized_name$Value(int index);\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.util.List<java.lang.Integer> $name$_;\n" - "private static final com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$> $name$_converter_ =\n" - " new com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$>() {\n" - " public $type$ convert(java.lang.Integer from) {\n" - " @SuppressWarnings(\"deprecation\")\n" - " $type$ result = $type$.$for_number$(from);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " };\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private java.util.List<java.lang.Integer> $name$_;\n" + "private static final " + "com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$> $name$_converter_ =\n" + " new com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$>() {\n" + " public $type$ convert(java.lang.Integer from) {\n" + " @SuppressWarnings(\"deprecation\")\n" + " $type$ result = $type$.$for_number$(from);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " };\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.get(index));\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<java.lang.Integer>\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public java.util.List<java.lang.Integer>\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (descriptor_->is_packed()) { - printer->Print(variables_, - "private int $name$MemoizedSerializedSize;\n"); + printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - // One field is the list and the other field keeps track of whether the - // list is immutable. If it's immutable, the invariant is that it must - // either an instance of Collections.emptyList() or it's an ArrayList - // wrapped in a Collections.unmodifiableList() wrapper and nobody else has - // a refererence to the underlying ArrayList. This invariant allows us to - // share instances of lists between protocol buffers avoiding expensive - // memory allocations. Note, immutable is a strong guarantee here -- not - // just that the list cannot be modified via the reference but that the - // list can never be modified. - "private java.util.List<java.lang.Integer> $name$_ =\n" - " java.util.Collections.emptyList();\n" - - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new java.util.ArrayList<java.lang.Integer>($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print( + variables_, + // One field is the list and the other field keeps track of whether the + // list is immutable. If it's immutable, the invariant is that it must + // either an instance of Collections.emptyList() or it's an ArrayList + // wrapped in a Collections.unmodifiableList() wrapper and nobody else has + // a refererence to the underlying ArrayList. This invariant allows us to + // share instances of lists between protocol buffers avoiding expensive + // memory allocations. Note, immutable is a strong guarantee here -- not + // just that the list cannot be modified via the reference but that the + // list can never be modified. + "private java.util.List<java.lang.Integer> $name$_ =\n" + " java.util.Collections.emptyList();\n" + + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new java.util.ArrayList<java.lang.Integer>($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + printer->Print( + variables_, + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.get(index));\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value.getNumber());\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value.getNumber());\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value.getNumber());\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value.getNumber());\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $type$> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for ($type$ value : values) {\n" - " $name$_.add(value.getNumber());\n" - " }\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $type$> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for ($type$ value : values) {\n" + " $name$_.add(value.getNumber());\n" + " }\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = java.util.Collections.emptyList();\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = java.util.Collections.emptyList();\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<java.lang.Integer>\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); + "$deprecation$public java.util.List<java.lang.Integer>\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" - " int index, int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" + " int index, int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$add$capitalized_name$Value$}$(int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" - " java.lang.Iterable<java.lang.Integer> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for (int value : values) {\n" - " $name$_.add(value);\n" - " }\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" + " java.lang.Iterable<java.lang.Integer> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for (int value : values) {\n" + " $name$_.add(value);\n" + " }\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } void RepeatedImmutableEnumFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for enums } -void RepeatedImmutableEnumFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. - printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + printer->Print( + variables_, + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { // Read and store the enum if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "$name$_.add(rawValue);\n"); + "int rawValue = input.readEnum();\n" + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "$name$_.add(rawValue);\n"); } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "@SuppressWarnings(\"deprecation\")\n" - "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n" - " unknownFields.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n" - " $set_mutable_bit_parser$;\n" - " }\n" - " $name$_.add(rawValue);\n" - "}\n"); + printer->Print( + variables_, + "int rawValue = input.readEnum();\n" + "@SuppressWarnings(\"deprecation\")\n" + "$type$ value = $type$.$for_number$(rawValue);\n" + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" + "} else {\n" + " if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n" + " $set_mutable_bit_parser$;\n" + " }\n" + " $name$_.add(rawValue);\n" + "}\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { // Wrap GenerateParsingCode's contents with a while loop. printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int oldLimit = input.pushLimit(length);\n" - "while(input.getBytesUntilLimit() > 0) {\n"); + "int length = input.readRawVarint32();\n" + "int oldLimit = input.pushLimit(length);\n" + "while(input.getBytesUntilLimit() > 0) {\n"); printer->Indent(); GenerateParsingCode(printer); printer->Outdent(); printer->Print(variables_, - "}\n" - "input.popLimit(oldLimit);\n"); + "}\n" + "input.popLimit(oldLimit);\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); +void RepeatedImmutableEnumFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { if (descriptor_->is_packed()) { printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnumNoTag($name$_.get(i));\n" - "}\n"); + "if (get$capitalized_name$List().size() > 0) {\n" + " output.writeUInt32NoTag($tag$);\n" + " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" + "}\n" + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.writeEnumNoTag($name$_.get(i));\n" + "}\n"); } else { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnum($number$, $name$_.get(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.writeEnum($number$, $name$_.get(i));\n" + "}\n"); } } -void RepeatedImmutableEnumFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSizeNoTag($name$_.get(i));\n" - "}\n"); - printer->Print( - "size += dataSize;\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += com.google.protobuf.CodedOutputStream\n" + " .computeEnumSizeNoTag($name$_.get(i));\n" + "}\n"); + printer->Print("size += dataSize;\n"); if (descriptor_->is_packed()) { printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeUInt32SizeNoTag(dataSize);\n" - "}"); + "if (!get$capitalized_name$List().isEmpty()) {" + " size += $tag_size$;\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeUInt32SizeNoTag(dataSize);\n" + "}"); } else { - printer->Print(variables_, - "size += $tag_size$ * $name$_.size();\n"); + printer->Print(variables_, "size += $tag_size$ * $name$_.size();\n"); } // cache the data size for packed fields. if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); + printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!$name$_.equals(other.$name$_)) return false;\n"); + "if (!$name$_.equals(other.$name$_)) return false;\n"); } -void RepeatedImmutableEnumFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldGenerator::GenerateHashCode( + io::Printer* printer) const { printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + $name$_.hashCode();\n" - "}\n"); + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + $name$_.hashCode();\n" + "}\n"); } std::string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.cc b/src/google/protobuf/compiler/java/java_enum_field_lite.cc index 725997577d..e0171abfc6 100644 --- a/src/google/protobuf/compiler/java/java_enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_field_lite.cc @@ -80,8 +80,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor))); // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -100,8 +100,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["clear_has_field_bit_message"] = ""; (*variables)["is_field_present_message"] = - (*variables)["name"] + "_ != " + - (*variables)["default"] + ".getNumber()"; + (*variables)["name"] + "_ != " + (*variables)["default"] + + ".getNumber()"; } // For repeated builders, the underlying list tracks mutability state. @@ -123,12 +123,12 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, // =================================================================== -ImmutableEnumFieldLiteGenerator:: -ImmutableEnumFieldLiteGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - context_(context), name_resolver_(context->GetNameResolver()) { +ImmutableEnumFieldLiteGenerator::ImmutableEnumFieldLiteGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetEnumVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -140,221 +140,197 @@ int ImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const { return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutableEnumFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value();\n"); + "$deprecation$int get$capitalized_name$Value();\n"); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableEnumFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private int $name$_;\n"); +void ImmutableEnumFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private int $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " $type$ result = $type$.forNumber($name$_);\n" - " return result == null ? $unknown$ : result;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " $type$ result = $type$.forNumber($name$_);\n" + " return result == null ? $unknown$ : result;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$Value(int value) {\n" - " $set_has_field_bit_message$" - " $name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$Value(int value) {\n" + " $set_has_field_bit_message$" + " $name$_ = value;\n" + "}\n"); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_has_field_bit_message$\n" - " $name$_ = value.getNumber();\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_has_field_bit_message$\n" + " $name$_ = value.getNumber();\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n" - " $name$_ = $default_number$;\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n" + " $name$_ = $default_number$;\n" + "}\n"); } -void ImmutableEnumFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return instance.get$capitalized_name$Value();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return instance.get$capitalized_name$Value();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableEnumFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for enums -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (!IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, "$name$_ = $default_number$;\n"); } } -void ImmutableEnumFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { +void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitInt(has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitInt($name$_ != $default_number$, $name$_," - " other.$name$_ != $default_number$, other.$name$_);\n"); + if (descriptor_->is_required()) { + printer->Print( + variables_, + "fieldInfoForProto2Required(\n" + " reflectField($classname$.class, \"$name$_\"),\n" + " $number$,\n" + " com.google.protobuf.FieldType.$annotation_field_type$,\n" + " $bit_field_name$,\n" + " $bit_field_mask$,\n" + " false,\n" + " $type$.internalGetValueMap())"); + } else { + printer->Print( + variables_, + "fieldInfoForProto2Optional(\n" + " reflectField($classname$.class, \"$name$_\"),\n" + " $number$,\n" + " com.google.protobuf.FieldType.$annotation_field_type$,\n" + " $bit_field_name$,\n" + " $bit_field_mask$,\n" + " false,\n" + " $type$.internalGetValueMap())"); + } } else { - GOOGLE_LOG(FATAL) << "Can't reach here."; + printer->Print( + variables_, + "fieldInfoWithEnumMap(\n" + " reflectField($classname$.class, \"$name$_\"),\n" + " $number$,\n" + " com.google.protobuf.FieldType.$annotation_field_type$,\n" + " $type$.internalGetValueMap())"); } } - -void ImmutableEnumFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_has_field_bit_message$\n" - "$name$_ = rawValue;\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_has_field_bit_message$\n" - " $name$_ = rawValue;\n" - "}\n"); +void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + if (SupportFieldPresence(descriptor_->file())) { + WriteIntToUtf16CharSequence(messageBitIndex_, output); + } + printer->Print(variables_, "\"$name$_\",\n"); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); } -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for enums -} - - -void ImmutableEnumFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeEnum($number$, $name$_);\n" - "}\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, $name$_);\n" - "}\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && $name$_ == other.$name$_;\n"); -} - -void ImmutableEnumFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + $name$_;\n"); } std::string ImmutableEnumFieldLiteGenerator::GetBoxedType() const { @@ -371,196 +347,154 @@ ImmutableEnumOneofFieldLiteGenerator::ImmutableEnumOneofFieldLiteGenerator( SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableEnumOneofFieldLiteGenerator:: -~ImmutableEnumOneofFieldLiteGenerator() {} +ImmutableEnumOneofFieldLiteGenerator::~ImmutableEnumOneofFieldLiteGenerator() {} -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return (java.lang.Integer) $oneof_name$_;\n" - " }\n" - " return $default_number$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return (java.lang.Integer) $oneof_name$_;\n" + " }\n" + " return $default_number$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $type$ result = $type$.forNumber((java.lang.Integer) $oneof_name$_);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " return $default$;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $type$ result = $type$.forNumber((java.lang.Integer) " + "$oneof_name$_);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$Value(int value) {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$Value(int value) {\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.getNumber();\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.getNumber();\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); +} + +void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer) const { + printer->Print(variables_, + "fieldInfoForOneofEnum(\n" + " $number$,\n" + " $oneof_name$_,\n" + " $oneof_stored_type$.class,\n" + " $type$.internalGetValueMap())"); +} +void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); + } } - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableEnumOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" - " return instance.get$capitalized_name$Value();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" + " return instance.get$capitalized_name$Value();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$set$capitalized_name$Value$}$(int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofInt(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = rawValue;\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = rawValue;\n" + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" "}\n"); - } -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n" - "}\n"); -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "result = result && get$capitalized_name$Value()\n" - " == other.get$capitalized_name$Value();\n"); - } else { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); - } -} - -void ImmutableEnumOneofFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$Value();\n"); - } else { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().getNumber();\n"); - } + printer->Annotate("{", "}", descriptor_); } // =================================================================== @@ -579,435 +513,311 @@ RepeatedImmutableEnumFieldLiteGenerator:: } RepeatedImmutableEnumFieldLiteGenerator:: -~RepeatedImmutableEnumFieldLiteGenerator() {} + ~RepeatedImmutableEnumFieldLiteGenerator() {} int RepeatedImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); + printer->Print( + variables_, + "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList();\n"); + "$deprecation$java.util.List<java.lang.Integer>\n" + "get$capitalized_name$ValueList();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Value(int index);\n"); + "$deprecation$int get$capitalized_name$Value(int index);\n"); } } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.IntList $name$_;\n" - "private static final com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$> $name$_converter_ =\n" - " new com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$>() {\n" - " @java.lang.Override\n" - " public $type$ convert(java.lang.Integer from) {\n" - " $type$ result = $type$.forNumber(from);\n" - " return result == null ? $unknown$ : result;\n" - " }\n" - " };\n"); +void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.IntList $name$_;\n" + "private static final " + "com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$> $name$_converter_ =\n" + " new com.google.protobuf.Internal.ListAdapter.Converter<\n" + " java.lang.Integer, $type$>() {\n" + " @java.lang.Override\n" + " public $type$ convert(java.lang.Integer from) {\n" + " $type$ result = $type$.forNumber(from);\n" + " return result == null ? $unknown$ : result;\n" + " }\n" + " };\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.ListAdapter<\n" + " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_converter_.convert($name$_.getInt(index));\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_converter_.convert($name$_.getInt(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<java.lang.Integer>\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return $name$_;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<java.lang.Integer>\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return $name$_.getInt(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return $name$_.getInt(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } if (!EnableExperimentalRuntimeForLite() && descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { - printer->Print(variables_, - "private int $name$MemoizedSerializedSize;\n"); + printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n"); } // Generate private setters for the builder to proxy into. - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.setInt(index, value.getNumber());\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.setInt(index, value.getNumber());\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addInt(value.getNumber());\n" - "}\n"); + "private void add$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addInt(value.getNumber());\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable<? extends $type$> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for ($type$ value : values) {\n" - " $name$_.addInt(value.getNumber());\n" - " }\n" - "}\n"); + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable<? extends $type$> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for ($type$ value : values) {\n" + " $name$_.addInt(value.getNumber());\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = emptyIntList();\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $name$_ = emptyIntList();\n" + "}\n"); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$Value(\n" - " int index, int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.setInt(index, value);\n" - "}\n"); + "private void set$capitalized_name$Value(\n" + " int index, int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.setInt(index, value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$Value(int value) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addInt(value);\n" - "}\n"); + "private void add$capitalized_name$Value(int value) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addInt(value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$Value(\n" - " java.lang.Iterable<java.lang.Integer> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " for (int value : values) {\n" - " $name$_.addInt(value);\n" - " }\n" - "}\n"); + "private void addAll$capitalized_name$Value(\n" + " java.lang.Iterable<java.lang.Integer> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " for (int value : values) {\n" + " $name$_.addInt(value);\n" + " }\n" + "}\n"); } } +void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer) const { + printer->Print(variables_, + "fieldInfoWithEnumMap(\n" + " reflectField($classname$.class, \"$name$_\"),\n" + " $number$,\n" + " com.google.protobuf.FieldType.$annotation_field_type$,\n" + " $type$.internalGetValueMap())"); +} +void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); + if (SupportFieldPresence(descriptor_->file())) { + PrintEnumVerifierLogic(printer, descriptor_, variables_, + /*var_name=*/"$type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); + } +} -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableEnumFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return instance.get$capitalized_name$List();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return instance.get$capitalized_name$List();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $type$> values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $type$> values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<java.lang.Integer>\n" - "${$get$capitalized_name$ValueList$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$ValueList());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<java.lang.Integer>\n" + "${$get$capitalized_name$ValueList$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$ValueList());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" - " return instance.get$capitalized_name$Value(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public int " + "${$get$capitalized_name$Value$}$(int index) {\n" + " return instance.get$capitalized_name$Value(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" - " int index, int value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Value(index, value);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" + " int index, int value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Value(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$add$capitalized_name$Value$}$(int value) {\n" - " instance.add$capitalized_name$Value(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" + " instance.add$capitalized_name$Value(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" - " java.lang.Iterable<java.lang.Integer> values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$Value(values);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" + " java.lang.Iterable<java.lang.Integer> values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$Value(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for enums -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = emptyIntList();\n"); } -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.visitIntList($name$_, other.$name$_);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // Read and store the enum - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_.addInt(input.readEnum());\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - // We store the unknown value in unknown fields. - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $name$_.addInt(rawValue);\n" - "}\n"); - } -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int oldLimit = input.pushLimit(length);\n" - "while(input.getBytesUntilLimit() > 0) {\n"); - printer->Indent(); - - // Read and store the enum - if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print(variables_, - "$name$_.addInt(input.readEnum());\n"); - } else { - printer->Print(variables_, - "int rawValue = input.readEnum();\n" - "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n" - // We store the unknown value in unknown fields. - " super.mergeVarintField($number$, rawValue);\n" - "} else {\n" - " $name$_.addInt(rawValue);\n" - "}\n"); - } - - printer->Outdent(); - printer->Print(variables_, - "}\n" - "input.popLimit(oldLimit);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - GOOGLE_CHECK(!EnableExperimentalRuntimeForLite()); - - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnumNoTag($name$_.getInt(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeEnum($number$, $name$_.getInt(i));\n" - "}\n"); - } -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - GOOGLE_CHECK(!EnableExperimentalRuntimeForLite()); - - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeEnumSizeNoTag($name$_.getInt(i));\n" - "}\n"); - printer->Print( - "size += dataSize;\n"); - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeUInt32SizeNoTag(dataSize);\n" - "}"); - } else { - printer->Print(variables_, - "size += $tag_size$ * $name$_.size();\n"); - } - - // cache the data size for packed fields. - if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); - } - - printer->Outdent(); - printer->Print("}\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && $name$_.equals(other.$name$_);\n"); -} - -void RepeatedImmutableEnumFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + $name$_.hashCode();\n" - "}\n"); -} - std::string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.h b/src/google/protobuf/compiler/java/java_enum_field_lite.h index bf25ffaafe..a8955871cf 100644 --- a/src/google/protobuf/compiler/java/java_enum_field_lite.h +++ b/src/google/protobuf/compiler/java/java_enum_field_lite.h @@ -69,15 +69,9 @@ class ImmutableEnumFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; @@ -101,12 +95,9 @@ class ImmutableEnumOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableEnumOneofFieldLiteGenerator); @@ -125,16 +116,9 @@ class RepeatedImmutableEnumFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingCodeFromPacked(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; diff --git a/src/google/protobuf/compiler/java/java_enum_lite.cc b/src/google/protobuf/compiler/java/java_enum_lite.cc index 8e414290f6..c4d6f8ae41 100644 --- a/src/google/protobuf/compiler/java/java_enum_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_lite.cc @@ -60,7 +60,7 @@ EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor, for (int i = 0; i < descriptor_->value_count(); i++) { const EnumValueDescriptor* value = descriptor_->value(i); const EnumValueDescriptor* canonical_value = - descriptor_->FindValueByNumber(value->number()); + descriptor_->FindValueByNumber(value->number()); if (value == canonical_value) { canonical_values_.push_back(value); @@ -93,8 +93,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { if (canonical_values_[i]->options().deprecated()) { printer->Print("@java.lang.Deprecated\n"); } - printer->Print(vars, - "$name$($number$),\n"); + printer->Print(vars, "$name$($number$),\n"); printer->Annotate("name", canonical_values_[i]); } @@ -104,8 +103,8 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { } printer->Print( - ";\n" - "\n"); + ";\n" + "\n"); // ----------------------------------------------------------------- @@ -115,8 +114,8 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { vars["name"] = aliases_[i].value->name(); vars["canonical_name"] = aliases_[i].canonical_value->name(); WriteEnumValueDocComment(printer, aliases_[i].value); - printer->Print(vars, - "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Print( + vars, "public static final $classname$ $name$ = $canonical_name$;\n"); printer->Annotate("name", aliases_[i].value); } @@ -128,7 +127,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); @@ -208,17 +207,17 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { "classname", descriptor_->name()); printer->Print( - "private final int value;\n\n" - "private $classname$(int value) {\n", - "classname", descriptor_->name()); + "private final int value;\n\n" + "private $classname$(int value) {\n", + "classname", descriptor_->name()); printer->Print( - " this.value = value;\n" - "}\n"); + " this.value = value;\n" + "}\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(enum_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(enum_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); diff --git a/src/google/protobuf/compiler/java/java_enum_lite.h b/src/google/protobuf/compiler/java/java_enum_lite.h index 9e20e6fb71..996d44ef1e 100644 --- a/src/google/protobuf/compiler/java/java_enum_lite.h +++ b/src/google/protobuf/compiler/java/java_enum_lite.h @@ -42,13 +42,13 @@ namespace google { namespace protobuf { namespace compiler { - namespace java { - class Context; // context.h - class ClassNameResolver; // name_resolver.h - } -} +namespace java { +class Context; // context.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler namespace io { - class Printer; // printer.h +class Printer; // printer.h } } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_extension.cc b/src/google/protobuf/compiler/java/java_extension.cc index 5b744aa02a..f197d418ec 100644 --- a/src/google/protobuf/compiler/java/java_extension.cc +++ b/src/google/protobuf/compiler/java/java_extension.cc @@ -49,11 +49,12 @@ namespace java { ImmutableExtensionGenerator::ImmutableExtensionGenerator( const FieldDescriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) { if (descriptor_->extension_scope() != NULL) { - scope_ = name_resolver_->GetImmutableClassName( - descriptor_->extension_scope()); + scope_ = + name_resolver_->GetImmutableClassName(descriptor_->extension_scope()); } else { scope_ = name_resolver_->GetImmutableClassName(descriptor_->file()); } @@ -74,8 +75,9 @@ void ExtensionGenerator::InitTemplateVars( vars["number"] = StrCat(descriptor->number()); vars["constant_name"] = FieldConstantName(descriptor); vars["index"] = StrCat(descriptor->index()); - vars["default"] = descriptor->is_repeated() ? - "" : DefaultValue(descriptor, immutable, name_resolver); + vars["default"] = descriptor->is_repeated() + ? "" + : DefaultValue(descriptor, immutable, name_resolver); vars["type_constant"] = FieldTypeName(GetType(descriptor)); vars["packed"] = descriptor->is_packed() ? "true" : "false"; vars["enum_map"] = "null"; @@ -85,13 +87,13 @@ void ExtensionGenerator::InitTemplateVars( std::string singular_type; switch (java_type) { case JAVATYPE_MESSAGE: - singular_type = name_resolver->GetClassName(descriptor->message_type(), - immutable); + singular_type = + name_resolver->GetClassName(descriptor->message_type(), immutable); vars["prototype"] = singular_type + ".getDefaultInstance()"; break; case JAVATYPE_ENUM: - singular_type = name_resolver->GetClassName(descriptor->enum_type(), - immutable); + singular_type = + name_resolver->GetClassName(descriptor->enum_type(), immutable); vars["enum_map"] = singular_type + ".internalGetValueMap()"; break; case JAVATYPE_STRING: @@ -104,8 +106,9 @@ void ExtensionGenerator::InitTemplateVars( singular_type = BoxedPrimitiveTypeName(java_type); break; } - vars["type"] = descriptor->is_repeated() ? - "java.util.List<" + singular_type + ">" : singular_type; + vars["type"] = descriptor->is_repeated() + ? "java.util.List<" + singular_type + ">" + : singular_type; vars["singular_type"] = singular_type; } @@ -114,8 +117,7 @@ void ImmutableExtensionGenerator::Generate(io::Printer* printer) { const bool kUseImmutableNames = true; InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_, &vars); - printer->Print(vars, - "public static final int $constant_name$ = $number$;\n"); + printer->Print(vars, "public static final int $constant_name$ = $number$;\n"); WriteFieldDocComment(printer, descriptor_); if (descriptor_->extension_scope() == NULL) { @@ -162,10 +164,8 @@ int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode( int ImmutableExtensionGenerator::GenerateRegistrationCode( io::Printer* printer) { - printer->Print( - "registry.add($scope$.$name$);\n", - "scope", scope_, - "name", UnderscoresToCamelCaseCheckReserved(descriptor_)); + printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name", + UnderscoresToCamelCaseCheckReserved(descriptor_)); return 7; } diff --git a/src/google/protobuf/compiler/java/java_extension_lite.cc b/src/google/protobuf/compiler/java/java_extension_lite.cc index 9db28a4bfe..27225ac150 100644 --- a/src/google/protobuf/compiler/java/java_extension_lite.cc +++ b/src/google/protobuf/compiler/java/java_extension_lite.cc @@ -44,11 +44,12 @@ namespace java { ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator( const FieldDescriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) { + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) { if (descriptor_->extension_scope() != NULL) { - scope_ = name_resolver_->GetImmutableClassName( - descriptor_->extension_scope()); + scope_ = + name_resolver_->GetImmutableClassName(descriptor_->extension_scope()); } else { scope_ = name_resolver_->GetImmutableClassName(descriptor_->file()); } @@ -61,8 +62,7 @@ void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) { const bool kUseImmutableNames = true; InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_, &vars); - printer->Print(vars, - "public static final int $constant_name$ = $number$;\n"); + printer->Print(vars, "public static final int $constant_name$ = $number$;\n"); WriteFieldDocComment(printer, descriptor_); if (descriptor_->is_repeated()) { @@ -106,10 +106,8 @@ int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode( int ImmutableExtensionLiteGenerator::GenerateRegistrationCode( io::Printer* printer) { - printer->Print( - "registry.add($scope$.$name$);\n", - "scope", scope_, - "name", UnderscoresToCamelCaseCheckReserved(descriptor_)); + printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name", + UnderscoresToCamelCaseCheckReserved(descriptor_)); return 7; } diff --git a/src/google/protobuf/compiler/java/java_field.cc b/src/google/protobuf/compiler/java/java_field.cc index a18f02fb53..18a86bad04 100644 --- a/src/google/protobuf/compiler/java/java_field.cc +++ b/src/google/protobuf/compiler/java/java_field.cc @@ -63,15 +63,16 @@ namespace java { namespace { -ImmutableFieldGenerator* MakeImmutableGenerator( - const FieldDescriptor* field, int messageBitIndex, int builderBitIndex, - Context* context) { +ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field, + int messageBitIndex, + int builderBitIndex, + Context* context) { if (field->is_repeated()) { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: if (IsMapEntry(field->message_type())) { - return new ImmutableMapFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableMapFieldGenerator(field, messageBitIndex, + builderBitIndex, context); } else { return new RepeatedImmutableMessageFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -93,8 +94,8 @@ ImmutableFieldGenerator* MakeImmutableGenerator( return new ImmutableMessageOneofFieldGenerator( field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: - return new ImmutableEnumOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_STRING: return new ImmutableStringOneofFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -105,17 +106,17 @@ ImmutableFieldGenerator* MakeImmutableGenerator( } else { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - return new ImmutableMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableMessageFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_ENUM: - return new ImmutableEnumFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableEnumFieldGenerator(field, messageBitIndex, + builderBitIndex, context); case JAVATYPE_STRING: - return new ImmutableStringFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutableStringFieldGenerator(field, messageBitIndex, + builderBitIndex, context); default: - return new ImmutablePrimitiveFieldGenerator( - field, messageBitIndex, builderBitIndex, context); + return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex, + builderBitIndex, context); } } } @@ -193,18 +194,13 @@ static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) { ImmutableFieldGenerator::~ImmutableFieldGenerator() {} -void ImmutableFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { +void ImmutableFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { ReportUnexpectedPackedFieldsCall(printer); } ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {} -void ImmutableFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - ReportUnexpectedPackedFieldsCall(printer); -} - // =================================================================== template <> @@ -224,7 +220,7 @@ FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap( } } -template<> +template <> FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap() {} template <> @@ -242,7 +238,7 @@ FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap( } } -template<> +template <> FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap() {} @@ -271,10 +267,10 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor, (*variables)["oneof_capitalized_name"] = info->capitalized_name; (*variables)["oneof_index"] = StrCat(descriptor->containing_oneof()->index()); + (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); (*variables)["set_oneof_case_message"] = info->name + "Case_ = " + StrCat(descriptor->number()); - (*variables)["clear_oneof_case_message"] = info->name + - "Case_ = 0"; + (*variables)["clear_oneof_case_message"] = info->name + "Case_ = 0"; (*variables)["has_oneof_case_message"] = info->name + "Case_ == " + StrCat(descriptor->number()); } diff --git a/src/google/protobuf/compiler/java/java_field.h b/src/google/protobuf/compiler/java/java_field.h index abefc58972..8a88d69bad 100644 --- a/src/google/protobuf/compiler/java/java_field.h +++ b/src/google/protobuf/compiler/java/java_field.h @@ -39,9 +39,9 @@ #include <memory> #include <string> +#include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> #include <google/protobuf/descriptor.h> -#include <google/protobuf/stubs/logging.h> namespace google { namespace protobuf { @@ -103,20 +103,8 @@ class ImmutableFieldLiteGenerator { virtual void GenerateMembers(io::Printer* printer) const = 0; virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; virtual void GenerateInitializationCode(io::Printer* printer) const = 0; - virtual void GenerateVisitCode(io::Printer* printer) const = 0; - virtual void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) - const = 0; - virtual void GenerateParsingCode(io::Printer* printer) const = 0; - virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const; - virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0; - virtual void GenerateSerializationCode(io::Printer* printer) const = 0; - virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; - virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer) - const = 0; - - virtual void GenerateEqualsCode(io::Printer* printer) const = 0; - virtual void GenerateHashCode(io::Printer* printer) const = 0; - + virtual void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const = 0; virtual std::string GetBoxedType() const = 0; @@ -126,11 +114,10 @@ class ImmutableFieldLiteGenerator { // Convenience class which constructs FieldGenerators for a Descriptor. -template<typename FieldGeneratorType> +template <typename FieldGeneratorType> class FieldGeneratorMap { public: - explicit FieldGeneratorMap(const Descriptor* descriptor, - Context* context); + explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context); ~FieldGeneratorMap(); const FieldGeneratorType& get(const FieldDescriptor* field) const; @@ -144,20 +131,19 @@ class FieldGeneratorMap { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; -template<typename FieldGeneratorType> -inline const FieldGeneratorType& -FieldGeneratorMap<FieldGeneratorType>::get(const FieldDescriptor* field) const { +template <typename FieldGeneratorType> +inline const FieldGeneratorType& FieldGeneratorMap<FieldGeneratorType>::get( + const FieldDescriptor* field) const { GOOGLE_CHECK_EQ(field->containing_type(), descriptor_); return *field_generators_[field->index()]; } // Instantiate template for mutable and immutable maps. -template<> -FieldGeneratorMap<ImmutableFieldGenerator>:: -FieldGeneratorMap(const Descriptor* descriptor, - Context* context); +template <> +FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap( + const Descriptor* descriptor, Context* context); -template<> +template <> FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap(); diff --git a/src/google/protobuf/compiler/java/java_file.cc b/src/google/protobuf/compiler/java/java_file.cc index e963035967..e8953911b3 100644 --- a/src/google/protobuf/compiler/java/java_file.cc +++ b/src/google/protobuf/compiler/java/java_file.cc @@ -63,26 +63,26 @@ namespace java { namespace { struct FieldDescriptorCompare { - bool operator ()(const FieldDescriptor* f1, const FieldDescriptor* f2) const { - if(f1 == NULL) { + bool operator()(const FieldDescriptor* f1, const FieldDescriptor* f2) const { + if (f1 == NULL) { return false; } - if(f2 == NULL) { + if (f2 == NULL) { return true; } return f1->full_name() < f2->full_name(); } }; -typedef std::set<const FieldDescriptor*, FieldDescriptorCompare> FieldDescriptorSet; +typedef std::set<const FieldDescriptor*, FieldDescriptorCompare> + FieldDescriptorSet; // Recursively searches the given message to collect extensions. // Returns true if all the extensions can be recognized. The extensions will be // appended in to the extensions parameter. // Returns false when there are unknown fields, in which case the data in the // extensions output parameter is not reliable and should be discarded. -bool CollectExtensions(const Message& message, - FieldDescriptorSet* extensions) { +bool CollectExtensions(const Message& message, FieldDescriptorSet* extensions) { const Reflection* reflection = message.GetReflection(); // There are unknown fields that could be extensions, thus this call fails. @@ -99,7 +99,7 @@ bool CollectExtensions(const Message& message, int size = reflection->FieldSize(message, fields[i]); for (int j = 0; j < size; j++) { const Message& sub_message = - reflection->GetRepeatedMessage(message, fields[i], j); + reflection->GetRepeatedMessage(message, fields[i], j); if (!CollectExtensions(sub_message, extensions)) return false; } } else { @@ -160,11 +160,9 @@ void CollectExtensions(const FileDescriptorProto& file_proto, // extend our methods by simply chaining them to another method // with a tail call. This inserts the sequence call-next-method, // end this one, begin-next-method as needed. -void MaybeRestartJavaMethod(io::Printer* printer, - int *bytecode_estimate, - int *method_num, - const char *chain_statement, - const char *method_decl) { +void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate, + int* method_num, const char* chain_statement, + const char* method_decl) { // The goal here is to stay under 64K bytes of jvm bytecode/method, // since otherwise we hit a hardcoded limit in the jvm and javac will // then fail with the error "code too large". This limit lets our @@ -194,8 +192,7 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, options_(options), immutable_api_(immutable_api) { classname_ = name_resolver_->GetFileClassName(file, immutable_api); - generator_factory_.reset( - new ImmutableGeneratorFactory(context_.get())); + generator_factory_.reset(new ImmutableGeneratorFactory(context_.get())); for (int i = 0; i < file_->message_type_count(); ++i) { message_generators_[i].reset( generator_factory_->NewMessageGenerator(file_->message_type(i))); @@ -217,12 +214,13 @@ bool FileGenerator::Validate(std::string* error) { NameEquality::EXACT_EQUAL)) { error->assign(file_->name()); error->append( - ": Cannot generate Java output because the file's outer class name, \""); + ": Cannot generate Java output because the file's outer class name, " + "\""); error->append(classname_); error->append( - "\", matches the name of one of the types declared inside it. " - "Please either rename the type or use the java_outer_classname " - "option to specify a different outer class name for the .proto file."); + "\", matches the name of one of the types declared inside it. " + "Please either rename the type or use the java_outer_classname " + "option to specify a different outer class name for the .proto file."); return false; } // Similar to the check above, but ignore the case this time. This is not a @@ -243,8 +241,7 @@ bool FileGenerator::Validate(std::string* error) { if (file_->options().optimize_for() == FileOptions::LITE_RUNTIME) { GOOGLE_LOG(WARNING) << "The optimize_for = LITE_RUNTIME option is no longer supported by " - << "protobuf Java code generator and may generate broken code. It " - << "will be ignored by protoc in the future and protoc will always " + << "protobuf Java code generator and is ignored--protoc will always " << "generate full runtime code for Java. To use Java Lite runtime, " << "users should use the Java Lite plugin instead. See:\n" << " " @@ -258,33 +255,32 @@ void FileGenerator::Generate(io::Printer* printer) { // We don't import anything because we refer to all classes by their // fully-qualified names in the generated source. printer->Print( - "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" - "// source: $filename$\n" - "\n", - "filename", file_->name()); + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", file_->name()); if (!java_package_.empty()) { printer->Print( - "package $package$;\n" - "\n", - "package", java_package_); + "package $package$;\n" + "\n", + "package", java_package_); } PrintGeneratedAnnotation( printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : ""); printer->Print( "$deprecation$public final class $classname$ {\n" " private $ctor$() {}\n", - "deprecation", file_->options().deprecated() ? - "@java.lang.Deprecated " : "", - "classname", classname_, - "ctor", classname_); + "deprecation", + file_->options().deprecated() ? "@java.lang.Deprecated " : "", + "classname", classname_, "ctor", classname_); printer->Annotate("classname", file_->name()); printer->Indent(); // ----------------------------------------------------------------- printer->Print( - "public static void registerAllExtensions(\n" - " com.google.protobuf.ExtensionRegistryLite registry) {\n"); + "public static void registerAllExtensions(\n" + " com.google.protobuf.ExtensionRegistryLite registry) {\n"); printer->Indent(); @@ -297,8 +293,7 @@ void FileGenerator::Generate(io::Printer* printer) { } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); if (HasDescriptorMethods(file_, context_->EnforceLite())) { // Overload registerAllExtensions for the non-lite usage to // redundantly maintain the original signature (this is @@ -306,12 +301,12 @@ void FileGenerator::Generate(io::Printer* printer) { // ExtensionRegistry in the non-lite usage). Intent is // to remove this in the future. printer->Print( - "\n" - "public static void registerAllExtensions(\n" - " com.google.protobuf.ExtensionRegistry registry) {\n" - " registerAllExtensions(\n" - " (com.google.protobuf.ExtensionRegistryLite) registry);\n" - "}\n"); + "\n" + "public static void registerAllExtensions(\n" + " com.google.protobuf.ExtensionRegistry registry) {\n" + " registerAllExtensions(\n" + " (com.google.protobuf.ExtensionRegistryLite) registry);\n" + "}\n"); } // ----------------------------------------------------------------- @@ -363,29 +358,27 @@ void FileGenerator::Generate(io::Printer* printer) { GenerateDescriptorInitializationCodeForMutable(printer); } } else { - printer->Print( - "static {\n"); + printer->Print("static {\n"); printer->Indent(); int bytecode_estimate = 0; int method_num = 0; for (int i = 0; i < file_->message_type_count(); i++) { - bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializers(printer); + bytecode_estimate += + message_generators_[i]->GenerateStaticVariableInitializers(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_$method_num$();\n", - "private static void _clinit_autosplit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_$method_num$();\n", + "private static void _clinit_autosplit_$method_num$() {\n"); } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Print( - "\n" - "// @@protoc_insertion_point(outer_class_scope)\n"); + "\n" + "// @@protoc_insertion_point(outer_class_scope)\n"); printer->Outdent(); printer->Print("}\n"); @@ -394,15 +387,15 @@ void FileGenerator::Generate(io::Printer* printer) { void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Descriptors.FileDescriptor\n" - " getDescriptor() {\n" - " return descriptor;\n" - "}\n" - "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" - " descriptor;\n" - "static {\n", - // TODO(dweis): Mark this as final. - "final", ""); + "public static com.google.protobuf.Descriptors.FileDescriptor\n" + " getDescriptor() {\n" + " return descriptor;\n" + "}\n" + "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" + " descriptor;\n" + "static {\n", + // TODO(dweis): Mark this as final. + "final", ""); printer->Indent(); SharedCodeGenerator shared_code_generator(file_, options_); @@ -412,20 +405,20 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( int method_num = 0; for (int i = 0; i < file_->message_type_count(); i++) { - bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializers(printer); + bytecode_estimate += + message_generators_[i]->GenerateStaticVariableInitializers(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$();\n", - "private static void _clinit_autosplit_dinit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$();\n", + "private static void _clinit_autosplit_dinit_$method_num$() {\n"); } for (int i = 0; i < file_->extension_count(); i++) { - bytecode_estimate += extension_generators_[i]->GenerateNonNestedInitializationCode(printer); + bytecode_estimate += + extension_generators_[i]->GenerateNonNestedInitializationCode(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$();\n", - "private static void _clinit_autosplit_dinit_$method_num$() {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$();\n", + "private static void _clinit_autosplit_dinit_$method_num$() {\n"); } // Proto compiler builds a DescriptorPool, which holds all the descriptors to @@ -454,23 +447,22 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( // Must construct an ExtensionRegistry containing all existing extensions // and use it to parse the descriptor data again to recognize extensions. printer->Print( - "com.google.protobuf.ExtensionRegistry registry =\n" - " com.google.protobuf.ExtensionRegistry.newInstance();\n"); + "com.google.protobuf.ExtensionRegistry registry =\n" + " com.google.protobuf.ExtensionRegistry.newInstance();\n"); FieldDescriptorSet::iterator it; for (it = extensions.begin(); it != extensions.end(); it++) { std::unique_ptr<ExtensionGenerator> generator( generator_factory_->NewExtensionGenerator(*it)); bytecode_estimate += generator->GenerateRegistrationCode(printer); MaybeRestartJavaMethod( - printer, - &bytecode_estimate, &method_num, - "_clinit_autosplit_dinit_$method_num$(registry);\n", - "private static void _clinit_autosplit_dinit_$method_num$(\n" - " com.google.protobuf.ExtensionRegistry registry) {\n"); + printer, &bytecode_estimate, &method_num, + "_clinit_autosplit_dinit_$method_num$(registry);\n", + "private static void _clinit_autosplit_dinit_$method_num$(\n" + " com.google.protobuf.ExtensionRegistry registry) {\n"); } printer->Print( - "com.google.protobuf.Descriptors.FileDescriptor\n" - " .internalUpdateFileDescriptor(descriptor, registry);\n"); + "com.google.protobuf.Descriptors.FileDescriptor\n" + " .internalUpdateFileDescriptor(descriptor, registry);\n"); } // Force descriptor initialization of all dependencies. @@ -478,32 +470,31 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( if (ShouldIncludeDependency(file_->dependency(i), true)) { std::string dependency = name_resolver_->GetImmutableClassName(file_->dependency(i)); - printer->Print( - "$dependency$.getDescriptor();\n", - "dependency", dependency); + printer->Print("$dependency$.getDescriptor();\n", "dependency", + dependency); } } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } -void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* printer) { +void FileGenerator::GenerateDescriptorInitializationCodeForMutable( + io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Descriptors.FileDescriptor\n" - " getDescriptor() {\n" - " return descriptor;\n" - "}\n" - "private static final com.google.protobuf.Descriptors.FileDescriptor\n" - " descriptor;\n" - "static {\n"); + "public static com.google.protobuf.Descriptors.FileDescriptor\n" + " getDescriptor() {\n" + " return descriptor;\n" + "}\n" + "private static final com.google.protobuf.Descriptors.FileDescriptor\n" + " descriptor;\n" + "static {\n"); printer->Indent(); printer->Print( - "descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n", - "immutable_package", FileJavaPackage(file_, true), - "descriptor_classname", name_resolver_->GetDescriptorClassName(file_)); + "descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n", + "immutable_package", FileJavaPackage(file_, true), "descriptor_classname", + name_resolver_->GetDescriptorClassName(file_)); for (int i = 0; i < file_->message_type_count(); i++) { message_generators_[i]->GenerateStaticVariableInitializers(printer); @@ -583,15 +574,13 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* if (ShouldIncludeDependency(file_->dependency(i), false)) { std::string dependency = name_resolver_->GetMutableClassName(file_->dependency(i)); - printer->Print( - "$dependency$.getDescriptor();\n", - "dependency", dependency); + printer->Print("$dependency$.getDescriptor();\n", "dependency", + dependency); } } printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } template <typename GeneratorClass, typename DescriptorClass> @@ -615,15 +604,15 @@ static void GenerateSibling( annotate_code ? &annotation_collector : NULL); printer.Print( - "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" - "// source: $filename$\n" - "\n", - "filename", descriptor->file()->name()); + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", descriptor->file()->name()); if (!java_package.empty()) { printer.Print( - "package $package$;\n" - "\n", - "package", java_package); + "package $package$;\n" + "\n", + "package", java_package); } (generator->*pfn)(&printer); @@ -683,8 +672,8 @@ void FileGenerator::GenerateSiblings( } } -bool FileGenerator::ShouldIncludeDependency( - const FileDescriptor* descriptor, bool immutable_api) { +bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor, + bool immutable_api) { return true; } diff --git a/src/google/protobuf/compiler/java/java_file.h b/src/google/protobuf/compiler/java/java_file.h index 9dca82bbcf..bb3e4a5765 100644 --- a/src/google/protobuf/compiler/java/java_file.h +++ b/src/google/protobuf/compiler/java/java_file.h @@ -43,20 +43,20 @@ namespace google { namespace protobuf { -class FileDescriptor; // descriptor.h +class FileDescriptor; // descriptor.h namespace io { - class Printer; // printer.h +class Printer; // printer.h } namespace compiler { - class GeneratorContext; // code_generator.h - namespace java { - class Context; // context.h - class MessageGenerator; // message.h - class GeneratorFactory; // generator_factory.h - class ExtensionGenerator; // extension.h - class ClassNameResolver; // name_resolver.h - } -} +class GeneratorContext; // code_generator.h +namespace java { +class Context; // context.h +class MessageGenerator; // message.h +class GeneratorFactory; // generator_factory.h +class ExtensionGenerator; // extension.h +class ClassNameResolver; // name_resolver.h +} // namespace java +} // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_generator.cc b/src/google/protobuf/compiler/java/java_generator.cc index 78bb6dc2ae..5045cfdc66 100644 --- a/src/google/protobuf/compiler/java/java_generator.cc +++ b/src/google/protobuf/compiler/java/java_generator.cc @@ -78,6 +78,10 @@ bool JavaGenerator::Generate(const FileDescriptor* file, file_options.generate_mutable_code = true; } else if (options[i].first == "shared") { file_options.generate_shared_code = true; + } else if (options[i].first == "lite") { + // Note: Java Lite does not guarantee API/ABI stability. We may choose to + // break existing API in order to boost performance / reduce code size. + file_options.enforce_lite = true; } else if (options[i].first == "annotate_code") { file_options.annotate_code = true; } else if (options[i].first == "annotation_list_file") { @@ -147,9 +151,9 @@ bool JavaGenerator::Generate(const FileDescriptor* file, GeneratedCodeInfo annotations; io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( &annotations); - io::Printer printer(output.get(), '$', file_options.annotate_code - ? &annotation_collector - : NULL); + io::Printer printer( + output.get(), '$', + file_options.annotate_code ? &annotation_collector : NULL); file_generator->Generate(&printer); diff --git a/src/google/protobuf/compiler/java/java_generator_factory.cc b/src/google/protobuf/compiler/java/java_generator_factory.cc index 3218b41076..3a62adb103 100644 --- a/src/google/protobuf/compiler/java/java_generator_factory.cc +++ b/src/google/protobuf/compiler/java/java_generator_factory.cc @@ -52,9 +52,8 @@ GeneratorFactory::~GeneratorFactory() {} // =================================================================== -ImmutableGeneratorFactory::ImmutableGeneratorFactory( - Context* context) : context_(context) { -} +ImmutableGeneratorFactory::ImmutableGeneratorFactory(Context* context) + : context_(context) {} ImmutableGeneratorFactory::~ImmutableGeneratorFactory() {} MessageGenerator* ImmutableGeneratorFactory::NewMessageGenerator( diff --git a/src/google/protobuf/compiler/java/java_generator_factory.h b/src/google/protobuf/compiler/java/java_generator_factory.h index 505cd3ac17..16688a56d0 100644 --- a/src/google/protobuf/compiler/java/java_generator_factory.h +++ b/src/google/protobuf/compiler/java/java_generator_factory.h @@ -37,17 +37,17 @@ namespace google { namespace protobuf { -class FieldDescriptor; // descriptor.h -class Descriptor; // descriptor.h -class ServiceDescriptor; // descriptor.h +class FieldDescriptor; // descriptor.h +class Descriptor; // descriptor.h +class ServiceDescriptor; // descriptor.h namespace compiler { - namespace java { - class MessageGenerator; // message.h - class ExtensionGenerator; // extension.h - class ServiceGenerator; // service.h - class Context; // context.h - } -} +namespace java { +class MessageGenerator; // message.h +class ExtensionGenerator; // extension.h +class ServiceGenerator; // service.h +class Context; // context.h +} // namespace java +} // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/java/java_helpers.cc b/src/google/protobuf/compiler/java/java_helpers.cc index 93b9294947..6e52b1dc38 100644 --- a/src/google/protobuf/compiler/java/java_helpers.cc +++ b/src/google/protobuf/compiler/java/java_helpers.cc @@ -58,9 +58,9 @@ using internal::WireFormat; using internal::WireFormatLite; const char kThickSeparator[] = - "// ===================================================================\n"; + "// ===================================================================\n"; const char kThinSeparator[] = - "// -------------------------------------------------------------------\n"; + "// -------------------------------------------------------------------\n"; namespace { @@ -70,21 +70,26 @@ const char* kDefaultPackage = ""; // Using them will cause the compiler to generate accessors whose names are // colliding with methods defined in base classes. const char* kForbiddenWordList[] = { - // message base class: - "cached_size", "serialized_size", - // java.lang.Object: - "class", + // message base class: + "cached_size", + "serialized_size", + // java.lang.Object: + "class", }; -const std::unordered_set<string> kReservedNames = { - "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", - "class", "const", "continue", "default", "do", "double", "else", "enum", - "extends", "final", "finally", "float", "for", "goto", "if", "implements", - "import", "instanceof", "int", "interface", "long", "native", "new", "package", - "private", "protected", "public", "return", "short", "static", "strictfp", "super", - "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", - "volatile", "while", -}; +const std::unordered_set<string>* kReservedNames = + new std::unordered_set<string>({ + "abstract", "assert", "boolean", "break", "byte", + "case", "catch", "char", "class", "const", + "continue", "default", "do", "double", "else", + "enum", "extends", "final", "finally", "float", + "for", "goto", "if", "implements", "import", + "instanceof", "int", "interface", "long", "native", + "new", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "try", "void", "volatile", "while", + }); const int kDefaultLookUpStartFieldNumber = 40; @@ -138,17 +143,16 @@ void PrintEnumVerifierLogic(io::Printer* printer, const char* var_name, const char* terminating_string, bool enforce_lite) { std::string enum_verifier_string = - (descriptor->enum_type()->file()->options().optimize_for() == - FileOptions::LITE_RUNTIME) || enforce_lite - ? StrCat(var_name, ".internalGetVerifier()") - : StrCat( - "new com.google.protobuf.Internal.EnumVerifier() {\n" - " @java.lang.Override\n" - " public boolean isInRange(int number) {\n" - " return ", var_name, ".forNumber(number) != null;\n" - " }\n" - " }" - ); + enforce_lite ? StrCat(var_name, ".internalGetVerifier()") + : StrCat( + "new com.google.protobuf.Internal.EnumVerifier() {\n" + " @java.lang.Override\n" + " public boolean isInRange(int number) {\n" + " return ", + var_name, + ".forNumber(number) != null;\n" + " }\n" + " }"); printer->Print( variables, StrCat(enum_verifier_string, terminating_string).c_str()); @@ -208,7 +212,7 @@ std::string UnderscoresToCamelCase(const MethodDescriptor* method) { std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field) { std::string name = UnderscoresToCamelCase(field); - if (kReservedNames.find(name) != kReservedNames.end()) { + if (kReservedNames->find(name) != kReservedNames->end()) { return name + "_"; } return name; @@ -376,8 +380,8 @@ JavaType GetJavaType(const FieldDescriptor* field) { case FieldDescriptor::TYPE_MESSAGE: return JAVATYPE_MESSAGE; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -386,18 +390,27 @@ JavaType GetJavaType(const FieldDescriptor* field) { const char* PrimitiveTypeName(JavaType type) { switch (type) { - case JAVATYPE_INT : return "int"; - case JAVATYPE_LONG : return "long"; - case JAVATYPE_FLOAT : return "float"; - case JAVATYPE_DOUBLE : return "double"; - case JAVATYPE_BOOLEAN: return "boolean"; - case JAVATYPE_STRING : return "java.lang.String"; - case JAVATYPE_BYTES : return "com.google.protobuf.ByteString"; - case JAVATYPE_ENUM : return NULL; - case JAVATYPE_MESSAGE: return NULL; - - // No default because we want the compiler to complain if any new - // JavaTypes are added. + case JAVATYPE_INT: + return "int"; + case JAVATYPE_LONG: + return "long"; + case JAVATYPE_FLOAT: + return "float"; + case JAVATYPE_DOUBLE: + return "double"; + case JAVATYPE_BOOLEAN: + return "boolean"; + case JAVATYPE_STRING: + return "java.lang.String"; + case JAVATYPE_BYTES: + return "com.google.protobuf.ByteString"; + case JAVATYPE_ENUM: + return NULL; + case JAVATYPE_MESSAGE: + return NULL; + + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -410,18 +423,27 @@ const char* PrimitiveTypeName(const FieldDescriptor* descriptor) { const char* BoxedPrimitiveTypeName(JavaType type) { switch (type) { - case JAVATYPE_INT : return "java.lang.Integer"; - case JAVATYPE_LONG : return "java.lang.Long"; - case JAVATYPE_FLOAT : return "java.lang.Float"; - case JAVATYPE_DOUBLE : return "java.lang.Double"; - case JAVATYPE_BOOLEAN: return "java.lang.Boolean"; - case JAVATYPE_STRING : return "java.lang.String"; - case JAVATYPE_BYTES : return "com.google.protobuf.ByteString"; - case JAVATYPE_ENUM : return NULL; - case JAVATYPE_MESSAGE: return NULL; - - // No default because we want the compiler to complain if any new - // JavaTypes are added. + case JAVATYPE_INT: + return "java.lang.Integer"; + case JAVATYPE_LONG: + return "java.lang.Long"; + case JAVATYPE_FLOAT: + return "java.lang.Float"; + case JAVATYPE_DOUBLE: + return "java.lang.Double"; + case JAVATYPE_BOOLEAN: + return "java.lang.Boolean"; + case JAVATYPE_STRING: + return "java.lang.String"; + case JAVATYPE_BYTES: + return "com.google.protobuf.ByteString"; + case JAVATYPE_ENUM: + return NULL; + case JAVATYPE_MESSAGE: + return NULL; + + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -432,30 +454,59 @@ const char* BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) { return BoxedPrimitiveTypeName(GetJavaType(descriptor)); } +std::string GetOneofStoredType(const FieldDescriptor* field) { + const JavaType javaType = GetJavaType(field); + switch (javaType) { + case JAVATYPE_ENUM: + return "java.lang.Integer"; + case JAVATYPE_MESSAGE: + return ClassName(field->message_type()); + default: + return BoxedPrimitiveTypeName(javaType); + } +} const char* FieldTypeName(FieldDescriptor::Type field_type) { switch (field_type) { - case FieldDescriptor::TYPE_INT32 : return "INT32"; - case FieldDescriptor::TYPE_UINT32 : return "UINT32"; - case FieldDescriptor::TYPE_SINT32 : return "SINT32"; - case FieldDescriptor::TYPE_FIXED32 : return "FIXED32"; - case FieldDescriptor::TYPE_SFIXED32: return "SFIXED32"; - case FieldDescriptor::TYPE_INT64 : return "INT64"; - case FieldDescriptor::TYPE_UINT64 : return "UINT64"; - case FieldDescriptor::TYPE_SINT64 : return "SINT64"; - case FieldDescriptor::TYPE_FIXED64 : return "FIXED64"; - case FieldDescriptor::TYPE_SFIXED64: return "SFIXED64"; - case FieldDescriptor::TYPE_FLOAT : return "FLOAT"; - case FieldDescriptor::TYPE_DOUBLE : return "DOUBLE"; - case FieldDescriptor::TYPE_BOOL : return "BOOL"; - case FieldDescriptor::TYPE_STRING : return "STRING"; - case FieldDescriptor::TYPE_BYTES : return "BYTES"; - case FieldDescriptor::TYPE_ENUM : return "ENUM"; - case FieldDescriptor::TYPE_GROUP : return "GROUP"; - case FieldDescriptor::TYPE_MESSAGE : return "MESSAGE"; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return "INT32"; + case FieldDescriptor::TYPE_UINT32: + return "UINT32"; + case FieldDescriptor::TYPE_SINT32: + return "SINT32"; + case FieldDescriptor::TYPE_FIXED32: + return "FIXED32"; + case FieldDescriptor::TYPE_SFIXED32: + return "SFIXED32"; + case FieldDescriptor::TYPE_INT64: + return "INT64"; + case FieldDescriptor::TYPE_UINT64: + return "UINT64"; + case FieldDescriptor::TYPE_SINT64: + return "SINT64"; + case FieldDescriptor::TYPE_FIXED64: + return "FIXED64"; + case FieldDescriptor::TYPE_SFIXED64: + return "SFIXED64"; + case FieldDescriptor::TYPE_FLOAT: + return "FLOAT"; + case FieldDescriptor::TYPE_DOUBLE: + return "DOUBLE"; + case FieldDescriptor::TYPE_BOOL: + return "BOOL"; + case FieldDescriptor::TYPE_STRING: + return "STRING"; + case FieldDescriptor::TYPE_BYTES: + return "BYTES"; + case FieldDescriptor::TYPE_ENUM: + return "ENUM"; + case FieldDescriptor::TYPE_GROUP: + return "GROUP"; + case FieldDescriptor::TYPE_MESSAGE: + return "MESSAGE"; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -517,8 +568,8 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable, if (field->has_default_value()) { // See comments in Internal.java for gory details. return strings::Substitute( - "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")", - CEscape(field->default_value_string())); + "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")", + CEscape(field->default_value_string())); } else { return "com.google.protobuf.ByteString.EMPTY"; } @@ -536,14 +587,14 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable, case FieldDescriptor::CPPTYPE_ENUM: return name_resolver->GetClassName(field->enum_type(), immutable) + "." + - field->default_value_enum()->name(); + field->default_value_enum()->name(); case FieldDescriptor::CPPTYPE_MESSAGE: return name_resolver->GetClassName(field->message_type(), immutable) + - ".getDefaultInstance()"; + ".getDefaultInstance()"; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -574,8 +625,8 @@ bool IsDefaultValueJavaDefault(const FieldDescriptor* field) { case FieldDescriptor::CPPTYPE_MESSAGE: return false; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -588,41 +639,17 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) { } const char* bit_masks[] = { - "0x00000001", - "0x00000002", - "0x00000004", - "0x00000008", - "0x00000010", - "0x00000020", - "0x00000040", - "0x00000080", - - "0x00000100", - "0x00000200", - "0x00000400", - "0x00000800", - "0x00001000", - "0x00002000", - "0x00004000", - "0x00008000", - - "0x00010000", - "0x00020000", - "0x00040000", - "0x00080000", - "0x00100000", - "0x00200000", - "0x00400000", - "0x00800000", - - "0x01000000", - "0x02000000", - "0x04000000", - "0x08000000", - "0x10000000", - "0x20000000", - "0x40000000", - "0x80000000", + "0x00000001", "0x00000002", "0x00000004", "0x00000008", + "0x00000010", "0x00000020", "0x00000040", "0x00000080", + + "0x00000100", "0x00000200", "0x00000400", "0x00000800", + "0x00001000", "0x00002000", "0x00004000", "0x00008000", + + "0x00010000", "0x00020000", "0x00040000", "0x00080000", + "0x00100000", "0x00200000", "0x00400000", "0x00800000", + + "0x01000000", "0x02000000", "0x04000000", "0x08000000", + "0x10000000", "0x20000000", "0x40000000", "0x80000000", }; std::string GetBitFieldName(int index) { @@ -693,18 +720,27 @@ std::string GenerateSetBitMutableLocal(int bitIndex) { bool IsReferenceType(JavaType type) { switch (type) { - case JAVATYPE_INT : return false; - case JAVATYPE_LONG : return false; - case JAVATYPE_FLOAT : return false; - case JAVATYPE_DOUBLE : return false; - case JAVATYPE_BOOLEAN: return false; - case JAVATYPE_STRING : return true; - case JAVATYPE_BYTES : return true; - case JAVATYPE_ENUM : return true; - case JAVATYPE_MESSAGE: return true; + case JAVATYPE_INT: + return false; + case JAVATYPE_LONG: + return false; + case JAVATYPE_FLOAT: + return false; + case JAVATYPE_DOUBLE: + return false; + case JAVATYPE_BOOLEAN: + return false; + case JAVATYPE_STRING: + return true; + case JAVATYPE_BYTES: + return true; + case JAVATYPE_ENUM: + return true; + case JAVATYPE_MESSAGE: + return true; - // No default because we want the compiler to complain if any new - // JavaTypes are added. + // No default because we want the compiler to complain if any new + // JavaTypes are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -713,29 +749,46 @@ bool IsReferenceType(JavaType type) { const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) { switch (GetType(field)) { - case FieldDescriptor::TYPE_INT32 : return "Int32"; - case FieldDescriptor::TYPE_UINT32 : return "UInt32"; - case FieldDescriptor::TYPE_SINT32 : return "SInt32"; - case FieldDescriptor::TYPE_FIXED32 : return "Fixed32"; - case FieldDescriptor::TYPE_SFIXED32: return "SFixed32"; - case FieldDescriptor::TYPE_INT64 : return "Int64"; - case FieldDescriptor::TYPE_UINT64 : return "UInt64"; - case FieldDescriptor::TYPE_SINT64 : return "SInt64"; - case FieldDescriptor::TYPE_FIXED64 : return "Fixed64"; - case FieldDescriptor::TYPE_SFIXED64: return "SFixed64"; - case FieldDescriptor::TYPE_FLOAT : return "Float"; - case FieldDescriptor::TYPE_DOUBLE : return "Double"; - case FieldDescriptor::TYPE_BOOL : return "Bool"; - case FieldDescriptor::TYPE_STRING : return "String"; - case FieldDescriptor::TYPE_BYTES : { + case FieldDescriptor::TYPE_INT32: + return "Int32"; + case FieldDescriptor::TYPE_UINT32: + return "UInt32"; + case FieldDescriptor::TYPE_SINT32: + return "SInt32"; + case FieldDescriptor::TYPE_FIXED32: + return "Fixed32"; + case FieldDescriptor::TYPE_SFIXED32: + return "SFixed32"; + case FieldDescriptor::TYPE_INT64: + return "Int64"; + case FieldDescriptor::TYPE_UINT64: + return "UInt64"; + case FieldDescriptor::TYPE_SINT64: + return "SInt64"; + case FieldDescriptor::TYPE_FIXED64: + return "Fixed64"; + case FieldDescriptor::TYPE_SFIXED64: + return "SFixed64"; + case FieldDescriptor::TYPE_FLOAT: + return "Float"; + case FieldDescriptor::TYPE_DOUBLE: + return "Double"; + case FieldDescriptor::TYPE_BOOL: + return "Bool"; + case FieldDescriptor::TYPE_STRING: + return "String"; + case FieldDescriptor::TYPE_BYTES: { return "Bytes"; } - case FieldDescriptor::TYPE_ENUM : return "Enum"; - case FieldDescriptor::TYPE_GROUP : return "Group"; - case FieldDescriptor::TYPE_MESSAGE : return "Message"; + case FieldDescriptor::TYPE_ENUM: + return "Enum"; + case FieldDescriptor::TYPE_GROUP: + return "Group"; + case FieldDescriptor::TYPE_MESSAGE: + return "Message"; - // No default because we want the compiler to complain if any new - // types are added. + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -746,29 +799,47 @@ const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) { // returns -1. int FixedSize(FieldDescriptor::Type type) { switch (type) { - case FieldDescriptor::TYPE_INT32 : return -1; - case FieldDescriptor::TYPE_INT64 : return -1; - case FieldDescriptor::TYPE_UINT32 : return -1; - case FieldDescriptor::TYPE_UINT64 : return -1; - case FieldDescriptor::TYPE_SINT32 : return -1; - case FieldDescriptor::TYPE_SINT64 : return -1; - case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size; - case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size; - case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size; - case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size; - case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize; - case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize; - - case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize; - case FieldDescriptor::TYPE_ENUM : return -1; - - case FieldDescriptor::TYPE_STRING : return -1; - case FieldDescriptor::TYPE_BYTES : return -1; - case FieldDescriptor::TYPE_GROUP : return -1; - case FieldDescriptor::TYPE_MESSAGE : return -1; - - // No default because we want the compiler to complain if any new - // types are added. + case FieldDescriptor::TYPE_INT32: + return -1; + case FieldDescriptor::TYPE_INT64: + return -1; + case FieldDescriptor::TYPE_UINT32: + return -1; + case FieldDescriptor::TYPE_UINT64: + return -1; + case FieldDescriptor::TYPE_SINT32: + return -1; + case FieldDescriptor::TYPE_SINT64: + return -1; + case FieldDescriptor::TYPE_FIXED32: + return WireFormatLite::kFixed32Size; + case FieldDescriptor::TYPE_FIXED64: + return WireFormatLite::kFixed64Size; + case FieldDescriptor::TYPE_SFIXED32: + return WireFormatLite::kSFixed32Size; + case FieldDescriptor::TYPE_SFIXED64: + return WireFormatLite::kSFixed64Size; + case FieldDescriptor::TYPE_FLOAT: + return WireFormatLite::kFloatSize; + case FieldDescriptor::TYPE_DOUBLE: + return WireFormatLite::kDoubleSize; + + case FieldDescriptor::TYPE_BOOL: + return WireFormatLite::kBoolSize; + case FieldDescriptor::TYPE_ENUM: + return -1; + + case FieldDescriptor::TYPE_STRING: + return -1; + case FieldDescriptor::TYPE_BYTES: + return -1; + case FieldDescriptor::TYPE_GROUP: + return -1; + case FieldDescriptor::TYPE_MESSAGE: + return -1; + + // No default because we want the compiler to complain if any new + // types are added. } GOOGLE_LOG(FATAL) << "Can't get here."; return -1; @@ -778,7 +849,7 @@ int FixedSize(FieldDescriptor::Type type) { // and return it. The caller should delete the returned array. const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) { const FieldDescriptor** fields = - new const FieldDescriptor*[descriptor->field_count()]; + new const FieldDescriptor*[descriptor->field_count()]; for (int i = 0; i < descriptor->field_count(); i++) { fields[i] = descriptor->field(i); } diff --git a/src/google/protobuf/compiler/java/java_helpers.h b/src/google/protobuf/compiler/java/java_helpers.h index 7bdd09ff47..3d36ec4249 100644 --- a/src/google/protobuf/compiler/java/java_helpers.h +++ b/src/google/protobuf/compiler/java/java_helpers.h @@ -157,10 +157,14 @@ inline bool IsDescriptorProto(const Descriptor* descriptor) { descriptor->file()->name() == "google/protobuf/descriptor.proto"; } +// Returns the stored type string used by the experimental runtime for oneof +// fields. +std::string GetOneofStoredType(const FieldDescriptor* field); + // Whether we should generate multiple java files for messages. -inline bool MultipleJavaFiles( - const FileDescriptor* descriptor, bool immutable) { +inline bool MultipleJavaFiles(const FileDescriptor* descriptor, + bool immutable) { return descriptor->options().java_multiple_files(); } @@ -245,24 +249,19 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field); // Does this message class have descriptor and reflection methods? inline bool HasDescriptorMethods(const Descriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->file()->options().optimize_for() != - FileOptions::LITE_RUNTIME; + return !enforce_lite; } inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->file()->options().optimize_for() != - FileOptions::LITE_RUNTIME; + return !enforce_lite; } inline bool HasDescriptorMethods(const FileDescriptor* descriptor, bool enforce_lite) { - return !enforce_lite && - descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; + return !enforce_lite; } // Should we generate generic services for this file? -inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { +inline bool HasGenericServices(const FileDescriptor* file, bool enforce_lite) { return file->service_count() > 0 && HasDescriptorMethods(file, enforce_lite) && file->options().java_generic_services(); @@ -399,7 +398,7 @@ inline bool IsWrappersProtoFile(const FileDescriptor* descriptor) { inline bool CheckUtf8(const FieldDescriptor* descriptor) { return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || - descriptor->file()->options().java_string_check_utf8(); + descriptor->file()->options().java_string_check_utf8(); } inline std::string GeneratedCodeVersionSuffix() { diff --git a/src/google/protobuf/compiler/java/java_map_field.cc b/src/google/protobuf/compiler/java/java_map_field.cc index a8b1074c45..5353745879 100644 --- a/src/google/protobuf/compiler/java/java_map_field.cc +++ b/src/google/protobuf/compiler/java/java_map_field.cc @@ -96,10 +96,14 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, boxed_key_type.substr(boxed_key_type.rfind('.') + 1); (*variables)["key_wire_type"] = WireType(key); (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver); - (*variables)["key_null_check"] = IsReferenceType(keyJavaType) ? - "if (key == null) { throw new java.lang.NullPointerException(); }" : ""; - (*variables)["value_null_check"] = IsReferenceType(valueJavaType) ? - "if (value == null) { throw new java.lang.NullPointerException(); }" : ""; + (*variables)["key_null_check"] = + IsReferenceType(keyJavaType) + ? "if (key == null) { throw new java.lang.NullPointerException(); }" + : ""; + (*variables)["value_null_check"] = + IsReferenceType(valueJavaType) + ? "if (value == null) { throw new java.lang.NullPointerException(); }" + : ""; if (valueJavaType == JAVATYPE_ENUM) { // We store enums as Integers internally. (*variables)["value_type"] = "int"; @@ -130,8 +134,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"]; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; // For repeated fields, one bit is used for whether the array is immutable @@ -141,62 +145,51 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["set_mutable_bit_parser"] = GenerateSetBitMutableLocal(builderBitIndex); - (*variables)["default_entry"] = (*variables)["capitalized_name"] + - "DefaultEntryHolder.defaultEntry"; + (*variables)["default_entry"] = + (*variables)["capitalized_name"] + "DefaultEntryHolder.defaultEntry"; (*variables)["map_field_parameter"] = (*variables)["default_entry"]; (*variables)["descriptor"] = - name_resolver->GetImmutableClassName(descriptor->file()) + - ".internal_" + UniqueFileScopeIdentifier(descriptor->message_type()) + - "_descriptor, "; + name_resolver->GetImmutableClassName(descriptor->file()) + ".internal_" + + UniqueFileScopeIdentifier(descriptor->message_type()) + "_descriptor, "; (*variables)["ver"] = GeneratedCodeVersionSuffix(); } } // namespace -ImmutableMapFieldGenerator:: -ImmutableMapFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { +ImmutableMapFieldGenerator::ImmutableMapFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - context, &variables_); + context->GetFieldGeneratorInfo(descriptor), context, + &variables_); } -ImmutableMapFieldGenerator:: -~ImmutableMapFieldGenerator() {} +ImmutableMapFieldGenerator::~ImmutableMapFieldGenerator() {} -int ImmutableMapFieldGenerator::GetNumBitsForMessage() const { - return 0; -} +int ImmutableMapFieldGenerator::GetNumBitsForMessage() const { return 0; } -int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const { - return 1; -} +int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void ImmutableMapFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Print(variables_, + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -228,63 +221,55 @@ GenerateInterfaceMembers(io::Printer* printer) const { "${$get$capitalized_name$Value$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$ValueMap$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$Map$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$Map$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateMembers(io::Printer* printer) const { printer->Print( variables_, "private static final class $capitalized_name$DefaultEntryHolder {\n" @@ -298,18 +283,17 @@ GenerateMembers(io::Printer* printer) const { " $value_wire_type$,\n" " $value_default_value$);\n" "}\n"); - printer->Print( - variables_, - "private com.google.protobuf.MapField<\n" - " $type_parameters$> $name$_;\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " if ($name$_ == null) {\n" - " return com.google.protobuf.MapField.emptyMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " return $name$_;\n" - "}\n"); + printer->Print(variables_, + "private com.google.protobuf.MapField<\n" + " $type_parameters$> $name$_;\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " if ($name$_ == null) {\n" + " return com.google.protobuf.MapField.emptyMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " return $name$_;\n" + "}\n"); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -333,53 +317,50 @@ GenerateMembers(io::Printer* printer) const { GenerateMapGetters(printer); } -void ImmutableMapFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print( - variables_, - "private com.google.protobuf.MapField<\n" - " $type_parameters$> $name$_;\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " if ($name$_ == null) {\n" - " return com.google.protobuf.MapField.emptyMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " return $name$_;\n" - "}\n" - "private com.google.protobuf.MapField<$type_parameters$>\n" - "internalGetMutable$capitalized_name$() {\n" - " $on_changed$;\n" - " if ($name$_ == null) {\n" - " $name$_ = com.google.protobuf.MapField.newMapField(\n" - " $map_field_parameter$);\n" - " }\n" - " if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.copy();\n" - " }\n" - " return $name$_;\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, + "private com.google.protobuf.MapField<\n" + " $type_parameters$> $name$_;\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " if ($name$_ == null) {\n" + " return com.google.protobuf.MapField.emptyMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " return $name$_;\n" + "}\n" + "private com.google.protobuf.MapField<$type_parameters$>\n" + "internalGetMutable$capitalized_name$() {\n" + " $on_changed$;\n" + " if ($name$_ == null) {\n" + " $name$_ = com.google.protobuf.MapField.newMapField(\n" + " $map_field_parameter$);\n" + " }\n" + " if (!$name$_.isMutable()) {\n" + " $name$_ = $name$_.copy();\n" + " }\n" + " return $name$_;\n" + "}\n"); GenerateMapGetters(printer); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$clear$capitalized_name$$}$() {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .clear();\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .clear();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$remove$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .remove(key);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$remove$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .remove(key);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -465,41 +446,38 @@ GenerateBuilderMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$" - "public Builder ${$put$capitalized_name$$}$(\n" - " $key_type$ key,\n" - " $value_type$ value) {\n" - " $key_null_check$\n" - " $value_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .put(key, value);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$" + "public Builder ${$put$capitalized_name$$}$(\n" + " $key_type$ key,\n" + " $value_type$ value) {\n" + " $key_null_check$\n" + " $value_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .put(key, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$putAll$capitalized_name$$}$(\n" - " java.util.Map<$type_parameters$> values) {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .putAll(values);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$putAll$capitalized_name$$}$(\n" + " java.util.Map<$type_parameters$> values) {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .putAll(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateMapGetters(io::Printer* printer) const { - printer->Print( - variables_, - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return internalGet$capitalized_name$().getMap().size();\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateMapGetters( + io::Printer* printer) const { + printer->Print(variables_, + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return internalGet$capitalized_name$().getMap().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -512,26 +490,24 @@ GenerateMapGetters(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$Map$}$() {\n" - " return internalGetAdapted$capitalized_name$Map(\n" - " internalGet$capitalized_name$().getMap());" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$Map$}$() {\n" + " return internalGetAdapted$capitalized_name$Map(\n" + " internalGet$capitalized_name$().getMap());" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -614,25 +590,23 @@ GenerateMapGetters(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return internalGet$capitalized_name$().getMap();\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return internalGet$capitalized_name$().getMap();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -648,65 +622,60 @@ GenerateMapGetters(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " internalGet$capitalized_name$().getMap();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " internalGet$capitalized_name$().getMap();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - printer->Print( - variables_, - "internalGetMutable$capitalized_name$().clear();\n"); +void ImmutableMapFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { + printer->Print(variables_, + "internalGetMutable$capitalized_name$().clear();\n"); } -void ImmutableMapFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { - printer->Print( - variables_, - "internalGetMutable$capitalized_name$().mergeFrom(\n" - " other.internalGet$capitalized_name$());\n"); +void ImmutableMapFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { + printer->Print(variables_, + "internalGetMutable$capitalized_name$().mergeFrom(\n" + " other.internalGet$capitalized_name$());\n"); } -void ImmutableMapFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - printer->Print( - variables_, - "result.$name$_ = internalGet$capitalized_name$();\n" - "result.$name$_.makeImmutable();\n"); +void ImmutableMapFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { + printer->Print(variables_, + "result.$name$_ = internalGet$capitalized_name$();\n" + "result.$name$_.makeImmutable();\n"); } -void ImmutableMapFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = com.google.protobuf.MapField.newMapField(\n" - " $map_field_parameter$);\n" - " $set_mutable_bit_parser$;\n" - "}\n"); +void ImmutableMapFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { + printer->Print(variables_, + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = com.google.protobuf.MapField.newMapField(\n" + " $map_field_parameter$);\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (!SupportUnknownEnumValue(descriptor_->file()) && GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -733,25 +702,24 @@ GenerateParsingCode(io::Printer* printer) const { } } -void ImmutableMapFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // Nothing to do here. } -void ImmutableMapFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print( - variables_, - "com.google.protobuf.GeneratedMessage$ver$\n" - " .serialize$short_key_type$MapTo(\n" - " output,\n" - " internalGet$capitalized_name$(),\n" - " $default_entry$,\n" - " $number$);\n"); +void ImmutableMapFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print(variables_, + "com.google.protobuf.GeneratedMessage$ver$\n" + " .serialize$short_key_type$MapTo(\n" + " output,\n" + " internalGet$capitalized_name$(),\n" + " $default_entry$,\n" + " $number$);\n"); } -void ImmutableMapFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print( variables_, "for (java.util.Map.Entry<$type_parameters$> entry\n" @@ -766,16 +734,14 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); } -void ImmutableMapFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!internalGet$capitalized_name$().equals(\n" - " other.internalGet$capitalized_name$())) return false;\n"); +void ImmutableMapFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print(variables_, + "if (!internalGet$capitalized_name$().equals(\n" + " other.internalGet$capitalized_name$())) return false;\n"); } -void ImmutableMapFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableMapFieldGenerator::GenerateHashCode(io::Printer* printer) const { printer->Print( variables_, "if (!internalGet$capitalized_name$().getMap().isEmpty()) {\n" diff --git a/src/google/protobuf/compiler/java/java_map_field.h b/src/google/protobuf/compiler/java/java_map_field.h index b123b0419f..2ff1f7673e 100644 --- a/src/google/protobuf/compiler/java/java_map_field.h +++ b/src/google/protobuf/compiler/java/java_map_field.h @@ -40,9 +40,9 @@ namespace java { class ImmutableMapFieldGenerator : public ImmutableFieldGenerator { public: - explicit ImmutableMapFieldGenerator( - const FieldDescriptor* descriptor, int messageBitIndex, - int builderBitIndex, Context* context); + explicit ImmutableMapFieldGenerator(const FieldDescriptor* descriptor, + int messageBitIndex, int builderBitIndex, + Context* context); ~ImmutableMapFieldGenerator(); // implements ImmutableFieldGenerator --------------------------------------- diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.cc b/src/google/protobuf/compiler/java/java_map_field_lite.cc index c6ffa5bc21..9055774844 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_map_field_lite.cc @@ -92,10 +92,14 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["boxed_key_type"] = TypeName(key, name_resolver, true); (*variables)["key_wire_type"] = WireType(key); (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver); - (*variables)["key_null_check"] = IsReferenceType(keyJavaType) ? - "if (key == null) { throw new java.lang.NullPointerException(); }" : ""; - (*variables)["value_null_check"] = IsReferenceType(valueJavaType) ? - "if (value == null) { throw new java.lang.NullPointerException(); }" : ""; + (*variables)["key_null_check"] = + IsReferenceType(keyJavaType) + ? "if (key == null) { throw new java.lang.NullPointerException(); }" + : ""; + (*variables)["value_null_check"] = + IsReferenceType(valueJavaType) + ? "if (value == null) { throw new java.lang.NullPointerException(); }" + : ""; if (GetJavaType(value) == JAVATYPE_ENUM) { // We store enums as Integers internally. @@ -127,55 +131,48 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"]; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - (*variables)["default_entry"] = (*variables)["capitalized_name"] + - "DefaultEntryHolder.defaultEntry"; + (*variables)["default_entry"] = + (*variables)["capitalized_name"] + "DefaultEntryHolder.defaultEntry"; } } // namespace -ImmutableMapFieldLiteGenerator:: -ImmutableMapFieldLiteGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()){ +ImmutableMapFieldLiteGenerator::ImmutableMapFieldLiteGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, Context* context) + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, 0, context->GetFieldGeneratorInfo(descriptor), context, &variables_); } -ImmutableMapFieldLiteGenerator:: -~ImmutableMapFieldLiteGenerator() {} +ImmutableMapFieldLiteGenerator::~ImmutableMapFieldLiteGenerator() {} -int ImmutableMapFieldLiteGenerator::GetNumBitsForMessage() const { - return 0; -} +int ImmutableMapFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void ImmutableMapFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Print(variables_, + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -207,63 +204,56 @@ GenerateInterfaceMembers(io::Printer* printer) const { "${$get$capitalized_name$Value$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$ValueMap$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$$}$();\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$java.util.Map<$type_parameters$>\n" - "${$get$capitalized_name$Map$}$();\n"); + printer->Print(variables_, + "$deprecation$java.util.Map<$type_parameters$>\n" + "${$get$capitalized_name$Map$}$();\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" - " $key_type$ key,\n" - " $value_type$ defaultValue);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" + " $key_type$ key,\n" + " $value_type$ defaultValue);\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key);\n"); + printer->Print(variables_, + "$deprecation$\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key);\n"); printer->Annotate("{", "}", descriptor_); } } -void ImmutableMapFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { printer->Print( variables_, "private static final class $capitalized_name$DefaultEntryHolder {\n" @@ -276,40 +266,37 @@ GenerateMembers(io::Printer* printer) const { " $value_wire_type$,\n" " $value_default_value$);\n" "}\n"); - printer->Print( - variables_, - "private com.google.protobuf.MapFieldLite<\n" - " $type_parameters$> $name$_ =\n" - " com.google.protobuf.MapFieldLite.emptyMapField();\n" - "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" - "internalGet$capitalized_name$() {\n" - " return $name$_;\n" - "}\n" - "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" - "internalGetMutable$capitalized_name$() {\n" - " if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.mutableCopy();\n" - " }\n" - " return $name$_;\n" - "}\n"); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return internalGet$capitalized_name$().size();\n" - "}\n"); + printer->Print(variables_, + "private com.google.protobuf.MapFieldLite<\n" + " $type_parameters$> $name$_ =\n" + " com.google.protobuf.MapFieldLite.emptyMapField();\n" + "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" + "internalGet$capitalized_name$() {\n" + " return $name$_;\n" + "}\n" + "private com.google.protobuf.MapFieldLite<$type_parameters$>\n" + "internalGetMutable$capitalized_name$() {\n" + " if (!$name$_.isMutable()) {\n" + " $name$_ = $name$_.mutableCopy();\n" + " }\n" + " return $name$_;\n" + "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return internalGet$capitalized_name$().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public boolean ${$contains$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " return internalGet$capitalized_name$().containsKey(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public boolean ${$contains$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " return internalGet$capitalized_name$().containsKey(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( @@ -320,16 +307,15 @@ GenerateMembers(io::Printer* printer) const { " com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n" " $value_enum_type$.internalGetValueMap(),\n" " $unrecognized_value$);\n"); - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -433,28 +419,26 @@ GenerateMembers(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Override\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Override\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " internalGet$capitalized_name$());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " internalGet$capitalized_name$());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -471,20 +455,19 @@ GenerateMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " internalGet$capitalized_name$();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " internalGet$capitalized_name$();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } @@ -511,25 +494,39 @@ GenerateMembers(io::Printer* printer) const { } } else { WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "private java.util.Map<$type_parameters$>\n" - "getMutable$capitalized_name$Map() {\n" - " return internalGetMutable$capitalized_name$();\n" - "}\n"); + printer->Print(variables_, + "private java.util.Map<$type_parameters$>\n" + "getMutable$capitalized_name$Map() {\n" + " return internalGetMutable$capitalized_name$();\n" + "}\n"); } } +void ImmutableMapFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, + "\"$name$_\",\n" + "$default_entry$,\n"); + if (SupportFieldPresence(descriptor_->file()) && + GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { + PrintEnumVerifierLogic(printer, ValueField(descriptor_), variables_, + /*var_name=*/"$value_enum_type$", + /*terminating_string=*/",\n", + /*enforce_lite=*/context_->EnforceLite()); + } +} -void ImmutableMapFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Map().size();\n" - "}\n"); +void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Map().size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -542,49 +539,45 @@ GenerateBuilderMembers(io::Printer* printer) const { " return instance.get$capitalized_name$Map().containsKey(key);\n" "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.getMutable$capitalized_name$Map().clear();\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.getMutable$capitalized_name$Map().clear();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "$deprecation$\n" - "public Builder ${$remove$capitalized_name$$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " copyOnWrite();\n" - " instance.getMutable$capitalized_name$Map().remove(key);\n" - " return this;\n" - "}\n"); + printer->Print(variables_, + "$deprecation$\n" + "public Builder ${$remove$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " copyOnWrite();\n" + " instance.getMutable$capitalized_name$Map().remove(key);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " instance.get$capitalized_name$Map());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " instance.get$capitalized_name$Map());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -719,28 +712,26 @@ GenerateBuilderMembers(io::Printer* printer) const { printer->Annotate("{", "}", descriptor_); } } else { - printer->Print( - variables_, - "/**\n" - " * Use {@link #get$capitalized_name$Map()} instead.\n" - " */\n" - "@java.lang.Override\n" - "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$$}$() {\n" - " return get$capitalized_name$Map();\n" - "}\n"); + printer->Print(variables_, + "/**\n" + " * Use {@link #get$capitalized_name$Map()} instead.\n" + " */\n" + "@java.lang.Override\n" + "@java.lang.Deprecated\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" + " return get$capitalized_name$Map();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$" - "public java.util.Map<$type_parameters$> " - "${$get$capitalized_name$Map$}$() {\n" - " return java.util.Collections.unmodifiableMap(\n" - " instance.get$capitalized_name$Map());\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" + " return java.util.Collections.unmodifiableMap(\n" + " instance.get$capitalized_name$Map());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -757,20 +748,19 @@ GenerateBuilderMembers(io::Printer* printer) const { "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$\n" - "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " java.util.Map<$type_parameters$> map =\n" - " instance.get$capitalized_name$Map();\n" - " if (!map.containsKey(key)) {\n" - " throw new java.lang.IllegalArgumentException();\n" - " }\n" - " return map.get(key);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " java.util.Map<$type_parameters$> map =\n" + " instance.get$capitalized_name$Map();\n" + " if (!map.containsKey(key)) {\n" + " throw new java.lang.IllegalArgumentException();\n" + " }\n" + " return map.get(key);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( @@ -800,103 +790,11 @@ GenerateBuilderMembers(io::Printer* printer) const { } } -void ImmutableMapFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMapFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { // Nothing to initialize. } -void ImmutableMapFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - // Nothing to initialize. -} - -void ImmutableMapFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print( - variables_, - "$name$_ = visitor.visitMap(\n" - " $name$_, other.internalGet$capitalized_name$());\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!$name$_.isMutable()) {\n" - " $name$_ = $name$_.mutableCopy();\n" - "}\n"); - if (!SupportUnknownEnumValue(descriptor_->file()) && - GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { - printer->Print( - variables_, - "com.google.protobuf.ByteString bytes = input.readBytes();\n" - "java.util.Map.Entry<$type_parameters$> $name$__ =\n" - " $default_entry$.parseEntry(bytes, extensionRegistry);\n"); - printer->Print( - variables_, - "if ($value_enum_type$.forNumber($name$__.getValue()) == null) {\n" - " super.mergeLengthDelimitedField($number$, bytes);\n" - "} else {\n" - " $name$_.put($name$__);\n" - "}\n"); - } else { - printer->Print( - variables_, - "$default_entry$.parseInto($name$_, input, extensionRegistry);"); - } -} - -void ImmutableMapFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // Nothing to do here. -} - -void ImmutableMapFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print( - variables_, - "for (java.util.Map.Entry<$type_parameters$> entry\n" - " : internalGet$capitalized_name$().entrySet()) {\n" - " $default_entry$.serializeTo(\n" - " output, $number$, entry.getKey(), entry.getValue());\n" - "}\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print( - variables_, - "for (java.util.Map.Entry<$type_parameters$> entry\n" - " : internalGet$capitalized_name$().entrySet()) {\n" - " size += $default_entry$.computeMessageSize(\n" - " $number$, entry.getKey(), entry.getValue());\n" - "}\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print( - variables_, - "result = result && internalGet$capitalized_name$().equals(\n" - " other.internalGet$capitalized_name$());\n"); -} - -void ImmutableMapFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print( - variables_, - "if (!internalGet$capitalized_name$().isEmpty()) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + internalGet$capitalized_name$().hashCode();\n" - "}\n"); -} - std::string ImmutableMapFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.h b/src/google/protobuf/compiler/java/java_map_field_lite.h index df5fe64d50..49cbf6cca7 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.h +++ b/src/google/protobuf/compiler/java/java_map_field_lite.h @@ -51,16 +51,8 @@ class ImmutableMapFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index 39b5d7cc06..5bbdccee08 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -77,16 +77,17 @@ std::string MapValueImmutableClassdName(const Descriptor* descriptor, // =================================================================== MessageGenerator::MessageGenerator(const Descriptor* descriptor) - : descriptor_(descriptor) {} + : descriptor_(descriptor) {} MessageGenerator::~MessageGenerator() {} // =================================================================== ImmutableMessageGenerator::ImmutableMessageGenerator( const Descriptor* descriptor, Context* context) - : MessageGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : MessageGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; @@ -108,8 +109,7 @@ void ImmutableMessageGenerator::GenerateStaticVariables( vars["index"] = StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { - vars["parent"] = UniqueFileScopeIdentifier( - descriptor_->containing_type()); + vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); } if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { // We can only make these package-private since the classes that use them @@ -125,12 +125,13 @@ void ImmutableMessageGenerator::GenerateStaticVariables( } // The descriptor for this type. - printer->Print(vars, - // TODO(teboring): final needs to be added back. The way to fix it is to - // generate methods that can construct the types, and then still declare the - // types, and then init them in clinit with the new method calls. - "$private$static $final$com.google.protobuf.Descriptors.Descriptor\n" - " internal_$identifier$_descriptor;\n"); + printer->Print( + vars, + // TODO(teboring): final needs to be added back. The way to fix it is to + // generate methods that can construct the types, and then still declare + // the types, and then init them in clinit with the new method calls. + "$private$static $final$com.google.protobuf.Descriptors.Descriptor\n" + " internal_$identifier$_descriptor;\n"); *bytecode_estimate += 30; // And the FieldAccessorTable. @@ -152,20 +153,20 @@ int ImmutableMessageGenerator::GenerateStaticVariableInitializers( vars["index"] = StrCat(descriptor_->index()); vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); if (descriptor_->containing_type() != NULL) { - vars["parent"] = UniqueFileScopeIdentifier( - descriptor_->containing_type()); + vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type()); } // The descriptor for this type. if (descriptor_->containing_type() == NULL) { printer->Print(vars, - "internal_$identifier$_descriptor =\n" - " getDescriptor().getMessageTypes().get($index$);\n"); + "internal_$identifier$_descriptor =\n" + " getDescriptor().getMessageTypes().get($index$);\n"); bytecode_estimate += 30; } else { - printer->Print(vars, - "internal_$identifier$_descriptor =\n" - " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); + printer->Print( + vars, + "internal_$identifier$_descriptor =\n" + " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); bytecode_estimate += 30; } @@ -182,8 +183,8 @@ int ImmutableMessageGenerator::GenerateStaticVariableInitializers( return bytecode_estimate; } -void ImmutableMessageGenerator:: -GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate) { +void ImmutableMessageGenerator::GenerateFieldAccessorTable( + io::Printer* printer, int* bytecode_estimate) { std::map<std::string, std::string> vars; vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { @@ -199,41 +200,38 @@ GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate) { vars["final"] = ""; } vars["ver"] = GeneratedCodeVersionSuffix(); - printer->Print(vars, - "$private$static $final$\n" - " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internal_$identifier$_fieldAccessorTable;\n"); + printer->Print( + vars, + "$private$static $final$\n" + " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internal_$identifier$_fieldAccessorTable;\n"); // 6 bytes per field and oneof - *bytecode_estimate += 10 + 6 * descriptor_->field_count() - + 6 * descriptor_->oneof_decl_count(); + *bytecode_estimate += + 10 + 6 * descriptor_->field_count() + 6 * descriptor_->oneof_decl_count(); } -int ImmutableMessageGenerator:: -GenerateFieldAccessorTableInitializer(io::Printer* printer) { +int ImmutableMessageGenerator::GenerateFieldAccessorTableInitializer( + io::Printer* printer) { int bytecode_estimate = 10; printer->Print( - "internal_$identifier$_fieldAccessorTable = new\n" - " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable(\n" - " internal_$identifier$_descriptor,\n" - " new java.lang.String[] { ", - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "internal_$identifier$_fieldAccessorTable = new\n" + " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable(\n" + " internal_$identifier$_descriptor,\n" + " new java.lang.String[] { ", + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = descriptor_->field(i); const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); bytecode_estimate += 6; - printer->Print( - "\"$field_name$\", ", - "field_name", info->capitalized_name); + printer->Print("\"$field_name$\", ", "field_name", info->capitalized_name); } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { const OneofDescriptor* oneof = descriptor_->oneof_decl(i); const OneofGeneratorInfo* info = context_->GetOneofGeneratorInfo(oneof); bytecode_estimate += 6; - printer->Print( - "\"$oneof_name$\", ", - "oneof_name", info->capitalized_name); + printer->Print("\"$oneof_name$\", ", "oneof_name", info->capitalized_name); } printer->Print("});\n"); return bytecode_estimate; @@ -250,42 +248,40 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { " $extra_interfaces$\n" " com.google.protobuf.GeneratedMessage$ver$.\n" " ExtendableMessageOrBuilder<$classname$> {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", "", "ver", GeneratedCodeVersionSuffix()); + "classname", descriptor_->name(), "{", "", "}", "", "ver", + GeneratedCodeVersionSuffix()); } else { printer->Print( "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageOrBuilder {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } printer->Annotate("{", "}", descriptor_); printer->Indent(); - for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\n"); - field_generators_.get(descriptor_->field(i)) - .GenerateInterfaceMembers(printer); - } - for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print( - "\n" - "public $classname$.$oneof_capitalized_name$Case " - "get$oneof_capitalized_name$Case();\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name, - "classname", - context_->GetNameResolver()->GetImmutableClassName( - descriptor_)); - } + for (int i = 0; i < descriptor_->field_count(); i++) { + printer->Print("\n"); + field_generators_.get(descriptor_->field(i)) + .GenerateInterfaceMembers(printer); + } + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + printer->Print( + "\n" + "public $classname$.$oneof_capitalized_name$Case " + "get$oneof_capitalized_name$Case();\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name, + "classname", + context_->GetNameResolver()->GetImmutableClassName(descriptor_)); + } printer->Outdent(); printer->Print("}\n"); @@ -301,8 +297,8 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { variables["classname"] = descriptor_->name(); variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_); variables["ver"] = GeneratedCodeVersionSuffix(); - variables["deprecation"] = descriptor_->options().deprecated() - ? "@java.lang.Deprecated " : ""; + variables["deprecation"] = + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; WriteMessageDocComment(printer, descriptor_); MaybePrintGeneratedAnnotation(context_, printer, descriptor_, @@ -331,15 +327,14 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "$deprecation$public $static$final class $classname$ extends\n"); printer->Annotate("classname", descriptor_); printer->Print(variables, - " com.google.protobuf.GeneratedMessage$ver$ implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n"); - builder_type = strings::Substitute( - "com.google.protobuf.GeneratedMessage$0.Builder<?>", - GeneratedCodeVersionSuffix()); + " com.google.protobuf.GeneratedMessage$ver$ implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n"); + builder_type = + strings::Substitute("com.google.protobuf.GeneratedMessage$0.Builder<?>", + GeneratedCodeVersionSuffix()); } - printer->Print( - "private static final long serialVersionUID = 0L;\n"); + printer->Print("private static final long serialVersionUID = 0L;\n"); printer->Indent(); // Using builder_type, instead of Builder, prevents the Builder class from @@ -347,29 +342,34 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // This optimizes the PermGen space usage for clients that do not modify // messages. printer->Print( - "// Use $classname$.newBuilder() to construct.\n" - "private $classname$($buildertype$ builder) {\n" - " super(builder);\n" - "}\n", - "classname", descriptor_->name(), - "buildertype", builder_type); - printer->Print( - "private $classname$() {\n", - "classname", descriptor_->name()); + "// Use $classname$.newBuilder() to construct.\n" + "private $classname$($buildertype$ builder) {\n" + " super(builder);\n" + "}\n", + "classname", descriptor_->name(), "buildertype", builder_type); + printer->Print("private $classname$() {\n", "classname", descriptor_->name()); printer->Indent(); GenerateInitializers(printer); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); + printer->Print(variables, + "@java.lang.Override\n" + "@SuppressWarnings({\"unused\"})\n" + "protected java.lang.Object newInstance(\n" + " UnusedPrivateParameter unused) {\n" + " return new $classname$();\n" + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public final com.google.protobuf.UnknownFieldSet\n" - "getUnknownFields() {\n" - " return this.unknownFields;\n" - "}\n"); + "@java.lang.Override\n" + "public final com.google.protobuf.UnknownFieldSet\n" + "getUnknownFields() {\n" + " return this.unknownFields;\n" + "}\n"); if (context_->HasGeneratedMethods(descriptor_)) { GenerateParsingConstructor(printer); @@ -379,15 +379,14 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // Nested types for (int i = 0; i < descriptor_->enum_type_count(); i++) { - EnumGenerator(descriptor_->enum_type(i), true, context_) - .Generate(printer); + EnumGenerator(descriptor_->enum_type(i), true, context_).Generate(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { // Don't generate Java classes for map entry messages. if (IsMapEntry(descriptor_->nested_type(i))) continue; - ImmutableMessageGenerator messageGenerator( - descriptor_->nested_type(i), context_); + ImmutableMessageGenerator messageGenerator(descriptor_->nested_type(i), + context_); messageGenerator.GenerateInterface(printer); messageGenerator.Generate(printer); } @@ -407,19 +406,20 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // oneof std::map<std::string, std::string> vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // OneofCase enum printer->Print(vars, - "public enum $oneof_capitalized_name$Case\n" - " implements com.google.protobuf.Internal.EnumLite {\n"); + "public enum $oneof_capitalized_name$Case\n" + " implements com.google.protobuf.Internal.EnumLite {\n"); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); @@ -429,26 +429,25 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "field_name", ToUpper(field->name()), "field_number", StrCat(field->number())); } - printer->Print( - "$cap_oneof_name$_NOT_SET(0);\n", - "cap_oneof_name", - ToUpper(vars["oneof_name"])); + printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", + ToUpper(vars["oneof_name"])); printer->Print(vars, - "private final int value;\n" - "private $oneof_capitalized_name$Case(int value) {\n" - " this.value = value;\n" - "}\n"); - printer->Print(vars, - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $oneof_capitalized_name$Case valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $oneof_capitalized_name$Case forNumber(int value) {\n" - " switch (value) {\n"); + "private final int value;\n" + "private $oneof_capitalized_name$Case(int value) {\n" + " this.value = value;\n" + "}\n"); + printer->Print( + vars, + "/**\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $oneof_capitalized_name$Case valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $oneof_capitalized_name$Case forNumber(int value) {\n" + " switch (value) {\n"); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print(" case $field_number$: return $field_name$;\n", @@ -456,24 +455,24 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "field_name", ToUpper(field->name())); } printer->Print( - " case 0: return $cap_oneof_name$_NOT_SET;\n" - " default: return null;\n" - " }\n" - "}\n" - "public int getNumber() {\n" - " return this.value;\n" - "}\n", - "cap_oneof_name", ToUpper(vars["oneof_name"])); + " case 0: return $cap_oneof_name$_NOT_SET;\n" + " default: return null;\n" + " }\n" + "}\n" + "public int getNumber() {\n" + " return this.value;\n" + "}\n", + "cap_oneof_name", ToUpper(vars["oneof_name"])); printer->Outdent(); printer->Print("};\n\n"); // oneofCase() printer->Print(vars, - "public $oneof_capitalized_name$Case\n" - "get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n"); + "public $oneof_capitalized_name$Case\n" + "get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n"); } if (IsAnyMessage(descriptor_)) { @@ -500,22 +499,21 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { GenerateBuilder(printer); printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); - + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); // Carefully initialize the default instance in such a way that it doesn't // conflict with other initialization. + printer->Print("private static final $classname$ DEFAULT_INSTANCE;\n", + "classname", + name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "private static final $classname$ DEFAULT_INSTANCE;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - printer->Print( - "static {\n" - " DEFAULT_INSTANCE = new $classname$();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "static {\n" + " DEFAULT_INSTANCE = new $classname$();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( "public static $classname$ getDefaultInstance() {\n" @@ -538,12 +536,12 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { GenerateParser(printer); printer->Print( - "@java.lang.Override\n" - "public $classname$ getDefaultInstanceForType() {\n" - " return DEFAULT_INSTANCE;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ getDefaultInstanceForType() {\n" + " return DEFAULT_INSTANCE;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); // Extensions must be declared after the DEFAULT_INSTANCE is initialized // because the DEFAULT_INSTANCE is used by the extension to lazily retrieve @@ -557,12 +555,11 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { printer->Print("}\n\n"); } - // =================================================================== -void ImmutableMessageGenerator:: -GenerateMessageSerializationMethods(io::Printer* printer) { - std::unique_ptr<const FieldDescriptor * []> sorted_fields( +void ImmutableMessageGenerator::GenerateMessageSerializationMethods( + io::Printer* printer) { + std::unique_ptr<const FieldDescriptor*[]> sorted_fields( SortFieldsByNumber(descriptor_)); std::vector<const Descriptor::ExtensionRange*> sorted_extensions; @@ -572,9 +569,9 @@ GenerateMessageSerializationMethods(io::Printer* printer) { std::sort(sorted_extensions.begin(), sorted_extensions.end(), ExtensionRangeOrdering()); printer->Print( - "@java.lang.Override\n" - "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" - " throws java.io.IOException {\n"); + "@java.lang.Override\n" + "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" + " throws java.io.IOException {\n"); printer->Indent(); if (HasPackedFields(descriptor_)) { @@ -639,8 +636,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { printer->Print("size = 0;\n"); for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(sorted_fields[i]) - .GenerateSerializedSizeCode(printer); + field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); } if (descriptor_->extension_range_count() > 0) { @@ -652,8 +648,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "size += unknownFields.getSerializedSizeAsMessageSet();\n"); + printer->Print("size += unknownFields.getSerializedSizeAsMessageSet();\n"); } else { printer->Print("size += unknownFields.getSerializedSize();\n"); } @@ -664,88 +659,89 @@ GenerateMessageSerializationMethods(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } -void ImmutableMessageGenerator:: -GenerateParseFromMethods(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateParseFromMethods(io::Printer* printer) { // Note: These are separate from GenerateMessageSerializationMethods() // because they need to be generated even for messages that are optimized // for code size. printer->Print( - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(byte[] data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " byte[] data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return PARSER.parseFrom(data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseDelimitedWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseDelimitedWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessage$ver$\n" - " .parseWithIOException(PARSER, input, extensionRegistry);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(byte[] data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " byte[] data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return PARSER.parseFrom(data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(java.io.InputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input, extensionRegistry);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(java.io.InputStream " + "input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseDelimitedWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseDelimitedWithIOException(PARSER, input, " + "extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessage$ver$\n" + " .parseWithIOException(PARSER, input, extensionRegistry);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } void ImmutableMessageGenerator::GenerateSerializeOneField( @@ -764,48 +760,48 @@ void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange( void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) { // LITE_RUNTIME implements this at the GeneratedMessageLite level. printer->Print( - "@java.lang.Override\n" - "public Builder newBuilderForType() { return newBuilder(); }\n"); + "@java.lang.Override\n" + "public Builder newBuilderForType() { return newBuilder(); }\n"); printer->Print( - "public static Builder newBuilder() {\n" - " return DEFAULT_INSTANCE.toBuilder();\n" - "}\n" - "public static Builder newBuilder($classname$ prototype) {\n" - " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" - "}\n" - "@java.lang.Override\n" - "public Builder toBuilder() {\n" - " return this == DEFAULT_INSTANCE\n" - " ? new Builder() : new Builder().mergeFrom(this);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public static Builder newBuilder() {\n" + " return DEFAULT_INSTANCE.toBuilder();\n" + "}\n" + "public static Builder newBuilder($classname$ prototype) {\n" + " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" + "}\n" + "@java.lang.Override\n" + "public Builder toBuilder() {\n" + " return this == DEFAULT_INSTANCE\n" + " ? new Builder() : new Builder().mergeFrom(this);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "protected Builder newBuilderForType(\n" - " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" - " Builder builder = new Builder(parent);\n" - " return builder;\n" - "}\n", - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected Builder newBuilderForType(\n" + " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" + " Builder builder = new Builder(parent);\n" + " return builder;\n" + "}\n", + "ver", GeneratedCodeVersionSuffix()); MessageBuilderGenerator builderGenerator(descriptor_, context_); builderGenerator.Generate(printer); } -void ImmutableMessageGenerator:: -GenerateDescriptorMethods(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateDescriptorMethods( + io::Printer* printer) { if (!descriptor_->options().no_standard_descriptor_accessor()) { printer->Print( - "public static final com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptor() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "public static final com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptor() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); } std::vector<const FieldDescriptor*> map_fields; for (int i = 0; i < descriptor_->field_count(); i++) { @@ -817,11 +813,11 @@ GenerateDescriptorMethods(io::Printer* printer) { } if (!map_fields.empty()) { printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "@java.lang.Override\n" - "protected com.google.protobuf.MapField internalGetMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "@java.lang.Override\n" + "protected com.google.protobuf.MapField internalGetMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { @@ -844,40 +840,38 @@ GenerateDescriptorMethods(io::Printer* printer) { "}\n"); } printer->Print( - "@java.lang.Override\n" - "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internalGetFieldAccessorTable() {\n" - " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" - " .ensureFieldAccessorsInitialized(\n" - " $classname$.class, $classname$.Builder.class);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internalGetFieldAccessorTable() {\n" + " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" + " .ensureFieldAccessorsInitialized(\n" + " $classname$.class, $classname$.Builder.class);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } // =================================================================== -void ImmutableMessageGenerator::GenerateIsInitialized( - io::Printer* printer) { +void ImmutableMessageGenerator::GenerateIsInitialized(io::Printer* printer) { // Memoizes whether the protocol buffer is fully initialized (has all // required fields). -1 means not yet computed. 0 means false and 1 means // true. + printer->Print("private byte memoizedIsInitialized = -1;\n"); printer->Print( - "private byte memoizedIsInitialized = -1;\n"); - printer->Print( - "@java.lang.Override\n" - "public final boolean isInitialized() {\n"); + "@java.lang.Override\n" + "public final boolean isInitialized() {\n"); printer->Indent(); // Don't directly compare to -1 to avoid an Android x86 JIT bug. printer->Print( - "byte isInitialized = memoizedIsInitialized;\n" - "if (isInitialized == 1) return true;\n" - "if (isInitialized == 0) return false;\n" - "\n"); + "byte isInitialized = memoizedIsInitialized;\n" + "if (isInitialized == 1) return true;\n" + "if (isInitialized == 0) return false;\n" + "\n"); // Check that all required fields in this message are set. // TODO(kenton): We can optimize this when we switch to putting all the @@ -888,11 +882,11 @@ void ImmutableMessageGenerator::GenerateIsInitialized( if (field->is_required()) { printer->Print( - "if (!has$name$()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n", - "name", info->capitalized_name); + "if (!has$name$()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n", + "name", info->capitalized_name); } } @@ -905,13 +899,13 @@ void ImmutableMessageGenerator::GenerateIsInitialized( switch (field->label()) { case FieldDescriptor::LABEL_REQUIRED: printer->Print( - "if (!get$name$().isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "if (!get$name$().isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); break; case FieldDescriptor::LABEL_OPTIONAL: if (!SupportFieldPresence(descriptor_->file()) && @@ -923,41 +917,41 @@ void ImmutableMessageGenerator::GenerateIsInitialized( "oneof_name", oneof_info->name, "field_number", StrCat(field->number())); } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", + info->capitalized_name); } printer->Print( - " if (!get$name$().isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" + " if (!get$name$().isInitialized()) {\n" " memoizedIsInitialized = 0;\n" " return false;\n" " }\n" "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), "name", info->capitalized_name); + break; + case FieldDescriptor::LABEL_REPEATED: + if (IsMapEntry(field->message_type())) { + printer->Print( + "for ($type$ item : get$name$Map().values()) {\n" + " if (!item.isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + " }\n" + "}\n", + "type", + MapValueImmutableClassdName(field->message_type(), + name_resolver_), + "name", info->capitalized_name); } else { printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "for (int i = 0; i < get$name$Count(); i++) {\n" + " if (!get$name$(i).isInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + " }\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); } break; } @@ -966,21 +960,20 @@ void ImmutableMessageGenerator::GenerateIsInitialized( if (descriptor_->extension_range_count() > 0) { printer->Print( - "if (!extensionsAreInitialized()) {\n" - " memoizedIsInitialized = 0;\n" - " return false;\n" - "}\n"); + "if (!extensionsAreInitialized()) {\n" + " memoizedIsInitialized = 0;\n" + " return false;\n" + "}\n"); } printer->Outdent(); - printer->Print( - " memoizedIsInitialized = 1;\n"); + printer->Print(" memoizedIsInitialized = 1;\n"); printer->Print( - " return true;\n" - "}\n" - "\n"); + " return true;\n" + "}\n" + "\n"); } // =================================================================== @@ -994,26 +987,26 @@ bool CheckHasBitsForEqualsAndHashCode(const FieldDescriptor* field) { return true; } return GetJavaType(field) == JAVATYPE_MESSAGE && - field->containing_oneof() == NULL; + field->containing_oneof() == NULL; } } // namespace -void ImmutableMessageGenerator:: -GenerateEqualsAndHashCode(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateEqualsAndHashCode( + io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public boolean equals(final java.lang.Object obj) {\n"); + "@java.lang.Override\n" + "public boolean equals(final java.lang.Object obj) {\n"); printer->Indent(); printer->Print( - "if (obj == this) {\n" - " return true;\n" - "}\n" - "if (!(obj instanceof $classname$)) {\n" - " return super.equals(obj);\n" - "}\n" - "$classname$ other = ($classname$) obj;\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "if (obj == this) {\n" + " return true;\n" + "}\n" + "if (!(obj instanceof $classname$)) {\n" + " return super.equals(obj);\n" + "}\n" + "$classname$ other = ($classname$) obj;\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = descriptor_->field(i); @@ -1022,16 +1015,15 @@ GenerateEqualsAndHashCode(io::Printer* printer) { bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); if (check_has_bits) { printer->Print( - "if (has$name$() != other.has$name$()) return false;\n" - "if (has$name$()) {\n", - "name", info->capitalized_name); + "if (has$name$() != other.has$name$()) return false;\n" + "if (has$name$()) {\n", + "name", info->capitalized_name); printer->Indent(); } field_generators_.get(field).GenerateEqualsCode(printer); if (check_has_bits) { printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } } } @@ -1039,16 +1031,14 @@ GenerateEqualsAndHashCode(io::Printer* printer) { // Compare oneofs. for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "if (!get$oneof_capitalized_name$Case().equals(" - "other.get$oneof_capitalized_name$Case())) return false;\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); + "if (!get$oneof_capitalized_name$Case().equals(" + "other.get$oneof_capitalized_name$Case())) return false;\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name); printer->Print( - "switch ($oneof_name$Case_) {\n", - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "switch ($oneof_name$Case_) {\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); @@ -1060,8 +1050,8 @@ GenerateEqualsAndHashCode(io::Printer* printer) { printer->Outdent(); } printer->Print( - "case 0:\n" - "default:\n"); + "case 0:\n" + "default:\n"); printer->Outdent(); printer->Print("}\n"); } @@ -1073,29 +1063,26 @@ GenerateEqualsAndHashCode(io::Printer* printer) { "if (!unknownFields.equals(other.unknownFields)) return false;\n"); if (descriptor_->extension_range_count() > 0) { printer->Print( - "if (!getExtensionFields().equals(other.getExtensionFields()))\n" - " return false;\n"); + "if (!getExtensionFields().equals(other.getExtensionFields()))\n" + " return false;\n"); } - printer->Print( - "return true;\n"); + printer->Print("return true;\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public int hashCode() {\n"); + "@java.lang.Override\n" + "public int hashCode() {\n"); printer->Indent(); - printer->Print( - "if (memoizedHashCode != 0) {\n"); + printer->Print("if (memoizedHashCode != 0) {\n"); printer->Indent(); - printer->Print( - "return memoizedHashCode;\n"); + printer->Print("return memoizedHashCode;\n"); printer->Outdent(); printer->Print( - "}\n" - "int hash = 41;\n"); + "}\n" + "int hash = 41;\n"); // If we output a getDescriptor() method, use that as it is more efficient. if (descriptor_->options().no_standard_descriptor_accessor()) { @@ -1111,9 +1098,7 @@ GenerateEqualsAndHashCode(io::Printer* printer) { const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); if (check_has_bits) { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", info->capitalized_name); printer->Indent(); } field_generators_.get(field).GenerateHashCode(printer); @@ -1127,10 +1112,8 @@ GenerateEqualsAndHashCode(io::Printer* printer) { // hashCode oneofs. for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "switch ($oneof_name$Case_) {\n", - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "switch ($oneof_name$Case_) {\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); @@ -1142,47 +1125,45 @@ GenerateEqualsAndHashCode(io::Printer* printer) { printer->Outdent(); } printer->Print( - "case 0:\n" - "default:\n"); + "case 0:\n" + "default:\n"); printer->Outdent(); printer->Print("}\n"); } if (descriptor_->extension_range_count() > 0) { - printer->Print( - "hash = hashFields(hash, getExtensionFields());\n"); + printer->Print("hash = hashFields(hash, getExtensionFields());\n"); } + printer->Print("hash = (29 * hash) + unknownFields.hashCode();\n"); printer->Print( - "hash = (29 * hash) + unknownFields.hashCode();\n"); - printer->Print( - "memoizedHashCode = hash;\n" - "return hash;\n"); + "memoizedHashCode = hash;\n" + "return hash;\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } // =================================================================== -void ImmutableMessageGenerator:: -GenerateExtensionRegistrationCode(io::Printer* printer) { +void ImmutableMessageGenerator::GenerateExtensionRegistrationCode( + io::Printer* printer) { for (int i = 0; i < descriptor_->extension_count(); i++) { ImmutableExtensionGenerator(descriptor_->extension(i), context_) - .GenerateRegistrationCode(printer); + .GenerateRegistrationCode(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { ImmutableMessageGenerator(descriptor_->nested_type(i), context_) - .GenerateExtensionRegistrationCode(printer); + .GenerateExtensionRegistrationCode(printer); } } // =================================================================== -void ImmutableMessageGenerator:: -GenerateParsingConstructor(io::Printer* printer) { - std::unique_ptr<const FieldDescriptor * []> sorted_fields( +void ImmutableMessageGenerator::GenerateParsingConstructor( + io::Printer* printer) { + std::unique_ptr<const FieldDescriptor*[]> sorted_fields( SortFieldsByNumber(descriptor_)); printer->Print( @@ -1209,26 +1190,25 @@ GenerateParsingConstructor(io::Printer* printer) { } int totalBuilderInts = (totalBuilderBits + 31) / 32; for (int i = 0; i < totalBuilderInts; i++) { - printer->Print("int mutable_$bit_field_name$ = 0;\n", - "bit_field_name", GetBitFieldName(i)); + printer->Print("int mutable_$bit_field_name$ = 0;\n", "bit_field_name", + GetBitFieldName(i)); } printer->Print( "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n" " com.google.protobuf.UnknownFieldSet.newBuilder();\n"); - printer->Print( - "try {\n"); + printer->Print("try {\n"); printer->Indent(); printer->Print( - "boolean done = false;\n" - "while (!done) {\n"); + "boolean done = false;\n" + "while (!done) {\n"); printer->Indent(); printer->Print( - "int tag = input.readTag();\n" - "switch (tag) {\n"); + "int tag = input.readTag();\n" + "switch (tag) {\n"); printer->Indent(); printer->Print( @@ -1238,8 +1218,8 @@ GenerateParsingConstructor(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = sorted_fields[i]; - uint32 tag = WireFormatLite::MakeTag(field->number(), - WireFormat::WireTypeForFieldType(field->type())); + uint32 tag = WireFormatLite::MakeTag( + field->number(), WireFormat::WireTypeForFieldType(field->type())); printer->Print("case $tag$: {\n", "tag", StrCat(static_cast<int32>(tag))); @@ -1249,14 +1229,14 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Outdent(); printer->Print( - " break;\n" - "}\n"); + " break;\n" + "}\n"); if (field->is_packable()) { // To make packed = true wire compatible, we generate parsing code from a // packed version of this field regardless of field->options().packed(). - uint32 packed_tag = WireFormatLite::MakeTag(field->number(), - WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + uint32 packed_tag = WireFormatLite::MakeTag( + field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); printer->Print("case $tag$: {\n", "tag", StrCat(static_cast<int32>(packed_tag))); printer->Indent(); @@ -1265,8 +1245,8 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Outdent(); printer->Print( - " break;\n" - "}\n"); + " break;\n" + "}\n"); } } @@ -1282,8 +1262,8 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" // switch (tag) - "}\n"); // while (!done) + " }\n" // switch (tag) + "}\n"); // while (!done) printer->Outdent(); printer->Print( @@ -1305,13 +1285,12 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Print("this.unknownFields = unknownFields.build();\n"); // Make extensions immutable. - printer->Print( - "makeExtensionsImmutable();\n"); + printer->Print("makeExtensionsImmutable();\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" // finally + " }\n" // finally "}\n"); } @@ -1354,8 +1333,7 @@ void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) { "return builder.buildPartial();\n"); printer->Outdent(); } - printer->Print( - "}\n"); + printer->Print("}\n"); printer->Outdent(); printer->Print( "};\n" @@ -1387,76 +1365,76 @@ void ImmutableMessageGenerator::GenerateInitializers(io::Printer* printer) { void ImmutableMessageGenerator::GenerateAnyMethods(io::Printer* printer) { printer->Print( - "private static String getTypeUrl(\n" - " java.lang.String typeUrlPrefix,\n" - " com.google.protobuf.Descriptors.Descriptor descriptor) {\n" - " return typeUrlPrefix.endsWith(\"/\")\n" - " ? typeUrlPrefix + descriptor.getFullName()\n" - " : typeUrlPrefix + \"/\" + descriptor.getFullName();\n" - "}\n" - "\n" - "private static String getTypeNameFromTypeUrl(\n" - " java.lang.String typeUrl) {\n" - " int pos = typeUrl.lastIndexOf('/');\n" - " return pos == -1 ? \"\" : typeUrl.substring(pos + 1);\n" - "}\n" - "\n" - "public static <T extends com.google.protobuf.Message> Any pack(\n" - " T message) {\n" - " return Any.newBuilder()\n" - " .setTypeUrl(getTypeUrl(\"type.googleapis.com\",\n" - " message.getDescriptorForType()))\n" - " .setValue(message.toByteString())\n" - " .build();\n" - "}\n" - "\n" - "/**\n" - " * Packs a message using the given type URL prefix. The type URL will\n" - " * be constructed by concatenating the message type's full name to the\n" - " * prefix with an optional \"/\" separator if the prefix doesn't end\n" - " * with \"/\" already.\n" - " */\n" - "public static <T extends com.google.protobuf.Message> Any pack(\n" - " T message, java.lang.String typeUrlPrefix) {\n" - " return Any.newBuilder()\n" - " .setTypeUrl(getTypeUrl(typeUrlPrefix,\n" - " message.getDescriptorForType()))\n" - " .setValue(message.toByteString())\n" - " .build();\n" - "}\n" - "\n" - "public <T extends com.google.protobuf.Message> boolean is(\n" - " java.lang.Class<T> clazz) {\n" - " T defaultInstance =\n" - " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" - " return getTypeNameFromTypeUrl(getTypeUrl()).equals(\n" - " defaultInstance.getDescriptorForType().getFullName());\n" - "}\n" - "\n" - "private volatile com.google.protobuf.Message cachedUnpackValue;\n" - "\n" - "@java.lang.SuppressWarnings(\"unchecked\")\n" - "public <T extends com.google.protobuf.Message> T unpack(\n" - " java.lang.Class<T> clazz)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " boolean invalidClazz = false;\n" - " if (cachedUnpackValue != null) {\n" - " if (cachedUnpackValue.getClass() == clazz) {\n" - " return (T) cachedUnpackValue;\n" - " }\n" - " invalidClazz = true;\n" - " }\n" - " if (invalidClazz || !is(clazz)) {\n" - " throw new com.google.protobuf.InvalidProtocolBufferException(\n" - " \"Type of the Any message does not match the given class.\");\n" - " }\n" - " T defaultInstance =\n" - " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" - " T result = (T) defaultInstance.getParserForType()\n" - " .parseFrom(getValue());\n" - " cachedUnpackValue = result;\n" - " return result;\n" - "}\n"); + "private static String getTypeUrl(\n" + " java.lang.String typeUrlPrefix,\n" + " com.google.protobuf.Descriptors.Descriptor descriptor) {\n" + " return typeUrlPrefix.endsWith(\"/\")\n" + " ? typeUrlPrefix + descriptor.getFullName()\n" + " : typeUrlPrefix + \"/\" + descriptor.getFullName();\n" + "}\n" + "\n" + "private static String getTypeNameFromTypeUrl(\n" + " java.lang.String typeUrl) {\n" + " int pos = typeUrl.lastIndexOf('/');\n" + " return pos == -1 ? \"\" : typeUrl.substring(pos + 1);\n" + "}\n" + "\n" + "public static <T extends com.google.protobuf.Message> Any pack(\n" + " T message) {\n" + " return Any.newBuilder()\n" + " .setTypeUrl(getTypeUrl(\"type.googleapis.com\",\n" + " message.getDescriptorForType()))\n" + " .setValue(message.toByteString())\n" + " .build();\n" + "}\n" + "\n" + "/**\n" + " * Packs a message using the given type URL prefix. The type URL will\n" + " * be constructed by concatenating the message type's full name to the\n" + " * prefix with an optional \"/\" separator if the prefix doesn't end\n" + " * with \"/\" already.\n" + " */\n" + "public static <T extends com.google.protobuf.Message> Any pack(\n" + " T message, java.lang.String typeUrlPrefix) {\n" + " return Any.newBuilder()\n" + " .setTypeUrl(getTypeUrl(typeUrlPrefix,\n" + " message.getDescriptorForType()))\n" + " .setValue(message.toByteString())\n" + " .build();\n" + "}\n" + "\n" + "public <T extends com.google.protobuf.Message> boolean is(\n" + " java.lang.Class<T> clazz) {\n" + " T defaultInstance =\n" + " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" + " return getTypeNameFromTypeUrl(getTypeUrl()).equals(\n" + " defaultInstance.getDescriptorForType().getFullName());\n" + "}\n" + "\n" + "private volatile com.google.protobuf.Message cachedUnpackValue;\n" + "\n" + "@java.lang.SuppressWarnings(\"unchecked\")\n" + "public <T extends com.google.protobuf.Message> T unpack(\n" + " java.lang.Class<T> clazz)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " boolean invalidClazz = false;\n" + " if (cachedUnpackValue != null) {\n" + " if (cachedUnpackValue.getClass() == clazz) {\n" + " return (T) cachedUnpackValue;\n" + " }\n" + " invalidClazz = true;\n" + " }\n" + " if (invalidClazz || !is(clazz)) {\n" + " throw new com.google.protobuf.InvalidProtocolBufferException(\n" + " \"Type of the Any message does not match the given class.\");\n" + " }\n" + " T defaultInstance =\n" + " com.google.protobuf.Internal.getDefaultInstance(clazz);\n" + " T result = (T) defaultInstance.getParserForType()\n" + " .parseFrom(getValue());\n" + " cachedUnpackValue = result;\n" + " return result;\n" + "}\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_message.h b/src/google/protobuf/compiler/java/java_message.h index ac0c765994..de2f359a41 100644 --- a/src/google/protobuf/compiler/java/java_message.h +++ b/src/google/protobuf/compiler/java/java_message.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ -#include <string> #include <map> +#include <string> #include <google/protobuf/compiler/java/java_field.h> namespace google { @@ -101,14 +101,13 @@ class ImmutableMessageGenerator : public MessageGenerator { virtual void Generate(io::Printer* printer); virtual void GenerateInterface(io::Printer* printer); virtual void GenerateExtensionRegistrationCode(io::Printer* printer); - virtual void GenerateStaticVariables( - io::Printer* printer, int* bytecode_estimate); + virtual void GenerateStaticVariables(io::Printer* printer, + int* bytecode_estimate); // Returns an estimate of the number of bytes the printed code will compile to virtual int GenerateStaticVariableInitializers(io::Printer* printer); private: - void GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate); // Returns an estimate of the number of bytes the printed code will compile to diff --git a/src/google/protobuf/compiler/java/java_message_builder.cc b/src/google/protobuf/compiler/java/java_message_builder.cc index 2ba77a2219..aa00a66f4c 100644 --- a/src/google/protobuf/compiler/java/java_message_builder.cc +++ b/src/google/protobuf/compiler/java/java_message_builder.cc @@ -69,11 +69,12 @@ std::string MapValueImmutableClassdName(const Descriptor* descriptor, } } // namespace -MessageBuilderGenerator::MessageBuilderGenerator( - const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { +MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor, + Context* context) + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; @@ -81,28 +82,28 @@ MessageBuilderGenerator::MessageBuilderGenerator( MessageBuilderGenerator::~MessageBuilderGenerator() {} -void MessageBuilderGenerator:: -Generate(io::Printer* printer) { +void MessageBuilderGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); if (descriptor_->extension_range_count() > 0) { printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessage$ver$.ExtendableBuilder<\n" - " $classname$, Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessage$ver$.ExtendableBuilder<\n" + " $classname$, Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } else { printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessage$ver$.Builder<Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessage$ver$.Builder<Builder> " + "implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } printer->Indent(); @@ -117,31 +118,32 @@ Generate(io::Printer* printer) { // oneof std::map<std::string, std::string> vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // oneofCase() and clearOneof() printer->Print(vars, - "public $oneof_capitalized_name$Case\n" - " get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n" - "public Builder clear$oneof_capitalized_name$() {\n" - " $oneof_name$Case_ = 0;\n" - " $oneof_name$_ = null;\n"); + "public $oneof_capitalized_name$Case\n" + " get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n" + "public Builder clear$oneof_capitalized_name$() {\n" + " $oneof_name$Case_ = 0;\n" + " $oneof_name$_ = null;\n"); printer->Print(" onChanged();\n"); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); } // Integers for bit fields. @@ -159,32 +161,32 @@ Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("\n"); field_generators_.get(descriptor_->field(i)) - .GenerateBuilderMembers(printer); + .GenerateBuilderMembers(printer); } - // Override methods declared in GeneratedMessage to return the concrete - // generated type so callsites won't depend on GeneratedMessage. This - // is needed to keep binary compatibility when we change generated code - // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release - // we changed all generated code to subclass GeneratedMessageV3). + // Override methods declared in GeneratedMessage to return the concrete + // generated type so callsites won't depend on GeneratedMessage. This + // is needed to keep binary compatibility when we change generated code + // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release + // we changed all generated code to subclass GeneratedMessageV3). printer->Print( - "@java.lang.Override\n" - "public final Builder setUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.setUnknownFields(unknownFields);\n" - "}\n" - "\n" - "@java.lang.Override\n" - "public final Builder mergeUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.mergeUnknownFields(unknownFields);\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public final Builder setUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.setUnknownFields(unknownFields);\n" + "}\n" + "\n" + "@java.lang.Override\n" + "public final Builder mergeUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.mergeUnknownFields(unknownFields);\n" + "}\n" + "\n"); printer->Print( - "\n" - "// @@protoc_insertion_point(builder_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(builder_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n"); @@ -192,17 +194,16 @@ Generate(io::Printer* printer) { // =================================================================== -void MessageBuilderGenerator:: -GenerateDescriptorMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) { if (!descriptor_->options().no_standard_descriptor_accessor()) { printer->Print( - "public static final com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptor() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "public static final com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptor() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); } std::vector<const FieldDescriptor*> map_fields; for (int i = 0; i < descriptor_->field_count(); i++) { @@ -214,10 +215,10 @@ GenerateDescriptorMethods(io::Printer* printer) { } if (!map_fields.empty()) { printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "protected com.google.protobuf.MapField internalGetMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "protected com.google.protobuf.MapField internalGetMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { @@ -239,16 +240,15 @@ GenerateDescriptorMethods(io::Printer* printer) { " }\n" "}\n"); printer->Print( - "@SuppressWarnings({\"rawtypes\"})\n" - "protected com.google.protobuf.MapField internalGetMutableMapField(\n" - " int number) {\n" - " switch (number) {\n"); + "@SuppressWarnings({\"rawtypes\"})\n" + "protected com.google.protobuf.MapField internalGetMutableMapField(\n" + " int number) {\n" + " switch (number) {\n"); printer->Indent(); printer->Indent(); for (int i = 0; i < map_fields.size(); ++i) { const FieldDescriptor* field = map_fields[i]; - const FieldGeneratorInfo* info = - context_->GetFieldGeneratorInfo(field); + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); printer->Print( "case $number$:\n" " return internalGetMutable$capitalized_name$();\n", @@ -266,24 +266,24 @@ GenerateDescriptorMethods(io::Printer* printer) { "}\n"); } printer->Print( - "@java.lang.Override\n" - "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" - " internalGetFieldAccessorTable() {\n" - " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" - " .ensureFieldAccessorsInitialized(\n" - " $classname$.class, $classname$.Builder.class);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "@java.lang.Override\n" + "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" + " internalGetFieldAccessorTable() {\n" + " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" + " .ensureFieldAccessorsInitialized(\n" + " $classname$.class, $classname$.Builder.class);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_), "ver", + GeneratedCodeVersionSuffix()); } // =================================================================== -void MessageBuilderGenerator:: -GenerateCommonBuilderMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateCommonBuilderMethods( + io::Printer* printer) { printer->Print( "// Construct using $classname$.newBuilder()\n" "private Builder() {\n" @@ -293,19 +293,19 @@ GenerateCommonBuilderMethods(io::Printer* printer) { "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "private Builder(\n" - " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" - " super(parent);\n" - " maybeForceBuilderInitialization();\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "ver", GeneratedCodeVersionSuffix()); + "private Builder(\n" + " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n" + " super(parent);\n" + " maybeForceBuilderInitialization();\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), "ver", + GeneratedCodeVersionSuffix()); printer->Print( - "private void maybeForceBuilderInitialization() {\n" - " if (com.google.protobuf.GeneratedMessage$ver$\n" - " .alwaysUseFieldBuilders) {\n", - "ver", GeneratedCodeVersionSuffix()); + "private void maybeForceBuilderInitialization() {\n" + " if (com.google.protobuf.GeneratedMessage$ver$\n" + " .alwaysUseFieldBuilders) {\n", + "ver", GeneratedCodeVersionSuffix()); printer->Indent(); printer->Indent(); @@ -319,13 +319,13 @@ GenerateCommonBuilderMethods(io::Printer* printer) { printer->Outdent(); printer->Print( - " }\n" - "}\n"); + " }\n" + "}\n"); printer->Print( - "@java.lang.Override\n" - "public Builder clear() {\n" - " super.clear();\n"); + "@java.lang.Override\n" + "public Builder clear() {\n" + " super.clear();\n"); printer->Indent(); @@ -338,55 +338,55 @@ GenerateCommonBuilderMethods(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "$oneof_name$Case_ = 0;\n" - "$oneof_name$_ = null;\n", - "oneof_name", context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + "$oneof_name$Case_ = 0;\n" + "$oneof_name$_ = null;\n", + "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); } printer->Outdent(); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); printer->Print( - "@java.lang.Override\n" - "public com.google.protobuf.Descriptors.Descriptor\n" - " getDescriptorForType() {\n" - " return $fileclass$.internal_$identifier$_descriptor;\n" - "}\n" - "\n", - "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), - "identifier", UniqueFileScopeIdentifier(descriptor_)); + "@java.lang.Override\n" + "public com.google.protobuf.Descriptors.Descriptor\n" + " getDescriptorForType() {\n" + " return $fileclass$.internal_$identifier$_descriptor;\n" + "}\n" + "\n", + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), + "identifier", UniqueFileScopeIdentifier(descriptor_)); // LITE runtime implements this in GeneratedMessageLite. printer->Print( - "@java.lang.Override\n" - "public $classname$ getDefaultInstanceForType() {\n" - " return $classname$.getDefaultInstance();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ getDefaultInstanceForType() {\n" + " return $classname$.getDefaultInstance();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "public $classname$ build() {\n" - " $classname$ result = buildPartial();\n" - " if (!result.isInitialized()) {\n" - " throw newUninitializedMessageException(result);\n" - " }\n" - " return result;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ build() {\n" + " $classname$ result = buildPartial();\n" + " if (!result.isInitialized()) {\n" + " throw newUninitializedMessageException(result);\n" + " }\n" + " return result;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "@java.lang.Override\n" - "public $classname$ buildPartial() {\n" - " $classname$ result = new $classname$(this);\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public $classname$ buildPartial() {\n" + " $classname$ result = new $classname$(this);\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); @@ -426,21 +426,20 @@ GenerateCommonBuilderMethods(io::Printer* printer) { } for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print("result.$oneof_name$Case_ = $oneof_name$Case_;\n", - "oneof_name", context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); + printer->Print( + "result.$oneof_name$Case_ = $oneof_name$Case_;\n", "oneof_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name); } printer->Outdent(); - printer->Print( - " onBuilt();\n"); + printer->Print(" onBuilt();\n"); printer->Print( - " return result;\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + " return result;\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); // Override methods declared in GeneratedMessage to return the concrete // generated type so callsites won't depend on GeneratedMessage. This @@ -448,190 +447,181 @@ GenerateCommonBuilderMethods(io::Printer* printer) { // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release // we changed all generated code to subclass GeneratedMessageV3). printer->Print( - "@java.lang.Override\n" - "public Builder clone() {\n" - " return super.clone();\n" - "}\n" - "@java.lang.Override\n" - "public Builder setField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " java.lang.Object value) {\n" - " return super.setField(field, value);\n" - "}\n" - "@java.lang.Override\n" - "public Builder clearField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field) {\n" - " return super.clearField(field);\n" - "}\n" - "@java.lang.Override\n" - "public Builder clearOneof(\n" - " com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n" - " return super.clearOneof(oneof);\n" - "}\n" - "@java.lang.Override\n" - "public Builder setRepeatedField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " int index, java.lang.Object value) {\n" - " return super.setRepeatedField(field, index, value);\n" - "}\n" - "@java.lang.Override\n" - "public Builder addRepeatedField(\n" - " com.google.protobuf.Descriptors.FieldDescriptor field,\n" - " java.lang.Object value) {\n" - " return super.addRepeatedField(field, value);\n" - "}\n"); - - if (descriptor_->extension_range_count() > 0) { - printer->Print( "@java.lang.Override\n" - "public <Type> Builder setExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, Type> extension,\n" - " Type value) {\n" - " return super.setExtension(extension, value);\n" + "public Builder clone() {\n" + " return super.clone();\n" "}\n" "@java.lang.Override\n" - "public <Type> Builder setExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List<Type>> extension,\n" - " int index, Type value) {\n" - " return super.setExtension(extension, index, value);\n" + "public Builder setField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " java.lang.Object value) {\n" + " return super.setField(field, value);\n" "}\n" "@java.lang.Override\n" - "public <Type> Builder addExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List<Type>> extension,\n" - " Type value) {\n" - " return super.addExtension(extension, value);\n" + "public Builder clearField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field) {\n" + " return super.clearField(field);\n" "}\n" "@java.lang.Override\n" - "public <Type> Builder clearExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, ?> extension) {\n" - " return super.clearExtension(extension);\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public Builder clearOneof(\n" + " com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n" + " return super.clearOneof(oneof);\n" + "}\n" + "@java.lang.Override\n" + "public Builder setRepeatedField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " int index, java.lang.Object value) {\n" + " return super.setRepeatedField(field, index, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder addRepeatedField(\n" + " com.google.protobuf.Descriptors.FieldDescriptor field,\n" + " java.lang.Object value) {\n" + " return super.addRepeatedField(field, value);\n" + "}\n"); + + if (descriptor_->extension_range_count() > 0) { + printer->Print( + "@java.lang.Override\n" + "public <Type> Builder setExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, Type> extension,\n" + " Type value) {\n" + " return super.setExtension(extension, value);\n" + "}\n" + "@java.lang.Override\n" + "public <Type> Builder setExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List<Type>> extension,\n" + " int index, Type value) {\n" + " return super.setExtension(extension, index, value);\n" + "}\n" + "@java.lang.Override\n" + "public <Type> Builder addExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List<Type>> extension,\n" + " Type value) {\n" + " return super.addExtension(extension, value);\n" + "}\n" + "@java.lang.Override\n" + "public <Type> Builder clearExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, ?> extension) {\n" + " return super.clearExtension(extension);\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // ----------------------------------------------------------------- if (context_->HasGeneratedMethods(descriptor_)) { printer->Print( - "@java.lang.Override\n" - "public Builder mergeFrom(com.google.protobuf.Message other) {\n" - " if (other instanceof $classname$) {\n" - " return mergeFrom(($classname$)other);\n" - " } else {\n" - " super.mergeFrom(other);\n" - " return this;\n" - " }\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public Builder mergeFrom(com.google.protobuf.Message other) {\n" + " if (other instanceof $classname$) {\n" + " return mergeFrom(($classname$)other);\n" + " } else {\n" + " super.mergeFrom(other);\n" + " return this;\n" + " }\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "public Builder mergeFrom($classname$ other) {\n" - // Optimization: If other is the default instance, we know none of its - // fields are set so we can skip the merge. - " if (other == $classname$.getDefaultInstance()) return this;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public Builder mergeFrom($classname$ other) {\n" + // Optimization: If other is the default instance, we know none of its + // fields are set so we can skip the merge. + " if (other == $classname$.getDefaultInstance()) return this;\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { if (!descriptor_->field(i)->containing_oneof()) { - field_generators_.get( - descriptor_->field(i)).GenerateMergingCode(printer); + field_generators_.get(descriptor_->field(i)) + .GenerateMergingCode(printer); } } // Merge oneof fields. for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - printer->Print( - "switch (other.get$oneof_capitalized_name$Case()) {\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); + printer->Print("switch (other.get$oneof_capitalized_name$Case()) {\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name); printer->Indent(); for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); - printer->Print( - "case $field_name$: {\n", - "field_name", - ToUpper(field->name())); + printer->Print("case $field_name$: {\n", "field_name", + ToUpper(field->name())); printer->Indent(); field_generators_.get(field).GenerateMergingCode(printer); - printer->Print( - "break;\n"); + printer->Print("break;\n"); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Print( - "case $cap_oneof_name$_NOT_SET: {\n" - " break;\n" - "}\n", - "cap_oneof_name", - ToUpper(context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name)); + "case $cap_oneof_name$_NOT_SET: {\n" + " break;\n" + "}\n", + "cap_oneof_name", + ToUpper( + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->name)); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } printer->Outdent(); // if message type has extensions if (descriptor_->extension_range_count() > 0) { - printer->Print( - " this.mergeExtensionFields(other);\n"); + printer->Print(" this.mergeExtensionFields(other);\n"); } - printer->Print( - " this.mergeUnknownFields(other.unknownFields);\n"); + printer->Print(" this.mergeUnknownFields(other.unknownFields);\n"); - printer->Print( - " onChanged();\n"); + printer->Print(" onChanged();\n"); printer->Print( - " return this;\n" - "}\n" - "\n"); + " return this;\n" + "}\n" + "\n"); } } // =================================================================== -void MessageBuilderGenerator:: -GenerateBuilderParsingMethods(io::Printer* printer) { +void MessageBuilderGenerator::GenerateBuilderParsingMethods( + io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public Builder mergeFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " $classname$ parsedMessage = null;\n" - " try {\n" - " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n" - " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" - " parsedMessage = ($classname$) e.getUnfinishedMessage();\n" - " throw e.unwrapIOException();\n" - " } finally {\n" - " if (parsedMessage != null) {\n" - " mergeFrom(parsedMessage);\n" - " }\n" - " }\n" - " return this;\n" - "}\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.Override\n" + "public Builder mergeFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " $classname$ parsedMessage = null;\n" + " try {\n" + " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n" + " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" + " parsedMessage = ($classname$) e.getUnfinishedMessage();\n" + " throw e.unwrapIOException();\n" + " } finally {\n" + " if (parsedMessage != null) {\n" + " mergeFrom(parsedMessage);\n" + " }\n" + " }\n" + " return this;\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== -void MessageBuilderGenerator::GenerateIsInitialized( - io::Printer* printer) { +void MessageBuilderGenerator::GenerateIsInitialized(io::Printer* printer) { printer->Print( - "@java.lang.Override\n" - "public final boolean isInitialized() {\n"); + "@java.lang.Override\n" + "public final boolean isInitialized() {\n"); printer->Indent(); // Check that all required fields in this message are set. @@ -643,10 +633,10 @@ void MessageBuilderGenerator::GenerateIsInitialized( if (field->is_required()) { printer->Print( - "if (!has$name$()) {\n" - " return false;\n" - "}\n", - "name", info->capitalized_name); + "if (!has$name$()) {\n" + " return false;\n" + "}\n", + "name", info->capitalized_name); } } @@ -659,12 +649,12 @@ void MessageBuilderGenerator::GenerateIsInitialized( switch (field->label()) { case FieldDescriptor::LABEL_REQUIRED: printer->Print( - "if (!get$name$().isInitialized()) {\n" - " return false;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "if (!get$name$().isInitialized()) {\n" + " return false;\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); break; case FieldDescriptor::LABEL_OPTIONAL: if (!SupportFieldPresence(descriptor_->file()) && @@ -676,38 +666,38 @@ void MessageBuilderGenerator::GenerateIsInitialized( "oneof_name", oneof_info->name, "field_number", StrCat(field->number())); } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); + printer->Print("if (has$name$()) {\n", "name", + info->capitalized_name); } printer->Print( - " if (!get$name$().isInitialized()) {\n" - " return false;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" + " if (!get$name$().isInitialized()) {\n" " return false;\n" " }\n" "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), "name", info->capitalized_name); + break; + case FieldDescriptor::LABEL_REPEATED: + if (IsMapEntry(field->message_type())) { + printer->Print( + "for ($type$ item : get$name$Map().values()) {\n" + " if (!item.isInitialized()) {\n" + " return false;\n" + " }\n" + "}\n", + "type", + MapValueImmutableClassdName(field->message_type(), + name_resolver_), + "name", info->capitalized_name); } else { printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " return false;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); + "for (int i = 0; i < get$name$Count(); i++) {\n" + " if (!get$name$(i).isInitialized()) {\n" + " return false;\n" + " }\n" + "}\n", + "type", + name_resolver_->GetImmutableClassName(field->message_type()), + "name", info->capitalized_name); } break; } @@ -716,17 +706,17 @@ void MessageBuilderGenerator::GenerateIsInitialized( if (descriptor_->extension_range_count() > 0) { printer->Print( - "if (!extensionsAreInitialized()) {\n" - " return false;\n" - "}\n"); + "if (!extensionsAreInitialized()) {\n" + " return false;\n" + "}\n"); } printer->Outdent(); printer->Print( - " return true;\n" - "}\n" - "\n"); + " return true;\n" + "}\n" + "\n"); } // =================================================================== diff --git a/src/google/protobuf/compiler/java/java_message_builder.h b/src/google/protobuf/compiler/java/java_message_builder.h index d67a9c9d4f..a31d0badf9 100644 --- a/src/google/protobuf/compiler/java/java_message_builder.h +++ b/src/google/protobuf/compiler/java/java_message_builder.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ -#include <string> #include <map> +#include <string> #include <google/protobuf/compiler/java/java_field.h> namespace google { diff --git a/src/google/protobuf/compiler/java/java_message_builder_lite.cc b/src/google/protobuf/compiler/java/java_message_builder_lite.cc index 0e2de150c2..ae34499131 100644 --- a/src/google/protobuf/compiler/java/java_message_builder_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_builder_lite.cc @@ -61,9 +61,10 @@ namespace java { MessageBuilderLiteGenerator::MessageBuilderLiteGenerator( const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : descriptor_(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; @@ -71,19 +72,17 @@ MessageBuilderLiteGenerator::MessageBuilderLiteGenerator( MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {} -void MessageBuilderLiteGenerator:: -Generate(io::Printer* printer) { +void MessageBuilderLiteGenerator::Generate(io::Printer* printer) { WriteMessageDocComment(printer, descriptor_); printer->Print( - "public static final class Builder extends\n" - " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n" - " $classname$, Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_), - "extra_interfaces", ExtraBuilderInterfaces(descriptor_), - "extendible", - descriptor_->extension_range_count() > 0 ? "Extendable" : ""); + "public static final class Builder extends\n" + " com.google.protobuf.GeneratedMessageLite.$extendible$Builder<\n" + " $classname$, Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_), + "extra_interfaces", ExtraBuilderInterfaces(descriptor_), "extendible", + descriptor_->extension_range_count() > 0 ? "Extendable" : ""); printer->Indent(); GenerateCommonBuilderMethods(printer); @@ -91,38 +90,39 @@ Generate(io::Printer* printer) { // oneof std::map<std::string, std::string> vars; for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - vars["oneof_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name; + vars["oneof_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i))->name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name; vars["oneof_index"] = StrCat(descriptor_->oneof_decl(i)->index()); // oneofCase() and clearOneof() printer->Print(vars, - "@java.lang.Override\n" - "public $oneof_capitalized_name$Case\n" - " get$oneof_capitalized_name$Case() {\n" - " return instance.get$oneof_capitalized_name$Case();\n" - "}\n" - "\n" - "public Builder clear$oneof_capitalized_name$() {\n" - " copyOnWrite();\n" - " instance.clear$oneof_capitalized_name$();\n" - " return this;\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public $oneof_capitalized_name$Case\n" + " get$oneof_capitalized_name$Case() {\n" + " return instance.get$oneof_capitalized_name$Case();\n" + "}\n" + "\n" + "public Builder clear$oneof_capitalized_name$() {\n" + " copyOnWrite();\n" + " instance.clear$oneof_capitalized_name$();\n" + " return this;\n" + "}\n" + "\n"); } for (int i = 0; i < descriptor_->field_count(); i++) { printer->Print("\n"); field_generators_.get(descriptor_->field(i)) - .GenerateBuilderMembers(printer); + .GenerateBuilderMembers(printer); } printer->Print( - "\n" - "// @@protoc_insertion_point(builder_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(builder_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n"); @@ -130,15 +130,15 @@ Generate(io::Printer* printer) { // =================================================================== -void MessageBuilderLiteGenerator:: -GenerateCommonBuilderMethods(io::Printer* printer) { +void MessageBuilderLiteGenerator::GenerateCommonBuilderMethods( + io::Printer* printer) { printer->Print( - "// Construct using $classname$.newBuilder()\n" - "private Builder() {\n" - " super(DEFAULT_INSTANCE);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "// Construct using $classname$.newBuilder()\n" + "private Builder() {\n" + " super(DEFAULT_INSTANCE);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== diff --git a/src/google/protobuf/compiler/java/java_message_builder_lite.h b/src/google/protobuf/compiler/java/java_message_builder_lite.h index 079d86a925..e0a48dc1f7 100644 --- a/src/google/protobuf/compiler/java/java_message_builder_lite.h +++ b/src/google/protobuf/compiler/java/java_message_builder_lite.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_LITE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_LITE_H__ -#include <string> #include <map> +#include <string> #include <google/protobuf/compiler/java/java_field.h> namespace google { diff --git a/src/google/protobuf/compiler/java/java_message_field.cc b/src/google/protobuf/compiler/java/java_message_field.cc index 011cc5602f..8c576752d0 100644 --- a/src/google/protobuf/compiler/java/java_message_field.cc +++ b/src/google/protobuf/compiler/java/java_message_field.cc @@ -36,9 +36,9 @@ #include <string> #include <google/protobuf/compiler/java/java_context.h> -#include <google/protobuf/compiler/java/java_message_field.h> #include <google/protobuf/compiler/java/java_doc_comment.h> #include <google/protobuf/compiler/java/java_helpers.h> +#include <google/protobuf/compiler/java/java_message_field.h> #include <google/protobuf/compiler/java/java_name_resolver.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/wire_format.h> @@ -62,17 +62,17 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = - (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? - "Group" : "Message"; + (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? "Group" + : "Message"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; (*variables)["ver"] = GeneratedCodeVersionSuffix(); (*variables)["get_parser"] = - ExposePublicParser(descriptor->message_type()->file()) - ? "PARSER" : "parser()"; + ExposePublicParser(descriptor->message_type()->file()) ? "PARSER" + : "parser()"; if (SupportFieldPresence(descriptor->file())) { // For singular messages and builders, one bit is used for the hasField bit. @@ -119,17 +119,17 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, // =================================================================== -ImmutableMessageFieldGenerator:: -ImmutableMessageFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { - SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, - context->GetFieldGeneratorInfo(descriptor), - name_resolver_, &variables_); +ImmutableMessageFieldGenerator::ImmutableMessageFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { + SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, + context->GetFieldGeneratorInfo(descriptor), + name_resolver_, &variables_); } ImmutableMessageFieldGenerator::~ImmutableMessageFieldGenerator() {} @@ -142,78 +142,80 @@ int ImmutableMessageFieldGenerator::GetNumBitsForBuilder() const { return GetNumBitsForMessage(); } -void ImmutableMessageFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having a method specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$boolean has$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder();\n"); + printer->Print( + variables_, + "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder();\n"); } -void ImmutableMessageFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $type$ $name$_;\n"); +void ImmutableMessageFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$_ != null;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " return get$capitalized_name$();\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " return get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } } void ImmutableMessageFieldGenerator::PrintNestedBuilderCondition( - io::Printer* printer, - const char* regular_case, + io::Printer* printer, const char* regular_case, const char* nested_builder_case) const { printer->Print(variables_, "if ($name$Builder_ == null) {\n"); printer->Indent(); @@ -227,10 +229,8 @@ void ImmutableMessageFieldGenerator::PrintNestedBuilderCondition( } void ImmutableMessageFieldGenerator::PrintNestedBuilderFunction( - io::Printer* printer, - const char* method_prototype, - const char* regular_case, - const char* nested_builder_case, + io::Printer* printer, const char* method_prototype, + const char* regular_case, const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); printer->Annotate("{", "}", descriptor_); @@ -244,8 +244,8 @@ void ImmutableMessageFieldGenerator::PrintNestedBuilderFunction( printer->Print("}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. @@ -255,11 +255,11 @@ GenerateBuilderMembers(io::Printer* printer) const { printer->Print(variables_, "private $type$ $name$_;\n"); printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" - "\n"); + // If this builder is non-null, it is used and the other fields + // are ignored. + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" + "\n"); // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". @@ -267,178 +267,178 @@ GenerateBuilderMembers(io::Printer* printer) const { // boolean hasField() WriteFieldDocComment(printer, descriptor_); if (support_field_presence) { - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$Builder_ != null || $name$_ != null;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$Builder_ != null || $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } // Field getField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$()", - "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n", - "return $name$Builder_.getMessage();\n", - NULL); + PrintNestedBuilderFunction( + printer, "$deprecation$public $type$ ${$get$capitalized_name$$}$()", + "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n", + "return $name$Builder_.getMessage();\n", NULL); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "$name$_ = value;\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "$name$_ = value;\n" + "$on_changed$\n", - "$name$Builder_.setMessage(value);\n", + "$name$Builder_.setMessage(value);\n", - "$set_has_field_bit_builder$\n" - "return this;\n"); + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "$name$_ = builderForValue.build();\n" - "$on_changed$\n", + "$name$_ = builderForValue.build();\n" + "$on_changed$\n", - "$name$Builder_.setMessage(builderForValue.build());\n", + "$name$Builder_.setMessage(builderForValue.build());\n", - "$set_has_field_bit_builder$\n" - "return this;\n"); + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", - - support_field_presence - ? "if ($get_has_field_bit_builder$ &&\n" - " $name$_ != null &&\n" - " $name$_ != $type$.getDefaultInstance()) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - "} else {\n" - " $name$_ = value;\n" - "}\n" - "$on_changed$\n" - : "if ($name$_ != null) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - "} else {\n" - " $name$_ = value;\n" - "}\n" - "$on_changed$\n", - - "$name$Builder_.mergeFrom(value);\n", - - "$set_has_field_bit_builder$\n" - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", + + support_field_presence + ? "if ($get_has_field_bit_builder$ &&\n" + " $name$_ != null &&\n" + " $name$_ != $type$.getDefaultInstance()) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + "} else {\n" + " $name$_ = value;\n" + "}\n" + "$on_changed$\n" + : "if ($name$_ != null) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + "} else {\n" + " $name$_ = value;\n" + "}\n" + "$on_changed$\n", + + "$name$Builder_.mergeFrom(value);\n", + + "$set_has_field_bit_builder$\n" + "return this;\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "$name$_ = null;\n" - "$on_changed$\n", + "$name$_ = null;\n" + "$on_changed$\n", - support_field_presence - ? "$name$Builder_.clear();\n" - : "$name$_ = null;\n" - "$name$Builder_ = null;\n", + support_field_presence ? "$name$Builder_.clear();\n" + : "$name$_ = null;\n" + "$name$Builder_ = null;\n", - "$clear_has_field_bit_builder$\n" - "return this;\n"); + "$clear_has_field_bit_builder$\n" + "return this;\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$get$capitalized_name$Builder$}$() {\n" - " $set_has_field_bit_builder$\n" - " $on_changed$\n" - " return get$capitalized_name$FieldBuilder().getBuilder();\n" - "}\n"); + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" + " $set_has_field_bit_builder$\n" + " $on_changed$\n" + " return get$capitalized_name$FieldBuilder().getBuilder();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if ($name$Builder_ != null) {\n" - " return $name$Builder_.getMessageOrBuilder();\n" - " } else {\n" - " return $name$_ == null ?\n" - " $type$.getDefaultInstance() : $name$_;\n" - " }\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if ($name$Builder_ != null) {\n" + " return $name$Builder_.getMessageOrBuilder();\n" + " } else {\n" + " return $name$_ == null ?\n" + " $type$.getDefaultInstance() : $name$_;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " get$capitalized_name$FieldBuilder() {\n" - " if ($name$Builder_ == null) {\n" - " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " get$capitalized_name$(),\n" - " getParentForChildren(),\n" - " isClean());\n" - " $name$_ = null;\n" - " }\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " get$capitalized_name$FieldBuilder() {\n" + " if ($name$Builder_ == null) {\n" + " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " get$capitalized_name$(),\n" + " getParentForChildren(),\n" + " isClean());\n" + " $name$_ = null;\n" + " }\n" + " return $name$Builder_;\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); } } +void ImmutableMessageFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const {} -void ImmutableMessageFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const {} - -void ImmutableMessageFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { - PrintNestedBuilderCondition(printer, - "$name$_ = null;\n", + PrintNestedBuilderCondition(printer, "$name$_ = null;\n", - "$name$Builder_.clear();\n"); + "$name$Builder_.clear();\n"); printer->Print(variables_, "$clear_has_field_bit_builder$\n"); } else { - PrintNestedBuilderCondition(printer, - "$name$_ = null;\n", + PrintNestedBuilderCondition(printer, "$name$_ = null;\n", - "$name$_ = null;\n" - "$name$Builder_ = null;\n"); + "$name$_ = null;\n" + "$name$Builder_ = null;\n"); } } -void ImmutableMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " merge$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " merge$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, "if ($get_has_field_bit_from_local$) {\n"); printer->Indent(); @@ -454,65 +454,68 @@ GenerateBuildingCode(io::Printer* printer) const { } } -void ImmutableMessageFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($is_field_present_message$) {\n" - " subBuilder = $name$_.toBuilder();\n" - "}\n"); + "$type$.Builder subBuilder = null;\n" + "if ($is_field_present_message$) {\n" + " subBuilder = $name$_.toBuilder();\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { printer->Print(variables_, - "$name$_ = input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry);\n"); + "$name$_ = input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry);\n"); } else { printer->Print(variables_, - "$name$_ = input.readMessage($type$.$get_parser$, extensionRegistry);\n"); + "$name$_ = input.readMessage($type$.$get_parser$, " + "extensionRegistry);\n"); } printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom($name$_);\n" - " $name$_ = subBuilder.buildPartial();\n" - "}\n" - "$set_has_field_bit_message$\n"); + "if (subBuilder != null) {\n" + " subBuilder.mergeFrom($name$_);\n" + " $name$_ = subBuilder.buildPartial();\n" + "}\n" + "$set_has_field_bit_message$\n"); } -void ImmutableMessageFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for messages. } -void ImmutableMessageFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$group_or_message$($number$, get$capitalized_name$());\n" - "}\n"); +void ImmutableMessageFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($is_field_present_message$) {\n" + " output.write$group_or_message$($number$, get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" - "}\n"); +void ImmutableMessageFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" + "}\n"); } -void ImmutableMessageFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$())) return false;\n"); + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } -void ImmutableMessageFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableMessageFieldGenerator::GenerateHashCode( + io::Printer* printer) const { printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + "hash = (37 * hash) + $constant_name$;\n" + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); } std::string ImmutableMessageFieldGenerator::GetBoxedType() const { @@ -521,63 +524,60 @@ std::string ImmutableMessageFieldGenerator::GetBoxedType() const { // =================================================================== -ImmutableMessageOneofFieldGenerator:: -ImmutableMessageOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableMessageFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableMessageOneofFieldGenerator::ImmutableMessageOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableMessageFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableMessageOneofFieldGenerator:: -~ImmutableMessageOneofFieldGenerator() {} +ImmutableMessageOneofFieldGenerator::~ImmutableMessageOneofFieldGenerator() {} -void ImmutableMessageOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" - "\n"); + // If this builder is non-null, it is used and the other fields + // are ignored. + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;" + "\n"); // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". @@ -585,226 +585,232 @@ GenerateBuilderMembers(io::Printer* printer) const { // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public $type$ ${$get$capitalized_name$$}$()", - "if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - "}\n" - "return $type$.getDefaultInstance();\n", + "if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + "}\n" + "return $type$.getDefaultInstance();\n", - "if ($has_oneof_case_message$) {\n" - " return $name$Builder_.getMessage();\n" - "}\n" - "return $type$.getDefaultInstance();\n", + "if ($has_oneof_case_message$) {\n" + " return $name$Builder_.getMessage();\n" + "}\n" + "return $type$.getDefaultInstance();\n", - NULL); + NULL); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "$oneof_name$_ = value;\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "$oneof_name$_ = value;\n" + "$on_changed$\n", - "$name$Builder_.setMessage(value);\n", + "$name$Builder_.setMessage(value);\n", - "$set_oneof_case_message$;\n" - "return this;\n"); + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "$oneof_name$_ = builderForValue.build();\n" - "$on_changed$\n", + "$oneof_name$_ = builderForValue.build();\n" + "$on_changed$\n", - "$name$Builder_.setMessage(builderForValue.build());\n", + "$name$Builder_.setMessage(builderForValue.build());\n", - "$set_oneof_case_message$;\n" - "return this;\n"); + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", - - "if ($has_oneof_case_message$ &&\n" - " $oneof_name$_ != $type$.getDefaultInstance()) {\n" - " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" - " .mergeFrom(value).buildPartial();\n" - "} else {\n" - " $oneof_name$_ = value;\n" - "}\n" - "$on_changed$\n", - - "if ($has_oneof_case_message$) {\n" - " $name$Builder_.mergeFrom(value);\n" - "}\n" - "$name$Builder_.setMessage(value);\n", - - "$set_oneof_case_message$;\n" - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", + + "if ($has_oneof_case_message$ &&\n" + " $oneof_name$_ != $type$.getDefaultInstance()) {\n" + " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" + " .mergeFrom(value).buildPartial();\n" + "} else {\n" + " $oneof_name$_ = value;\n" + "}\n" + "$on_changed$\n", + + "if ($has_oneof_case_message$) {\n" + " $name$Builder_.mergeFrom(value);\n" + "}\n" + "$name$Builder_.setMessage(value);\n", + + "$set_oneof_case_message$;\n" + "return this;\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - "}\n", + "if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + "}\n", - "if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - "}\n" - "$name$Builder_.clear();\n", + "if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + "}\n" + "$name$Builder_.clear();\n", - "return this;\n"); + "return this;\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$get$capitalized_name$Builder$}$() {\n" - " return get$capitalized_name$FieldBuilder().getBuilder();\n" - "}\n"); + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" + " return get$capitalized_name$FieldBuilder().getBuilder();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$() {\n" - " if (($has_oneof_case_message$) && ($name$Builder_ != null)) {\n" - " return $name$Builder_.getMessageOrBuilder();\n" - " } else {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" + " if (($has_oneof_case_message$) && ($name$Builder_ != null)) {\n" + " return $name$Builder_.getMessageOrBuilder();\n" + " } else {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " ${$get$capitalized_name$FieldBuilder$}$() {\n" - " if ($name$Builder_ == null) {\n" - " if (!($has_oneof_case_message$)) {\n" - " $oneof_name$_ = $type$.getDefaultInstance();\n" - " }\n" - " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " ($type$) $oneof_name$_,\n" - " getParentForChildren(),\n" - " isClean());\n" - " $oneof_name$_ = null;\n" - " }\n" - " $set_oneof_case_message$;\n" - " $on_changed$;\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "private com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " ${$get$capitalized_name$FieldBuilder$}$() {\n" + " if ($name$Builder_ == null) {\n" + " if (!($has_oneof_case_message$)) {\n" + " $oneof_name$_ = $type$.getDefaultInstance();\n" + " }\n" + " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " ($type$) $oneof_name$_,\n" + " getParentForChildren(),\n" + " isClean());\n" + " $oneof_name$_ = null;\n" + " }\n" + " $set_oneof_case_message$;\n" + " $on_changed$;\n" + " return $name$Builder_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n"); +void ImmutableMessageOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { + printer->Print(variables_, "if ($has_oneof_case_message$) {\n"); printer->Indent(); - PrintNestedBuilderCondition(printer, - "result.$oneof_name$_ = $oneof_name$_;\n", + PrintNestedBuilderCondition( + printer, "result.$oneof_name$_ = $oneof_name$_;\n", - "result.$oneof_name$_ = $name$Builder_.build();\n"); + "result.$oneof_name$_ = $name$Builder_.build();\n"); printer->Outdent(); printer->Print("}\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "merge$capitalized_name$(other.get$capitalized_name$());\n"); + "merge$capitalized_name$(other.get$capitalized_name$());\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableMessageOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($has_oneof_case_message$) {\n" - " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" - "}\n"); + "$type$.Builder subBuilder = null;\n" + "if ($has_oneof_case_message$) {\n" + " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$oneof_name$_ = input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry);\n"); + printer->Print( + variables_, + "$oneof_name$_ = input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry);\n"); } else { - printer->Print(variables_, - "$oneof_name$_ =\n" - " input.readMessage($type$.$get_parser$, extensionRegistry);\n"); + printer->Print( + variables_, + "$oneof_name$_ =\n" + " input.readMessage($type$.$get_parser$, extensionRegistry);\n"); } printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" - " $oneof_name$_ = subBuilder.buildPartial();\n" - "}\n"); - printer->Print(variables_, - "$set_oneof_case_message$;\n"); + "if (subBuilder != null) {\n" + " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" + " $oneof_name$_ = subBuilder.buildPartial();\n" + "}\n"); + printer->Print(variables_, "$set_oneof_case_message$;\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" - "}\n"); +void ImmutableMessageOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" + "}\n"); } -void ImmutableMessageOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" - "}\n"); +void ImmutableMessageOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" + "}\n"); } // =================================================================== -RepeatedImmutableMessageFieldGenerator:: -RepeatedImmutableMessageFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableMessageFieldGenerator::RepeatedImmutableMessageFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetMessageVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableMessageFieldGenerator:: -~RepeatedImmutableMessageFieldGenerator() {} + ~RepeatedImmutableMessageFieldGenerator() {} int RepeatedImmutableMessageFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -814,78 +820,79 @@ int RepeatedImmutableMessageFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableMessageFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having methods specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$type$> \n" - " get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$type$> \n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<? extends $type$OrBuilder> \n" - " get$capitalized_name$OrBuilderList();\n"); + "$deprecation$java.util.List<? extends $type$OrBuilder> \n" + " get$capitalized_name$OrBuilderList();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder(\n" - " int index);\n"); + printer->Print( + variables_, + "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder(\n" + " int index);\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.util.List<$type$> $name$_;\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private java.util.List<$type$> $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - } void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderCondition( - io::Printer* printer, - const char* regular_case, + io::Printer* printer, const char* regular_case, const char* nested_builder_case) const { printer->Print(variables_, "if ($name$Builder_ == null) {\n"); printer->Indent(); @@ -899,10 +906,8 @@ void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderCondition( } void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderFunction( - io::Printer* printer, - const char* method_prototype, - const char* regular_case, - const char* nested_builder_case, + io::Printer* printer, const char* method_prototype, + const char* regular_case, const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); printer->Annotate("{", "}", descriptor_); @@ -916,406 +921,429 @@ void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderFunction( printer->Print("}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // When using nested-builders, the code initially works just like the // non-nested builder case. It only creates a nested builder lazily on // demand and then forever delegates to it after creation. - printer->Print(variables_, - // Used when the builder is null. - // One field is the list and the other field keeps track of whether the - // list is immutable. If it's immutable, the invariant is that it must - // either an instance of Collections.emptyList() or it's an ArrayList - // wrapped in a Collections.unmodifiableList() wrapper and nobody else has - // a refererence to the underlying ArrayList. This invariant allows us to - // share instances of lists between protocol buffers avoiding expensive - // memory allocations. Note, immutable is a strong guarantee here -- not - // just that the list cannot be modified via the reference but that the - // list can never be modified. - "private java.util.List<$type$> $name$_ =\n" - " java.util.Collections.emptyList();\n" - - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new java.util.ArrayList<$type$>($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n" - "\n"); + printer->Print( + variables_, + // Used when the builder is null. + // One field is the list and the other field keeps track of whether the + // list is immutable. If it's immutable, the invariant is that it must + // either an instance of Collections.emptyList() or it's an ArrayList + // wrapped in a Collections.unmodifiableList() wrapper and nobody else has + // a refererence to the underlying ArrayList. This invariant allows us to + // share instances of lists between protocol buffers avoiding expensive + // memory allocations. Note, immutable is a strong guarantee here -- not + // just that the list cannot be modified via the reference but that the + // list can never be modified. + "private java.util.List<$type$> $name$_ =\n" + " java.util.Collections.emptyList();\n" + + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new java.util.ArrayList<$type$>($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n" + "\n"); - printer->Print(variables_, - // If this builder is non-null, it is used and the other fields are - // ignored. - "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n" - "\n"); + printer->Print( + variables_, + // If this builder is non-null, it is used and the other fields are + // ignored. + "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n" + "\n"); // The comments above the methods below are based on a hypothetical // repeated field of type "Field" called "RepeatedField". // List<Field> getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$()", + PrintNestedBuilderFunction( + printer, + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$()", - "return java.util.Collections.unmodifiableList($name$_);\n", - "return $name$Builder_.getMessageList();\n", + "return java.util.Collections.unmodifiableList($name$_);\n", + "return $name$Builder_.getMessageList();\n", - NULL); + NULL); // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public int ${$get$capitalized_name$Count$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public int ${$get$capitalized_name$Count$}$()", - "return $name$_.size();\n", - "return $name$Builder_.getCount();\n", + "return $name$_.size();\n", "return $name$Builder_.getCount();\n", - NULL); + NULL); // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index)", - "return $name$_.get(index);\n", + "return $name$_.get(index);\n", - "return $name$Builder_.getMessage(index);\n", + "return $name$Builder_.getMessage(index);\n", - NULL); + NULL); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.set(index, value);\n" - "$on_changed$\n", - "$name$Builder_.setMessage(index, value);\n", - "return this;\n"); + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value)", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.set(index, value);\n" + "$on_changed$\n", + "$name$Builder_.setMessage(index, value);\n", "return this;\n"); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.set(index, builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.set(index, builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.setMessage(index, builderForValue.build());\n", + "$name$Builder_.setMessage(index, builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(value);\n" + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(value);\n" - "$on_changed$\n", + "$on_changed$\n", - "$name$Builder_.addMessage(value);\n", + "$name$Builder_.addMessage(value);\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$ value)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$ value)", - "if (value == null) {\n" - " throw new NullPointerException();\n" - "}\n" - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(index, value);\n" - "$on_changed$\n", + "if (value == null) {\n" + " throw new NullPointerException();\n" + "}\n" + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(index, value);\n" + "$on_changed$\n", - "$name$Builder_.addMessage(index, value);\n", + "$name$Builder_.addMessage(index, value);\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.addMessage(builderForValue.build());\n", + "$name$Builder_.addMessage(builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.add(index, builderForValue.build());\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.add(index, builderForValue.build());\n" + "$on_changed$\n", - "$name$Builder_.addMessage(index, builderForValue.build());\n", + "$name$Builder_.addMessage(index, builderForValue.build());\n", - "return this;\n"); + "return this;\n"); // Builder addAllRepeatedField(Iterable<Field> values) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $type$> values)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $type$> values)", - "ensure$capitalized_name$IsMutable();\n" - "com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + "$on_changed$\n", - "$name$Builder_.addAllMessages(values);\n", + "$name$Builder_.addAllMessages(values);\n", - "return this;\n"); + "return this;\n"); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$clear$capitalized_name$$}$()", + PrintNestedBuilderFunction( + printer, "$deprecation$public Builder ${$clear$capitalized_name$$}$()", - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n" - "$on_changed$\n", + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n" + "$on_changed$\n", - "$name$Builder_.clear();\n", + "$name$Builder_.clear();\n", - "return this;\n"); + "return this;\n"); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - PrintNestedBuilderFunction(printer, - "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index)", + PrintNestedBuilderFunction( + printer, + "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index)", - "ensure$capitalized_name$IsMutable();\n" - "$name$_.remove(index);\n" - "$on_changed$\n", + "ensure$capitalized_name$IsMutable();\n" + "$name$_.remove(index);\n" + "$on_changed$\n", - "$name$Builder_.remove(index);\n", + "$name$Builder_.remove(index);\n", - "return this;\n"); + "return this;\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder ${$get$capitalized_name$Builder$}$(\n" - " int index) {\n" - " return get$capitalized_name$FieldBuilder().getBuilder(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$.Builder ${$get$capitalized_name$Builder$}$(\n" + " int index) {\n" + " return get$capitalized_name$FieldBuilder().getBuilder(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " if ($name$Builder_ == null) {\n" - " return $name$_.get(index);" - " } else {\n" - " return $name$Builder_.getMessageOrBuilder(index);\n" - " }\n" - "}\n"); + printer->Print(variables_, + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " if ($name$Builder_ == null) {\n" + " return $name$_.get(index);" + " } else {\n" + " return $name$Builder_.getMessageOrBuilder(index);\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " if ($name$Builder_ != null) {\n" - " return $name$Builder_.getMessageOrBuilderList();\n" - " } else {\n" - " return java.util.Collections.unmodifiableList($name$_);\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " if ($name$Builder_ != null) {\n" + " return $name$Builder_.getMessageOrBuilderList();\n" + " } else {\n" + " return java.util.Collections.unmodifiableList($name$_);\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder " - "${$add$capitalized_name$Builder$}$() {\n" - " return get$capitalized_name$FieldBuilder().addBuilder(\n" - " $type$.getDefaultInstance());\n" - "}\n"); + printer->Print(variables_, + "$deprecation$public $type$.Builder " + "${$add$capitalized_name$Builder$}$() {\n" + " return get$capitalized_name$FieldBuilder().addBuilder(\n" + " $type$.getDefaultInstance());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$.Builder ${$add$capitalized_name$Builder$}$(\n" - " int index) {\n" - " return get$capitalized_name$FieldBuilder().addBuilder(\n" - " index, $type$.getDefaultInstance());\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$.Builder ${$add$capitalized_name$Builder$}$(\n" + " int index) {\n" + " return get$capitalized_name$FieldBuilder().addBuilder(\n" + " index, $type$.getDefaultInstance());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$type$.Builder> \n" - " ${$get$capitalized_name$BuilderList$}$() {\n" - " return get$capitalized_name$FieldBuilder().getBuilderList();\n" - "}\n" - "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder> \n" - " get$capitalized_name$FieldBuilder() {\n" - " if ($name$Builder_ == null) {\n" - " $name$Builder_ = new com.google.protobuf.RepeatedFieldBuilder$ver$<\n" - " $type$, $type$.Builder, $type$OrBuilder>(\n" - " $name$_,\n" - " $get_mutable_bit_builder$,\n" - " getParentForChildren(),\n" - " isClean());\n" - " $name$_ = null;\n" - " }\n" - " return $name$Builder_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<$type$.Builder> \n" + " ${$get$capitalized_name$BuilderList$}$() {\n" + " return get$capitalized_name$FieldBuilder().getBuilderList();\n" + "}\n" + "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder> \n" + " get$capitalized_name$FieldBuilder() {\n" + " if ($name$Builder_ == null) {\n" + " $name$Builder_ = new " + "com.google.protobuf.RepeatedFieldBuilder$ver$<\n" + " $type$, $type$.Builder, $type$OrBuilder>(\n" + " $name$_,\n" + " $get_mutable_bit_builder$,\n" + " getParentForChildren(),\n" + " isClean());\n" + " $name$_ = null;\n" + " }\n" + " return $name$Builder_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableMessageFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { PrintNestedBuilderCondition(printer, - "$name$_ = java.util.Collections.emptyList();\n" - "$clear_mutable_bit_builder$;\n", + "$name$_ = java.util.Collections.emptyList();\n" + "$clear_mutable_bit_builder$;\n", - "$name$Builder_.clear();\n"); + "$name$Builder_.clear();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations (non-nested builder case): // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. - PrintNestedBuilderCondition(printer, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n", - - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$Builder_.isEmpty()) {\n" - " $name$Builder_.dispose();\n" - " $name$Builder_ = null;\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " $name$Builder_ = \n" - " com.google.protobuf.GeneratedMessage$ver$.alwaysUseFieldBuilders ?\n" - " get$capitalized_name$FieldBuilder() : null;\n" - " } else {\n" - " $name$Builder_.addAllMessages(other.$name$_);\n" - " }\n" - "}\n"); + PrintNestedBuilderCondition( + printer, + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n", + + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$Builder_.isEmpty()) {\n" + " $name$Builder_.dispose();\n" + " $name$Builder_ = null;\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " $name$Builder_ = \n" + " com.google.protobuf.GeneratedMessage$ver$.alwaysUseFieldBuilders " + "?\n" + " get$capitalized_name$FieldBuilder() : null;\n" + " } else {\n" + " $name$Builder_.addAllMessages(other.$name$_);\n" + " }\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below (non-nested builder case) ensures that the result has an // immutable list. If our list is immutable, we can just reuse it. If not, // we make it immutable. - PrintNestedBuilderCondition(printer, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n", - - "result.$name$_ = $name$Builder_.build();\n"); + PrintNestedBuilderCondition( + printer, + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n", + + "result.$name$_ = $name$Builder_.build();\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new java.util.ArrayList<$type$>();\n" - " $set_mutable_bit_parser$;\n" - "}\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new java.util.ArrayList<$type$>();\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_.add(input.readGroup($number$, $type$.$get_parser$,\n" - " extensionRegistry));\n"); + printer->Print( + variables_, + "$name$_.add(input.readGroup($number$, $type$.$get_parser$,\n" + " extensionRegistry));\n"); } else { - printer->Print(variables_, - "$name$_.add(\n" - " input.readMessage($type$.$get_parser$, extensionRegistry));\n"); + printer->Print( + variables_, + "$name$_.add(\n" + " input.readMessage($type$.$get_parser$, extensionRegistry));\n"); } } -void RepeatedImmutableMessageFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$group_or_message$($number$, $name$_.get(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$group_or_message$($number$, $name$_.get(i));\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, $name$_.get(i));\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$group_or_message$Size($number$, $name$_.get(i));\n" + "}\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "if (!get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List())) return false;\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutableMessageFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableMessageFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } std::string RepeatedImmutableMessageFieldGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.cc b/src/google/protobuf/compiler/java/java_message_field_lite.cc index c992ed399c..c4059818cf 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_field_lite.cc @@ -63,12 +63,12 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = - (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? - "Group" : "Message"; + (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? "Group" + : "Message"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -120,258 +120,185 @@ int ImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const { return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutableMessageFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$boolean has$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutableMessageFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { - printer->Print(variables_, - "private $type$ $name$_;\n"); + printer->Print(variables_, "private $type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } else { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $name$_ != null;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $name$_ != null;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $name$_ = value;\n" - " $set_has_field_bit_message$\n" - " }\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $name$_ = value;\n" + " $set_has_field_bit_message$\n" + " }\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " $name$_ = builderForValue.build();\n" - " $set_has_field_bit_message$\n" - "}\n"); + "private void set$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " $name$_ = builderForValue.build();\n" + " $set_has_field_bit_message$\n" + "}\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n" - "private void merge$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " if ($name$_ != null &&\n" - " $name$_ != $type$.getDefaultInstance()) {\n" - " $name$_ =\n" - " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" - " } else {\n" - " $name$_ = value;\n" - " }\n" - " $set_has_field_bit_message$\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n" + "private void merge$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " if ($name$_ != null &&\n" + " $name$_ != $type$.getDefaultInstance()) {\n" + " $name$_ =\n" + " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n" + " } else {\n" + " $name$_ = value;\n" + " }\n" + " $set_has_field_bit_message$\n" + "}\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {" - " $name$_ = null;\n" - " $clear_has_field_bit_message$\n" - "}\n"); + "private void clear$capitalized_name$() {" + " $name$_ = null;\n" + " $clear_has_field_bit_message$\n" + "}\n"); } -void ImmutableMessageFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - " }\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + " }\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$merge$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.merge$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.merge$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + +void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); + WriteIntToUtf16CharSequence(messageBitIndex_, output); } + printer->Print(variables_, "\"$name$_\",\n"); } - -void ImmutableMessageFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const {} - -void ImmutableMessageFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_ = visitor.visitMessage($name$_, other.$name$_);\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // TODO(dweis): Update this code to avoid the builder allocation and instead - // only allocate a submessage that isn't made immutable. Rely on the top - // message calling makeImmutable once done to actually traverse the tree and - // finalize state. This will avoid: - // - transitive builder allocations - // - the extra transitive iteration for streamed fields - // - reallocations for copying repeated fields - printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($is_field_present_message$) {\n" - " subBuilder = $name$_.toBuilder();\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_ = input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry);\n"); - } else { - printer->Print(variables_, - "$name$_ = input.readMessage($type$.parser(), extensionRegistry);\n"); - } - - printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom($name$_);\n" - " $name$_ = subBuilder.buildPartial();\n" - "}\n" - "$set_has_field_bit_message$\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for messages. -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$group_or_message$($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); -} - -void ImmutableMessageFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n" - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); -} +void ImmutableMessageFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const {} std::string ImmutableMessageFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); @@ -390,197 +317,155 @@ ImmutableMessageOneofFieldLiteGenerator:: } ImmutableMessageOneofFieldLiteGenerator:: -~ImmutableMessageOneofFieldLiteGenerator() {} + ~ImmutableMessageOneofFieldLiteGenerator() {} -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($type$) $oneof_name$_;\n" - " }\n" - " return $type$.getDefaultInstance();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($type$) $oneof_name$_;\n" + " }\n" + " return $type$.getDefaultInstance();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " $oneof_name$_ = value;\n" - " $set_oneof_case_message$;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " $oneof_name$_ = value;\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " $oneof_name$_ = builderForValue.build();\n" - " $set_oneof_case_message$;\n" - "}\n"); + "private void set$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " $oneof_name$_ = builderForValue.build();\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "private void merge$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " if ($has_oneof_case_message$ &&\n" - " $oneof_name$_ != $type$.getDefaultInstance()) {\n" - " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" - " .mergeFrom(value).buildPartial();\n" - " } else {\n" - " $oneof_name$_ = value;\n" - " }\n" - " $set_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "private void merge$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " if ($has_oneof_case_message$ &&\n" + " $oneof_name$_ != $type$.getDefaultInstance()) {\n" + " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n" + " .mergeFrom(value).buildPartial();\n" + " } else {\n" + " $oneof_name$_ = value;\n" + " }\n" + " $set_oneof_case_message$;\n" + "}\n"); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); } +void ImmutableMessageOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); + printer->Print(variables_, "$oneof_stored_type$.class,\n"); +} -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableMessageOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder " - "${$merge$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.merge$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.merge$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofMessage(\n" - " $has_oneof_case_message$,\n" - " $oneof_name$_,\n" - " other.$oneof_name$_);\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$type$.Builder subBuilder = null;\n" - "if ($has_oneof_case_message$) {\n" - " subBuilder = (($type$) $oneof_name$_).toBuilder();\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$oneof_name$_ = input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry);\n"); - } else { - printer->Print(variables_, - "$oneof_name$_ =\n" - " input.readMessage($type$.parser(), extensionRegistry);\n"); - } - - printer->Print(variables_, - "if (subBuilder != null) {\n" - " subBuilder.mergeFrom(($type$) $oneof_name$_);\n" - " $oneof_name$_ = subBuilder.buildPartial();\n" - "}\n"); - printer->Print(variables_, - "$set_oneof_case_message$;\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$group_or_message$($number$, ($type$) $oneof_name$_);\n" - "}\n"); -} - -void ImmutableMessageOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, ($type$) $oneof_name$_);\n" - "}\n"); -} - // =================================================================== RepeatedImmutableMessageFieldLiteGenerator:: @@ -597,381 +482,323 @@ RepeatedImmutableMessageFieldLiteGenerator:: } RepeatedImmutableMessageFieldLiteGenerator:: -~RepeatedImmutableMessageFieldLiteGenerator() {} + ~RepeatedImmutableMessageFieldLiteGenerator() {} int RepeatedImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { // TODO(jonp): In the future, consider having methods specific to the // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$type$> \n" - " get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$type$> \n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n"); +void RepeatedImmutableMessageFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " ${$get$capitalized_name$OrBuilderList$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder " - "${$get$capitalized_name$OrBuilder$}$(\n" - " int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" + " int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n" - "\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n" + "\n"); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + "}\n"); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, builderForValue.build());\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, builderForValue.build());\n" + "}\n"); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - "}\n"); + "private void add$capitalized_name$($type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + "}\n"); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " int index, $type$ value) {\n" - " if (value == null) {\n" - " throw new NullPointerException();\n" - " }\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(index, value);\n" - "}\n"); + "private void add$capitalized_name$(\n" + " int index, $type$ value) {\n" + " if (value == null) {\n" + " throw new NullPointerException();\n" + " }\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(index, value);\n" + "}\n"); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(builderForValue.build());\n" - "}\n"); + "private void add$capitalized_name$(\n" + " $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(builderForValue.build());\n" + "}\n"); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " int index, $type$.Builder builderForValue) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(index, builderForValue.build());\n" - "}\n"); + "private void add$capitalized_name$(\n" + " int index, $type$.Builder builderForValue) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(index, builderForValue.build());\n" + "}\n"); // Builder addAllRepeatedField(Iterable<Field> values) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable<? extends $type$> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable<? extends $type$> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = emptyProtobufList();\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $name$_ = emptyProtobufList();\n" + "}\n"); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void remove$capitalized_name$(int index) {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.remove(index);\n" - "}\n"); + "private void remove$capitalized_name$(int index) {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.remove(index);\n" + "}\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // repeated field of type "Field" called "RepeatedField". // List<Field> getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}"); printer->Annotate("{", "}", descriptor_); // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " int index, $type$.Builder builderForValue) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(index, builderForValue);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " int index, $type$.Builder builderForValue) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(index, builderForValue);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder addAllRepeatedField(Iterable<Field> values) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $type$> values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $type$> values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index) {\n" - " copyOnWrite();\n" - " instance.remove$capitalized_name$(index);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$remove$capitalized_name$$}$(int index) {\n" + " copyOnWrite();\n" + " instance.remove$capitalized_name$(index);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - printer->Print(variables_, - "get$capitalized_name$FieldBuilder();\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = emptyProtobufList();\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { +void RepeatedImmutableMessageFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); printer->Print(variables_, - "$name$_= visitor.visitList($name$_, other.$name$_);\n"); + "\"$name$_\",\n" + "$type$.class,\n"); } -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - - if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "$name$_.add(input.readGroup($number$, $type$.parser(),\n" - " extensionRegistry));\n"); - } else { - printer->Print(variables_, - "$name$_.add(\n" - " input.readMessage($type$.parser(), extensionRegistry));\n"); - } -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$group_or_message$($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$group_or_message$Size($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); -} - -void RepeatedImmutableMessageFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableMessageFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = emptyProtobufList();\n"); } std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.h b/src/google/protobuf/compiler/java/java_message_field_lite.h index 7e78ca4f3f..c409728ee4 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.h +++ b/src/google/protobuf/compiler/java/java_message_field_lite.h @@ -69,15 +69,8 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; @@ -102,11 +95,8 @@ class ImmutableMessageOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageOneofFieldLiteGenerator); @@ -125,16 +115,8 @@ class RepeatedImmutableMessageFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; diff --git a/src/google/protobuf/compiler/java/java_message_lite.cc b/src/google/protobuf/compiler/java/java_message_lite.cc index c6d39915a0..c798febdec 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_lite.cc @@ -66,14 +66,6 @@ using internal::WireFormat; using internal::WireFormatLite; namespace { -bool EnableExperimentalRuntimeForLite() { -#ifdef PROTOBUF_EXPERIMENT - return PROTOBUF_EXPERIMENT; -#else // PROTOBUF_EXPERIMENT - return false; -#endif // !PROTOBUF_EXPERIMENT -} - std::string MapValueImmutableClassdName(const Descriptor* descriptor, ClassNameResolver* name_resolver) { const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); @@ -85,9 +77,10 @@ std::string MapValueImmutableClassdName(const Descriptor* descriptor, // =================================================================== ImmutableMessageLiteGenerator::ImmutableMessageLiteGenerator( const Descriptor* descriptor, Context* context) - : MessageGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + : MessageGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()), + field_generators_(descriptor, context_) { GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; @@ -130,41 +123,39 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { " com.google.protobuf.GeneratedMessageLite.\n" " ExtendableMessageOrBuilder<\n" " $classname$, $classname$.Builder> {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } else { printer->Print( "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageLiteOrBuilder {\n", - "deprecation", descriptor_->options().deprecated() ? - "@java.lang.Deprecated " : "", + "deprecation", + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), - "classname", descriptor_->name(), - "{", "", "}", ""); + "classname", descriptor_->name(), "{", "", "}", ""); } printer->Annotate("{", "}", descriptor_); printer->Indent(); - for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\n"); - field_generators_.get(descriptor_->field(i)) - .GenerateInterfaceMembers(printer); - } - for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print( - "\n" - "public $classname$.$oneof_capitalized_name$Case " - "get$oneof_capitalized_name$Case();\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name, - "classname", - context_->GetNameResolver()->GetImmutableClassName(descriptor_)); - } + for (int i = 0; i < descriptor_->field_count(); i++) { + printer->Print("\n"); + field_generators_.get(descriptor_->field(i)) + .GenerateInterfaceMembers(printer); + } + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + printer->Print( + "\n" + "public $classname$.$oneof_capitalized_name$Case " + "get$oneof_capitalized_name$Case();\n", + "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(descriptor_->oneof_decl(i)) + ->capitalized_name, + "classname", + context_->GetNameResolver()->GetImmutableClassName(descriptor_)); + } printer->Outdent(); printer->Print("}\n"); @@ -179,8 +170,8 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { variables["static"] = is_own_file ? " " : " static "; variables["classname"] = descriptor_->name(); variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_); - variables["deprecation"] = descriptor_->options().deprecated() - ? "@java.lang.Deprecated " : ""; + variables["deprecation"] = + descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; WriteMessageDocComment(printer, descriptor_); MaybePrintGeneratedAnnotation(context_, printer, descriptor_, @@ -190,17 +181,19 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { // The builder_type stores the super type name of the nested Builder class. std::string builder_type; if (descriptor_->extension_range_count() > 0) { - printer->Print(variables, - "$deprecation$public $static$final class $classname$ extends\n" - " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n" - " $classname$, $classname$.Builder> implements\n" - " $extra_interfaces$\n" - " $classname$OrBuilder {\n"); + printer->Print( + variables, + "$deprecation$public $static$final class $classname$ extends\n" + " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n" + " $classname$, $classname$.Builder> implements\n" + " $extra_interfaces$\n" + " $classname$OrBuilder {\n"); builder_type = strings::Substitute( "com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>", name_resolver_->GetImmutableClassName(descriptor_)); } else { - printer->Print(variables, + printer->Print( + variables, "$deprecation$public $static$final class $classname$ extends\n" " com.google.protobuf.GeneratedMessageLite<\n" " $classname$, $classname$.Builder> implements\n" @@ -222,8 +215,8 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->nested_type_count(); i++) { // Don't generate Java classes for map entry messages. if (IsMapEntry(descriptor_->nested_type(i))) continue; - ImmutableMessageLiteGenerator messageGenerator( - descriptor_->nested_type(i), context_); + ImmutableMessageLiteGenerator messageGenerator(descriptor_->nested_type(i), + context_); messageGenerator.GenerateInterface(printer); messageGenerator.Generate(printer); } @@ -245,17 +238,17 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { const OneofDescriptor* oneof = descriptor_->oneof_decl(i); vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name; - vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( - oneof)->capitalized_name; + vars["oneof_capitalized_name"] = + context_->GetOneofGeneratorInfo(oneof)->capitalized_name; vars["oneof_index"] = StrCat(oneof->index()); // oneofCase_ and oneof_ printer->Print(vars, - "private int $oneof_name$Case_ = 0;\n" - "private java.lang.Object $oneof_name$_;\n"); + "private int $oneof_name$Case_ = 0;\n" + "private java.lang.Object $oneof_name$_;\n"); // OneofCase enum printer->Print(vars, - "public enum $oneof_capitalized_name$Case\n" - " implements com.google.protobuf.Internal.EnumLite {\n"); + "public enum $oneof_capitalized_name$Case\n" + " implements com.google.protobuf.Internal.EnumLite {\n"); printer->Indent(); for (int j = 0; j < oneof->field_count(); j++) { const FieldDescriptor* field = oneof->field(j); @@ -263,26 +256,25 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { ToUpper(field->name()), "field_number", StrCat(field->number())); } - printer->Print( - "$cap_oneof_name$_NOT_SET(0);\n", - "cap_oneof_name", - ToUpper(vars["oneof_name"])); + printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name", + ToUpper(vars["oneof_name"])); printer->Print(vars, - "private final int value;\n" - "private $oneof_capitalized_name$Case(int value) {\n" - " this.value = value;\n" - "}\n"); - printer->Print(vars, - "/**\n" - " * @deprecated Use {@link #forNumber(int)} instead.\n" - " */\n" - "@java.lang.Deprecated\n" - "public static $oneof_capitalized_name$Case valueOf(int value) {\n" - " return forNumber(value);\n" - "}\n" - "\n" - "public static $oneof_capitalized_name$Case forNumber(int value) {\n" - " switch (value) {\n"); + "private final int value;\n" + "private $oneof_capitalized_name$Case(int value) {\n" + " this.value = value;\n" + "}\n"); + printer->Print( + vars, + "/**\n" + " * @deprecated Use {@link #forNumber(int)} instead.\n" + " */\n" + "@java.lang.Deprecated\n" + "public static $oneof_capitalized_name$Case valueOf(int value) {\n" + " return forNumber(value);\n" + "}\n" + "\n" + "public static $oneof_capitalized_name$Case forNumber(int value) {\n" + " switch (value) {\n"); for (int j = 0; j < oneof->field_count(); j++) { const FieldDescriptor* field = oneof->field(j); printer->Print(" case $field_number$: return $field_name$;\n", @@ -290,31 +282,31 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { "field_name", ToUpper(field->name())); } printer->Print( - " case 0: return $cap_oneof_name$_NOT_SET;\n" - " default: return null;\n" - " }\n" - "}\n" - "@java.lang.Override\n" - "public int getNumber() {\n" - " return this.value;\n" - "}\n", - "cap_oneof_name", ToUpper(vars["oneof_name"])); + " case 0: return $cap_oneof_name$_NOT_SET;\n" + " default: return null;\n" + " }\n" + "}\n" + "@java.lang.Override\n" + "public int getNumber() {\n" + " return this.value;\n" + "}\n", + "cap_oneof_name", ToUpper(vars["oneof_name"])); printer->Outdent(); printer->Print("};\n\n"); // oneofCase() printer->Print(vars, - "@java.lang.Override\n" - "public $oneof_capitalized_name$Case\n" - "get$oneof_capitalized_name$Case() {\n" - " return $oneof_capitalized_name$Case.forNumber(\n" - " $oneof_name$Case_);\n" - "}\n" - "\n" - "private void clear$oneof_capitalized_name$() {\n" - " $oneof_name$Case_ = 0;\n" - " $oneof_name$_ = null;\n" - "}\n" - "\n"); + "@java.lang.Override\n" + "public $oneof_capitalized_name$Case\n" + "get$oneof_capitalized_name$Case() {\n" + " return $oneof_capitalized_name$Case.forNumber(\n" + " $oneof_name$Case_);\n" + "}\n" + "\n" + "private void clear$oneof_capitalized_name$() {\n" + " $oneof_name$Case_ = 0;\n" + " $oneof_name$_ = null;\n" + "}\n" + "\n"); } // Fields @@ -326,7 +318,6 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Print("\n"); } - GenerateMessageSerializationMethods(printer); GenerateParseFromMethods(printer); GenerateBuilder(printer); @@ -334,8 +325,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { // Memoizes whether the protocol buffer is fully initialized (has all // required fields). 0 means false, 1 means true, and all other values // mean not yet computed. - printer->Print( - "private byte memoizedIsInitialized = 2;\n"); + printer->Print("private byte memoizedIsInitialized = 2;\n"); } printer->Print( @@ -353,74 +343,46 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Indent(); printer->Indent(); - printer->Print( - "case NEW_BUILDER: {\n"); + printer->Print("case NEW_BUILDER: {\n"); printer->Indent(); GenerateDynamicMethodNewBuilder(printer); printer->Outdent(); - if (!EnableExperimentalRuntimeForLite()) { - printer->Print( + printer->Print( "}\n" - "case IS_INITIALIZED: {\n"); - printer->Indent(); - GenerateDynamicMethodIsInitialized(printer); - printer->Outdent(); - - printer->Print("}\n"); - - printer->Print( - "case MAKE_IMMUTABLE: {\n"); - - printer->Indent(); - GenerateDynamicMethodMakeImmutable(printer); - printer->Outdent(); - - printer->Print( - "}\n" - "case VISIT: {\n"); - - printer->Indent(); - GenerateDynamicMethodVisit(printer); - printer->Outdent(); - - printer->Print( - "}\n" - "case MERGE_FROM_STREAM: {\n"); - - printer->Indent(); - GenerateDynamicMethodMergeFromStream(printer); - printer->Outdent(); - } + "case BUILD_MESSAGE_INFO: {\n"); + printer->Indent(); + GenerateDynamicMethodNewBuildMessageInfo(printer); + printer->Outdent(); printer->Print( - "}\n" - "// fall through\n" - "case GET_DEFAULT_INSTANCE: {\n" - " return DEFAULT_INSTANCE;\n" - "}\n" - "case GET_PARSER: {\n" - // Generally one would use the lazy initialization holder pattern for - // manipulating static fields but that has exceptional cost on Android as - // it will generate an extra class for every message. Instead, use the - // double-check locking pattern which works just as well. - // - // The "parser" temporary mirrors the "PARSER" field to eliminate a read - // at the final return statement. - " com.google.protobuf.Parser<$classname$> parser = PARSER;\n" - " if (parser == null) {\n" - " synchronized ($classname$.class) {\n" - " parser = PARSER;\n" - " if (parser == null) {\n" - " parser = new DefaultInstanceBasedParser(DEFAULT_INSTANCE);\n" - " PARSER = parser;\n" - " }\n" - " }\n" - " }\n" - " return parser;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "}\n" + "// fall through\n" + "case GET_DEFAULT_INSTANCE: {\n" + " return DEFAULT_INSTANCE;\n" + "}\n" + "case GET_PARSER: {\n" + // Generally one would use the lazy initialization holder pattern for + // manipulating static fields but that has exceptional cost on Android as + // it will generate an extra class for every message. Instead, use the + // double-check locking pattern which works just as well. + // + // The "parser" temporary mirrors the "PARSER" field to eliminate a read + // at the final return statement. + " com.google.protobuf.Parser<$classname$> parser = PARSER;\n" + " if (parser == null) {\n" + " synchronized ($classname$.class) {\n" + " parser = PARSER;\n" + " if (parser == null) {\n" + " parser = new DefaultInstanceBasedParser(DEFAULT_INSTANCE);\n" + " PARSER = parser;\n" + " }\n" + " }\n" + " }\n" + " return parser;\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Outdent(); @@ -447,43 +409,40 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Print( - " }\n" - " throw new UnsupportedOperationException();\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + " }\n" + " throw new UnsupportedOperationException();\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); - + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); // Carefully initialize the default instance in such a way that it doesn't // conflict with other initialization. - printer->Print( - "private static final $classname$ DEFAULT_INSTANCE;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + printer->Print("private static final $classname$ DEFAULT_INSTANCE;\n", + "classname", + name_resolver_->GetImmutableClassName(descriptor_)); printer->Print( - "static {\n" - " // New instances are implicitly immutable so no need to make\n" - " // immutable.\n" - " DEFAULT_INSTANCE = new $classname$();\n" - "}\n" - "\n", - "classname", descriptor_->name()); - if (EnableExperimentalRuntimeForLite()) { - // Register the default instance in a map. This map will be used by - // experimental runtime to lookup default instance given a class instance - // without using Java reflection. - printer->Print( - "static {\n" - " com.google.protobuf.GeneratedMessageLite.registerDefaultInstance(\n" - " $classname$.class, DEFAULT_INSTANCE);\n" - "}\n", - "classname", descriptor_->name()); - } + "static {\n" + " // New instances are implicitly immutable so no need to make\n" + " // immutable.\n" + " DEFAULT_INSTANCE = new $classname$();\n" + "}\n" + "\n", + "classname", descriptor_->name()); + // Register the default instance in a map. This map will be used by + // experimental runtime to lookup default instance given a class instance + // without using Java reflection. + printer->Print( + "static {\n" + " com.google.protobuf.GeneratedMessageLite.registerDefaultInstance(\n" + " $classname$.class, DEFAULT_INSTANCE);\n" + "}\n", + "classname", descriptor_->name()); printer->Print( "public static $classname$ getDefaultInstance() {\n" @@ -513,216 +472,194 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { .Generate(printer); } - printer->Outdent(); printer->Print("}\n\n"); } +void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo( + io::Printer* printer) { + printer->Indent(); -// =================================================================== + // Collect field info into a sequence of UTF-16 chars. It will be embedded + // as a Java string in the generated code. + std::vector<uint16> chars; -void ImmutableMessageLiteGenerator:: -GenerateMessageSerializationMethods(io::Printer* printer) { - if (EnableExperimentalRuntimeForLite()) { - return; + int flags = 0; + if (SupportFieldPresence(descriptor_->file())) { + flags |= 0x1; } - - std::unique_ptr<const FieldDescriptor * []> sorted_fields( - SortFieldsByNumber(descriptor_)); - - std::vector<const Descriptor::ExtensionRange*> sorted_extensions; - for (int i = 0; i < descriptor_->extension_range_count(); ++i) { - sorted_extensions.push_back(descriptor_->extension_range(i)); + if (descriptor_->options().message_set_wire_format()) { + flags |= 0x2; } - std::sort(sorted_extensions.begin(), sorted_extensions.end(), - ExtensionRangeOrdering()); + WriteIntToUtf16CharSequence(flags, &chars); + WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars); - printer->Print( - "@java.lang.Override\n" - "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" - " throws java.io.IOException {\n"); - printer->Indent(); - if (HasPackedFields(descriptor_)) { - // writeTo(CodedOutputStream output) might be invoked without - // getSerializedSize() ever being called, but we need the memoized - // sizes in case this message has packed fields. Rather than emit checks - // for each packed field, just call getSerializedSize() up front. In most - // cases, getSerializedSize() will have already been called anyway by one - // of the wrapper writeTo() methods, making this call cheap. - printer->Print("getSerializedSize();\n"); - } + if (descriptor_->field_count() == 0) { + printer->Print("java.lang.Object[] objects = null;"); + } else { + // A single array of all fields (including oneof, oneofCase, hasBits). + printer->Print("java.lang.Object[] objects = new java.lang.Object[] {\n"); + printer->Indent(); - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "com.google.protobuf.GeneratedMessageLite\n" - " .ExtendableMessage<$classname$, $classname$.Builder>\n" - " .ExtensionWriter extensionWriter =\n" - " newMessageSetExtensionWriter();\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - } else { + // Record the number of oneofs. + WriteIntToUtf16CharSequence(descriptor_->oneof_decl_count(), &chars); + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { + const OneofDescriptor* oneof = descriptor_->oneof_decl(i); printer->Print( - "com.google.protobuf.GeneratedMessageLite\n" - " .ExtendableMessage<$classname$, $classname$.Builder>\n" - " .ExtensionWriter extensionWriter =\n" - " newExtensionWriter();\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "\"$oneof_name$_\",\n" + "\"$oneof_name$Case_\",\n", + "oneof_name", context_->GetOneofGeneratorInfo(oneof)->name); } - } - // Merge the fields and the extension ranges, both sorted by field number. - for (int i = 0, j = 0; - i < descriptor_->field_count() || j < sorted_extensions.size();) { - if (i == descriptor_->field_count()) { - GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); - } else if (j == sorted_extensions.size()) { - GenerateSerializeOneField(printer, sorted_fields[i++]); - } else if (sorted_fields[i]->number() < sorted_extensions[j]->start) { - GenerateSerializeOneField(printer, sorted_fields[i++]); - } else { - GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); + // Integers for bit fields. + int total_bits = 0; + for (int i = 0; i < descriptor_->field_count(); i++) { + total_bits += + field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage(); } - } + int total_ints = (total_bits + 31) / 32; + for (int i = 0; i < total_ints; i++) { + printer->Print("\"$bit_field_name$\",\n", "bit_field_name", + GetBitFieldName(i)); + } + WriteIntToUtf16CharSequence(total_ints, &chars); - if (descriptor_->options().message_set_wire_format()) { - printer->Print("unknownFields.writeAsMessageSetTo(output);\n"); - } else { - printer->Print("unknownFields.writeTo(output);\n"); - } + int map_count = 0; + int repeated_count = 0; + std::unique_ptr<const FieldDescriptor*[]> sorted_fields( + SortFieldsByNumber(descriptor_)); + for (int i = 0; i < descriptor_->field_count(); i++) { + const FieldDescriptor* field = sorted_fields[i]; + if (field->is_map()) { + map_count++; + } else if (field->is_repeated()) { + repeated_count++; + } + } - printer->Outdent(); - printer->Print( - "}\n" - "\n" - "@java.lang.Override\n" - "public int getSerializedSize() {\n" - " int size = memoizedSerializedSize;\n" - " if (size != -1) return size;\n" - "\n"); - printer->Indent(); - printer->Print( - "size = 0;\n"); + WriteIntToUtf16CharSequence(sorted_fields[0]->number(), &chars); + WriteIntToUtf16CharSequence( + sorted_fields[descriptor_->field_count() - 1]->number(), &chars); + WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars); + WriteIntToUtf16CharSequence(map_count, &chars); + WriteIntToUtf16CharSequence(repeated_count, &chars); - for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); - } + std::vector<const FieldDescriptor*> fields_for_is_initialized_check; + for (int i = 0; i < descriptor_->field_count(); i++) { + if (descriptor_->field(i)->is_required() || + (GetJavaType(descriptor_->field(i)) == JAVATYPE_MESSAGE && + HasRequiredFields(descriptor_->field(i)->message_type()))) { + fields_for_is_initialized_check.push_back(descriptor_->field(i)); + } + } + WriteIntToUtf16CharSequence(fields_for_is_initialized_check.size(), &chars); - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print("size += extensionsSerializedSizeAsMessageSet();\n"); - } else { - printer->Print("size += extensionsSerializedSize();\n"); + for (int i = 0; i < descriptor_->field_count(); i++) { + const FieldDescriptor* field = sorted_fields[i]; + field_generators_.get(field).GenerateFieldInfo(printer, &chars); } + printer->Outdent(); + printer->Print("};\n"); } - if (descriptor_->options().message_set_wire_format()) { - printer->Print("size += unknownFields.getSerializedSizeAsMessageSet();\n"); - } else { - printer->Print("size += unknownFields.getSerializedSize();\n"); + printer->Print("java.lang.String info =\n"); + std::string line; + for (size_t i = 0; i < chars.size(); i++) { + uint16 code = chars[i]; + EscapeUtf16ToString(code, &line); + if (line.size() >= 80) { + printer->Print(" \"$string$\" +\n", "string", line); + line.clear(); + } } + printer->Print(" \"$string$\";\n", "string", line); - printer->Print( - "memoizedSerializedSize = size;\n" - "return size;\n"); - + printer->Print("return newMessageInfo(DEFAULT_INSTANCE, info, objects);\n"); printer->Outdent(); - printer->Print( - "}\n" - "\n"); } -void ImmutableMessageLiteGenerator:: -GenerateParseFromMethods(io::Printer* printer) { - // Note: These are separate from GenerateMessageSerializationMethods() - // because they need to be generated even for messages that are optimized - // for code size. - printer->Print( - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.nio.ByteBuffer data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.ByteString data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(byte[] data)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " byte[] data,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws com.google.protobuf.InvalidProtocolBufferException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, data, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(java.io.InputStream input)\n" - " throws java.io.IOException {\n" - " return parseDelimitedFrom(DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseDelimitedFrom(\n" - " java.io.InputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return parseDelimitedFrom(DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input);\n" - "}\n" - "public static $classname$ parseFrom(\n" - " com.google.protobuf.CodedInputStream input,\n" - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" - " throws java.io.IOException {\n" - " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" - " DEFAULT_INSTANCE, input, extensionRegistry);\n" - "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); -} - -void ImmutableMessageLiteGenerator::GenerateSerializeOneField( - io::Printer* printer, const FieldDescriptor* field) { - field_generators_.get(field).GenerateSerializationCode(printer); -} +// =================================================================== -void ImmutableMessageLiteGenerator::GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range) { - printer->Print("extensionWriter.writeUntil($end$, output);\n", "end", - StrCat(range->end)); +void ImmutableMessageLiteGenerator::GenerateParseFromMethods( + io::Printer* printer) { + printer->Print( + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.nio.ByteBuffer data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.ByteString data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(byte[] data)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " byte[] data,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws com.google.protobuf.InvalidProtocolBufferException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, data, extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(java.io.InputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input, extensionRegistry);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(java.io.InputStream " + "input)\n" + " throws java.io.IOException {\n" + " return parseDelimitedFrom(DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseDelimitedFrom(\n" + " java.io.InputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return parseDelimitedFrom(DEFAULT_INSTANCE, input, " + "extensionRegistry);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input);\n" + "}\n" + "public static $classname$ parseFrom(\n" + " com.google.protobuf.CodedInputStream input,\n" + " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" + " throws java.io.IOException {\n" + " return com.google.protobuf.GeneratedMessageLite.parseFrom(\n" + " DEFAULT_INSTANCE, input, extensionRegistry);\n" + "}\n" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } // =================================================================== @@ -744,383 +681,43 @@ void ImmutableMessageLiteGenerator::GenerateBuilder(io::Printer* printer) { // =================================================================== -void ImmutableMessageLiteGenerator::GenerateDynamicMethodIsInitialized( - io::Printer* printer) { - // Returns null for false, DEFAULT_INSTANCE for true. - if (!HasRequiredFields(descriptor_)) { - printer->Print("return DEFAULT_INSTANCE;\n"); - return; - } - - // TODO(xiaofeng): Remove this when b/64445758 is fixed. We don't need to - // check memoizedIsInitialized here because the caller does that already, - // but right now proguard proto shrinker asserts on the bytecode layout of - // this code so it can't be removed until proguard is updated. - printer->Print( - "byte isInitialized = memoizedIsInitialized;\n" - "if (isInitialized == 1) return DEFAULT_INSTANCE;\n" - "if (isInitialized == 0) return null;\n" - "\n" - "boolean shouldMemoize = ((Boolean) arg0).booleanValue();\n"); - - // Check that all required fields in this message are set. - // TODO(kenton): We can optimize this when we switch to putting all the - // "has" fields into a single bitfield. - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = descriptor_->field(i); - const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); - - if (field->is_required()) { - printer->Print( - "if (!has$name$()) {\n" - " return null;\n" - "}\n", - "name", info->capitalized_name); - } - } - - // Now check that all embedded messages are initialized. - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = descriptor_->field(i); - const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); - if (GetJavaType(field) == JAVATYPE_MESSAGE && - HasRequiredFields(field->message_type())) { - switch (field->label()) { - case FieldDescriptor::LABEL_REQUIRED: - printer->Print( - "if (!get$name$().isInitialized()) {\n" - " return null;\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_OPTIONAL: - if (!SupportFieldPresence(descriptor_->file()) && - field->containing_oneof() != NULL) { - const OneofDescriptor* oneof = field->containing_oneof(); - const OneofGeneratorInfo* oneof_info = - context_->GetOneofGeneratorInfo(oneof); - printer->Print("if ($oneof_name$Case_ == $field_number$) {\n", - "oneof_name", oneof_info->name, "field_number", - StrCat(field->number())); - } else { - printer->Print( - "if (has$name$()) {\n", - "name", info->capitalized_name); - } - printer->Print( - " if (!get$name$().isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "name", info->capitalized_name); - break; - case FieldDescriptor::LABEL_REPEATED: - if (IsMapEntry(field->message_type())) { - printer->Print( - "for ($type$ item : get$name$Map().values()) {\n" - " if (!item.isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "type", MapValueImmutableClassdName(field->message_type(), - name_resolver_), - "name", info->capitalized_name); - } else { - printer->Print( - "for (int i = 0; i < get$name$Count(); i++) {\n" - " if (!get$name$(i).isInitialized()) {\n" - " return null;\n" - " }\n" - "}\n", - "type", name_resolver_->GetImmutableClassName( - field->message_type()), - "name", info->capitalized_name); - } - break; - } - } - } - - if (descriptor_->extension_range_count() > 0) { - printer->Print( - "if (!extensionsAreInitialized()) {\n" - " return null;\n" - "}\n"); - } - - printer->Print( - "return DEFAULT_INSTANCE;\n" - "\n"); -} - -// =================================================================== - -void ImmutableMessageLiteGenerator::GenerateDynamicMethodMakeImmutable( - io::Printer* printer) { - - // Output generation code for each field. - for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(descriptor_->field(i)) - .GenerateDynamicMethodMakeImmutableCode(printer); - } - - printer->Print( - "return null;\n"); -} - -// =================================================================== - void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuilder( io::Printer* printer) { - printer->Print( - "return new Builder();\n"); -} - -// =================================================================== - -void ImmutableMessageLiteGenerator::GenerateDynamicMethodVisit( - io::Printer* printer) { - printer->Print( - "Visitor visitor = (Visitor) arg0;\n" - "$classname$ other = ($classname$) arg1;\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); - - for (int i = 0; i < descriptor_->field_count(); i++) { - if (!descriptor_->field(i)->containing_oneof()) { - field_generators_.get( - descriptor_->field(i)).GenerateVisitCode(printer); - } - } - - // Merge oneof fields. - for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - printer->Print( - "switch (other.get$oneof_capitalized_name$Case()) {\n", - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->capitalized_name); - printer->Indent(); - for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { - const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); - printer->Print( - "case $field_name$: {\n", - "field_name", - ToUpper(field->name())); - printer->Indent(); - field_generators_.get(field).GenerateVisitCode(printer); - printer->Print( - "break;\n"); - printer->Outdent(); - printer->Print( - "}\n"); - } - printer->Print( - "case $cap_oneof_name$_NOT_SET: {\n" - " visitor.visitOneofNotSet($oneof_name$Case_ != 0);\n" - " break;\n" - "}\n", - "cap_oneof_name", - ToUpper(context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name), - "oneof_name", - context_->GetOneofGeneratorInfo( - descriptor_->oneof_decl(i))->name); - printer->Outdent(); - printer->Print( - "}\n"); - } - - printer->Print( - "if (visitor == com.google.protobuf.GeneratedMessageLite.MergeFromVisitor\n" - " .INSTANCE) {\n"); - printer->Indent(); - for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) { - const OneofDescriptor* field = descriptor_->oneof_decl(i); - printer->Print( - "if (other.$oneof_name$Case_ != 0) {\n" - " $oneof_name$Case_ = other.$oneof_name$Case_;\n" - "}\n", - "oneof_name", context_->GetOneofGeneratorInfo(field)->name); - } - - // Integers for bit fields. - int totalBits = 0; - for (int i = 0; i < descriptor_->field_count(); i++) { - totalBits += - field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage(); - } - int totalInts = (totalBits + 31) / 32; - - for (int i = 0; i < totalInts; i++) { - printer->Print("$bit_field_name$ |= other.$bit_field_name$;\n", - "bit_field_name", GetBitFieldName(i)); - } - printer->Outdent(); - printer->Print( - "}\n"); - - - printer->Print( - "return this;\n"); + printer->Print("return new Builder();\n"); } // =================================================================== -void ImmutableMessageLiteGenerator::GenerateDynamicMethodMergeFromStream( +void ImmutableMessageLiteGenerator::GenerateExtensionRegistrationCode( io::Printer* printer) { - printer->Print( - "com.google.protobuf.CodedInputStream input =\n" - " (com.google.protobuf.CodedInputStream) arg0;\n" - "com.google.protobuf.ExtensionRegistryLite extensionRegistry =\n" - " (com.google.protobuf.ExtensionRegistryLite) arg1;\n" - "if (extensionRegistry == null) {\n" - " throw new java.lang.NullPointerException();\n" - "}\n"); - printer->Print( - "try {\n"); - printer->Indent(); - printer->Print( - "boolean done = false;\n" - "while (!done) {\n"); - printer->Indent(); - - printer->Print( - "int tag = input.readTag();\n" - "switch (tag) {\n"); - printer->Indent(); - - printer->Print( - "case 0:\n" // zero signals EOF / limit reached - " done = true;\n" - " break;\n"); - - std::unique_ptr<const FieldDescriptor* []> sorted_fields( - SortFieldsByNumber(descriptor_)); - for (int i = 0; i < descriptor_->field_count(); i++) { - const FieldDescriptor* field = sorted_fields[i]; - uint32 tag = WireFormatLite::MakeTag( - field->number(), WireFormat::WireTypeForFieldType(field->type())); - - printer->Print("case $tag$: {\n", "tag", - StrCat(static_cast<int32>(tag))); - printer->Indent(); - - field_generators_.get(field).GenerateParsingCode(printer); - - printer->Outdent(); - printer->Print( - " break;\n" - "}\n"); - - if (field->is_packable()) { - // To make packed = true wire compatible, we generate parsing code from - // a packed version of this field regardless of - // field->options().packed(). - uint32 packed_tag = WireFormatLite::MakeTag( - field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); - printer->Print("case $tag$: {\n", "tag", - StrCat(static_cast<int32>(packed_tag))); - printer->Indent(); - - field_generators_.get(field).GenerateParsingCodeFromPacked(printer); - - printer->Outdent(); - printer->Print( - " break;\n" - "}\n"); - } - } - - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "default: {\n" - " if (!parseUnknownFieldAsMessageSet(\n" - " getDefaultInstanceForType(), input, extensionRegistry,\n" - " tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } else { - printer->Print( - "default: {\n" - " if (!parseUnknownField(getDefaultInstanceForType(),\n" - " input, extensionRegistry, tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } - } else { - printer->Print( - "default: {\n" - " if (!parseUnknownField(tag, input)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } - - printer->Outdent(); - printer->Outdent(); - printer->Print( - " }\n" // switch (tag) - "}\n"); // while (!done) - - printer->Outdent(); - printer->Print( - "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" - " throw new RuntimeException(e.setUnfinishedMessage(this));\n" - "} catch (java.io.IOException e) {\n" - " throw new RuntimeException(\n" - " new com.google.protobuf.InvalidProtocolBufferException(\n" - " e.getMessage()).setUnfinishedMessage(this));\n" - "} finally {\n"); - printer->Indent(); - - printer->Outdent(); - printer->Print( - "}\n"); // finally -} - -// =================================================================== - -void ImmutableMessageLiteGenerator:: -GenerateExtensionRegistrationCode(io::Printer* printer) { for (int i = 0; i < descriptor_->extension_count(); i++) { ImmutableExtensionLiteGenerator(descriptor_->extension(i), context_) - .GenerateRegistrationCode(printer); + .GenerateRegistrationCode(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_) - .GenerateExtensionRegistrationCode(printer); + .GenerateExtensionRegistrationCode(printer); } } // =================================================================== -void ImmutableMessageLiteGenerator:: -GenerateConstructor(io::Printer* printer) { - printer->Print( - "private $classname$() {\n", - "classname", descriptor_->name()); +void ImmutableMessageLiteGenerator::GenerateConstructor(io::Printer* printer) { + printer->Print("private $classname$() {\n", "classname", descriptor_->name()); printer->Indent(); // Initialize all fields to default. GenerateInitializers(printer); printer->Outdent(); - printer->Print( - "}\n"); + printer->Print("}\n"); } // =================================================================== void ImmutableMessageLiteGenerator::GenerateParser(io::Printer* printer) { printer->Print( - "private static volatile com.google.protobuf.Parser<$classname$> PARSER;\n" + "private static volatile com.google.protobuf.Parser<$classname$> " + "PARSER;\n" "\n" "public static com.google.protobuf.Parser<$classname$> parser() {\n" " return DEFAULT_INSTANCE.getParserForType();\n" diff --git a/src/google/protobuf/compiler/java/java_message_lite.h b/src/google/protobuf/compiler/java/java_message_lite.h index d38c54a7f9..61e81dfe0c 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.h +++ b/src/google/protobuf/compiler/java/java_message_lite.h @@ -35,8 +35,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ -#include <string> #include <map> +#include <string> #include <google/protobuf/compiler/java/java_field.h> #include <google/protobuf/compiler/java/java_message.h> @@ -53,29 +53,20 @@ class ImmutableMessageLiteGenerator : public MessageGenerator { virtual void Generate(io::Printer* printer); virtual void GenerateInterface(io::Printer* printer); virtual void GenerateExtensionRegistrationCode(io::Printer* printer); - virtual void GenerateStaticVariables( - io::Printer* printer, int* bytecode_estimate); + virtual void GenerateStaticVariables(io::Printer* printer, + int* bytecode_estimate); virtual int GenerateStaticVariableInitializers(io::Printer* printer); private: - - void GenerateMessageSerializationMethods(io::Printer* printer); void GenerateParseFromMethods(io::Printer* printer); - void GenerateSerializeOneField(io::Printer* printer, - const FieldDescriptor* field); - void GenerateSerializeOneExtensionRange( - io::Printer* printer, const Descriptor::ExtensionRange* range); void GenerateBuilder(io::Printer* printer); - void GenerateDynamicMethodIsInitialized(io::Printer* printer); - void GenerateDynamicMethodMakeImmutable(io::Printer* printer); - void GenerateDynamicMethodVisit(io::Printer* printer); - void GenerateDynamicMethodMergeFromStream(io::Printer* printer); void GenerateDynamicMethodNewBuilder(io::Printer* printer); void GenerateInitializers(io::Printer* printer); void GenerateEqualsAndHashCode(io::Printer* printer); void GenerateParser(io::Printer* printer); void GenerateConstructor(io::Printer* printer); + void GenerateDynamicMethodNewBuildMessageInfo(io::Printer* printer); Context* context_; ClassNameResolver* name_resolver_; diff --git a/src/google/protobuf/compiler/java/java_name_resolver.cc b/src/google/protobuf/compiler/java/java_name_resolver.cc index f8e4b3a39a..d07c628e50 100644 --- a/src/google/protobuf/compiler/java/java_name_resolver.cc +++ b/src/google/protobuf/compiler/java/java_name_resolver.cc @@ -66,8 +66,7 @@ std::string StripPackageName(const std::string& full_name, // Get the name of a message's Java class without package name prefix. std::string ClassNameWithoutPackage(const Descriptor* descriptor, bool immutable) { - return StripPackageName(descriptor->full_name(), - descriptor->file()); + return StripPackageName(descriptor->full_name(), descriptor->file()); } // Get the name of an enum's Java class without package name prefix. @@ -78,8 +77,8 @@ std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor, if (message_descriptor == NULL) { return descriptor->name(); } else { - return ClassNameWithoutPackage(message_descriptor, immutable) + - "." + descriptor->name(); + return ClassNameWithoutPackage(message_descriptor, immutable) + "." + + descriptor->name(); } } @@ -128,11 +127,9 @@ bool MessageHasConflictingClassName(const Descriptor* message, } // namespace -ClassNameResolver::ClassNameResolver() { -} +ClassNameResolver::ClassNameResolver() {} -ClassNameResolver::~ClassNameResolver() { -} +ClassNameResolver::~ClassNameResolver() {} std::string ClassNameResolver::GetFileDefaultImmutableClassName( const FileDescriptor* file) { @@ -174,9 +171,9 @@ std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file, // Check whether there is any type defined in the proto file that has // the given class name. -bool ClassNameResolver::HasConflictingClassName( - const FileDescriptor* file, const std::string& classname, - NameEquality equality_mode) { +bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file, + const std::string& classname, + NameEquality equality_mode) { for (int i = 0; i < file->enum_type_count(); i++) { if (CheckNameEquality(file->enum_type(i)->name(), classname) == equality_mode) { @@ -274,16 +271,14 @@ std::string ClassNameResolver::GetExtensionIdentifierName( std::string ClassNameResolver::GetJavaImmutableClassName( const Descriptor* descriptor) { - return GetJavaClassFullName( - ClassNameWithoutPackage(descriptor, true), - descriptor->file(), true); + return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true), + descriptor->file(), true); } std::string ClassNameResolver::GetJavaImmutableClassName( const EnumDescriptor* descriptor) { - return GetJavaClassFullName( - ClassNameWithoutPackage(descriptor, true), - descriptor->file(), true); + return GetJavaClassFullName(ClassNameWithoutPackage(descriptor, true), + descriptor->file(), true); } diff --git a/src/google/protobuf/compiler/java/java_plugin_unittest.cc b/src/google/protobuf/compiler/java/java_plugin_unittest.cc index b3e220f113..744b2c8be7 100644 --- a/src/google/protobuf/compiler/java/java_plugin_unittest.cc +++ b/src/google/protobuf/compiler/java/java_plugin_unittest.cc @@ -38,8 +38,8 @@ #include <google/protobuf/compiler/java/java_generator.h> #include <google/protobuf/compiler/command_line_interface.h> -#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/printer.h> +#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/testing/file.h> #include <google/protobuf/testing/file.h> @@ -107,13 +107,8 @@ TEST(JavaPluginTest, PluginTest) { std::string java_out = "--java_out=" + TestTempDir(); std::string test_out = "--test_out=" + TestTempDir(); - const char* argv[] = { - "protoc", - proto_path.c_str(), - java_out.c_str(), - test_out.c_str(), - "test.proto" - }; + const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } diff --git a/src/google/protobuf/compiler/java/java_primitive_field.cc b/src/google/protobuf/compiler/java/java_primitive_field.cc index d26d43d16a..43c7f8fa40 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field.cc @@ -69,10 +69,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType); (*variables)["field_type"] = (*variables)["type"]; - if (javaType == JAVATYPE_BOOLEAN || - javaType == JAVATYPE_DOUBLE || - javaType == JAVATYPE_FLOAT || - javaType == JAVATYPE_INT || + if (javaType == JAVATYPE_BOOLEAN || javaType == JAVATYPE_DOUBLE || + javaType == JAVATYPE_FLOAT || javaType == JAVATYPE_INT || javaType == JAVATYPE_LONG) { std::string capitalized_type = UnderscoresToCamelCase( PrimitiveTypeName(javaType), /*cap_first_letter=*/true); @@ -108,8 +106,10 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); - (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ? - "" : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); + (*variables)["default_init"] = + IsDefaultValueJavaDefault(descriptor) + ? "" + : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); (*variables)["capitalized_type"] = GetCapitalizedType(descriptor, /* immutable = */ true); (*variables)["tag"] = @@ -126,8 +126,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { (*variables)["fixed_size"] = StrCat(fixed_size); @@ -184,14 +184,14 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutablePrimitiveFieldGenerator:: -ImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +ImmutablePrimitiveFieldGenerator::ImmutablePrimitiveFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -207,129 +207,130 @@ int ImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { return GetNumBitsForMessage(); } -void ImmutablePrimitiveFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_type$ $name$_;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_type$ $name$_ $default_init$;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_type$ $name$_ $default_init$;\n"); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n"); printer->Annotate("{", "}", descriptor_); JavaType type = GetJavaType(descriptor_); if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. - printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + printer->Print( + variables_, + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); } else { - printer->Print(variables_, - " $name$_ = $default$;\n"); + printer->Print(variables_, " $name$_ = $default$;\n"); } printer->Print(variables_, - " $on_changed$\n" - " return this;\n" - "}\n"); + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for primitives } -void ImmutablePrimitiveFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (!IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, "$name$_ = $default$;\n"); } } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } else { printer->Print(variables_, - "if (other.get$capitalized_name$() != $default$) {\n" - " set$capitalized_name$(other.get$capitalized_name$());\n" - "}\n"); + "if (other.get$capitalized_name$() != $default$) {\n" + " set$capitalized_name$(other.get$capitalized_name$());\n" + "}\n"); } } -void ImmutablePrimitiveFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { if (IsDefaultValueJavaDefault(descriptor_)) { printer->Print(variables_, @@ -349,65 +350,68 @@ GenerateBuildingCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$set_has_field_bit_message$\n" - "$name$_ = input.read$capitalized_type$();\n"); + "$set_has_field_bit_message$\n" + "$name$_ = input.read$capitalized_type$();\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for primitives. } -void ImmutablePrimitiveFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$capitalized_type$($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " output.write$capitalized_type$($number$, $name$_);\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$Size($number$, $name$_);\n" + "}\n"); } -void ImmutablePrimitiveFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { switch (GetJavaType(descriptor_)) { case JAVATYPE_INT: case JAVATYPE_LONG: case JAVATYPE_BOOLEAN: printer->Print(variables_, - "if (get$capitalized_name$()\n" - " != other.get$capitalized_name$()) return false;\n"); + "if (get$capitalized_name$()\n" + " != other.get$capitalized_name$()) return false;\n"); break; case JAVATYPE_FLOAT: - printer->Print(variables_, - "if (java.lang.Float.floatToIntBits(get$capitalized_name$())\n" - " != java.lang.Float.floatToIntBits(\n" - " other.get$capitalized_name$())) return false;\n"); + printer->Print( + variables_, + "if (java.lang.Float.floatToIntBits(get$capitalized_name$())\n" + " != java.lang.Float.floatToIntBits(\n" + " other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_DOUBLE: - printer->Print(variables_, - "if (java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" - " != java.lang.Double.doubleToLongBits(\n" - " other.get$capitalized_name$())) return false;\n"); + printer->Print( + variables_, + "if (java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" + " != java.lang.Double.doubleToLongBits(\n" + " other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_STRING: case JAVATYPE_BYTES: - printer->Print(variables_, - "if (!get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$())) return false;\n"); + printer->Print( + variables_, + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); break; case JAVATYPE_ENUM: @@ -418,44 +422,47 @@ GenerateEqualsCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); +void ImmutablePrimitiveFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print(variables_, "hash = (37 * hash) + $constant_name$;\n"); switch (GetJavaType(descriptor_)) { case JAVATYPE_INT: printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$();\n"); + "hash = (53 * hash) + get$capitalized_name$();\n"); break; case JAVATYPE_LONG: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " get$capitalized_name$());\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" - " get$capitalized_name$());\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_FLOAT: printer->Print(variables_, - "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" - " get$capitalized_name$());\n"); + "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" + " get$capitalized_name$());\n"); break; case JAVATYPE_DOUBLE: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); + printer->Print( + variables_, + "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" + " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); break; case JAVATYPE_STRING: case JAVATYPE_BYTES: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + printer->Print( + variables_, + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); break; case JAVATYPE_ENUM: @@ -472,165 +479,166 @@ std::string ImmutablePrimitiveFieldGenerator::GetBoxedType() const { // =================================================================== -ImmutablePrimitiveOneofFieldGenerator:: -ImmutablePrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutablePrimitiveFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutablePrimitiveOneofFieldGenerator::ImmutablePrimitiveOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutablePrimitiveFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } ImmutablePrimitiveOneofFieldGenerator:: -~ImmutablePrimitiveOneofFieldGenerator() {} + ~ImmutablePrimitiveOneofFieldGenerator() {} -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } - -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { printer->Print(variables_, - "set$capitalized_name$(other.get$capitalized_name$());\n"); + "set$capitalized_name$(other.get$capitalized_name$());\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = input.read$capitalized_type$();\n"); + "$set_oneof_case_message$;\n" + "$oneof_name$_ = input.read$capitalized_type$();\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$capitalized_type$(\n"); + "if ($has_oneof_case_message$) {\n" + " output.write$capitalized_type$(\n"); // $type$ and $boxed_type$ is the same for bytes fields so we don't need to // do redundant casts. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { - printer->Print(variables_, - " $number$, ($type$) $oneof_name$_);\n"); + printer->Print(variables_, " $number$, ($type$) $oneof_name$_);\n"); } else { - printer->Print(variables_, - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + printer->Print( + variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); } - printer->Print( - "}\n"); + printer->Print("}\n"); } -void ImmutablePrimitiveOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size(\n"); + "if ($has_oneof_case_message$) {\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$Size(\n"); // $type$ and $boxed_type$ is the same for bytes fields so we don't need to // do redundant casts. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { - printer->Print(variables_, - " $number$, ($type$) $oneof_name$_);\n"); + printer->Print(variables_, " $number$, ($type$) $oneof_name$_);\n"); } else { - printer->Print(variables_, - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + printer->Print( + variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); } - printer->Print( - "}\n"); + printer->Print("}\n"); } // =================================================================== RepeatedImmutablePrimitiveFieldGenerator:: -RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { + RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, + int messageBitIndex, + int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutablePrimitiveFieldGenerator:: -~RepeatedImmutablePrimitiveFieldGenerator() {} + ~RepeatedImmutablePrimitiveFieldGenerator() {} int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -640,53 +648,54 @@ int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$boxed_type$> " + "get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); } - -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_list_type$ $name$_;\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_list_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (descriptor_->is_packed()) { printer->Print(variables_, - "private int $name$MemoizedSerializedSize = -1;\n"); + "private int $name$MemoizedSerializedSize = -1;\n"); } } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // One field is the list and the bit field keeps track of whether the // list is immutable. If it's immutable, the invariant is that it must // either an instance of Collections.emptyList() or it's an ArrayList @@ -697,244 +706,254 @@ GenerateBuilderMembers(io::Printer* printer) const { // just that the list cannot be modified via the reference but that the // list can never be modified. printer->Print(variables_, - "private $field_list_type$ $name$_ = $empty_list$;\n"); + "private $field_list_type$ $name$_ = $empty_list$;\n"); printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = $mutable_copy_list$;\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); - - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = $mutable_copy_list$;\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); + + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $get_mutable_bit_builder$ ?\n" - " java.util.Collections.unmodifiableList($name$_) : $name$_;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $get_mutable_bit_builder$ ?\n" + " java.util.Collections.unmodifiableList($name$_) : $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_set$(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_set$(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_add$(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_add$(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $boxed_type$> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $boxed_type$> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = $empty_list$;\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = $empty_list$;\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $empty_list$;\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = $empty_list$;\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name_make_immutable$;\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + "if ($get_mutable_bit_builder$) {\n" + " $name_make_immutable$;\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = $create_list$;\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "$repeated_add$(input.read$capitalized_type$());\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = $create_list$;\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "$repeated_add$(input.read$capitalized_type$());\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int limit = input.pushLimit(length);\n" - "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n" - " $name$_ = $create_list$;\n" - " $set_mutable_bit_parser$;\n" - "}\n" - "while (input.getBytesUntilLimit() > 0) {\n" - " $repeated_add$(input.read$capitalized_type$());\n" - "}\n" - "input.popLimit(limit);\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingCodeFromPacked( + io::Printer* printer) const { + printer->Print( + variables_, + "int length = input.readRawVarint32();\n" + "int limit = input.pushLimit(length);\n" + "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n" + " $name$_ = $create_list$;\n" + " $set_mutable_bit_parser$;\n" + "}\n" + "while (input.getBytesUntilLimit() > 0) {\n" + " $repeated_add$(input.read$capitalized_type$());\n" + "}\n" + "input.popLimit(limit);\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name_make_immutable$; // C\n" - "}\n"); + "if ($get_mutable_bit_parser$) {\n" + " $name_make_immutable$; // C\n" + "}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { if (descriptor_->is_packed()) { // We invoke getSerializedSize in writeTo for messages that have packed // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. // That makes it safe to rely on the memoized size here. printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$NoTag($repeated_get$(i));\n" - "}\n"); + "if (get$capitalized_name$List().size() > 0) {\n" + " output.writeUInt32NoTag($tag$);\n" + " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" + "}\n" + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$capitalized_type$NoTag($repeated_get$(i));\n" + "}\n"); } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$($number$, $repeated_get$(i));\n" - "}\n"); + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " output.write$capitalized_type$($number$, $repeated_get$(i));\n" + "}\n"); } } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); if (FixedSize(GetType(descriptor_)) == -1) { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" - "}\n"); + printer->Print( + variables_, + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += com.google.protobuf.CodedOutputStream\n" + " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" + "}\n"); } else { - printer->Print(variables_, - "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); + printer->Print( + variables_, + "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); } - printer->Print( - "size += dataSize;\n"); + printer->Print("size += dataSize;\n"); if (descriptor_->is_packed()) { printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {\n" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeInt32SizeNoTag(dataSize);\n" - "}\n"); + "if (!get$capitalized_name$List().isEmpty()) {\n" + " size += $tag_size$;\n" + " size += com.google.protobuf.CodedOutputStream\n" + " .computeInt32SizeNoTag(dataSize);\n" + "}\n"); } else { - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); + printer->Print( + variables_, + "size += $tag_size$ * get$capitalized_name$List().size();\n"); } // cache the data size for packed fields. if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); + printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "if (!get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List())) return false;\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutablePrimitiveFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutablePrimitiveFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } std::string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc index be5f0f1ac5..7ca3073f9f 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc @@ -134,8 +134,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, } // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { (*variables)["fixed_size"] = StrCat(fixed_size); @@ -196,20 +196,19 @@ int ImmutablePrimitiveFieldLiteGenerator::GetNumBitsForMessage() const { return SupportFieldPresence(descriptor_->file()) ? 1 : 0; } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$();\n"); + printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { if (IsByteStringWithCustomDefaultValue(descriptor_)) { // allocate this once statically since we know ByteStrings are immutable // values that can be reused. @@ -217,100 +216,107 @@ GenerateMembers(io::Printer* printer) const { variables_, "private static final $field_type$ $bytes_default$ = $default$;\n"); } - printer->Print(variables_, - "private $field_type$ $name$_;\n"); + printer->Print(variables_, "private $field_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - "$null_check$" - " $set_has_field_bit_message$\n" - " $name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + "$null_check$" + " $set_has_field_bit_message$\n" + " $name$_ = value;\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n"); + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n"); JavaType type = GetJavaType(descriptor_); if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. - printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + printer->Print( + variables_, + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); } else { - printer->Print(variables_, - " $name$_ = $default$;\n"); + printer->Print(variables_, " $name$_ = $default$;\n"); } - printer->Print(variables_, - "}\n"); + printer->Print(variables_, "}\n"); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for primitives +void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + if (SupportFieldPresence(descriptor_->file())) { + WriteIntToUtf16CharSequence(messageBitIndex_, output); + } + printer->Print(variables_, "\"$name$_\",\n"); } - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { if (IsByteStringWithCustomDefaultValue(descriptor_)) { printer->Print(variables_, "$name$_ = $bytes_default$;\n"); } else if (!IsDefaultValueJavaDefault(descriptor_)) { @@ -318,154 +324,6 @@ GenerateInitializationCode(io::Printer* printer) const { } } -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visit$visit_type$(\n" - " has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else { - printer->Print(variables_, - "$name$_ = visitor.visit$visit_type$($name$_ != $default$, $name$_,\n" - " other.$name$_ != $default$, other.$name$_);\n"); - } -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$set_has_field_bit_message$\n" - "$name$_ = input.read$capitalized_type$();\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for primitives. -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.write$capitalized_type$($number$, $name$_);\n" - "}\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size($number$, $name$_);\n" - "}\n"); -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - switch (GetJavaType(descriptor_)) { - case JAVATYPE_INT: - case JAVATYPE_LONG: - case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "result = result && (get$capitalized_name$()\n" - " == other.get$capitalized_name$());\n"); - break; - - case JAVATYPE_FLOAT: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Float.floatToIntBits(get$capitalized_name$())\n" - " == java.lang.Float.floatToIntBits(\n" - " other.get$capitalized_name$()));\n"); - break; - - case JAVATYPE_DOUBLE: - printer->Print(variables_, - "result = result && (\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" - " == java.lang.Double.doubleToLongBits(\n" - " other.get$capitalized_name$()));\n"); - break; - - case JAVATYPE_STRING: - case JAVATYPE_BYTES: - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); - break; - - case JAVATYPE_ENUM: - case JAVATYPE_MESSAGE: - default: - GOOGLE_LOG(FATAL) << "Can't get here."; - break; - } -} - -void ImmutablePrimitiveFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - switch (GetJavaType(descriptor_)) { - case JAVATYPE_INT: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$();\n"); - break; - - case JAVATYPE_LONG: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_BOOLEAN: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_FLOAT: - printer->Print(variables_, - "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" - " get$capitalized_name$());\n"); - break; - - case JAVATYPE_DOUBLE: - printer->Print(variables_, - "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" - " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); - break; - - case JAVATYPE_STRING: - case JAVATYPE_BYTES: - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); - break; - - case JAVATYPE_ENUM: - case JAVATYPE_MESSAGE: - default: - GOOGLE_LOG(FATAL) << "Can't get here."; - break; - } -} - std::string ImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); } @@ -484,128 +342,101 @@ ImmutablePrimitiveOneofFieldLiteGenerator:: } ImmutablePrimitiveOneofFieldLiteGenerator:: -~ImmutablePrimitiveOneofFieldLiteGenerator() {} + ~ImmutablePrimitiveOneofFieldLiteGenerator() {} -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " return ($boxed_type$) $oneof_name$_;\n" - " }\n" - " return $default$;\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " return ($boxed_type$) $oneof_name$_;\n" + " }\n" + " return $default$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$($type$ value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$($type$ value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); } +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); +} -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$set$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneof$visit_type$(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = input.read$capitalized_type$();\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.write$capitalized_type$(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" - "}\n"); -} - -void ImmutablePrimitiveOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" - "}\n"); -} - // =================================================================== RepeatedImmutablePrimitiveFieldLiteGenerator:: @@ -622,326 +453,176 @@ RepeatedImmutablePrimitiveFieldLiteGenerator:: } RepeatedImmutablePrimitiveFieldLiteGenerator:: -~RepeatedImmutablePrimitiveFieldLiteGenerator() {} + ~RepeatedImmutablePrimitiveFieldLiteGenerator() {} int RepeatedImmutablePrimitiveFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"); + "$deprecation$java.util.List<$boxed_type$> " + "get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$$type$ get$capitalized_name$(int index);\n"); + "$deprecation$$type$ get$capitalized_name$(int index);\n"); } - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $field_list_type$ $name$_;\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private $field_list_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return $repeated_get$(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return $repeated_get$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); if (!EnableExperimentalRuntimeForLite() && descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { printer->Print(variables_, - "private int $name$MemoizedSerializedSize = -1;\n"); + "private int $name$MemoizedSerializedSize = -1;\n"); } - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, $type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_set$(index, value);\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, $type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_set$(index, value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$($type$ value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $repeated_add$(value);\n" - "}\n"); + "private void add$capitalized_name$($type$ value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $repeated_add$(value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable<? extends $boxed_type$> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable<? extends $boxed_type$> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = $empty_list$;\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $name$_ = $empty_list$;\n" + "}\n"); } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$boxed_type$>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<$boxed_type$>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, $type$ value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, $type$ value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder " + "${$add$capitalized_name$$}$($type$ value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<? extends $boxed_type$> values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<? extends $boxed_type$> values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for primitives -} - - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = $empty_list$;\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { - // noop for primitives -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.$visit_type_list$($name$_, other.$name$_);\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateBuildingCode(io::Printer* printer) const { - // noop for primitives -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - // TODO(dweis): Scan the input buffer to count, then initialize - // appropriately. - // TODO(dweis): Scan the input buffer to count and ensure capacity. - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n" - "$repeated_add$(input.read$capitalized_type$());\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingCodeFromPacked(io::Printer* printer) const { - printer->Print(variables_, - "int length = input.readRawVarint32();\n" - "int limit = input.pushLimit(length);\n" - "if (!$is_mutable$ && input.getBytesUntilLimit() > 0) {\n"); - - int fixed_size = FixedSize(GetType(descriptor_)); - if (fixed_size == -1) { - // TODO(dweis): Scan the input buffer to count, then initialize - // appropriately. - printer->Print(variables_, - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"); - } else { - printer->Print(variables_, - " final int currentSize = $name$_.size();\n" - " $name$_ = $name$_.mutableCopyWithCapacity(\n" - " currentSize + (length/$fixed_size$));\n"); - } - - // TODO(dweis): Scan the input buffer to count and ensure capacity. - printer->Print(variables_, - "}\n" - "while (input.getBytesUntilLimit() > 0) {\n" - " $repeated_add$(input.read$capitalized_type$());\n" - "}\n" - "input.popLimit(limit);\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $make_name_unmodifiable$;\n" - "}\n"); -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - GOOGLE_CHECK(!EnableExperimentalRuntimeForLite()); - - if (descriptor_->is_packed()) { - // We invoke getSerializedSize in writeTo for messages that have packed - // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. - // That makes it safe to rely on the memoized size here. - printer->Print(variables_, - "if (get$capitalized_name$List().size() > 0) {\n" - " output.writeUInt32NoTag($tag$);\n" - " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" - "}\n" - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$NoTag($repeated_get$(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.write$capitalized_type$($number$, $repeated_get$(i));\n" - "}\n"); - } -} - -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - GOOGLE_CHECK(!EnableExperimentalRuntimeForLite()); - - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - if (FixedSize(GetType(descriptor_)) == -1) { - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$SizeNoTag($repeated_get$(i));\n" - "}\n"); - } else { - printer->Print(variables_, - "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); - } - printer->Print( - "size += dataSize;\n"); - - if (descriptor_->is_packed()) { - printer->Print(variables_, - "if (!get$capitalized_name$List().isEmpty()) {\n" - " size += $tag_size$;\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeInt32SizeNoTag(dataSize);\n" + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" "}\n"); - } else { - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); - } - - // cache the data size for packed fields. - if (descriptor_->is_packed()) { - printer->Print(variables_, - "$name$MemoizedSerializedSize = dataSize;\n"); - } - - printer->Outdent(); - printer->Print("}\n"); + printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); } -void RepeatedImmutablePrimitiveFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = $empty_list$;\n"); } std::string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_primitive_field_lite.h b/src/google/protobuf/compiler/java/java_primitive_field_lite.h index 6c05521f06..61d1e4c9aa 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field_lite.h +++ b/src/google/protobuf/compiler/java/java_primitive_field_lite.h @@ -69,18 +69,8 @@ class ImmutablePrimitiveFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateBuilderClearCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; @@ -105,12 +95,9 @@ class ImmutablePrimitiveOneofFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutablePrimitiveOneofFieldLiteGenerator); @@ -129,19 +116,8 @@ class RepeatedImmutablePrimitiveFieldLiteGenerator void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateBuilderClearCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateBuildingCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingCodeFromPacked(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; diff --git a/src/google/protobuf/compiler/java/java_service.cc b/src/google/protobuf/compiler/java/java_service.cc index c5983309ce..17d8e707c0 100644 --- a/src/google/protobuf/compiler/java/java_service.cc +++ b/src/google/protobuf/compiler/java/java_service.cc @@ -48,15 +48,16 @@ namespace compiler { namespace java { ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor) - : descriptor_(descriptor) {} + : descriptor_(descriptor) {} ServiceGenerator::~ServiceGenerator() {} // =================================================================== ImmutableServiceGenerator::ImmutableServiceGenerator( const ServiceDescriptor* descriptor, Context* context) - : ServiceGenerator(descriptor), context_(context), - name_resolver_(context->GetNameResolver()) {} + : ServiceGenerator(descriptor), + context_(context), + name_resolver_(context->GetNameResolver()) {} ImmutableServiceGenerator::~ImmutableServiceGenerator() {} @@ -66,15 +67,13 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { MaybePrintGeneratedAnnotation(context_, printer, descriptor_, /* immutable = */ true); printer->Print( - "public $static$ abstract class $classname$\n" - " implements com.google.protobuf.Service {\n", - "static", is_own_file ? "" : "static", - "classname", descriptor_->name()); + "public $static$ abstract class $classname$\n" + " implements com.google.protobuf.Service {\n", + "static", is_own_file ? "" : "static", "classname", descriptor_->name()); printer->Indent(); - printer->Print( - "protected $classname$() {}\n\n", - "classname", descriptor_->name()); + printer->Print("protected $classname$() {}\n\n", "classname", + descriptor_->name()); GenerateInterface(printer); @@ -103,9 +102,9 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { // Add an insertion point. printer->Print( - "\n" - "// @@protoc_insertion_point(class_scope:$full_name$)\n", - "full_name", descriptor_->full_name()); + "\n" + "// @@protoc_insertion_point(class_scope:$full_name$)\n", + "full_name", descriptor_->full_name()); printer->Outdent(); printer->Print("}\n\n"); @@ -114,10 +113,10 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) { void ImmutableServiceGenerator::GenerateGetDescriptorForType( io::Printer* printer) { printer->Print( - "public final com.google.protobuf.Descriptors.ServiceDescriptor\n" - " getDescriptorForType() {\n" - " return getDescriptor();\n" - "}\n"); + "public final com.google.protobuf.Descriptors.ServiceDescriptor\n" + " getDescriptorForType() {\n" + " return getDescriptor();\n" + "}\n"); } void ImmutableServiceGenerator::GenerateInterface(io::Printer* printer) { @@ -131,10 +130,10 @@ void ImmutableServiceGenerator::GenerateInterface(io::Printer* printer) { void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.Service newReflectiveService(\n" - " final Interface impl) {\n" - " return new $classname$() {\n", - "classname", descriptor_->name()); + "public static com.google.protobuf.Service newReflectiveService(\n" + " final Interface impl) {\n" + " return new $classname$() {\n", + "classname", descriptor_->name()); printer->Indent(); printer->Indent(); @@ -143,10 +142,10 @@ void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( printer->Print("@java.lang.Override\n"); GenerateMethodSignature(printer, method, IS_CONCRETE); printer->Print( - " {\n" - " impl.$method$(controller, request, done);\n" - "}\n\n", - "method", UnderscoresToCamelCase(method)); + " {\n" + " impl.$method$(controller, request, done);\n" + "}\n\n", + "method", UnderscoresToCamelCase(method)); } printer->Outdent(); @@ -158,9 +157,9 @@ void ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod( void ImmutableServiceGenerator::GenerateNewReflectiveBlockingServiceMethod( io::Printer* printer) { printer->Print( - "public static com.google.protobuf.BlockingService\n" - " newReflectiveBlockingService(final BlockingInterface impl) {\n" - " return new com.google.protobuf.BlockingService() {\n"); + "public static com.google.protobuf.BlockingService\n" + " newReflectiveBlockingService(final BlockingInterface impl) {\n" + " return new com.google.protobuf.BlockingService() {\n"); printer->Indent(); printer->Indent(); @@ -192,19 +191,19 @@ std::string ImmutableServiceGenerator::GetOutput( void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { printer->Print( - "\n" - "public final void callMethod(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method,\n" - " com.google.protobuf.RpcController controller,\n" - " com.google.protobuf.Message request,\n" - " com.google.protobuf.RpcCallback<\n" - " com.google.protobuf.Message> done) {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.callMethod() given method descriptor for wrong \" +\n" - " \"service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n"); + "\n" + "public final void callMethod(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method,\n" + " com.google.protobuf.RpcController controller,\n" + " com.google.protobuf.Message request,\n" + " com.google.protobuf.RpcCallback<\n" + " com.google.protobuf.Message> done) {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.callMethod() given method descriptor for wrong \" +\n" + " \"service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n"); printer->Indent(); printer->Indent(); @@ -213,45 +212,45 @@ void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { std::map<std::string, std::string> vars; vars["index"] = StrCat(i); vars["method"] = UnderscoresToCamelCase(method); - vars["input"] = name_resolver_->GetImmutableClassName( - method->input_type()); + vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); - printer->Print(vars, - "case $index$:\n" - " this.$method$(controller, ($input$)request,\n" - " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" - " done));\n" - " return;\n"); + printer->Print( + vars, + "case $index$:\n" + " this.$method$(controller, ($input$)request,\n" + " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" + " done));\n" + " return;\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateCallBlockingMethod( io::Printer* printer) { printer->Print( - "\n" - "public final com.google.protobuf.Message callBlockingMethod(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method,\n" - " com.google.protobuf.RpcController controller,\n" - " com.google.protobuf.Message request)\n" - " throws com.google.protobuf.ServiceException {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.callBlockingMethod() given method descriptor for \" +\n" - " \"wrong service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n"); + "\n" + "public final com.google.protobuf.Message callBlockingMethod(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method,\n" + " com.google.protobuf.RpcController controller,\n" + " com.google.protobuf.Message request)\n" + " throws com.google.protobuf.ServiceException {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.callBlockingMethod() given method descriptor for \" +\n" + " \"wrong service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n"); printer->Indent(); printer->Indent(); @@ -260,44 +259,43 @@ void ImmutableServiceGenerator::GenerateCallBlockingMethod( std::map<std::string, std::string> vars; vars["index"] = StrCat(i); vars["method"] = UnderscoresToCamelCase(method); - vars["input"] = name_resolver_->GetImmutableClassName( - method->input_type()); + vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); printer->Print(vars, - "case $index$:\n" - " return impl.$method$(controller, ($input$)request);\n"); + "case $index$:\n" + " return impl.$method$(controller, ($input$)request);\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateGetPrototype(RequestOrResponse which, - io::Printer* printer) { + io::Printer* printer) { /* * TODO(cpovirk): The exception message says "Service.foo" when it may be * "BlockingService.foo." Consider fixing. */ printer->Print( - "public final com.google.protobuf.Message\n" - " get$request_or_response$Prototype(\n" - " com.google.protobuf.Descriptors.MethodDescriptor method) {\n" - " if (method.getService() != getDescriptor()) {\n" - " throw new java.lang.IllegalArgumentException(\n" - " \"Service.get$request_or_response$Prototype() given method \" +\n" - " \"descriptor for wrong service type.\");\n" - " }\n" - " switch(method.getIndex()) {\n", - "request_or_response", (which == REQUEST) ? "Request" : "Response"); + "public final com.google.protobuf.Message\n" + " get$request_or_response$Prototype(\n" + " com.google.protobuf.Descriptors.MethodDescriptor method) {\n" + " if (method.getService() != getDescriptor()) {\n" + " throw new java.lang.IllegalArgumentException(\n" + " \"Service.get$request_or_response$Prototype() given method \" +\n" + " \"descriptor for wrong service type.\");\n" + " }\n" + " switch(method.getIndex()) {\n", + "request_or_response", (which == REQUEST) ? "Request" : "Response"); printer->Indent(); printer->Indent(); @@ -310,45 +308,46 @@ void ImmutableServiceGenerator::GenerateGetPrototype(RequestOrResponse which, ? name_resolver_->GetImmutableClassName(method->input_type()) : GetOutput(method); printer->Print(vars, - "case $index$:\n" - " return $type$.getDefaultInstance();\n"); + "case $index$:\n" + " return $type$.getDefaultInstance();\n"); } printer->Print( - "default:\n" - " throw new java.lang.AssertionError(\"Can't get here.\");\n"); + "default:\n" + " throw new java.lang.AssertionError(\"Can't get here.\");\n"); printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n" - "\n"); + " }\n" + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Print( - "public static Stub newStub(\n" - " com.google.protobuf.RpcChannel channel) {\n" - " return new Stub(channel);\n" - "}\n" - "\n" - "public static final class Stub extends $classname$ implements Interface {" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "public static Stub newStub(\n" + " com.google.protobuf.RpcChannel channel) {\n" + " return new Stub(channel);\n" + "}\n" + "\n" + "public static final class Stub extends $classname$ implements Interface " + "{" + "\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); printer->Print( - "private Stub(com.google.protobuf.RpcChannel channel) {\n" - " this.channel = channel;\n" - "}\n" - "\n" - "private final com.google.protobuf.RpcChannel channel;\n" - "\n" - "public com.google.protobuf.RpcChannel getChannel() {\n" - " return channel;\n" - "}\n"); + "private Stub(com.google.protobuf.RpcChannel channel) {\n" + " this.channel = channel;\n" + "}\n" + "\n" + "private final com.google.protobuf.RpcChannel channel;\n" + "\n" + "public com.google.protobuf.RpcChannel getChannel() {\n" + " return channel;\n" + "}\n"); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); @@ -361,15 +360,15 @@ void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { vars["index"] = StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, - "channel.callMethod(\n" - " getDescriptor().getMethods().get($index$),\n" - " controller,\n" - " request,\n" - " $output$.getDefaultInstance(),\n" - " com.google.protobuf.RpcUtil.generalizeCallback(\n" - " done,\n" - " $output$.class,\n" - " $output$.getDefaultInstance()));\n"); + "channel.callMethod(\n" + " getDescriptor().getMethods().get($index$),\n" + " controller,\n" + " request,\n" + " $output$.getDefaultInstance(),\n" + " com.google.protobuf.RpcUtil.generalizeCallback(\n" + " done,\n" + " $output$.class,\n" + " $output$.getDefaultInstance()));\n"); printer->Outdent(); printer->Print("}\n"); @@ -377,20 +376,19 @@ void ImmutableServiceGenerator::GenerateStub(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Print( - "public static BlockingInterface newBlockingStub(\n" - " com.google.protobuf.BlockingRpcChannel channel) {\n" - " return new BlockingStub(channel);\n" - "}\n" - "\n"); + "public static BlockingInterface newBlockingStub(\n" + " com.google.protobuf.BlockingRpcChannel channel) {\n" + " return new BlockingStub(channel);\n" + "}\n" + "\n"); - printer->Print( - "public interface BlockingInterface {"); + printer->Print("public interface BlockingInterface {"); printer->Indent(); for (int i = 0; i < descriptor_->method_count(); i++) { @@ -401,19 +399,20 @@ void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); printer->Print( - "private static final class BlockingStub implements BlockingInterface {\n"); + "private static final class BlockingStub implements BlockingInterface " + "{\n"); printer->Indent(); printer->Print( - "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n" - " this.channel = channel;\n" - "}\n" - "\n" - "private final com.google.protobuf.BlockingRpcChannel channel;\n"); + "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n" + " this.channel = channel;\n" + "}\n" + "\n" + "private final com.google.protobuf.BlockingRpcChannel channel;\n"); for (int i = 0; i < descriptor_->method_count(); i++) { const MethodDescriptor* method = descriptor_->method(i); @@ -425,50 +424,49 @@ void ImmutableServiceGenerator::GenerateBlockingStub(io::Printer* printer) { vars["index"] = StrCat(i); vars["output"] = GetOutput(method); printer->Print(vars, - "return ($output$) channel.callBlockingMethod(\n" - " getDescriptor().getMethods().get($index$),\n" - " controller,\n" - " request,\n" - " $output$.getDefaultInstance());\n"); + "return ($output$) channel.callBlockingMethod(\n" + " getDescriptor().getMethods().get($index$),\n" + " controller,\n" + " request,\n" + " $output$.getDefaultInstance());\n"); printer->Outdent(); printer->Print( - "}\n" - "\n"); + "}\n" + "\n"); } printer->Outdent(); printer->Print("}\n"); } -void ImmutableServiceGenerator::GenerateMethodSignature(io::Printer* printer, - const MethodDescriptor* method, - IsAbstract is_abstract) { +void ImmutableServiceGenerator::GenerateMethodSignature( + io::Printer* printer, const MethodDescriptor* method, + IsAbstract is_abstract) { std::map<std::string, std::string> vars; vars["name"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : ""; printer->Print(vars, - "public $abstract$ void $name$(\n" - " com.google.protobuf.RpcController controller,\n" - " $input$ request,\n" - " com.google.protobuf.RpcCallback<$output$> done)"); + "public $abstract$ void $name$(\n" + " com.google.protobuf.RpcController controller,\n" + " $input$ request,\n" + " com.google.protobuf.RpcCallback<$output$> done)"); } void ImmutableServiceGenerator::GenerateBlockingMethodSignature( - io::Printer* printer, - const MethodDescriptor* method) { + io::Printer* printer, const MethodDescriptor* method) { std::map<std::string, std::string> vars; vars["method"] = UnderscoresToCamelCase(method); vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); vars["output"] = GetOutput(method); printer->Print(vars, - "\n" - "public $output$ $method$(\n" - " com.google.protobuf.RpcController controller,\n" - " $input$ request)\n" - " throws com.google.protobuf.ServiceException"); + "\n" + "public $output$ $method$(\n" + " com.google.protobuf.RpcController controller,\n" + " $input$ request)\n" + " throws com.google.protobuf.ServiceException"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_service.h b/src/google/protobuf/compiler/java/java_service.h index 24b0ccaa02..1c82c16c1c 100644 --- a/src/google/protobuf/compiler/java/java_service.h +++ b/src/google/protobuf/compiler/java/java_service.h @@ -83,7 +83,6 @@ class ImmutableServiceGenerator : public ServiceGenerator { virtual void Generate(io::Printer* printer); private: - // Generate the getDescriptorForType() method. void GenerateGetDescriptorForType(io::Printer* printer); diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.cc b/src/google/protobuf/compiler/java/java_shared_code_generator.cc index a7931f0568..f673e68301 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc @@ -52,8 +52,7 @@ SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file, const Options& options) : name_resolver_(new ClassNameResolver), file_(file), options_(options) {} -SharedCodeGenerator::~SharedCodeGenerator() { -} +SharedCodeGenerator::~SharedCodeGenerator() {} void SharedCodeGenerator::Generate( GeneratorContext* context, std::vector<std::string>* file_list, @@ -82,9 +81,9 @@ void SharedCodeGenerator::Generate( "filename", file_->name()); if (!java_package.empty()) { printer->Print( - "package $package$;\n" - "\n", - "package", java_package); + "package $package$;\n" + "\n", + "package", java_package); } PrintGeneratedAnnotation(printer.get(), '$', options_.annotate_code ? info_relative_path : ""); @@ -101,8 +100,8 @@ void SharedCodeGenerator::Generate( printer->Outdent(); printer->Outdent(); printer->Print( - " }\n" - "}\n"); + " }\n" + "}\n"); if (options_.annotate_code) { std::unique_ptr<io::ZeroCopyOutputStream> info_output( @@ -133,8 +132,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { std::string file_data; file_proto.SerializeToString(&file_data); - printer->Print( - "java.lang.String[] descriptorData = {\n"); + printer->Print("java.lang.String[] descriptorData = {\n"); printer->Indent(); // Limit the number of bytes per line. @@ -152,8 +150,8 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { printer->Print(" +\n"); } } - printer->Print("\"$data$\"", - "data", CEscape(file_data.substr(i, kBytesPerLine))); + printer->Print("\"$data$\"", "data", + CEscape(file_data.substr(i, kBytesPerLine))); } printer->Outdent(); @@ -179,20 +177,18 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { // ----------------------------------------------------------------- // Invoke internalBuildGeneratedFileFrom() to build the file. printer->Print( - "descriptor = com.google.protobuf.Descriptors.FileDescriptor\n" - " .internalBuildGeneratedFileFrom(descriptorData,\n"); + "descriptor = com.google.protobuf.Descriptors.FileDescriptor\n" + " .internalBuildGeneratedFileFrom(descriptorData,\n"); printer->Print( - " new com.google.protobuf.Descriptors.FileDescriptor[] {\n"); + " new com.google.protobuf.Descriptors.FileDescriptor[] {\n"); for (int i = 0; i < dependencies.size(); i++) { const std::string& dependency = dependencies[i].second; - printer->Print( - " $dependency$.getDescriptor(),\n", - "dependency", dependency); + printer->Print(" $dependency$.getDescriptor(),\n", "dependency", + dependency); } - printer->Print( - " });\n"); + printer->Print(" });\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_shared_code_generator.h b/src/google/protobuf/compiler/java/java_shared_code_generator.h index 2f09c3a9d9..7c79abe2e1 100644 --- a/src/google/protobuf/compiler/java/java_shared_code_generator.h +++ b/src/google/protobuf/compiler/java/java_shared_code_generator.h @@ -82,7 +82,6 @@ class SharedCodeGenerator { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SharedCodeGenerator); }; - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/java_string_field.cc b/src/google/protobuf/compiler/java/java_string_field.cc index a20d7ef450..d68f178ca8 100644 --- a/src/google/protobuf/compiler/java/java_string_field.cc +++ b/src/google/protobuf/compiler/java/java_string_field.cc @@ -79,17 +79,16 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, " if (value == null) {\n" " throw new NullPointerException();\n" " }\n"; - (*variables)["writeString"] = - "com.google.protobuf.GeneratedMessage" + GeneratedCodeVersionSuffix() + - ".writeString"; - (*variables)["computeStringSize"] = - "com.google.protobuf.GeneratedMessage" + GeneratedCodeVersionSuffix() + - ".computeStringSize"; + (*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" + + GeneratedCodeVersionSuffix() + ".writeString"; + (*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" + + GeneratedCodeVersionSuffix() + + ".computeStringSize"; // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["on_changed"] = "onChanged();"; if (SupportFieldPresence(descriptor->file())) { @@ -137,14 +136,14 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // =================================================================== -ImmutableStringFieldGenerator:: -ImmutableStringFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +ImmutableStringFieldGenerator::ImmutableStringFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); @@ -192,275 +191,275 @@ int ImmutableStringFieldGenerator::GetNumBitsForBuilder() const { // For single fields, the logic for this is done inside the generated code. For // repeated fields, the logic is done in LazyStringArrayList and // UnmodifiableLazyStringList. -void ImmutableStringFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$();\n"); + "$deprecation$java.lang.String get$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes();\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes();\n"); } -void ImmutableStringFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private volatile java.lang.Object $name$_;\n"); +void ImmutableStringFieldGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private volatile java.lang.Object $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof java.lang.String) {\n" - " return (java.lang.String) ref;\n" - " } else {\n" - " com.google.protobuf.ByteString bs = \n" - " (com.google.protobuf.ByteString) ref;\n" + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof java.lang.String) {\n" + " return (java.lang.String) ref;\n" + " } else {\n" + " com.google.protobuf.ByteString bs = \n" + " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $name$_ = s;\n"); + printer->Print(variables_, " $name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " }\n" - "}\n"); + " return s;\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof java.lang.String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " $name$_ = b;\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof java.lang.String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " $name$_ = b;\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { printer->Print(variables_, - "private java.lang.Object $name$_ $default_init$;\n"); + "private java.lang.Object $name$_ $default_init$;\n"); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_builder$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_builder$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (!(ref instanceof java.lang.String)) {\n" - " com.google.protobuf.ByteString bs =\n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n"); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (!(ref instanceof java.lang.String)) {\n" + " com.google.protobuf.ByteString bs =\n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $name$_ = s;\n"); + printer->Print(variables_, " $name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " } else {\n" - " return (java.lang.String) ref;\n" - " }\n" - "}\n"); + " return s;\n" + " } else {\n" + " return (java.lang.String) ref;\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref = $name$_;\n" - " if (ref instanceof String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " $name$_ = b;\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref = $name$_;\n" + " if (ref instanceof String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " $name$_ = b;\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $clear_has_field_bit_builder$\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $clear_has_field_bit_builder$\n"); printer->Annotate("{", "}", descriptor_); // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. printer->Print(variables_, - " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); + " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); printer->Print(variables_, - " $on_changed$\n" - " return this;\n" - "}\n"); + " $on_changed$\n" + " return this;\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_has_field_bit_builder$\n" - " $name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " $set_has_field_bit_builder$\n" + " $name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateFieldBuilderInitializationCode( + io::Printer* printer) const { // noop for primitives } -void ImmutableStringFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $default$;\n"); } -void ImmutableStringFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $default$;\n" - "$clear_has_field_bit_builder$\n"); + "$name$_ = $default$;\n" + "$clear_has_field_bit_builder$\n"); } -void ImmutableStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { // Allow a slight breach of abstraction here in order to avoid forcing // all string fields to Strings when copying fields from a Message. printer->Print(variables_, - "if (other.has$capitalized_name$()) {\n" - " $set_has_field_bit_builder$\n" - " $name$_ = other.$name$_;\n" - " $on_changed$\n" - "}\n"); + "if (other.has$capitalized_name$()) {\n" + " $set_has_field_bit_builder$\n" + " $name$_ = other.$name$_;\n" + " $on_changed$\n" + "}\n"); } else { printer->Print(variables_, - "if (!other.get$capitalized_name$().isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $on_changed$\n" - "}\n"); + "if (!other.get$capitalized_name$().isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $on_changed$\n" + "}\n"); } } -void ImmutableStringFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { printer->Print(variables_, - "if ($get_has_field_bit_from_local$) {\n" - " $set_has_field_bit_to_local$;\n" - "}\n"); + "if ($get_has_field_bit_from_local$) {\n" + " $set_has_field_bit_to_local$;\n" + "}\n"); } - printer->Print(variables_, - "result.$name$_ = $name$_;\n"); + printer->Print(variables_, "result.$name$_ = $name$_;\n"); } -void ImmutableStringFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); + "java.lang.String s = input.readStringRequireUtf8();\n" + "$set_has_field_bit_message$\n" + "$name$_ = s;\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n" - "$set_has_field_bit_message$\n" - "$name$_ = bs;\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n" + "$set_has_field_bit_message$\n" + "$name$_ = bs;\n"); } } -void ImmutableStringFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { // noop for strings. } -void ImmutableStringFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " $writeString$(output, $number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " $writeString$(output, $number$, $name$_);\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += $computeStringSize$($number$, $name$_);\n" - "}\n"); + "if ($is_field_present_message$) {\n" + " size += $computeStringSize$($number$, $name$_);\n" + "}\n"); } -void ImmutableStringFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { printer->Print(variables_, - "if (!get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$())) return false;\n"); + "if (!get$capitalized_name$()\n" + " .equals(other.get$capitalized_name$())) return false;\n"); } -void ImmutableStringFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { +void ImmutableStringFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print(variables_, "hash = (37 * hash) + $constant_name$;\n"); printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); + "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); } std::string ImmutableStringFieldGenerator::GetBoxedType() const { @@ -469,257 +468,258 @@ std::string ImmutableStringFieldGenerator::GetBoxedType() const { // =================================================================== -ImmutableStringOneofFieldGenerator:: -ImmutableStringOneofFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : ImmutableStringFieldGenerator( - descriptor, messageBitIndex, builderBitIndex, context) { +ImmutableStringOneofFieldGenerator::ImmutableStringOneofFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : ImmutableStringFieldGenerator(descriptor, messageBitIndex, + builderBitIndex, context) { const OneofGeneratorInfo* info = context->GetOneofGeneratorInfo(descriptor->containing_oneof()); SetCommonOneofVariables(descriptor, info, &variables_); } -ImmutableStringOneofFieldGenerator:: -~ImmutableStringOneofFieldGenerator() {} +ImmutableStringOneofFieldGenerator::~ImmutableStringOneofFieldGenerator() {} -void ImmutableStringOneofFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); + WriteFieldDocComment(printer, descriptor_); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof java.lang.String) {\n" - " return (java.lang.String) ref;\n" - " } else {\n" - " com.google.protobuf.ByteString bs = \n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n"); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof java.lang.String) {\n" + " return (java.lang.String) ref;\n" + " } else {\n" + " com.google.protobuf.ByteString bs = \n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " return s;\n" - " }\n" - "}\n"); + " return s;\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof java.lang.String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = b;\n" - " }\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof java.lang.String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = b;\n" + " }\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringOneofFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (!(ref instanceof java.lang.String)) {\n" - " com.google.protobuf.ByteString bs =\n" - " (com.google.protobuf.ByteString) ref;\n" - " java.lang.String s = bs.toStringUtf8();\n" - " if ($has_oneof_case_message$) {\n"); + printer->Print( + variables_, + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (!(ref instanceof java.lang.String)) {\n" + " com.google.protobuf.ByteString bs =\n" + " (com.google.protobuf.ByteString) ref;\n" + " java.lang.String s = bs.toStringUtf8();\n" + " if ($has_oneof_case_message$) {\n"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " $oneof_name$_ = s;\n"); + printer->Print(variables_, " $oneof_name$_ = s;\n"); } else { printer->Print(variables_, - " if (bs.isValidUtf8()) {\n" - " $oneof_name$_ = s;\n" - " }\n"); + " if (bs.isValidUtf8()) {\n" + " $oneof_name$_ = s;\n" + " }\n"); } printer->Print(variables_, - " }\n" - " return s;\n" - " } else {\n" - " return (java.lang.String) ref;\n" - " }\n" - "}\n"); + " }\n" + " return s;\n" + " } else {\n" + " return (java.lang.String) ref;\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.Object ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = $oneof_name$_;\n" - " }\n" - " if (ref instanceof String) {\n" - " com.google.protobuf.ByteString b = \n" - " com.google.protobuf.ByteString.copyFromUtf8(\n" - " (java.lang.String) ref);\n" - " if ($has_oneof_case_message$) {\n" - " $oneof_name$_ = b;\n" - " }\n" - " return b;\n" - " } else {\n" - " return (com.google.protobuf.ByteString) ref;\n" - " }\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.Object ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = $oneof_name$_;\n" + " }\n" + " if (ref instanceof String) {\n" + " com.google.protobuf.ByteString b = \n" + " com.google.protobuf.ByteString.copyFromUtf8(\n" + " (java.lang.String) ref);\n" + " if ($has_oneof_case_message$) {\n" + " $oneof_name$_ = b;\n" + " }\n" + " return b;\n" + " } else {\n" + " return (com.google.protobuf.ByteString) ref;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " $on_changed$\n" - " }\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " $on_changed$\n" + " }\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // Allow a slight breach of abstraction here in order to avoid forcing // all string fields to Strings when copying fields from a Message. printer->Print(variables_, - "$set_oneof_case_message$;\n" - "$oneof_name$_ = other.$oneof_name$_;\n" - "$on_changed$\n"); + "$set_oneof_case_message$;\n" + "$oneof_name$_ = other.$oneof_name$_;\n" + "$on_changed$\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " result.$oneof_name$_ = $oneof_name$_;\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " result.$oneof_name$_ = $oneof_name$_;\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); + "java.lang.String s = input.readStringRequireUtf8();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = s;\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = bs;\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n" + "$set_oneof_case_message$;\n" + "$oneof_name$_ = bs;\n"); } } -void ImmutableStringOneofFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " $writeString$(output, $number$, $oneof_name$_);\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " $writeString$(output, $number$, $oneof_name$_);\n" + "}\n"); } -void ImmutableStringOneofFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void ImmutableStringOneofFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += $computeStringSize$($number$, $oneof_name$_);\n" - "}\n"); + "if ($has_oneof_case_message$) {\n" + " size += $computeStringSize$($number$, $oneof_name$_);\n" + "}\n"); } // =================================================================== -RepeatedImmutableStringFieldGenerator:: -RepeatedImmutableStringFieldGenerator(const FieldDescriptor* descriptor, - int messageBitIndex, - int builderBitIndex, - Context* context) - : descriptor_(descriptor), messageBitIndex_(messageBitIndex), - builderBitIndex_(builderBitIndex), context_(context), - name_resolver_(context->GetNameResolver()) { +RepeatedImmutableStringFieldGenerator::RepeatedImmutableStringFieldGenerator( + const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, + Context* context) + : descriptor_(descriptor), + messageBitIndex_(messageBitIndex), + builderBitIndex_(builderBitIndex), + context_(context), + name_resolver_(context->GetNameResolver()) { SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, context->GetFieldGeneratorInfo(descriptor), name_resolver_, &variables_); } RepeatedImmutableStringFieldGenerator:: -~RepeatedImmutableStringFieldGenerator() {} + ~RepeatedImmutableStringFieldGenerator() {} int RepeatedImmutableStringFieldGenerator::GetNumBitsForMessage() const { return 0; @@ -729,67 +729,69 @@ int RepeatedImmutableStringFieldGenerator::GetNumBitsForBuilder() const { return 1; } -void RepeatedImmutableStringFieldGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - // NOTE: the same method in the implementation class actually returns - // com.google.protobuf.ProtocolStringList (a subclass of List). It's - // changed between protobuf 2.5.0 release and protobuf 2.6.1 release. - // To retain binary compatibility with both 2.5.0 and 2.6.1 generated - // code, we make this interface method return List so both methods - // with different return types exist in the compiled byte code. - "$deprecation$java.util.List<java.lang.String>\n" - " get$capitalized_name$List();\n"); + printer->Print( + variables_, + // NOTE: the same method in the implementation class actually returns + // com.google.protobuf.ProtocolStringList (a subclass of List). It's + // changed between protobuf 2.5.0 release and protobuf 2.6.1 release. + // To retain binary compatibility with both 2.5.0 and 2.6.1 generated + // code, we make this interface method return List so both methods + // with different return types exist in the compiled byte code. + "$deprecation$java.util.List<java.lang.String>\n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); + printer->Print( + variables_, + "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index);\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes(int index);\n"); } - -void RepeatedImmutableStringFieldGenerator:: -GenerateMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateMembers( + io::Printer* printer) const { printer->Print(variables_, - "private com.google.protobuf.LazyStringList $name$_;\n"); + "private com.google.protobuf.LazyStringList $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "$deprecation$public com.google.protobuf.ProtocolStringList\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return $name$_.getByteString(index);\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return $name$_.getByteString(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuilderMembers( + io::Printer* printer) const { // One field is the list and the bit field keeps track of whether the // list is immutable. If it's immutable, the invariant is that it must // either an instance of Collections.emptyList() or it's an ArrayList @@ -799,234 +801,237 @@ GenerateBuilderMembers(io::Printer* printer) const { // memory allocations. Note, immutable is a strong guarantee here -- not // just that the list cannot be modified via the reference but that the // list can never be modified. - printer->Print(variables_, - "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n"); + printer->Print( + variables_, + "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n"); - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$get_mutable_bit_builder$) {\n" - " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n" - " $set_mutable_bit_builder$;\n" - " }\n" - "}\n"); - - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$get_mutable_bit_builder$) {\n" + " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n" + " $set_mutable_bit_builder$;\n" + " }\n" + "}\n"); + + // Note: We return an unmodifiable list because otherwise the caller + // could hold on to the returned list and modify it after the message + // has been built, thus mutating the message which is supposed to be + // immutable. WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " ${$get$capitalized_name$List$}$() {\n" - " return $name$_.getUnmodifiableView();\n" - "}\n"); + "$deprecation$public com.google.protobuf.ProtocolStringList\n" + " ${$get$capitalized_name$List$}$() {\n" + " return $name$_.getUnmodifiableView();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return $name$_.getByteString(index);\n" - "}\n"); + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return $name$_.getByteString(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<java.lang.String> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" - " values, $name$_);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<java.lang.String> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" + " values, $name$_);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " $name$_ = $empty_list$;\n" - " $clear_mutable_bit_builder$;\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " $name$_ = $empty_list$;\n" + " $clear_mutable_bit_builder$;\n" + " $on_changed$\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + printer->Print( + variables_, + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - " $on_changed$\n" - " return this;\n" - "}\n"); + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + " $on_changed$\n" + " return this;\n" + "}\n"); } void RepeatedImmutableStringFieldGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives } -void RepeatedImmutableStringFieldGenerator:: -GenerateInitializationCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateInitializationCode( + io::Printer* printer) const { printer->Print(variables_, "$name$_ = $empty_list$;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuilderClearCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuilderClearCode( + io::Printer* printer) const { printer->Print(variables_, - "$name$_ = $empty_list$;\n" - "$clear_mutable_bit_builder$;\n"); + "$name$_ = $empty_list$;\n" + "$clear_mutable_bit_builder$;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateMergingCode( + io::Printer* printer) const { // The code below does two optimizations: // 1. If the other list is empty, there's nothing to do. This ensures we // don't allocate a new array if we already have an immutable one. // 2. If the other list is non-empty and our current list is empty, we can // reuse the other list which is guaranteed to be immutable. printer->Print(variables_, - "if (!other.$name$_.isEmpty()) {\n" - " if ($name$_.isEmpty()) {\n" - " $name$_ = other.$name$_;\n" - " $clear_mutable_bit_builder$;\n" - " } else {\n" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.addAll(other.$name$_);\n" - " }\n" - " $on_changed$\n" - "}\n"); + "if (!other.$name$_.isEmpty()) {\n" + " if ($name$_.isEmpty()) {\n" + " $name$_ = other.$name$_;\n" + " $clear_mutable_bit_builder$;\n" + " } else {\n" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.addAll(other.$name$_);\n" + " }\n" + " $on_changed$\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateBuildingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateBuildingCode( + io::Printer* printer) const { // The code below ensures that the result has an immutable list. If our // list is immutable, we can just reuse it. If not, we make it immutable. printer->Print(variables_, - "if ($get_mutable_bit_builder$) {\n" - " $name$_ = $name$_.getUnmodifiableView();\n" - " $clear_mutable_bit_builder$;\n" - "}\n" - "result.$name$_ = $name$_;\n"); + "if ($get_mutable_bit_builder$) {\n" + " $name$_ = $name$_.getUnmodifiableView();\n" + " $clear_mutable_bit_builder$;\n" + "}\n" + "result.$name$_ = $name$_;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateParsingCode( + io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n"); + "java.lang.String s = input.readStringRequireUtf8();\n"); } else { printer->Print(variables_, - "com.google.protobuf.ByteString bs = input.readBytes();\n"); + "com.google.protobuf.ByteString bs = input.readBytes();\n"); } printer->Print(variables_, - "if (!$get_mutable_bit_parser$) {\n" - " $name$_ = new com.google.protobuf.LazyStringArrayList();\n" - " $set_mutable_bit_parser$;\n" - "}\n"); + "if (!$get_mutable_bit_parser$) {\n" + " $name$_ = new com.google.protobuf.LazyStringArrayList();\n" + " $set_mutable_bit_parser$;\n" + "}\n"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "$name$_.add(s);\n"); + printer->Print(variables_, "$name$_.add(s);\n"); } else { - printer->Print(variables_, - "$name$_.add(bs);\n"); + printer->Print(variables_, "$name$_.add(bs);\n"); } } -void RepeatedImmutableStringFieldGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateParsingDoneCode( + io::Printer* printer) const { printer->Print(variables_, - "if ($get_mutable_bit_parser$) {\n" - " $name$_ = $name$_.getUnmodifiableView();\n" - "}\n"); + "if ($get_mutable_bit_parser$) {\n" + " $name$_ = $name$_.getUnmodifiableView();\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateSerializationCode( + io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " $writeString$(output, $number$, $name$_.getRaw(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " $writeString$(output, $number$, $name$_.getRaw(i));\n" + "}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { +void RepeatedImmutableStringFieldGenerator::GenerateSerializedSizeCode( + io::Printer* printer) const { printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); + "{\n" + " int dataSize = 0;\n"); printer->Indent(); printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n" - "}\n"); + "for (int i = 0; i < $name$_.size(); i++) {\n" + " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n" + "}\n"); - printer->Print( - "size += dataSize;\n"); + printer->Print("size += dataSize;\n"); printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); + "size += $tag_size$ * get$capitalized_name$List().size();\n"); printer->Outdent(); printer->Print("}\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "if (!get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List())) return false;\n"); +void RepeatedImmutableStringFieldGenerator::GenerateEqualsCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (!get$capitalized_name$List()\n" + " .equals(other.get$capitalized_name$List())) return false;\n"); } -void RepeatedImmutableStringFieldGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableStringFieldGenerator::GenerateHashCode( + io::Printer* printer) const { + printer->Print( + variables_, + "if (get$capitalized_name$Count() > 0) {\n" + " hash = (37 * hash) + $constant_name$;\n" + " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" + "}\n"); } std::string RepeatedImmutableStringFieldGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_string_field_lite.cc b/src/google/protobuf/compiler/java/java_string_field_lite.cc index c1588728c1..5c94cb30cd 100644 --- a/src/google/protobuf/compiler/java/java_string_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_string_field_lite.cc @@ -83,8 +83,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported // by the proto compiler - (*variables)["deprecation"] = descriptor->options().deprecated() - ? "@java.lang.Deprecated " : ""; + (*variables)["deprecation"] = + descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (SupportFieldPresence(descriptor->file())) { @@ -161,234 +161,164 @@ int ImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { // TODO(dweis): Consider dropping all of the *Bytes() methods. They really // shouldn't be necessary or used on devices. -void ImmutableStringFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); + "$deprecation$boolean has$capitalized_name$();\n"); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$();\n"); + "$deprecation$java.lang.String get$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes();\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes();\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private java.lang.String $name$_;\n"); +void ImmutableStringFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print(variables_, "private java.lang.String $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $get_has_field_bit_message$;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $get_has_field_bit_message$;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return $name$_;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return $name$_;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_has_field_bit_message$\n" - " $name$_ = value;\n" - "}\n"); + "private void set$capitalized_name$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_has_field_bit_message$\n" + " $name$_ = value;\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $clear_has_field_bit_message$\n" - // The default value is not a simple literal so we want to avoid executing - // it multiple times. Instead, get the default out of the default instance. - " $name$_ = getDefaultInstance().get$capitalized_name$();\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $clear_has_field_bit_message$\n" + // The default value is not a simple literal so we want to + // avoid executing it multiple times. Instead, get the default + // out of the default instance. + " $name$_ = getDefaultInstance().get$capitalized_name$();\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$Bytes(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + "private void set$capitalized_name$Bytes(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_has_field_bit_message$\n" - " $name$_ = value.toStringUtf8();\n" - "}\n"); + " $set_has_field_bit_message$\n" + " $name$_ = value.toStringUtf8();\n" + "}\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return instance.get$capitalized_name$Bytes();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return instance.get$capitalized_name$Bytes();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { - // noop for strings -} - - -void ImmutableStringFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = $default$;\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { +void ImmutableStringFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); if (SupportFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "$name$_ = visitor.visitString(\n" - " has$capitalized_name$(), $name$_,\n" - " other.has$capitalized_name$(), other.$name$_);\n"); - } else { - printer->Print(variables_, - "$name$_ = visitor.visitString(!$name$_.isEmpty(), $name$_,\n" - " !other.$name$_.isEmpty(), other.$name$_);\n"); + WriteIntToUtf16CharSequence(messageBitIndex_, output); } + printer->Print(variables_, "\"$name$_\",\n"); } -void ImmutableStringFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - // noop for scalars -} - -void ImmutableStringFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n" - "$set_has_field_bit_message$\n" - "$name$_ = s;\n"); - } -} - -void ImmutableStringFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - // noop for strings -} - -void ImmutableStringFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " output.writeString($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($is_field_present_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeStringSize($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$()\n" - " .equals(other.get$capitalized_name$());\n"); -} - -void ImmutableStringFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "hash = (37 * hash) + $constant_name$;\n"); - printer->Print(variables_, - "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); +void ImmutableStringFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = $default$;\n"); } std::string ImmutableStringFieldLiteGenerator::GetBoxedType() const { @@ -406,190 +336,153 @@ ImmutableStringOneofFieldLiteGenerator::ImmutableStringOneofFieldLiteGenerator( } ImmutableStringOneofFieldLiteGenerator:: -~ImmutableStringOneofFieldLiteGenerator() {} + ~ImmutableStringOneofFieldLiteGenerator() {} -void ImmutableStringOneofFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - printer->Annotate("{", "}", descriptor_); + WriteFieldDocComment(printer, descriptor_); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " java.lang.String ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = (java.lang.String) $oneof_name$_;\n" - " }\n" - " return ref;\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " java.lang.String ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = (java.lang.String) $oneof_name$_;\n" + " }\n" + " return ref;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " java.lang.String ref $default_init$;\n" - " if ($has_oneof_case_message$) {\n" - " ref = (java.lang.String) $oneof_name$_;\n" - " }\n" - " return com.google.protobuf.ByteString.copyFromUtf8(ref);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " java.lang.String ref $default_init$;\n" + " if ($has_oneof_case_message$) {\n" + " ref = (java.lang.String) $oneof_name$_;\n" + " }\n" + " return com.google.protobuf.ByteString.copyFromUtf8(ref);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - "$null_check$" - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value;\n" - "}\n"); + "private void ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + "$null_check$" + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void ${$clear$capitalized_name$$}$() {\n" - " if ($has_oneof_case_message$) {\n" - " $clear_oneof_case_message$;\n" - " $oneof_name$_ = null;\n" - " }\n" - "}\n"); + "private void ${$clear$capitalized_name$$}$() {\n" + " if ($has_oneof_case_message$) {\n" + " $clear_oneof_case_message$;\n" + " $oneof_name$_ = null;\n" + " }\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + "private void ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " $set_oneof_case_message$;\n" - " $oneof_name$_ = value.toStringUtf8();\n" - "}\n"); + " $set_oneof_case_message$;\n" + " $oneof_name$_ = value.toStringUtf8();\n" + "}\n"); } +void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output); +} -void ImmutableStringOneofFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void ImmutableStringOneofFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" - " return instance.get$capitalized_name$();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" + " return instance.get$capitalized_name$();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$() {\n" - " return instance.get$capitalized_name$Bytes();\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$() {\n" + " return instance.get$capitalized_name$Bytes();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } -void ImmutableStringOneofFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$oneof_name$_ = visitor.visitOneofString(\n" - " $has_oneof_case_message$, $oneof_name$_, other.$oneof_name$_);\n"); -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n" - "$set_oneof_case_message$;\n" - "$oneof_name$_ = s;\n"); - } -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " output.writeString($number$, get$capitalized_name$());\n" - "}\n"); -} - -void ImmutableStringOneofFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "if ($has_oneof_case_message$) {\n" - " size += com.google.protobuf.CodedOutputStream\n" - " .computeStringSize($number$, get$capitalized_name$());\n" - "}\n"); -} - // =================================================================== RepeatedImmutableStringFieldLiteGenerator:: @@ -606,309 +499,225 @@ RepeatedImmutableStringFieldLiteGenerator:: } RepeatedImmutableStringFieldLiteGenerator:: -~RepeatedImmutableStringFieldLiteGenerator() {} + ~RepeatedImmutableStringFieldLiteGenerator() {} int RepeatedImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { return 0; } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateInterfaceMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<java.lang.String>\n" - " get$capitalized_name$List();\n"); + "$deprecation$java.util.List<java.lang.String>\n" + " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int get$capitalized_name$Count();\n"); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); + printer->Print( + variables_, + "$deprecation$java.lang.String get$capitalized_name$(int index);\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index);\n"); + "$deprecation$com.google.protobuf.ByteString\n" + " get$capitalized_name$Bytes(int index);\n"); } - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private com.google.protobuf.Internal.ProtobufList<java.lang.String> " - "$name$_;\n"); +void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers( + io::Printer* printer) const { + printer->Print( + variables_, + "private com.google.protobuf.Internal.ProtobufList<java.lang.String> " + "$name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<java.lang.String> " - "${$get$capitalized_name$List$}$() {\n" - " return $name$_;\n" // note: unmodifiable list - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<java.lang.String> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return $name$_.size();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return $name$_.size();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return $name$_.get(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return $name$_.get(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return com.google.protobuf.ByteString.copyFromUtf8(\n" - " $name$_.get(index));\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return com.google.protobuf.ByteString.copyFromUtf8(\n" + " $name$_.get(index));\n" + "}\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "private void ensure$capitalized_name$IsMutable() {\n" - " if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - " }\n" - "}\n"); + printer->Print( + variables_, + "private void ensure$capitalized_name$IsMutable() {\n" + " if (!$is_mutable$) {\n" + " $name$_ =\n" + " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" + " }\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" - " int index, java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.set(index, value);\n" - "}\n"); + "private void set$capitalized_name$(\n" + " int index, java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.set(index, value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$(\n" - " java.lang.String value) {\n" - "$null_check$" - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value);\n" - "}\n"); + "private void add$capitalized_name$(\n" + " java.lang.String value) {\n" + "$null_check$" + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void addAll$capitalized_name$(\n" - " java.lang.Iterable<java.lang.String> values) {\n" - " ensure$capitalized_name$IsMutable();\n" - " com.google.protobuf.AbstractMessageLite.addAll(\n" - " values, $name$_);\n" - "}\n"); + "private void addAll$capitalized_name$(\n" + " java.lang.Iterable<java.lang.String> values) {\n" + " ensure$capitalized_name$IsMutable();\n" + " com.google.protobuf.AbstractMessageLite.addAll(\n" + " values, $name$_);\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" - " $name$_ = $empty_list$;\n" - "}\n"); + "private void clear$capitalized_name$() {\n" + " $name$_ = $empty_list$;\n" + "}\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void add$capitalized_name$Bytes(\n" - " com.google.protobuf.ByteString value) {\n" - "$null_check$"); + "private void add$capitalized_name$Bytes(\n" + " com.google.protobuf.ByteString value) {\n" + "$null_check$"); if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - " checkByteStringIsUtf8(value);\n"); + printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); } printer->Print(variables_, - " ensure$capitalized_name$IsMutable();\n" - " $name$_.add(value.toStringUtf8());\n" - "}\n"); + " ensure$capitalized_name$IsMutable();\n" + " $name$_.add(value.toStringUtf8());\n" + "}\n"); } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateBuilderMembers(io::Printer* printer) const { +void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers( + io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<java.lang.String>\n" - " ${$get$capitalized_name$List$}$() {\n" - " return java.util.Collections.unmodifiableList(\n" - " instance.get$capitalized_name$List());\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.util.List<java.lang.String>\n" + " ${$get$capitalized_name$List$}$() {\n" + " return java.util.Collections.unmodifiableList(\n" + " instance.get$capitalized_name$List());\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" - " return instance.get$capitalized_name$Count();\n" - "}\n"); + printer->Print( + variables_, + "@java.lang.Override\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" + " return instance.get$capitalized_name$Count();\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public java.lang.String " - "${$get$capitalized_name$$}$(int index) {\n" - " return instance.get$capitalized_name$(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" + " return instance.get$capitalized_name$(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "@java.lang.Override\n" - "$deprecation$public com.google.protobuf.ByteString\n" - " ${$get$capitalized_name$Bytes$}$(int index) {\n" - " return instance.get$capitalized_name$Bytes(index);\n" - "}\n"); + "@java.lang.Override\n" + "$deprecation$public com.google.protobuf.ByteString\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" + " return instance.get$capitalized_name$Bytes(index);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" - " int index, java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.set$capitalized_name$(index, value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" + " int index, java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.set$capitalized_name$(index, value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" - " java.lang.String value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$(value);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" + " java.lang.String value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" - " java.lang.Iterable<java.lang.String> values) {\n" - " copyOnWrite();\n" - " instance.addAll$capitalized_name$(values);\n" - " return this;\n" - "}\n"); + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" + " java.lang.Iterable<java.lang.String> values) {\n" + " copyOnWrite();\n" + " instance.addAll$capitalized_name$(values);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" - " copyOnWrite();\n" - " instance.clear$capitalized_name$();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" + " copyOnWrite();\n" + " instance.clear$capitalized_name$();\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" - " com.google.protobuf.ByteString value) {\n" - " copyOnWrite();\n" - " instance.add$capitalized_name$Bytes(value);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" + " com.google.protobuf.ByteString value) {\n" + " copyOnWrite();\n" + " instance.add$capitalized_name$Bytes(value);\n" + " return this;\n" + "}\n"); printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableStringFieldLiteGenerator:: -GenerateFieldBuilderInitializationCode(io::Printer* printer) const { + GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for strings } - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateInitializationCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_ = $empty_list$;\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateVisitCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_= visitor.visitList($name$_, other.$name$_);\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.makeImmutable();\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateParsingCode(io::Printer* printer) const { - if (CheckUtf8(descriptor_)) { - printer->Print(variables_, - "java.lang.String s = input.readStringRequireUtf8();\n"); - } else { - // Lite runtime should attempt to reduce allocations by attempting to - // construct the string directly from the input stream buffer. This avoids - // spurious intermediary ByteString allocations, cutting overall allocations - // in half. - printer->Print(variables_, - "java.lang.String s = input.readString();\n"); - } - printer->Print(variables_, - "if (!$is_mutable$) {\n" - " $name$_ =\n" - " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n" - "}\n"); - printer->Print(variables_, - "$name$_.add(s);\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateParsingDoneCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($is_mutable$) {\n" - " $name$_.makeImmutable();\n" - "}\n"); +void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo( + io::Printer* printer, std::vector<uint16>* output) const { + WriteIntToUtf16CharSequence(descriptor_->number(), output); + WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), + output); + printer->Print(variables_, "\"$name$_\",\n"); } -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by serializing the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " output.writeString($number$, $name$_.get(i));\n" - "}\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - // Lite runtime should reduce allocations by computing on the string directly. - // This avoids spurious intermediary ByteString allocations, cutting overall - // allocations in half. - printer->Print(variables_, - "{\n" - " int dataSize = 0;\n"); - printer->Indent(); - - printer->Print(variables_, - "for (int i = 0; i < $name$_.size(); i++) {\n" - " dataSize += com.google.protobuf.CodedOutputStream\n" - " .computeStringSizeNoTag($name$_.get(i));\n" - "}\n"); - - printer->Print( - "size += dataSize;\n"); - - - printer->Print(variables_, - "size += $tag_size$ * get$capitalized_name$List().size();\n"); - - printer->Outdent(); - printer->Print("}\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "result = result && get$capitalized_name$List()\n" - " .equals(other.get$capitalized_name$List());\n"); -} - -void RepeatedImmutableStringFieldLiteGenerator:: -GenerateHashCode(io::Printer* printer) const { - printer->Print(variables_, - "if (get$capitalized_name$Count() > 0) {\n" - " hash = (37 * hash) + $constant_name$;\n" - " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" - "}\n"); +void RepeatedImmutableStringFieldLiteGenerator::GenerateInitializationCode( + io::Printer* printer) const { + printer->Print(variables_, "$name$_ = $empty_list$;\n"); } std::string RepeatedImmutableStringFieldLiteGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/java/java_string_field_lite.h b/src/google/protobuf/compiler/java/java_string_field_lite.h index 89d5c1ae7b..960f4689cc 100644 --- a/src/google/protobuf/compiler/java/java_string_field_lite.h +++ b/src/google/protobuf/compiler/java/java_string_field_lite.h @@ -70,16 +70,8 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; void GenerateInitializationCode(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateParsingDoneCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; @@ -104,11 +96,8 @@ class ImmutableStringOneofFieldLiteGenerator private: void GenerateMembers(io::Printer* printer) const; void GenerateBuilderMembers(io::Printer* printer) const; - void GenerateVisitCode(io::Printer* printer) const; - void GenerateParsingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableStringOneofFieldLiteGenerator); }; @@ -135,7 +124,8 @@ class RepeatedImmutableStringFieldLiteGenerator void GenerateFieldBuilderInitializationCode(io::Printer* printer) const; void GenerateEqualsCode(io::Printer* printer) const; void GenerateHashCode(io::Printer* printer) const; - + void GenerateFieldInfo(io::Printer* printer, + std::vector<uint16>* output) const; std::string GetBoxedType() const; diff --git a/src/google/protobuf/compiler/js/js_generator.cc b/src/google/protobuf/compiler/js/js_generator.cc index e00637e6ea..b41e63a2ae 100644 --- a/src/google/protobuf/compiler/js/js_generator.cc +++ b/src/google/protobuf/compiler/js/js_generator.cc @@ -60,64 +60,18 @@ namespace js { // Sorted list of JavaScript keywords. These cannot be used as names. If they // appear, we prefix them with "pb_". const char* kKeyword[] = { - "abstract", - "boolean", - "break", - "byte", - "case", - "catch", - "char", - "class", - "const", - "continue", - "debugger", - "default", - "delete", - "do", - "double", - "else", - "enum", - "export", - "extends", - "false", - "final", - "finally", - "float", - "for", - "function", - "goto", - "if", - "implements", - "import", - "in", - "instanceof", - "int", - "interface", - "long", - "native", - "new", - "null", - "package", - "private", - "protected", - "public", - "return", - "short", - "static", - "super", - "switch", - "synchronized", - "this", - "throw", - "throws", - "transient", - "try", - "typeof", - "var", - "void", - "volatile", - "while", - "with", + "abstract", "boolean", "break", "byte", "case", + "catch", "char", "class", "const", "continue", + "debugger", "default", "delete", "do", "double", + "else", "enum", "export", "extends", "false", + "final", "finally", "float", "for", "function", + "goto", "if", "implements", "import", "in", + "instanceof", "int", "interface", "long", "native", + "new", "null", "package", "private", "protected", + "public", "return", "short", "static", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "try", "typeof", "var", "void", + "volatile", "while", "with", }; static const int kNumKeyword = sizeof(kKeyword) / sizeof(char*); @@ -429,8 +383,8 @@ std::string GetExtensionFileName(const GeneratorOptions& options, bool with_filename) { std::string snake_name = StripProto(GetSnakeFilename(file->name())); return options.output_dir + "/" + ToLower(GetNamespace(options, file)) + - (with_filename ? ("_" + snake_name + "_extensions") : "") + - options.GetFileNameExtension(); + (with_filename ? ("_" + snake_name + "_extensions") : "") + + options.GetFileNameExtension(); } // When we're generating one output file per SCC, this is the filename // that all messages in the SCC should go in. @@ -468,7 +422,7 @@ std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc, GetSnakeFilename(scc->GetRepresentative()->file()->name())); (*long_name_dict)[scc->GetRepresentative()] = StrCat(snake_name, "_long_sccs_", - static_cast<uint64>((*long_name_dict).size())); + static_cast<uint64>((*long_name_dict).size())); } filename_base = (*long_name_dict)[scc->GetRepresentative()]; } @@ -482,11 +436,9 @@ std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc, std::string GetEnumFileName(const GeneratorOptions& options, const EnumDescriptor* desc, bool with_package) { return options.output_dir + "/" + - (with_package - ? ToLower(GetNamespace(options, desc->file()) + "_") - : "") + - ToLower(desc->name()) + - options.GetFileNameExtension(); + (with_package ? ToLower(GetNamespace(options, desc->file()) + "_") + : "") + + ToLower(desc->name()) + options.GetFileNameExtension(); } // Returns the message/response ID, if set. @@ -513,9 +465,7 @@ bool IgnoreField(const FieldDescriptor* field) { // Do we ignore this message type? -bool IgnoreMessage(const Descriptor* d) { - return d->options().map_entry(); -} +bool IgnoreMessage(const Descriptor* d) { return d->options().map_entry(); } // Does JSPB ignore this entire oneof? True only if all fields are ignored. bool IgnoreOneof(const OneofDescriptor* oneof) { @@ -532,13 +482,12 @@ std::string JSIdent(const GeneratorOptions& options, bool is_map, bool drop_list) { std::string result; if (field->type() == FieldDescriptor::TYPE_GROUP) { - result = is_upper_camel ? - ToUpperCamel(ParseUpperCamel(field->message_type()->name())) : - ToLowerCamel(ParseUpperCamel(field->message_type()->name())); + result = is_upper_camel + ? ToUpperCamel(ParseUpperCamel(field->message_type()->name())) + : ToLowerCamel(ParseUpperCamel(field->message_type()->name())); } else { - result = is_upper_camel ? - ToUpperCamel(ParseLowerUnderscore(field->name())) : - ToLowerCamel(ParseLowerUnderscore(field->name())); + result = is_upper_camel ? ToUpperCamel(ParseLowerUnderscore(field->name())) + : ToLowerCamel(ParseLowerUnderscore(field->name())); } if (is_map || field->is_map()) { // JSPB-style or proto3-style map. @@ -670,13 +619,15 @@ uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) { *length = expected; switch (expected) { - case 1: return bytes[0]; - case 2: return ((bytes[0] & 0x1F) << 6) | - ((bytes[1] & 0x3F) << 0); - case 3: return ((bytes[0] & 0x0F) << 12) | - ((bytes[1] & 0x3F) << 6) | - ((bytes[2] & 0x3F) << 0); - default: return 0; + case 1: + return bytes[0]; + case 2: + return ((bytes[0] & 0x1F) << 6) | ((bytes[1] & 0x3F) << 0); + case 3: + return ((bytes[0] & 0x0F) << 12) | ((bytes[1] & 0x3F) << 6) | + ((bytes[2] & 0x3F) << 0); + default: + return 0; } } @@ -703,18 +654,42 @@ bool EscapeJSString(const std::string& in, std::string* out) { decoded = have_bytes; switch (codepoint) { - case '\'': *out += "\\x27"; break; - case '"': *out += "\\x22"; break; - case '<': *out += "\\x3c"; break; - case '=': *out += "\\x3d"; break; - case '>': *out += "\\x3e"; break; - case '&': *out += "\\x26"; break; - case '\b': *out += "\\b"; break; - case '\t': *out += "\\t"; break; - case '\n': *out += "\\n"; break; - case '\f': *out += "\\f"; break; - case '\r': *out += "\\r"; break; - case '\\': *out += "\\\\"; break; + case '\'': + *out += "\\x27"; + break; + case '"': + *out += "\\x22"; + break; + case '<': + *out += "\\x3c"; + break; + case '=': + *out += "\\x3d"; + break; + case '>': + *out += "\\x3e"; + break; + case '&': + *out += "\\x26"; + break; + case '\b': + *out += "\\b"; + break; + case '\t': + *out += "\\t"; + break; + case '\n': + *out += "\\n"; + break; + case '\f': + *out += "\\f"; + break; + case '\r': + *out += "\\r"; + break; + case '\\': + *out += "\\\\"; + break; default: // TODO(b/115551870): Once we're supporting codepoints outside the BMP, // use a single Unicode codepoint escape if the output language is @@ -739,18 +714,17 @@ std::string EscapeBase64(const std::string& in) { std::string result; for (size_t i = 0; i < in.size(); i += 3) { - int value = (in[i] << 16) | - (((i + 1) < in.size()) ? (in[i + 1] << 8) : 0) | - (((i + 2) < in.size()) ? (in[i + 2] << 0) : 0); + int value = (in[i] << 16) | (((i + 1) < in.size()) ? (in[i + 1] << 8) : 0) | + (((i + 2) < in.size()) ? (in[i + 2] << 0) : 0); result += kAlphabet[(value >> 18) & 0x3f]; result += kAlphabet[(value >> 12) & 0x3f]; if ((i + 1) < in.size()) { - result += kAlphabet[(value >> 6) & 0x3f]; + result += kAlphabet[(value >> 6) & 0x3f]; } else { result += '='; } if ((i + 2) < in.size()) { - result += kAlphabet[(value >> 0) & 0x3f]; + result += kAlphabet[(value >> 0) & 0x3f]; } else { result += '='; } @@ -1259,7 +1233,7 @@ std::string OneofGroupList(const Descriptor* desc) { std::string JSOneofArray(const GeneratorOptions& options, const FieldDescriptor* field) { return OneofFieldsArrayName(options, field->containing_type()) + "[" + - JSOneofIndex(field->containing_oneof()) + "]"; + JSOneofIndex(field->containing_oneof()) + "]"; } std::string RelativeTypeName(const FieldDescriptor* field) { @@ -1326,10 +1300,8 @@ std::string FieldDefinition(const GeneratorOptions& options, } else { value_type = ProtoTypeName(options, value_field); } - return StringPrintf("map<%s, %s> %s = %d;", - key_type.c_str(), - value_type.c_str(), - field->name().c_str(), + return StringPrintf("map<%s, %s> %s = %d;", key_type.c_str(), + value_type.c_str(), field->name().c_str(), field->number()); } else { std::string qualifier = @@ -1347,11 +1319,8 @@ std::string FieldDefinition(const GeneratorOptions& options, type = ProtoTypeName(options, field); name = field->name(); } - return StringPrintf("%s %s %s = %d;", - qualifier.c_str(), - type.c_str(), - name.c_str(), - field->number()); + return StringPrintf("%s %s %s = %d;", qualifier.c_str(), type.c_str(), + name.c_str(), field->number()); } } @@ -1366,9 +1335,7 @@ std::string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { } bool ShouldGenerateExtension(const FieldDescriptor* field) { - return - field->is_extension() && - !IgnoreField(field); + return field->is_extension() && !IgnoreField(field); } bool HasExtensions(const Descriptor* desc) { @@ -1442,8 +1409,8 @@ std::string GetPivot(const Descriptor* desc) { int pivot = -1; if (IsExtendable(desc) || (max_field_number >= kDefaultPivot)) { - pivot = ((max_field_number + 1) < kDefaultPivot) ? - (max_field_number + 1) : kDefaultPivot; + pivot = ((max_field_number + 1) < kDefaultPivot) ? (max_field_number + 1) + : kDefaultPivot; } return StrCat(pivot); @@ -1618,23 +1585,20 @@ bool GenerateJspbAllowedMap(const GeneratorOptions& options, for (int i = 0; i < files_ordered.size(); i++) { for (int j = 0; j < files_ordered[i]->message_type_count(); j++) { const Descriptor* desc = files_ordered[i]->message_type(j); - if (added.insert(analyzer->GetSCC(desc)).second && !dedup.AddFile( - std::make_pair( - GetMessagesFileName(options, analyzer->GetSCC(desc), false), - GetMessagesFileName(options, analyzer->GetSCC(desc), true)), - analyzer->GetSCC(desc), - error)) { + if (added.insert(analyzer->GetSCC(desc)).second && + !dedup.AddFile( + std::make_pair( + GetMessagesFileName(options, analyzer->GetSCC(desc), false), + GetMessagesFileName(options, analyzer->GetSCC(desc), true)), + analyzer->GetSCC(desc), error)) { return false; } } for (int j = 0; j < files_ordered[i]->enum_type_count(); j++) { const EnumDescriptor* desc = files_ordered[i]->enum_type(j); - if (!dedup.AddFile( - std::make_pair( - GetEnumFileName(options, desc, false), - GetEnumFileName(options, desc, true)), - desc, - error)) { + if (!dedup.AddFile(std::make_pair(GetEnumFileName(options, desc, false), + GetEnumFileName(options, desc, true)), + desc, error)) { return false; } } @@ -1650,10 +1614,10 @@ bool GenerateJspbAllowedMap(const GeneratorOptions& options, if (has_extension) { if (!dedup.AddFile( - std::make_pair( - GetExtensionFileName(options, files_ordered[i], false), - GetExtensionFileName(options, files_ordered[i], true)), - files_ordered[i], error)) { + std::make_pair( + GetExtensionFileName(options, files_ordered[i], false), + GetExtensionFileName(options, files_ordered[i], true)), + files_ordered[i], error)) { return false; } } @@ -1688,17 +1652,18 @@ bool IsWellKnownTypeFile(const FileDescriptor* file) { void Generator::GenerateHeader(const GeneratorOptions& options, io::Printer* printer) const { - printer->Print("/**\n" - " * @fileoverview\n" - " * @enhanceable\n" - " * @suppress {messageConventions} JS Compiler reports an " - "error if a variable or\n" - " * field starts with 'MSG_' and isn't a translatable " - "message.\n" - " * @public\n" - " */\n" - "// GENERATED CODE -- DO NOT EDIT!\n" - "\n"); + printer->Print( + "/**\n" + " * @fileoverview\n" + " * @enhanceable\n" + " * @suppress {messageConventions} JS Compiler reports an " + "error if a variable or\n" + " * field starts with 'MSG_' and isn't a translatable " + "message.\n" + " * @public\n" + " */\n" + "// GENERATED CODE -- DO NOT EDIT!\n" + "\n"); } void Generator::FindProvidesForFile(const GeneratorOptions& options, @@ -1736,12 +1701,10 @@ void Generator::FindProvidesForMessage(const GeneratorOptions& options, provided->insert(name); for (int i = 0; i < desc->enum_type_count(); i++) { - FindProvidesForEnum(options, printer, desc->enum_type(i), - provided); + FindProvidesForEnum(options, printer, desc->enum_type(i), provided); } for (int i = 0; i < desc->nested_type_count(); i++) { - FindProvidesForMessage(options, printer, desc->nested_type(i), - provided); + FindProvidesForMessage(options, printer, desc->nested_type(i), provided); } } @@ -1820,9 +1783,9 @@ void Generator::GenerateRequiresForSCC(const GeneratorOptions& options, } GenerateRequiresImpl(options, printer, &required, &forwards, provided, - /* require_jspb = */ have_message, - /* require_extension = */ has_extension, - /* require_map = */ has_map); + /* require_jspb = */ have_message, + /* require_extension = */ has_extension, + /* require_map = */ has_map); } void Generator::GenerateRequiresForLibrary( @@ -1860,7 +1823,7 @@ void Generator::GenerateRequiresForLibrary( continue; } if (extension->containing_type()->full_name() != - "google.protobuf.bridge.MessageSet") { + "google.protobuf.bridge.MessageSet") { required.insert(GetMessagePath(options, extension->containing_type())); } FindRequiresForField(options, extension, &required, &forwards); @@ -1919,8 +1882,7 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options, if (provided->find(*it) != provided->end()) { continue; } - printer->Print("goog.require('$name$');\n", - "name", *it); + printer->Print("goog.require('$name$');\n", "name", *it); } printer->Print("\n"); @@ -1929,8 +1891,7 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options, if (provided->find(*it) != provided->end()) { continue; } - printer->Print("goog.forwardDeclare('$name$');\n", - "name", *it); + printer->Print("goog.forwardDeclare('$name$');\n", "name", *it); } } @@ -2010,8 +1971,8 @@ void Generator::GenerateClassesAndEnums(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file) const { for (int i = 0; i < file->message_type_count(); i++) { - GenerateClassConstructorAndDeclareExtensionFieldInfo( - options, printer, file->message_type(i)); + GenerateClassConstructorAndDeclareExtensionFieldInfo(options, printer, + file->message_type(i)); } for (int i = 0; i < file->message_type_count(); i++) { GenerateClass(options, printer, file->message_type(i)); @@ -2083,19 +2044,19 @@ void Generator::GenerateClassConstructor(const GeneratorOptions& options, " * @constructor\n" " */\n" "$classprefix$$classname$ = function(opt_data) {\n", - "classprefix", GetMessagePathPrefix(options, desc), - "classname", desc->name()); + "classprefix", GetMessagePathPrefix(options, desc), "classname", + desc->name()); printer->Annotate("classname", desc); std::string message_id = GetMessageId(desc); printer->Print( " jspb.Message.initialize(this, opt_data, $messageId$, $pivot$, " "$rptfields$, $oneoffields$);\n", - "messageId", !message_id.empty() ? - ("'" + message_id + "'") : - (IsResponse(desc) ? "''" : "0"), - "pivot", GetPivot(desc), - "rptfields", RepeatedFieldsArrayName(options, desc), - "oneoffields", OneofFieldsArrayName(options, desc)); + "messageId", + !message_id.empty() ? ("'" + message_id + "'") + : (IsResponse(desc) ? "''" : "0"), + "pivot", GetPivot(desc), "rptfields", + RepeatedFieldsArrayName(options, desc), "oneoffields", + OneofFieldsArrayName(options, desc)); printer->Print( "};\n" "goog.inherits($classname$, jspb.Message);\n" @@ -2112,8 +2073,7 @@ void Generator::GenerateClassConstructor(const GeneratorOptions& options, } void Generator::GenerateClassConstructorAndDeclareExtensionFieldInfo( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const { if (!NamespaceOnly(desc)) { GenerateClassConstructor(options, printer, desc); @@ -2141,9 +2101,9 @@ void Generator::GenerateClassFieldInfo(const GeneratorOptions& options, " */\n" "$classname$$rptfieldarray$ = $rptfields$;\n" "\n", - "classname", GetMessagePath(options, desc), - "rptfieldarray", kRepeatedFieldArrayName, - "rptfields", RepeatedFieldNumberList(options, desc)); + "classname", GetMessagePath(options, desc), "rptfieldarray", + kRepeatedFieldArrayName, "rptfields", + RepeatedFieldNumberList(options, desc)); } if (HasOneofFields(desc)) { @@ -2162,9 +2122,8 @@ void Generator::GenerateClassFieldInfo(const GeneratorOptions& options, " */\n" "$classname$$oneofgrouparray$ = $oneofgroups$;\n" "\n", - "classname", GetMessagePath(options, desc), - "oneofgrouparray", kOneofGroupArrayName, - "oneofgroups", OneofGroupList(desc)); + "classname", GetMessagePath(options, desc), "oneofgrouparray", + kOneofGroupArrayName, "oneofgroups", OneofGroupList(desc)); for (int i = 0; i < desc->oneof_decl_count(); i++) { if (IgnoreOneof(desc->oneof_decl(i))) { @@ -2186,8 +2145,7 @@ void Generator::GenerateClassXid(const GeneratorOptions& options, } void Generator::GenerateOneofCaseDefinition( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const OneofDescriptor* oneof) const { printer->Print( "/**\n" @@ -2195,9 +2153,8 @@ void Generator::GenerateOneofCaseDefinition( " */\n" "$classname$.$oneof$Case = {\n" " $upcase$_NOT_SET: 0", - "classname", GetMessagePath(options, oneof->containing_type()), - "oneof", JSOneofName(oneof), - "upcase", ToEnumCase(oneof->name())); + "classname", GetMessagePath(options, oneof->containing_type()), "oneof", + JSOneofName(oneof), "upcase", ToEnumCase(oneof->name())); for (int i = 0; i < oneof->field_count(); i++) { if (IgnoreField(oneof->field(i))) { @@ -2207,8 +2164,8 @@ void Generator::GenerateOneofCaseDefinition( printer->Print( ",\n" " $upcase$: $number$", - "upcase", ToEnumCase(oneof->field(i)->name()), - "number", JSFieldIndex(oneof->field(i))); + "upcase", ToEnumCase(oneof->field(i)->name()), "number", + JSFieldIndex(oneof->field(i))); printer->Annotate("upcase", oneof->field(i)); } @@ -2224,9 +2181,8 @@ void Generator::GenerateOneofCaseDefinition( "computeOneofCase(this, $class$.oneofGroups_[$oneofindex$]));\n" "};\n" "\n", - "class", GetMessagePath(options, oneof->containing_type()), - "oneof", JSOneofName(oneof), - "oneofindex", JSOneofIndex(oneof)); + "class", GetMessagePath(options, oneof->containing_type()), "oneof", + JSOneofName(oneof), "oneofindex", JSOneofIndex(oneof)); } void Generator::GenerateClassToObject(const GeneratorOptions& options, @@ -2315,7 +2271,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options, } void Generator::GenerateFieldValueExpression(io::Printer* printer, - const char *obj_reference, + const char* obj_reference, const FieldDescriptor* field, bool use_default) const { const bool is_float_or_double = @@ -2348,29 +2304,25 @@ void Generator::GenerateFieldValueExpression(io::Printer* printer, // - getRepeatedFloatingPointField if (is_float_or_double && !field->is_repeated() && !use_default) { printer->Print( - "jspb.Message.getOptionalFloatingPointField($obj$, " - "$index$$default$)", - "obj", obj_reference, - "index", JSFieldIndex(field), - "default", default_arg); + "jspb.Message.getOptionalFloatingPointField($obj$, " + "$index$$default$)", + "obj", obj_reference, "index", JSFieldIndex(field), "default", + default_arg); } else { printer->Print( - "jspb.Message.get$cardinality$$type$Field$with_default$($obj$, " - "$index$$default$)", - "cardinality", cardinality, - "type", type, - "with_default", with_default, - "obj", obj_reference, - "index", JSFieldIndex(field), - "default", default_arg); + "jspb.Message.get$cardinality$$type$Field$with_default$($obj$, " + "$index$$default$)", + "cardinality", cardinality, "type", type, "with_default", with_default, + "obj", obj_reference, "index", JSFieldIndex(field), "default", + default_arg); } } void Generator::GenerateClassFieldToObject(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { - printer->Print("$fieldname$: ", - "fieldname", JSObjectFieldName(options, field)); + printer->Print("$fieldname$: ", "fieldname", + JSObjectFieldName(options, field)); if (field->is_map()) { const FieldDescriptor* value_field = MapFieldValue(field); @@ -2391,21 +2343,23 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options, // Message field. if (field->is_repeated()) { { - printer->Print("jspb.Message.toObjectList(msg.get$getter$(),\n" - " $type$.toObject, includeInstance)", - "getter", JSGetterName(options, field), - "type", SubmessageTypeRef(options, field)); + printer->Print( + "jspb.Message.toObjectList(msg.get$getter$(),\n" + " $type$.toObject, includeInstance)", + "getter", JSGetterName(options, field), "type", + SubmessageTypeRef(options, field)); } } else { - printer->Print("(f = msg.get$getter$()) && " - "$type$.toObject(includeInstance, f)", - "getter", JSGetterName(options, field), - "type", SubmessageTypeRef(options, field)); + printer->Print( + "(f = msg.get$getter$()) && " + "$type$.toObject(includeInstance, f)", + "getter", JSGetterName(options, field), "type", + SubmessageTypeRef(options, field)); } } else if (field->type() == FieldDescriptor::TYPE_BYTES) { // For bytes fields we want to always return the B64 data. - printer->Print("msg.get$getter$()", - "getter", JSGetterName(options, field, BYTES_B64)); + printer->Print("msg.get$getter$()", "getter", + JSGetterName(options, field, BYTES_B64)); } else { bool use_default = field->has_default_value(); @@ -2450,15 +2404,14 @@ void Generator::GenerateObjectTypedef(const GeneratorOptions& options, " * @record\n" " */\n" "$typeName$ = function() {};\n\n", - "messageName", desc->name(), - "typeName", type_name); + "messageName", desc->name(), "typeName", type_name); for (int i = 0; i < desc->field_count(); i++) { printer->Print( "/** @type {$fieldType$|undefined} */\n" "$typeName$.prototype.$fieldName$;\n\n", - "typeName", type_name, - "fieldName", JSObjectFieldName(options, desc->field(i)), + "typeName", type_name, "fieldName", + JSObjectFieldName(options, desc->field(i)), // TODO(b/121097361): Add type checking for field values. "fieldType", "?"); } @@ -2467,8 +2420,7 @@ void Generator::GenerateObjectTypedef(const GeneratorOptions& options, void Generator::GenerateClassFromObject(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const { - printer->Print( - "if (jspb.Message.GENERATE_FROM_OBJECT) {\n\n"); + printer->Print("if (jspb.Message.GENERATE_FROM_OBJECT) {\n\n"); GenerateObjectTypedef(options, printer, desc); @@ -2497,8 +2449,7 @@ void Generator::GenerateClassFromObject(const GeneratorOptions& options, } void Generator::GenerateClassFieldFromObject( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { if (field->is_map()) { const FieldDescriptor* value_field = MapFieldValue(field); @@ -2509,9 +2460,9 @@ void Generator::GenerateClassFieldFromObject( " obj.$name$ && jspb.Message.setWrapperField(\n" " msg, $index$, jspb.Map.fromObject(obj.$name$, $fieldclass$, " "$fieldclass$.fromObject));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", GetMessagePath(options, value_field->message_type())); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", + GetMessagePath(options, value_field->message_type())); } else { // `msg` is a newly-constructed message object that has not yet built any // map containers wrapping underlying arrays, so we can simply directly @@ -2519,8 +2470,8 @@ void Generator::GenerateClassFieldFromObject( printer->Print( " obj.$name$ && " "jspb.Message.setField(msg, $index$, obj.$name$);\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field)); } } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // Message field (singular or repeated) @@ -2531,25 +2482,24 @@ void Generator::GenerateClassFieldFromObject( "jspb.Message.setRepeatedWrapperField(\n" " msg, $index$, obj.$name$.map(\n" " $fieldclass$.fromObject));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", SubmessageTypeRef(options, field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", + SubmessageTypeRef(options, field)); } } else { printer->Print( " obj.$name$ && jspb.Message.setWrapperField(\n" " msg, $index$, $fieldclass$.fromObject(obj.$name$));\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field), - "fieldclass", SubmessageTypeRef(options, field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field), "fieldclass", SubmessageTypeRef(options, field)); } } else { // Simple (primitive) field. printer->Print( " obj.$name$ != null && jspb.Message.setField(msg, $index$, " "obj.$name$);\n", - "name", JSObjectFieldName(options, field), - "index", JSFieldIndex(field)); + "name", JSObjectFieldName(options, field), "index", + JSFieldIndex(field)); } } @@ -2576,10 +2526,8 @@ void Generator::GenerateClassFields(const GeneratorOptions& options, } } -void GenerateBytesWrapper(const GeneratorOptions& options, - io::Printer* printer, - const FieldDescriptor* field, - BytesMode bytes_mode) { +void GenerateBytesWrapper(const GeneratorOptions& options, io::Printer* printer, + const FieldDescriptor* field, BytesMode bytes_mode) { std::string type = JSFieldTypeAnnotation(options, field, /* is_setter_argument = */ false, @@ -2598,14 +2546,13 @@ void GenerateBytesWrapper(const GeneratorOptions& options, "};\n" "\n" "\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, bytes_mode), - "type", type, - "class", GetMessagePath(options, field->containing_type()), - "name", JSGetterName(options, field, bytes_mode), - "list", field->is_repeated() ? "List" : "", - "suffix", JSByteGetterSuffix(bytes_mode), - "defname", JSGetterName(options, field, BYTES_DEFAULT)); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, bytes_mode), "type", type, "class", + GetMessagePath(options, field->containing_type()), "name", + JSGetterName(options, field, bytes_mode), "list", + field->is_repeated() ? "List" : "", "suffix", + JSByteGetterSuffix(bytes_mode), "defname", + JSGetterName(options, field, BYTES_DEFAULT)); } void Generator::GenerateClassField(const GeneratorOptions& options, @@ -2633,15 +2580,13 @@ void Generator::GenerateClassField(const GeneratorOptions& options, " * empty, instead returning `undefined`\n" " * @return {!jspb.Map<$keytype$,$valuetype$>}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "keytype", key_type, + "fielddef", FieldDefinition(options, field), "keytype", key_type, "valuetype", value_type); printer->Print( "$class$.prototype.$gettername$ = function(opt_noLazyCreate) {\n" " return /** @type {!jspb.Map<$keytype$,$valuetype$>} */ (\n", "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field), - "keytype", key_type, + "gettername", "get" + JSGetterName(options, field), "keytype", key_type, "valuetype", value_type); printer->Annotate("gettername", field); printer->Print( @@ -2654,12 +2599,12 @@ void Generator::GenerateClassField(const GeneratorOptions& options, " $messageType$", "messageType", GetMessagePath(options, value_field->message_type())); } else { - printer->Print(",\n" + printer->Print( + ",\n" " null"); } - printer->Print( - "));\n"); + printer->Print("));\n"); printer->Print( "};\n" @@ -2675,12 +2620,12 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "$comment$" " * @return {$type$}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, BYTES_DEFAULT), - "type", JSFieldTypeAnnotation(options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false)); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, BYTES_DEFAULT), "type", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false)); printer->Print( "$class$.prototype.$gettername$ = function() {\n" " return /** @type{$type$} */ (\n" @@ -2690,16 +2635,15 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "\n" "\n", "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field), - "type", JSFieldTypeAnnotation(options, field, - /* is_setter_argument = */ false, - /* force_present = */ false, - /* singular_if_not_packed = */ false), - "rpt", (field->is_repeated() ? "Repeated" : ""), - "index", JSFieldIndex(field), - "wrapperclass", SubmessageTypeRef(options, field), - "required", (field->label() == FieldDescriptor::LABEL_REQUIRED ? - ", 1" : "")); + "gettername", "get" + JSGetterName(options, field), "type", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ false, + /* singular_if_not_packed = */ false), + "rpt", (field->is_repeated() ? "Repeated" : ""), "index", + JSFieldIndex(field), "wrapperclass", SubmessageTypeRef(options, field), + "required", + (field->label() == FieldDescriptor::LABEL_REQUIRED ? ", 1" : "")); printer->Annotate("gettername", field); printer->Print( "/** @param {$optionaltype$} value$returndoc$ */\n" @@ -2710,11 +2654,11 @@ void Generator::GenerateClassField(const GeneratorOptions& options, /* is_setter_argument = */ true, /* force_present = */ false, /* singular_if_not_packed = */ false), - "returndoc", JSReturnDoc(options, field), - "class", GetMessagePath(options, field->containing_type()), - "settername", "set" + JSGetterName(options, field), - "oneoftag", (field->containing_oneof() ? "Oneof" : ""), - "repeatedtag", (field->is_repeated() ? "Repeated" : "")); + "returndoc", JSReturnDoc(options, field), "class", + GetMessagePath(options, field->containing_type()), "settername", + "set" + JSGetterName(options, field), "oneoftag", + (field->containing_oneof() ? "Oneof" : ""), "repeatedtag", + (field->is_repeated() ? "Repeated" : "")); printer->Annotate("settername", field); printer->Print( @@ -2722,9 +2666,9 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "};\n" "\n" "\n", - "index", JSFieldIndex(field), - "oneofgroup", (field->containing_oneof() ? - (", " + JSOneofArray(options, field)) : ""), + "index", JSFieldIndex(field), "oneofgroup", + (field->containing_oneof() ? (", " + JSOneofArray(options, field)) + : ""), "returnvalue", JSReturnClause(field)); if (field->is_repeated()) { @@ -2741,8 +2685,9 @@ void Generator::GenerateClassField(const GeneratorOptions& options, // at this point we "lie" to non-binary users and tell the return // type is always base64 string, pending a LSC to migrate to typed getters. BytesMode bytes_mode = - field->type() == FieldDescriptor::TYPE_BYTES && !options.binary ? - BYTES_B64 : BYTES_DEFAULT; + field->type() == FieldDescriptor::TYPE_BYTES && !options.binary + ? BYTES_B64 + : BYTES_DEFAULT; std::string typed_annotation = JSFieldTypeAnnotation(options, field, /* is_setter_argument = */ false, @@ -2761,24 +2706,20 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "$comment$" " * @return {$type$}\n" " */\n", - "fielddef", FieldDefinition(options, field), - "comment", FieldComments(field, bytes_mode), - "type", typed_annotation); + "fielddef", FieldDefinition(options, field), "comment", + FieldComments(field, bytes_mode), "type", typed_annotation); } - printer->Print( - "$class$.prototype.$gettername$ = function() {\n", - "class", GetMessagePath(options, field->containing_type()), - "gettername", "get" + JSGetterName(options, field)); + printer->Print("$class$.prototype.$gettername$ = function() {\n", "class", + GetMessagePath(options, field->containing_type()), + "gettername", "get" + JSGetterName(options, field)); printer->Annotate("gettername", field); if (untyped) { - printer->Print( - " return "); + printer->Print(" return "); } else { - printer->Print( - " return /** @type {$type$} */ (", - "type", typed_annotation); + printer->Print(" return /** @type {$type$} */ (", "type", + typed_annotation); } bool use_default = !ReturnsNullWhenUnset(options, field); @@ -2824,11 +2765,10 @@ void Generator::GenerateClassField(const GeneratorOptions& options, } else { printer->Print( "/** @param {$optionaltype$} value$returndoc$ */\n", "optionaltype", - JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ true, - /* force_present = */ false, - /* singular_if_not_packed = */ false), + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ true, + /* force_present = */ false, + /* singular_if_not_packed = */ false), "returndoc", JSReturnDoc(options, field)); } @@ -2881,7 +2821,6 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "returndoc", JSReturnDoc(options, field)); } - if (field->is_repeated()) { GenerateRepeatedPrimitiveHelperMethods(options, printer, field, untyped); } @@ -2974,9 +2913,8 @@ void Generator::GenerateClassField(const GeneratorOptions& options, "};\n" "\n" "\n", - "class", GetMessagePath(options, field->containing_type()), - "hasername", "has" + JSGetterName(options, field), - "index", JSFieldIndex(field)); + "class", GetMessagePath(options, field->containing_type()), "hasername", + "has" + JSGetterName(options, field), "index", JSFieldIndex(field)); printer->Annotate("hasername", field); } } @@ -3133,15 +3071,13 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options, " var field = reader.getFieldNumber();\n" " switch (field) {\n"); - for (int i = 0; i < desc->field_count(); i++) { if (!IgnoreField(desc->field(i))) { GenerateClassDeserializeBinaryField(options, printer, desc->field(i)); } } - printer->Print( - " default:\n"); + printer->Print(" default:\n"); if (IsExtendable(desc)) { printer->Print( " jspb.Message.readBinaryExtension(msg, reader,\n" @@ -3168,8 +3104,7 @@ void Generator::GenerateClassDeserializeBinary(const GeneratorOptions& options, } void Generator::GenerateClassDeserializeBinaryField( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { printer->Print(" case $num$:\n", "num", StrCat(field->number())); @@ -3181,20 +3116,20 @@ void Generator::GenerateClassDeserializeBinaryField( " reader.readMessage(value, function(message, reader) {\n", "name", JSGetterName(options, field)); - printer->Print(" jspb.Map.deserializeBinary(message, reader, " - "$keyReaderFn$, $valueReaderFn$", - "keyReaderFn", JSBinaryReaderMethodName(options, key_field), - "valueReaderFn", JSBinaryReaderMethodName(options, value_field)); + printer->Print( + " jspb.Map.deserializeBinary(message, reader, " + "$keyReaderFn$, $valueReaderFn$", + "keyReaderFn", JSBinaryReaderMethodName(options, key_field), + "valueReaderFn", JSBinaryReaderMethodName(options, value_field)); if (value_field->type() == FieldDescriptor::TYPE_MESSAGE) { printer->Print(", $messageType$.deserializeBinaryFromReader", - "messageType", GetMessagePath(options, value_field->message_type())); + "messageType", + GetMessagePath(options, value_field->message_type())); } else { printer->Print(", null"); } - printer->Print(", $defaultKey$", - "defaultKey", JSFieldDefault(key_field) - ); + printer->Print(", $defaultKey$", "defaultKey", JSFieldDefault(key_field)); printer->Print(");\n"); printer->Print(" });\n"); } else { @@ -3213,9 +3148,9 @@ void Generator::GenerateClassDeserializeBinaryField( printer->Print( " var value = /** @type {$fieldtype$} */ " "(reader.read$reader$());\n", - "fieldtype", JSFieldTypeAnnotation(options, field, false, true, - /* singular_if_not_packed */ true, - BYTES_U8), + "fieldtype", + JSFieldTypeAnnotation(options, field, false, true, + /* singular_if_not_packed */ true, BYTES_U8), "reader", JSBinaryReadWriteMethodName(field, /* is_writer = */ false)); } @@ -3228,9 +3163,8 @@ void Generator::GenerateClassDeserializeBinaryField( // Singular fields, and packed repeated fields, receive a |value| either // as the field's value or as the array of all the field's values; set // this as the field's value directly. - printer->Print( - " msg.set$name$(value);\n", - "name", JSGetterName(options, field)); + printer->Print(" msg.set$name$(value);\n", "name", + JSGetterName(options, field)); } } @@ -3274,8 +3208,8 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options, printer->Print( " jspb.Message.serializeBinaryExtensions(message, writer,\n" " $extobj$Binary, $class$.prototype.getExtension);\n", - "extobj", JSExtensionsObjectName(options, desc->file(), desc), - "class", GetMessagePath(options, desc)); + "extobj", JSExtensionsObjectName(options, desc->file(), desc), "class", + GetMessagePath(options, desc)); } printer->Print( @@ -3285,8 +3219,7 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options, } void Generator::GenerateClassSerializeBinaryField( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { if (HasFieldPresence(options, field) && field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { @@ -3299,12 +3232,11 @@ void Generator::GenerateClassSerializeBinaryField( printer->Print( " f = /** @type {$type$} */ " "(jspb.Message.getField(message, $index$));\n", - "index", JSFieldIndex(field), - "type", typed_annotation); + "index", JSFieldIndex(field), "type", typed_annotation); } else { printer->Print( - " f = message.get$name$($nolazy$);\n", - "name", JSGetterName(options, field, BYTES_U8), + " f = message.get$name$($nolazy$);\n", "name", + JSGetterName(options, field, BYTES_U8), // No lazy creation for maps containers -- fastpath the empty case. "nolazy", field->is_map() ? "true" : ""); } @@ -3312,15 +3244,12 @@ void Generator::GenerateClassSerializeBinaryField( // Print an `if (condition)` statement that evaluates to true if the field // goes on the wire. if (field->is_map()) { - printer->Print( - " if (f && f.getLength() > 0) {\n"); + printer->Print(" if (f && f.getLength() > 0) {\n"); } else if (field->is_repeated()) { - printer->Print( - " if (f.length > 0) {\n"); + printer->Print(" if (f.length > 0) {\n"); } else { if (HasFieldPresence(options, field)) { - printer->Print( - " if (f != null) {\n"); + printer->Print(" if (f != null) {\n"); } else { // No field presence: serialize onto the wire only if value is // non-default. Defaults are documented here: @@ -3345,16 +3274,13 @@ void Generator::GenerateClassSerializeBinaryField( case FieldDescriptor::CPPTYPE_ENUM: case FieldDescriptor::CPPTYPE_FLOAT: case FieldDescriptor::CPPTYPE_DOUBLE: - printer->Print( - " if (f !== 0.0) {\n"); + printer->Print(" if (f !== 0.0) {\n"); break; case FieldDescriptor::CPPTYPE_BOOL: - printer->Print( - " if (f) {\n"); + printer->Print(" if (f) {\n"); break; case FieldDescriptor::CPPTYPE_STRING: - printer->Print( - " if (f.length > 0) {\n"); + printer->Print(" if (f.length > 0) {\n"); break; default: assert(false); @@ -3375,8 +3301,8 @@ void Generator::GenerateClassSerializeBinaryField( JSBinaryWriterMethodName(options, value_field)); if (value_field->type() == FieldDescriptor::TYPE_MESSAGE) { - printer->Print(", $messageType$.serializeBinaryToWriter", - "messageType", GetMessagePath(options, value_field->message_type())); + printer->Print(", $messageType$.serializeBinaryToWriter", "messageType", + GetMessagePath(options, value_field->message_type())); } printer->Print(");\n"); @@ -3393,18 +3319,16 @@ void Generator::GenerateClassSerializeBinaryField( printer->Print( ",\n" " $submsg$.serializeBinaryToWriter\n", - "submsg", SubmessageTypeRef(options, field)); + "submsg", SubmessageTypeRef(options, field)); } else { printer->Print("\n"); } - printer->Print( - " );\n"); + printer->Print(" );\n"); } // Close the `if`. - printer->Print( - " }\n"); + printer->Print(" }\n"); } void Generator::GenerateEnum(const GeneratorOptions& options, @@ -3415,8 +3339,8 @@ void Generator::GenerateEnum(const GeneratorOptions& options, " * @enum {number}\n" " */\n" "$enumprefix$$name$ = {\n", - "enumprefix", GetEnumPathPrefix(options, enumdesc), - "name", enumdesc->name()); + "enumprefix", GetEnumPathPrefix(options, enumdesc), "name", + enumdesc->name()); printer->Annotate("name", enumdesc); for (int i = 0; i < enumdesc->value_count(); i++) { @@ -3450,14 +3374,12 @@ void Generator::GenerateExtension(const GeneratorOptions& options, " * @type {!jspb.ExtensionFieldInfo<$extensionType$>}\n" " */\n" "$class$.$name$ = new jspb.ExtensionFieldInfo(\n", - "nameInComment", extension_object_name, - "name", extension_object_name, - "class", extension_scope, - "extensionType", JSFieldTypeAnnotation( - options, field, - /* is_setter_argument = */ false, - /* force_present = */ true, - /* singular_if_not_packed = */ false)); + "nameInComment", extension_object_name, "name", extension_object_name, + "class", extension_scope, "extensionType", + JSFieldTypeAnnotation(options, field, + /* is_setter_argument = */ false, + /* force_present = */ true, + /* singular_if_not_packed = */ false)); printer->Annotate("name", field); printer->Print( " $index$,\n" @@ -3619,8 +3541,7 @@ GeneratorOptions::OutputMode GeneratorOptions::output_mode() const { } void Generator::GenerateFilesInDepOrder( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const std::vector<const FileDescriptor*>& files) const { // Build a std::set over all files so that the DFS can detect when it recurses // into a dep not specified in the user's command line. @@ -3633,10 +3554,8 @@ void Generator::GenerateFilesInDepOrder( } void Generator::GenerateFileAndDeps( - const GeneratorOptions& options, - io::Printer* printer, - const FileDescriptor* root, - std::set<const FileDescriptor*>* all_files, + const GeneratorOptions& options, io::Printer* printer, + const FileDescriptor* root, std::set<const FileDescriptor*>* all_files, std::set<const FileDescriptor*>* generated) const { // Skip if already generated. if (generated->find(root) != generated->end()) { @@ -3659,7 +3578,7 @@ void Generator::GenerateFileAndDeps( } bool Generator::GenerateFile(const FileDescriptor* file, - const GeneratorOptions &options, + const GeneratorOptions& options, GeneratorContext* context, bool use_short_name) const { std::string filename = @@ -3672,10 +3591,8 @@ bool Generator::GenerateFile(const FileDescriptor* file, GeneratedCodeInfo annotations; io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( &annotations); - io::Printer printer( - output.get(), '$', options.annotate_code ? - &annotation_collector : nullptr); - + io::Printer printer(output.get(), '$', + options.annotate_code ? &annotation_collector : nullptr); GenerateFile(options, &printer, file); @@ -3753,8 +3670,8 @@ void Generator::GenerateFile(const GeneratorOptions& options, // if provided is empty, do not export anything if (options.import_style == GeneratorOptions::kImportCommonJs && !provided.empty()) { - printer->Print("goog.object.extend(exports, $package$);\n", - "package", GetNamespace(options, file)); + printer->Print("goog.object.extend(exports, $package$);\n", "package", + GetNamespace(options, file)); } else if (options.import_style == GeneratorOptions::kImportCommonJsStrict) { printer->Print("goog.object.extend(exports, proto);\n", "package", GetNamespace(options, file)); @@ -3823,8 +3740,8 @@ bool Generator::GenerateAll(const std::vector<const FileDescriptor*>& files, std::set<const Descriptor*> have_printed; SCCAnalyzer<DepsGenerator> analyzer; std::map<const void*, std::string> allowed_map; - if (!GenerateJspbAllowedMap( - options, files, &allowed_map, &analyzer, error)) { + if (!GenerateJspbAllowedMap(options, files, &allowed_map, &analyzer, + error)) { return false; } @@ -3950,8 +3867,7 @@ bool Generator::GenerateAll(const std::vector<const FileDescriptor*>& files, std::string filename = options.output_dir + "/" + "empty_no_content_void_file" + options.GetFileNameExtension(); - std::unique_ptr<io::ZeroCopyOutputStream> output( - context->Open(filename)); + std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); } } else /* options.output_mode() == kOneOutputFilePerInputFile */ { // Generate one output file per input (.proto) file. diff --git a/src/google/protobuf/compiler/js/js_generator.h b/src/google/protobuf/compiler/js/js_generator.h index e99622fde2..b1c1878a5d 100644 --- a/src/google/protobuf/compiler/js/js_generator.h +++ b/src/google/protobuf/compiler/js/js_generator.h @@ -33,8 +33,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ -#include <string> #include <set> +#include <string> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> @@ -52,7 +52,9 @@ class FieldDescriptor; class OneofDescriptor; class FileDescriptor; -namespace io { class Printer; } +namespace io { +class Printer; +} namespace compiler { namespace js { @@ -123,8 +125,11 @@ struct GeneratorOptions { std::string extension; // Create a separate output file for each input file? bool one_output_file_per_input_file; - // If true, we should build .meta files that contain annotations for - // generated code. See GeneratedCodeInfo in descriptor.proto. + // If true, we should append annotations as commen on the last line for + // generated .js file. Annotations used by tools like https://kythe.io + // to provide cross-references between .js and .proto files. Annotations + // are enced as base64 proto of GeneratedCodeInfo message (see + // descriptor.proto). bool annotate_code; }; @@ -215,12 +220,9 @@ class PROTOC_EXPORT Generator : public CodeGenerator { // Generate all things in a proto file into one file. // If use_short_name is true, the generated file's name will only be short // name that without directory, otherwise filename equals file->name() - bool GenerateFile(const FileDescriptor* file, - const GeneratorOptions &options, - GeneratorContext* context, - bool use_short_name) const; - void GenerateFile(const GeneratorOptions& options, - io::Printer* printer, + bool GenerateFile(const FileDescriptor* file, const GeneratorOptions& options, + GeneratorContext* context, bool use_short_name) const; + void GenerateFile(const GeneratorOptions& options, io::Printer* printer, const FileDescriptor* file) const; // Generate definitions for all message classes and enums in all files, @@ -230,8 +232,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator { const std::vector<const FileDescriptor*>& file) const; // Helper for above. void GenerateFileAndDeps(const GeneratorOptions& options, - io::Printer* printer, - const FileDescriptor* root, + io::Printer* printer, const FileDescriptor* root, std::set<const FileDescriptor*>* all_files, std::set<const FileDescriptor*>* generated) const; @@ -246,8 +247,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator { bool use_default) const; // Generate definition for one class. - void GenerateClass(const GeneratorOptions& options, - io::Printer* printer, + void GenerateClass(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateClassConstructor(const GeneratorOptions& options, io::Printer* printer, @@ -256,11 +256,9 @@ class PROTOC_EXPORT Generator : public CodeGenerator { io::Printer* printer, const Descriptor* desc) const; void GenerateClassConstructorAndDeclareExtensionFieldInfo( - const GeneratorOptions& options, - io::Printer* printer, + const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; - void GenerateClassXid(const GeneratorOptions& options, - io::Printer* printer, + void GenerateClassXid(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateOneofCaseDefinition(const GeneratorOptions& options, io::Printer* printer, @@ -280,17 +278,14 @@ class PROTOC_EXPORT Generator : public CodeGenerator { void GenerateClassFieldFromObject(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const; - void GenerateClassClone(const GeneratorOptions& options, - io::Printer* printer, + void GenerateClassClone(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateClassRegistration(const GeneratorOptions& options, io::Printer* printer, const Descriptor* desc) const; void GenerateClassFields(const GeneratorOptions& options, - io::Printer* printer, - const Descriptor* desc) const; - void GenerateClassField(const GeneratorOptions& options, - io::Printer* printer, + io::Printer* printer, const Descriptor* desc) const; + void GenerateClassField(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* desc) const; void GenerateClassExtensionFieldInfo(const GeneratorOptions& options, io::Printer* printer, @@ -312,13 +307,11 @@ class PROTOC_EXPORT Generator : public CodeGenerator { const FieldDescriptor* field) const; // Generate definition for one enum. - void GenerateEnum(const GeneratorOptions& options, - io::Printer* printer, + void GenerateEnum(const GeneratorOptions& options, io::Printer* printer, const EnumDescriptor* enumdesc) const; // Generate an extension definition. - void GenerateExtension(const GeneratorOptions& options, - io::Printer* printer, + void GenerateExtension(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const; // Generate addFoo() method for repeated primitive fields. diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc index 46326e2073..07760649f2 100644 --- a/src/google/protobuf/compiler/mock_code_generator.cc +++ b/src/google/protobuf/compiler/mock_code_generator.cc @@ -114,16 +114,16 @@ void MockCodeGenerator::ExpectGenerated( } EXPECT_EQ(lines.size(), 3 + insertion_list.size() * 2); - EXPECT_EQ(GetOutputFileContent(name, parameter, file, - first_parsed_file_name, first_message_name), + EXPECT_EQ(GetOutputFileContent(name, parameter, file, first_parsed_file_name, + first_message_name), lines[0]); EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]); EXPECT_EQ(kSecondInsertionPoint, lines[2 + insertion_list.size() * 2]); for (size_t i = 0; i < insertion_list.size(); i++) { - EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", - file, file, first_message_name), + EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert", file, + file, first_message_name), lines[1 + i]); // Second insertion point is indented, so the inserted text should // automatically be indented too. @@ -200,8 +200,8 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file, } else if (command == "HasJsonName") { FieldDescriptorProto field_descriptor_proto; file->message_type(i)->field(0)->CopyTo(&field_descriptor_proto); - std::cerr << "Saw json_name: " - << field_descriptor_proto.has_json_name() << std::endl; + std::cerr << "Saw json_name: " << field_descriptor_proto.has_json_name() + << std::endl; abort(); } else if (command == "Annotate") { annotate = true; @@ -210,8 +210,8 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file, context->GetCompilerVersion(&compiler_version); std::cerr << "Saw compiler_version: " << compiler_version.major() * 1000000 + - compiler_version.minor() * 1000 + - compiler_version.patch() + compiler_version.minor() * 1000 + + compiler_version.patch() << " " << compiler_version.suffix() << std::endl; abort(); } else { @@ -230,8 +230,8 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file, std::unique_ptr<io::ZeroCopyOutputStream> output(context->OpenForInsert( GetOutputFileName(insert_into[i], file), kFirstInsertionPointName)); io::Printer printer(output.get(), '$'); - printer.PrintRaw(GetOutputFileContent(name_, "first_insert", - file, context)); + printer.PrintRaw( + GetOutputFileContent(name_, "first_insert", file, context)); if (printer.failed()) { *error = "MockCodeGenerator detected write error."; return false; @@ -243,8 +243,8 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file, context->OpenForInsert(GetOutputFileName(insert_into[i], file), kSecondInsertionPointName)); io::Printer printer(output.get(), '$'); - printer.PrintRaw(GetOutputFileContent(name_, "second_insert", - file, context)); + printer.PrintRaw( + GetOutputFileContent(name_, "second_insert", file, context)); if (printer.failed()) { *error = "MockCodeGenerator detected write error."; return false; @@ -310,19 +310,17 @@ std::string MockCodeGenerator::GetOutputFileContent( std::vector<const FileDescriptor*> all_files; context->ListParsedFiles(&all_files); return GetOutputFileContent( - generator_name, parameter, file->name(), - CommaSeparatedList(all_files), - file->message_type_count() > 0 ? - file->message_type(0)->name() : "(none)"); + generator_name, parameter, file->name(), CommaSeparatedList(all_files), + file->message_type_count() > 0 ? file->message_type(0)->name() + : "(none)"); } std::string MockCodeGenerator::GetOutputFileContent( const std::string& generator_name, const std::string& parameter, const std::string& file, const std::string& parsed_file_list, const std::string& first_message_name) { - return strings::Substitute("$0: $1, $2, $3, $4\n", - generator_name, parameter, file, - first_message_name, parsed_file_list); + return strings::Substitute("$0: $1, $2, $3, $4\n", generator_name, parameter, + file, first_message_name, parsed_file_list); } } // namespace compiler diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc index 3d0b0d2933..bcf4d39a65 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc @@ -47,7 +47,7 @@ #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/io/zero_copy_stream_impl.h> -#include <google/protobuf/stubs/io_win32.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/stubs/port.h> #include <google/protobuf/stubs/strutil.h> @@ -63,7 +63,7 @@ namespace objectivec { // in this port namespace to avoid ambiguous definition. namespace posix { #ifdef _WIN32 -using ::google::protobuf::internal::win32::open; +using ::google::protobuf::io::win32::open; #else using ::open; #endif diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index 9264710419..c96fc174a2 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.cc @@ -65,23 +65,23 @@ typedef std::unordered_map<std::string, FieldDescriptorProto::Type> TypeNameMap; TypeNameMap MakeTypeNameTable() { TypeNameMap result; - result["double" ] = FieldDescriptorProto::TYPE_DOUBLE; - result["float" ] = FieldDescriptorProto::TYPE_FLOAT; - result["uint64" ] = FieldDescriptorProto::TYPE_UINT64; - result["fixed64" ] = FieldDescriptorProto::TYPE_FIXED64; - result["fixed32" ] = FieldDescriptorProto::TYPE_FIXED32; - result["bool" ] = FieldDescriptorProto::TYPE_BOOL; - result["string" ] = FieldDescriptorProto::TYPE_STRING; - result["group" ] = FieldDescriptorProto::TYPE_GROUP; - - result["bytes" ] = FieldDescriptorProto::TYPE_BYTES; - result["uint32" ] = FieldDescriptorProto::TYPE_UINT32; + result["double"] = FieldDescriptorProto::TYPE_DOUBLE; + result["float"] = FieldDescriptorProto::TYPE_FLOAT; + result["uint64"] = FieldDescriptorProto::TYPE_UINT64; + result["fixed64"] = FieldDescriptorProto::TYPE_FIXED64; + result["fixed32"] = FieldDescriptorProto::TYPE_FIXED32; + result["bool"] = FieldDescriptorProto::TYPE_BOOL; + result["string"] = FieldDescriptorProto::TYPE_STRING; + result["group"] = FieldDescriptorProto::TYPE_GROUP; + + result["bytes"] = FieldDescriptorProto::TYPE_BYTES; + result["uint32"] = FieldDescriptorProto::TYPE_UINT32; result["sfixed32"] = FieldDescriptorProto::TYPE_SFIXED32; result["sfixed64"] = FieldDescriptorProto::TYPE_SFIXED64; - result["int32" ] = FieldDescriptorProto::TYPE_INT32; - result["int64" ] = FieldDescriptorProto::TYPE_INT64; - result["sint32" ] = FieldDescriptorProto::TYPE_SINT32; - result["sint64" ] = FieldDescriptorProto::TYPE_SINT64; + result["int32"] = FieldDescriptorProto::TYPE_INT32; + result["int64"] = FieldDescriptorProto::TYPE_INT64; + result["sint32"] = FieldDescriptorProto::TYPE_SINT32; + result["sint64"] = FieldDescriptorProto::TYPE_SINT64; return result; } @@ -119,21 +119,23 @@ std::string MapEntryName(const std::string& field_name) { // Makes code slightly more readable. The meaning of "DO(foo)" is // "Execute foo and fail if it fails.", where failure is indicated by // returning false. -#define DO(STATEMENT) if (STATEMENT) {} else return false +#define DO(STATEMENT) \ + if (STATEMENT) { \ + } else \ + return false // =================================================================== Parser::Parser() - : input_(NULL), - error_collector_(NULL), - source_location_table_(NULL), - had_errors_(false), - require_syntax_identifier_(false), - stop_after_syntax_identifier_(false) { + : input_(NULL), + error_collector_(NULL), + source_location_table_(NULL), + had_errors_(false), + require_syntax_identifier_(false), + stop_after_syntax_identifier_(false) { } -Parser::~Parser() { -} +Parser::~Parser() {} // =================================================================== @@ -145,9 +147,7 @@ inline bool Parser::LookingAtType(io::Tokenizer::TokenType token_type) { return input_->current().type == token_type; } -inline bool Parser::AtEnd() { - return LookingAtType(io::Tokenizer::TYPE_END); -} +inline bool Parser::AtEnd() { return LookingAtType(io::Tokenizer::TYPE_END); } bool Parser::TryConsume(const char* text) { if (LookingAt(text)) { @@ -190,8 +190,8 @@ bool Parser::ConsumeIdentifier(std::string* output, const char* error) { bool Parser::ConsumeInteger(int* output, const char* error) { if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { uint64 value = 0; - if (!io::Tokenizer::ParseInteger(input_->current().text, - kint32max, &value)) { + if (!io::Tokenizer::ParseInteger(input_->current().text, kint32max, + &value)) { AddError("Integer out of range."); // We still return true because we did, in fact, parse an integer. } @@ -243,8 +243,8 @@ bool Parser::ConsumeNumber(double* output, const char* error) { } else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { // Also accept integers. uint64 value = 0; - if (!io::Tokenizer::ParseInteger(input_->current().text, - kuint64max, &value)) { + if (!io::Tokenizer::ParseInteger(input_->current().text, kuint64max, + &value)) { AddError("Integer out of range."); // We still return true because we did, in fact, parse a number. } @@ -281,8 +281,8 @@ bool Parser::ConsumeString(std::string* output, const char* error) { } } -bool Parser::TryConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location) { +bool Parser::TryConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location) { if (LookingAt(text)) { std::string leading, trailing; std::vector<std::string> detached; @@ -312,8 +312,8 @@ bool Parser::TryConsumeEndOfDeclaration( } } -bool Parser::ConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location) { +bool Parser::ConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location) { if (TryConsumeEndOfDeclaration(text, location)) { return true; } else { @@ -408,7 +408,8 @@ void Parser::LocationRecorder::EndAt(const io::Tokenizer::Token& token) { location_->add_span(token.end_column); } -void Parser::LocationRecorder::RecordLegacyLocation(const Message* descriptor, +void Parser::LocationRecorder::RecordLegacyLocation( + const Message* descriptor, DescriptorPool::ErrorCollector::ErrorLocation location) { if (parser_->source_location_table_ != NULL) { parser_->source_location_table_->Add( @@ -416,6 +417,14 @@ void Parser::LocationRecorder::RecordLegacyLocation(const Message* descriptor, } } +void Parser::LocationRecorder::RecordLegacyImportLocation( + const Message* descriptor, const string& name) { + if (parser_->source_location_table_ != nullptr) { + parser_->source_location_table_->AddImport( + descriptor, name, location_->span(0), location_->span(1)); + } +} + int Parser::LocationRecorder::CurrentPathSize() const { return location_->path_size(); } @@ -433,8 +442,7 @@ void Parser::LocationRecorder::AttachComments( location_->mutable_trailing_comments()->swap(*trailing); } for (int i = 0; i < detached_comments->size(); ++i) { - location_->add_leading_detached_comments()->swap( - (*detached_comments)[i]); + location_->add_leading_detached_comments()->swap((*detached_comments)[i]); } detached_comments->clear(); } @@ -551,6 +559,8 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) { { LocationRecorder root_location(this); + root_location.RecordLegacyLocation(file, + DescriptorPool::ErrorCollector::OTHER); if (require_syntax_identifier_ || LookingAt("syntax")) { if (!ParseSyntaxIdentifier(root_location)) { @@ -561,8 +571,8 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) { // Store the syntax into the file. if (file != NULL) file->set_syntax(syntax_identifier_); } else if (!stop_after_syntax_identifier_) { - GOOGLE_LOG(WARNING) << "No syntax specified for the proto file: " - << file->name() << ". Please use 'syntax = \"proto2\";' " + GOOGLE_LOG(WARNING) << "No syntax specified for the proto file: " << file->name() + << ". Please use 'syntax = \"proto2\";' " << "or 'syntax = \"proto3\";' to specify a syntax " << "version. (Defaulted to proto2 syntax.)"; syntax_identifier_ = "proto2"; @@ -610,8 +620,9 @@ bool Parser::ParseSyntaxIdentifier(const LocationRecorder& parent) { if (syntax != "proto2" && syntax != "proto3" && !stop_after_syntax_identifier_) { AddError(syntax_token.line, syntax_token.column, - "Unrecognized syntax identifier \"" + syntax + "\". This parser " - "only recognizes \"proto2\" and \"proto3\"."); + "Unrecognized syntax identifier \"" + syntax + + "\". This parser " + "only recognizes \"proto2\" and \"proto3\"."); return false; } @@ -625,34 +636,34 @@ bool Parser::ParseTopLevelStatement(FileDescriptorProto* file, return true; } else if (LookingAt("message")) { LocationRecorder location(root_location, - FileDescriptorProto::kMessageTypeFieldNumber, file->message_type_size()); + FileDescriptorProto::kMessageTypeFieldNumber, + file->message_type_size()); return ParseMessageDefinition(file->add_message_type(), location, file); } else if (LookingAt("enum")) { LocationRecorder location(root_location, - FileDescriptorProto::kEnumTypeFieldNumber, file->enum_type_size()); + FileDescriptorProto::kEnumTypeFieldNumber, + file->enum_type_size()); return ParseEnumDefinition(file->add_enum_type(), location, file); } else if (LookingAt("service")) { LocationRecorder location(root_location, - FileDescriptorProto::kServiceFieldNumber, file->service_size()); + FileDescriptorProto::kServiceFieldNumber, + file->service_size()); return ParseServiceDefinition(file->add_service(), location, file); } else if (LookingAt("extend")) { LocationRecorder location(root_location, - FileDescriptorProto::kExtensionFieldNumber); - return ParseExtend(file->mutable_extension(), - file->mutable_message_type(), - root_location, - FileDescriptorProto::kMessageTypeFieldNumber, - location, file); + FileDescriptorProto::kExtensionFieldNumber); + return ParseExtend( + file->mutable_extension(), file->mutable_message_type(), root_location, + FileDescriptorProto::kMessageTypeFieldNumber, location, file); } else if (LookingAt("import")) { return ParseImport(file->mutable_dependency(), file->mutable_public_dependency(), - file->mutable_weak_dependency(), - root_location, file); + file->mutable_weak_dependency(), root_location, file); } else if (LookingAt("package")) { return ParsePackage(file, root_location, file); } else if (LookingAt("option")) { LocationRecorder location(root_location, - FileDescriptorProto::kOptionsFieldNumber); + FileDescriptorProto::kOptionsFieldNumber); return ParseOption(file->mutable_options(), location, file, OPTION_STATEMENT); } else { @@ -665,15 +676,14 @@ bool Parser::ParseTopLevelStatement(FileDescriptorProto* file, // Messages bool Parser::ParseMessageDefinition( - DescriptorProto* message, - const LocationRecorder& message_location, + DescriptorProto* message, const LocationRecorder& message_location, const FileDescriptorProto* containing_file) { DO(Consume("message")); { LocationRecorder location(message_location, DescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - message, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(message, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(message->mutable_name(), "Expected message name.")); } DO(ParseMessageBlock(message, message_location, containing_file)); @@ -702,9 +712,8 @@ bool IsMessageSetWireFormatMessage(const DescriptorProto& message) { // tag number can only be determined after all options have been parsed. void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) { const bool is_message_set = IsMessageSetWireFormatMessage(*message); - const int max_extension_number = is_message_set ? - kint32max : - FieldDescriptor::kMaxNumber + 1; + const int max_extension_number = + is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1; for (int i = 0; i < message->extension_range_size(); ++i) { if (message->extension_range(i).end() == kMaxRangeSentinel) { message->mutable_extension_range(i)->set_end(max_extension_number); @@ -717,9 +726,8 @@ void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) { // tag number can only be determined after all options have been parsed. void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) { const bool is_message_set = IsMessageSetWireFormatMessage(*message); - const int max_field_number = is_message_set ? - kint32max : - FieldDescriptor::kMaxNumber + 1; + const int max_field_number = + is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1; for (int i = 0; i < message->reserved_range_size(); ++i) { if (message->reserved_range(i).end() == kMaxRangeSentinel) { message->mutable_reserved_range(i)->set_end(max_field_number); @@ -784,34 +792,28 @@ bool Parser::ParseMessageStatement(DescriptorProto* message, LocationRecorder location(message_location, DescriptorProto::kExtensionFieldNumber); return ParseExtend(message->mutable_extension(), - message->mutable_nested_type(), - message_location, - DescriptorProto::kNestedTypeFieldNumber, - location, containing_file); + message->mutable_nested_type(), message_location, + DescriptorProto::kNestedTypeFieldNumber, location, + containing_file); } else if (LookingAt("option")) { LocationRecorder location(message_location, DescriptorProto::kOptionsFieldNumber); - return ParseOption(message->mutable_options(), location, - containing_file, OPTION_STATEMENT); + return ParseOption(message->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else if (LookingAt("oneof")) { int oneof_index = message->oneof_decl_size(); - LocationRecorder oneof_location(message_location, - DescriptorProto::kOneofDeclFieldNumber, - oneof_index); + LocationRecorder oneof_location( + message_location, DescriptorProto::kOneofDeclFieldNumber, oneof_index); - return ParseOneof(message->add_oneof_decl(), message, - oneof_index, oneof_location, message_location, - containing_file); + return ParseOneof(message->add_oneof_decl(), message, oneof_index, + oneof_location, message_location, containing_file); } else { LocationRecorder location(message_location, DescriptorProto::kFieldFieldNumber, message->field_size()); - return ParseMessageField(message->add_field(), - message->mutable_nested_type(), - message_location, - DescriptorProto::kNestedTypeFieldNumber, - location, - containing_file); + return ParseMessageField( + message->add_field(), message->mutable_nested_type(), message_location, + DescriptorProto::kNestedTypeFieldNumber, location, containing_file); } } @@ -839,13 +841,11 @@ bool Parser::ParseMessageField(FieldDescriptorProto* field, return ParseMessageFieldNoLabel(field, messages, parent_location, location_field_number_for_nested_type, - field_location, - containing_file); + field_location, containing_file); } bool Parser::ParseMessageFieldNoLabel( - FieldDescriptorProto* field, - RepeatedPtrField<DescriptorProto>* messages, + FieldDescriptorProto* field, RepeatedPtrField<DescriptorProto>* messages, const LocationRecorder& parent_location, int location_field_number_for_nested_type, const LocationRecorder& field_location, @@ -936,8 +936,8 @@ bool Parser::ParseMessageFieldNoLabel( { LocationRecorder location(field_location, FieldDescriptorProto::kNumberFieldNumber); - location.RecordLegacyLocation( - field, DescriptorPool::ErrorCollector::NUMBER); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::NUMBER); int number; DO(ConsumeInteger(&number, "Expected field number.")); field->set_number(number); @@ -964,8 +964,8 @@ bool Parser::ParseMessageFieldNoLabel( DescriptorProto::kNameFieldNumber); location.StartAt(name_token); location.EndAt(name_token); - location.RecordLegacyLocation( - group, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(group, + DescriptorPool::ErrorCollector::NAME); } // The field's type_name also comes from the name. Confusing! @@ -981,7 +981,7 @@ bool Parser::ParseMessageFieldNoLabel( // not use groups; it should use nested messages. if (group->name()[0] < 'A' || 'Z' < group->name()[0]) { AddError(name_token.line, name_token.column, - "Group names must start with a capital letter."); + "Group names must start with a capital letter."); } LowerString(field->mutable_name()); @@ -1057,12 +1057,12 @@ void Parser::GenerateMapEntry(const MapField& map_field, option.name(0).name_part() == "enforce_utf8" && !option.name(0).is_extension()) { if (key_field->type() == FieldDescriptorProto::TYPE_STRING) { - key_field->mutable_options()->add_uninterpreted_option() - ->CopyFrom(option); + key_field->mutable_options()->add_uninterpreted_option()->CopyFrom( + option); } if (value_field->type() == FieldDescriptorProto::TYPE_STRING) { - value_field->mutable_options()->add_uninterpreted_option() - ->CopyFrom(option); + value_field->mutable_options()->add_uninterpreted_option()->CopyFrom( + option); } } } @@ -1088,8 +1088,8 @@ bool Parser::ParseFieldOptions(FieldDescriptorProto* field, // Like default value, this "json_name" is not an actual option. DO(ParseJsonName(field, field_location, containing_file)); } else { - DO(ParseOption(field->mutable_options(), location, - containing_file, OPTION_ASSIGNMENT)); + DO(ParseOption(field->mutable_options(), location, containing_file, + OPTION_ASSIGNMENT)); } } while (TryConsume(",")); @@ -1098,8 +1098,7 @@ bool Parser::ParseFieldOptions(FieldDescriptorProto* field, } bool Parser::ParseDefaultAssignment( - FieldDescriptorProto* field, - const LocationRecorder& field_location, + FieldDescriptorProto* field, const LocationRecorder& field_location, const FileDescriptorProto* containing_file) { if (field->has_default_value()) { AddError("Already set option \"default\"."); @@ -1111,8 +1110,8 @@ bool Parser::ParseDefaultAssignment( LocationRecorder location(field_location, FieldDescriptorProto::kDefaultValueFieldNumber); - location.RecordLegacyLocation( - field, DescriptorPool::ErrorCollector::DEFAULT_VALUE); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::DEFAULT_VALUE); std::string* default_value = field->mutable_default_value(); if (!field->has_type()) { @@ -1211,7 +1210,8 @@ bool Parser::ParseDefaultAssignment( // Note: When file opton java_string_check_utf8 is true, if a // non-string representation (eg byte[]) is later supported, it must // be checked for UTF-8-ness. - DO(ConsumeString(default_value, "Expected string for field default " + DO(ConsumeString(default_value, + "Expected string for field default " "value.")); break; @@ -1221,8 +1221,9 @@ bool Parser::ParseDefaultAssignment( break; case FieldDescriptorProto::TYPE_ENUM: - DO(ConsumeIdentifier(default_value, "Expected enum identifier for field " - "default value.")); + DO(ConsumeIdentifier(default_value, + "Expected enum identifier for field " + "default value.")); break; case FieldDescriptorProto::TYPE_MESSAGE: @@ -1234,34 +1235,37 @@ bool Parser::ParseDefaultAssignment( return true; } -bool Parser::ParseJsonName( - FieldDescriptorProto* field, - const LocationRecorder& field_location, - const FileDescriptorProto* containing_file) { +bool Parser::ParseJsonName(FieldDescriptorProto* field, + const LocationRecorder& field_location, + const FileDescriptorProto* containing_file) { if (field->has_json_name()) { AddError("Already set option \"json_name\"."); field->clear_json_name(); } + LocationRecorder location(field_location, + FieldDescriptorProto::kJsonNameFieldNumber); + location.RecordLegacyLocation(field, + DescriptorPool::ErrorCollector::OPTION_NAME); + DO(Consume("json_name")); DO(Consume("=")); - LocationRecorder location(field_location, - FieldDescriptorProto::kJsonNameFieldNumber); - location.RecordLegacyLocation( + LocationRecorder value_location(location); + value_location.RecordLegacyLocation( field, DescriptorPool::ErrorCollector::OPTION_VALUE); + DO(ConsumeString(field->mutable_json_name(), "Expected string for JSON name.")); return true; } - bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option, const LocationRecorder& part_location, const FileDescriptorProto* containing_file) { UninterpretedOption::NamePart* name = uninterpreted_option->add_name(); std::string identifier; // We parse identifiers into this string. - if (LookingAt("(")) { // This is an extension. + if (LookingAt("(")) { // This is an extension. DO(Consume("(")); { @@ -1325,8 +1329,8 @@ bool Parser::ParseOption(Message* options, const FileDescriptorProto* containing_file, OptionStyle style) { // Create an entry in the uninterpreted_option field. - const FieldDescriptor* uninterpreted_option_field = options->GetDescriptor()-> - FindFieldByName("uninterpreted_option"); + const FieldDescriptor* uninterpreted_option_field = + options->GetDescriptor()->FindFieldByName("uninterpreted_option"); GOOGLE_CHECK(uninterpreted_option_field != NULL) << "No field named \"uninterpreted_option\" in the Options proto."; @@ -1340,9 +1344,9 @@ bool Parser::ParseOption(Message* options, DO(Consume("option")); } - UninterpretedOption* uninterpreted_option = down_cast<UninterpretedOption*>( - options->GetReflection()->AddMessage(options, - uninterpreted_option_field)); + UninterpretedOption* uninterpreted_option = + down_cast<UninterpretedOption*>(options->GetReflection()->AddMessage( + options, uninterpreted_option_field)); // Parse dot-separated name. { @@ -1473,8 +1477,8 @@ bool Parser::ParseExtensions(DescriptorProto* message, message->extension_range_size()); DescriptorProto::ExtensionRange* range = message->add_extension_range(); - location.RecordLegacyLocation( - range, DescriptorPool::ErrorCollector::NUMBER); + location.RecordLegacyLocation(range, + DescriptorPool::ErrorCollector::NUMBER); int start, end; io::Tokenizer::Token start_token; @@ -1539,8 +1543,8 @@ bool Parser::ParseExtensions(DescriptorProto* message, // Then copy the extension range options to all of the other ranges we've // parsed. for (int i = old_range_size + 1; i < message->extension_range_size(); i++) { - message->mutable_extension_range(i)->mutable_options() - ->CopyFrom(*options); + message->mutable_extension_range(i)->mutable_options()->CopyFrom( + *options); } // and copy source locations to the other ranges, too for (int i = old_range_size; i < message->extension_range_size(); i++) { @@ -1604,9 +1608,8 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message, LocationRecorder start_location( location, DescriptorProto::ReservedRange::kStartFieldNumber); start_token = input_->current(); - DO(ConsumeInteger(&start, (first ? - "Expected field name or number range." : - "Expected field number range."))); + DO(ConsumeInteger(&start, (first ? "Expected field name or number range." + : "Expected field number range."))); } if (TryConsume("to")) { @@ -1683,9 +1686,9 @@ bool Parser::ParseReservedNumbers(EnumDescriptorProto* message, LocationRecorder start_location( location, EnumDescriptorProto::EnumReservedRange::kStartFieldNumber); start_token = input_->current(); - DO(ConsumeSignedInteger(&start, (first ? - "Expected enum value or number range." : - "Expected enum number range."))); + DO(ConsumeSignedInteger(&start, + (first ? "Expected enum value or number range." + : "Expected enum number range."))); } if (TryConsume("to")) { @@ -1761,8 +1764,7 @@ bool Parser::ParseExtend(RepeatedPtrField<FieldDescriptorProto>* extensions, field->set_extendee(extendee); if (!ParseMessageField(field, messages, parent_location, - location_field_number_for_nested_type, - location, + location_field_number_for_nested_type, location, containing_file)) { // This statement failed to parse. Skip it, but keep looping to parse // other statements. @@ -1774,8 +1776,7 @@ bool Parser::ParseExtend(RepeatedPtrField<FieldDescriptorProto>* extensions, } bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, - DescriptorProto* containing_type, - int oneof_index, + DescriptorProto* containing_type, int oneof_index, const LocationRecorder& oneof_location, const LocationRecorder& containing_type_location, const FileDescriptorProto* containing_file) { @@ -1807,11 +1808,11 @@ bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, // Print a nice error if the user accidentally tries to place a label // on an individual member of a oneof. - if (LookingAt("required") || - LookingAt("optional") || + if (LookingAt("required") || LookingAt("optional") || LookingAt("repeated")) { - AddError("Fields in oneofs must not have labels (required / optional " - "/ repeated)."); + AddError( + "Fields in oneofs must not have labels (required / optional " + "/ repeated)."); // We can continue parsing here because we understand what the user // meant. The error report will still make parsing fail overall. input_->Next(); @@ -1825,12 +1826,10 @@ bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl, field->set_label(FieldDescriptorProto::LABEL_OPTIONAL); field->set_oneof_index(oneof_index); - if (!ParseMessageFieldNoLabel(field, - containing_type->mutable_nested_type(), + if (!ParseMessageFieldNoLabel(field, containing_type->mutable_nested_type(), containing_type_location, DescriptorProto::kNestedTypeFieldNumber, - field_location, - containing_file)) { + field_location, containing_file)) { // This statement failed to parse. Skip it, but keep looping to parse // other statements. SkipStatement(); @@ -1851,8 +1850,8 @@ bool Parser::ParseEnumDefinition(EnumDescriptorProto* enum_type, { LocationRecorder location(enum_location, EnumDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - enum_type, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(enum_type, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(enum_type->mutable_name(), "Expected enum name.")); } @@ -1893,13 +1892,14 @@ bool Parser::ParseEnumStatement(EnumDescriptorProto* enum_type, } else if (LookingAt("option")) { LocationRecorder location(enum_location, EnumDescriptorProto::kOptionsFieldNumber); - return ParseOption(enum_type->mutable_options(), location, - containing_file, OPTION_STATEMENT); + return ParseOption(enum_type->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else if (LookingAt("reserved")) { return ParseReserved(enum_type, enum_location); } else { LocationRecorder location(enum_location, - EnumDescriptorProto::kValueFieldNumber, enum_type->value_size()); + EnumDescriptorProto::kValueFieldNumber, + enum_type->value_size()); return ParseEnumConstant(enum_type->add_value(), location, containing_file); } } @@ -1911,8 +1911,8 @@ bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value, { LocationRecorder location(enum_value_location, EnumValueDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - enum_value, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(enum_value, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(enum_value->mutable_name(), "Expected enum constant name.")); } @@ -1921,10 +1921,10 @@ bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value, // Parse value. { - LocationRecorder location( - enum_value_location, EnumValueDescriptorProto::kNumberFieldNumber); - location.RecordLegacyLocation( - enum_value, DescriptorPool::ErrorCollector::NUMBER); + LocationRecorder location(enum_value_location, + EnumValueDescriptorProto::kNumberFieldNumber); + location.RecordLegacyLocation(enum_value, + DescriptorPool::ErrorCollector::NUMBER); int number; DO(ConsumeSignedInteger(&number, "Expected integer.")); @@ -1945,14 +1945,14 @@ bool Parser::ParseEnumConstantOptions( const FileDescriptorProto* containing_file) { if (!LookingAt("[")) return true; - LocationRecorder location( - enum_value_location, EnumValueDescriptorProto::kOptionsFieldNumber); + LocationRecorder location(enum_value_location, + EnumValueDescriptorProto::kOptionsFieldNumber); DO(Consume("[")); do { - DO(ParseOption(value->mutable_options(), location, - containing_file, OPTION_ASSIGNMENT)); + DO(ParseOption(value->mutable_options(), location, containing_file, + OPTION_ASSIGNMENT)); } while (TryConsume(",")); DO(Consume("]")); @@ -1963,16 +1963,15 @@ bool Parser::ParseEnumConstantOptions( // Services bool Parser::ParseServiceDefinition( - ServiceDescriptorProto* service, - const LocationRecorder& service_location, + ServiceDescriptorProto* service, const LocationRecorder& service_location, const FileDescriptorProto* containing_file) { DO(Consume("service")); { LocationRecorder location(service_location, ServiceDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - service, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(service, + DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(service->mutable_name(), "Expected service name.")); } @@ -2008,13 +2007,14 @@ bool Parser::ParseServiceStatement(ServiceDescriptorProto* service, // empty statement; ignore return true; } else if (LookingAt("option")) { - LocationRecorder location( - service_location, ServiceDescriptorProto::kOptionsFieldNumber); - return ParseOption(service->mutable_options(), location, - containing_file, OPTION_STATEMENT); + LocationRecorder location(service_location, + ServiceDescriptorProto::kOptionsFieldNumber); + return ParseOption(service->mutable_options(), location, containing_file, + OPTION_STATEMENT); } else { LocationRecorder location(service_location, - ServiceDescriptorProto::kMethodFieldNumber, service->method_size()); + ServiceDescriptorProto::kMethodFieldNumber, + service->method_size()); return ParseServiceMethod(service->add_method(), location, containing_file); } } @@ -2027,8 +2027,7 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, { LocationRecorder location(method_location, MethodDescriptorProto::kNameFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::NAME); + location.RecordLegacyLocation(method, DescriptorPool::ErrorCollector::NAME); DO(ConsumeIdentifier(method->mutable_name(), "Expected method name.")); } @@ -2038,16 +2037,16 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, if (LookingAt("stream")) { LocationRecorder location( method_location, MethodDescriptorProto::kClientStreamingFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OTHER); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OTHER); method->set_client_streaming(true); DO(Consume("stream")); } LocationRecorder location(method_location, MethodDescriptorProto::kInputTypeFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::INPUT_TYPE); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::INPUT_TYPE); DO(ParseUserDefinedType(method->mutable_input_type())); } DO(Consume(")")); @@ -2059,16 +2058,16 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method, if (LookingAt("stream")) { LocationRecorder location( method_location, MethodDescriptorProto::kServerStreamingFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OTHER); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OTHER); DO(Consume("stream")); method->set_server_streaming(true); } LocationRecorder location(method_location, MethodDescriptorProto::kOutputTypeFieldNumber); - location.RecordLegacyLocation( - method, DescriptorPool::ErrorCollector::OUTPUT_TYPE); + location.RecordLegacyLocation(method, + DescriptorPool::ErrorCollector::OUTPUT_TYPE); DO(ParseUserDefinedType(method->mutable_output_type())); } DO(Consume(")")); @@ -2101,10 +2100,9 @@ bool Parser::ParseMethodOptions(const LocationRecorder& parent_location, if (TryConsumeEndOfDeclaration(";", NULL)) { // empty statement; ignore } else { - LocationRecorder location(parent_location, - optionsFieldNumber); - if (!ParseOption(mutable_options, location, - containing_file, OPTION_STATEMENT)) { + LocationRecorder location(parent_location, optionsFieldNumber); + if (!ParseOption(mutable_options, location, containing_file, + OPTION_STATEMENT)) { // This statement failed to parse. Skip it, but keep looping to // parse other statements. SkipStatement(); @@ -2231,12 +2229,17 @@ bool Parser::ParseImport(RepeatedPtrField<std::string>* dependency, LocationRecorder weak_location( root_location, FileDescriptorProto::kWeakDependencyFieldNumber, weak_dependency->size()); + weak_location.RecordLegacyImportLocation(containing_file, "weak"); DO(Consume("weak")); *weak_dependency->Add() = dependency->size(); } - DO(ConsumeString(dependency->Add(), + string import_file; + DO(ConsumeString(&import_file, "Expected a string naming the file to import.")); + *dependency->Add() = import_file; + location.RecordLegacyImportLocation(containing_file, import_file); + DO(ConsumeEndOfDeclaration(";", &location)); return true; @@ -2249,16 +2252,32 @@ SourceLocationTable::~SourceLocationTable() {} bool SourceLocationTable::Find( const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int* line, int* column) const { + DescriptorPool::ErrorCollector::ErrorLocation location, int* line, + int* column) const { const std::pair<int, int>* result = FindOrNull(location_map_, std::make_pair(descriptor, location)); if (result == NULL) { - *line = -1; + *line = -1; *column = 0; return false; } else { - *line = result->first; + *line = result->first; + *column = result->second; + return true; + } +} + +bool SourceLocationTable::FindImport(const Message* descriptor, + const string& name, int* line, + int* column) const { + const std::pair<int, int>* result = + FindOrNull(import_location_map_, std::make_pair(descriptor, name)); + if (result == nullptr) { + *line = -1; + *column = 0; + return false; + } else { + *line = result->first; *column = result->second; return true; } @@ -2266,16 +2285,20 @@ bool SourceLocationTable::Find( void SourceLocationTable::Add( const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int line, int column) { + DescriptorPool::ErrorCollector::ErrorLocation location, int line, + int column) { location_map_[std::make_pair(descriptor, location)] = std::make_pair(line, column); } -void SourceLocationTable::Clear() { - location_map_.clear(); +void SourceLocationTable::AddImport(const Message* descriptor, + const string& name, int line, int column) { + import_location_map_[std::make_pair(descriptor, name)] = + std::make_pair(line, column); } +void SourceLocationTable::Clear() { location_map_.clear(); } + } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/parser.h b/src/google/protobuf/compiler/parser.h index a5cb7a87a9..6ec19a1f19 100644 --- a/src/google/protobuf/compiler/parser.h +++ b/src/google/protobuf/compiler/parser.h @@ -188,13 +188,13 @@ class PROTOBUF_EXPORT Parser { // have been passed around by const reference, for no particularly good // reason. We should probably go through and change them all to mutable // pointer to make this more intuitive. - bool TryConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location); - bool TryConsumeEndOfDeclarationFinishScope( - const char* text, const LocationRecorder* location); + bool TryConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location); + bool TryConsumeEndOfDeclarationFinishScope(const char* text, + const LocationRecorder* location); - bool ConsumeEndOfDeclaration( - const char* text, const LocationRecorder* location); + bool ConsumeEndOfDeclaration(const char* text, + const LocationRecorder* location); // ----------------------------------------------------------------- // Error logging helpers @@ -254,8 +254,11 @@ class PROTOBUF_EXPORT Parser { // was passed to RecordSourceLocationsTo(), if any. SourceLocationTable // is an older way of keeping track of source locations which is still // used in some places. - void RecordLegacyLocation(const Message* descriptor, + void RecordLegacyLocation( + const Message* descriptor, DescriptorPool::ErrorCollector::ErrorLocation location); + void RecordLegacyImportLocation(const Message* descriptor, + const string& name); // Returns the number of path components in the recorder's current location. int CurrentPathSize() const; @@ -402,8 +405,7 @@ class PROTOBUF_EXPORT Parser { // oneof_decl->label() since it will have had to parse the label before it // knew it was parsing a oneof. bool ParseOneof(OneofDescriptorProto* oneof_decl, - DescriptorProto* containing_type, - int oneof_index, + DescriptorProto* containing_type, int oneof_index, const LocationRecorder& oneof_location, const LocationRecorder& containing_type_location, const FileDescriptorProto* containing_file); @@ -467,8 +469,7 @@ class PROTOBUF_EXPORT Parser { // Parse a single option name/value pair, e.g. "ctype = CORD". The name // identifies a field of the given Message, and the value of that field // is set to the parsed value. - bool ParseOption(Message* options, - const LocationRecorder& options_location, + bool ParseOption(Message* options, const LocationRecorder& options_location, const FileDescriptorProto* containing_file, OptionStyle style); @@ -560,22 +561,29 @@ class PROTOBUF_EXPORT SourceLocationTable { // location" in the ErrorCollector interface). Returns true if found, false // otherwise. bool Find(const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int* line, int* column) const; + DescriptorPool::ErrorCollector::ErrorLocation location, int* line, + int* column) const; + bool FindImport(const Message* descriptor, const string& name, int* line, + int* column) const; // Adds a location to the table. void Add(const Message* descriptor, - DescriptorPool::ErrorCollector::ErrorLocation location, - int line, int column); + DescriptorPool::ErrorCollector::ErrorLocation location, int line, + int column); + void AddImport(const Message* descriptor, const string& name, int line, + int column); // Clears the contents of the table. void Clear(); private: typedef std::map< - std::pair<const Message*, DescriptorPool::ErrorCollector::ErrorLocation>, - std::pair<int, int> > LocationMap; + std::pair<const Message*, DescriptorPool::ErrorCollector::ErrorLocation>, + std::pair<int, int> > + LocationMap; LocationMap location_map_; + std::map<std::pair<const Message*, string>, std::pair<int, int> > + import_location_map_; }; } // namespace compiler diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc index 98280f85d9..cf7a85c603 100644 --- a/src/google/protobuf/compiler/parser_unittest.cc +++ b/src/google/protobuf/compiler/parser_unittest.cc @@ -32,10 +32,10 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include <memory> -#include <vector> #include <algorithm> #include <map> +#include <memory> +#include <vector> #include <google/protobuf/compiler/parser.h> @@ -69,8 +69,7 @@ class MockErrorCollector : public io::ErrorCollector { // implements ErrorCollector --------------------------------------- void AddError(int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line, column, message); + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line, column, message); } }; @@ -78,8 +77,8 @@ class MockValidationErrorCollector : public DescriptorPool::ErrorCollector { public: MockValidationErrorCollector(const SourceLocationTable& source_locations, io::ErrorCollector* wrapped_collector) - : source_locations_(source_locations), - wrapped_collector_(wrapped_collector) {} + : source_locations_(source_locations), + wrapped_collector_(wrapped_collector) {} ~MockValidationErrorCollector() {} // implements ErrorCollector --------------------------------------- @@ -87,7 +86,11 @@ class MockValidationErrorCollector : public DescriptorPool::ErrorCollector { const Message* descriptor, ErrorLocation location, const std::string& message) { int line, column; - source_locations_.Find(descriptor, location, &line, &column); + if (location == DescriptorPool::ErrorCollector::IMPORT) { + source_locations_.FindImport(descriptor, element_name, &line, &column); + } else { + source_locations_.Find(descriptor, location, &line, &column); + } wrapped_collector_->AddError(line, column, message); } @@ -98,8 +101,7 @@ class MockValidationErrorCollector : public DescriptorPool::ErrorCollector { class ParserTest : public testing::Test { protected: - ParserTest() - : require_syntax_identifier_(false) {} + ParserTest() : require_syntax_identifier_(false) {} // Set up the parser to parse the given text. void SetupParser(const char* text) { @@ -164,10 +166,10 @@ class ParserTest : public testing::Test { EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); ASSERT_EQ("", error_collector_.text_); - MockValidationErrorCollector validation_error_collector( - source_locations, &error_collector_); + MockValidationErrorCollector validation_error_collector(source_locations, + &error_collector_); EXPECT_TRUE(pool_.BuildFileCollectingErrors( - file, &validation_error_collector) == NULL); + file, &validation_error_collector) == NULL); EXPECT_EQ(expected_errors, error_collector_.text_); } @@ -184,9 +186,9 @@ class ParserTest : public testing::Test { TEST_F(ParserTest, StopAfterSyntaxIdentifier) { SetupParser( - "// blah\n" - "syntax = \"foobar\";\n" - "this line will not be parsed\n"); + "// blah\n" + "syntax = \"foobar\";\n" + "this line will not be parsed\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_TRUE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("", error_collector_.text_); @@ -195,8 +197,8 @@ TEST_F(ParserTest, StopAfterSyntaxIdentifier) { TEST_F(ParserTest, StopAfterOmittedSyntaxIdentifier) { SetupParser( - "// blah\n" - "this line will not be parsed\n"); + "// blah\n" + "this line will not be parsed\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_TRUE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("", error_collector_.text_); @@ -205,8 +207,8 @@ TEST_F(ParserTest, StopAfterOmittedSyntaxIdentifier) { TEST_F(ParserTest, StopAfterSyntaxIdentifierWithErrors) { SetupParser( - "// blah\n" - "syntax = error;\n"); + "// blah\n" + "syntax = error;\n"); parser_->SetStopAfterSyntaxIdentifier(true); EXPECT_FALSE(parser_->Parse(input_.get(), NULL)); EXPECT_EQ("1:9: Expected syntax identifier.\n", error_collector_.text_); @@ -217,8 +219,8 @@ TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) { FileDescriptorProto file; CaptureTestStderr(); EXPECT_TRUE(parser_->Parse(input_.get(), &file)); - EXPECT_TRUE( - GetCapturedTestStderr().find("No syntax specified") != string::npos); + EXPECT_TRUE(GetCapturedTestStderr().find("No syntax specified") != + string::npos); } // =================================================================== @@ -226,22 +228,25 @@ TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) { typedef ParserTest ParseMessageTest; TEST_F(ParseMessageTest, IgnoreBOM) { - char input[] = " message TestMessage {\n" + char input[] = + " message TestMessage {\n" " required int32 foo = 1;\n" "}\n"; // Set UTF-8 BOM. input[0] = (char)0xEF; input[1] = (char)0xBB; input[2] = (char)0xBF; - ExpectParsesTo(input, - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + ExpectParsesTo( + input, + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); } TEST_F(ParseMessageTest, BOMError) { - char input[] = " message TestMessage {\n" + char input[] = + " message TestMessage {\n" " required int32 foo = 1;\n" "}\n"; input[0] = (char)0xEF; @@ -253,590 +258,634 @@ TEST_F(ParseMessageTest, BOMError) { TEST_F(ParseMessageTest, SimpleMessage) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); } TEST_F(ParseMessageTest, ImplicitSyntaxIdentifier) { require_syntax_identifier_ = false; ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, ExplicitSyntaxIdentifier) { ExpectParsesTo( - "syntax = \"proto2\";\n" - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "syntax: 'proto2' " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "syntax = \"proto2\";\n" + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "syntax: 'proto2' " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, ExplicitRequiredSyntaxIdentifier) { require_syntax_identifier_ = true; ExpectParsesTo( - "syntax = \"proto2\";\n" - "message TestMessage {\n" - " required int32 foo = 1;\n" - "}\n", - - "syntax: 'proto2' " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - "}"); + "syntax = \"proto2\";\n" + "message TestMessage {\n" + " required int32 foo = 1;\n" + "}\n", + + "syntax: 'proto2' " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + "}"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseMessageTest, SimpleFields) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 15;\n" - " optional int32 bar = 34;\n" - " repeated int32 baz = 3;\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 15;\n" + " optional int32 bar = 34;\n" + " repeated int32 baz = 3;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:15 }" - " field { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:34 }" - " field { name:\"baz\" label:LABEL_REPEATED type:TYPE_INT32 number:3 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:15 }" + " field { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:34 }" + " field { name:\"baz\" label:LABEL_REPEATED type:TYPE_INT32 number:3 }" + "}"); } TEST_F(ParseMessageTest, PrimitiveFieldTypes) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - " required int64 foo = 1;\n" - " required uint32 foo = 1;\n" - " required uint64 foo = 1;\n" - " required sint32 foo = 1;\n" - " required sint64 foo = 1;\n" - " required fixed32 foo = 1;\n" - " required fixed64 foo = 1;\n" - " required sfixed32 foo = 1;\n" - " required sfixed64 foo = 1;\n" - " required float foo = 1;\n" - " required double foo = 1;\n" - " required string foo = 1;\n" - " required bytes foo = 1;\n" - " required bool foo = 1;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED32 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED64 number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FLOAT number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_DOUBLE number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_STRING number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BYTES number:1 }" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BOOL number:1 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + " required int64 foo = 1;\n" + " required uint32 foo = 1;\n" + " required uint64 foo = 1;\n" + " required sint32 foo = 1;\n" + " required sint64 foo = 1;\n" + " required fixed32 foo = 1;\n" + " required fixed64 foo = 1;\n" + " required sfixed32 foo = 1;\n" + " required sfixed64 foo = 1;\n" + " required float foo = 1;\n" + " required double foo = 1;\n" + " required string foo = 1;\n" + " required bytes foo = 1;\n" + " required bool foo = 1;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_UINT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SINT64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FIXED64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED32 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_SFIXED64 number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_FLOAT number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_DOUBLE number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_STRING number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BYTES number:1 " + "}" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_BOOL number:1 " + "}" + "}"); } TEST_F(ParseMessageTest, FieldDefaults) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1 [default= 1 ];\n" - " required int32 foo = 1 [default= -2 ];\n" - " required int64 foo = 1 [default= 3 ];\n" - " required int64 foo = 1 [default= -4 ];\n" - " required uint32 foo = 1 [default= 5 ];\n" - " required uint64 foo = 1 [default= 6 ];\n" - " required float foo = 1 [default= 7.5];\n" - " required float foo = 1 [default= -8.5];\n" - " required float foo = 1 [default= 9 ];\n" - " required double foo = 1 [default= 10.5];\n" - " required double foo = 1 [default=-11.5];\n" - " required double foo = 1 [default= 12 ];\n" - " required double foo = 1 [default= inf ];\n" - " required double foo = 1 [default=-inf ];\n" - " required double foo = 1 [default= nan ];\n" - " required string foo = 1 [default='13\\001'];\n" - " required string foo = 1 [default='a' \"b\" \n \"c\"];\n" - " required bytes foo = 1 [default='14\\002'];\n" - " required bytes foo = 1 [default='a' \"b\" \n 'c'];\n" - " required bool foo = 1 [default=true ];\n" - " required Foo foo = 1 [default=FOO ];\n" - - " required int32 foo = 1 [default= 0x7FFFFFFF];\n" - " required int32 foo = 1 [default=-0x80000000];\n" - " required uint32 foo = 1 [default= 0xFFFFFFFF];\n" - " required int64 foo = 1 [default= 0x7FFFFFFFFFFFFFFF];\n" - " required int64 foo = 1 [default=-0x8000000000000000];\n" - " required uint64 foo = 1 [default= 0xFFFFFFFFFFFFFFFF];\n" - " required double foo = 1 [default= 0xabcd];\n" - "}\n", + "message TestMessage {\n" + " required int32 foo = 1 [default= 1 ];\n" + " required int32 foo = 1 [default= -2 ];\n" + " required int64 foo = 1 [default= 3 ];\n" + " required int64 foo = 1 [default= -4 ];\n" + " required uint32 foo = 1 [default= 5 ];\n" + " required uint64 foo = 1 [default= 6 ];\n" + " required float foo = 1 [default= 7.5];\n" + " required float foo = 1 [default= -8.5];\n" + " required float foo = 1 [default= 9 ];\n" + " required double foo = 1 [default= 10.5];\n" + " required double foo = 1 [default=-11.5];\n" + " required double foo = 1 [default= 12 ];\n" + " required double foo = 1 [default= inf ];\n" + " required double foo = 1 [default=-inf ];\n" + " required double foo = 1 [default= nan ];\n" + " required string foo = 1 [default='13\\001'];\n" + " required string foo = 1 [default='a' \"b\" \n \"c\"];\n" + " required bytes foo = 1 [default='14\\002'];\n" + " required bytes foo = 1 [default='a' \"b\" \n 'c'];\n" + " required bool foo = 1 [default=true ];\n" + " required Foo foo = 1 [default=FOO ];\n" + + " required int32 foo = 1 [default= 0x7FFFFFFF];\n" + " required int32 foo = 1 [default=-0x80000000];\n" + " required uint32 foo = 1 [default= 0xFFFFFFFF];\n" + " required int64 foo = 1 [default= 0x7FFFFFFFFFFFFFFF];\n" + " required int64 foo = 1 [default=-0x8000000000000000];\n" + " required uint64 foo = 1 [default= 0xFFFFFFFFFFFFFFFF];\n" + " required double foo = 1 [default= 0xabcd];\n" + "}\n", #define ETC "name:\"foo\" label:LABEL_REQUIRED number:1" - "message_type {" - " name: \"TestMessage\"" - " field { type:TYPE_INT32 default_value:\"1\" " ETC " }" - " field { type:TYPE_INT32 default_value:\"-2\" " ETC " }" - " field { type:TYPE_INT64 default_value:\"3\" " ETC " }" - " field { type:TYPE_INT64 default_value:\"-4\" " ETC " }" - " field { type:TYPE_UINT32 default_value:\"5\" " ETC " }" - " field { type:TYPE_UINT64 default_value:\"6\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"7.5\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"-8.5\" " ETC " }" - " field { type:TYPE_FLOAT default_value:\"9\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"10.5\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"-11.5\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"12\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"inf\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"-inf\" " ETC " }" - " field { type:TYPE_DOUBLE default_value:\"nan\" " ETC " }" - " field { type:TYPE_STRING default_value:\"13\\001\" " ETC " }" - " field { type:TYPE_STRING default_value:\"abc\" " ETC " }" - " field { type:TYPE_BYTES default_value:\"14\\\\002\" " ETC " }" - " field { type:TYPE_BYTES default_value:\"abc\" " ETC " }" - " field { type:TYPE_BOOL default_value:\"true\" " ETC " }" - " field { type_name:\"Foo\" default_value:\"FOO\" " ETC " }" - - " field {" - " type:TYPE_INT32 default_value:\"2147483647\" " ETC - " }" - " field {" - " type:TYPE_INT32 default_value:\"-2147483648\" " ETC - " }" - " field {" - " type:TYPE_UINT32 default_value:\"4294967295\" " ETC - " }" - " field {" - " type:TYPE_INT64 default_value:\"9223372036854775807\" " ETC - " }" - " field {" - " type:TYPE_INT64 default_value:\"-9223372036854775808\" " ETC - " }" - " field {" - " type:TYPE_UINT64 default_value:\"18446744073709551615\" " ETC - " }" - " field {" - " type:TYPE_DOUBLE default_value:\"43981\" " ETC - " }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " field { type:TYPE_INT32 default_value:\"1\" " ETC + " }" + " field { type:TYPE_INT32 default_value:\"-2\" " ETC + " }" + " field { type:TYPE_INT64 default_value:\"3\" " ETC + " }" + " field { type:TYPE_INT64 default_value:\"-4\" " ETC + " }" + " field { type:TYPE_UINT32 default_value:\"5\" " ETC + " }" + " field { type:TYPE_UINT64 default_value:\"6\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"7.5\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"-8.5\" " ETC + " }" + " field { type:TYPE_FLOAT default_value:\"9\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"10.5\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"-11.5\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"12\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"inf\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"-inf\" " ETC + " }" + " field { type:TYPE_DOUBLE default_value:\"nan\" " ETC + " }" + " field { type:TYPE_STRING default_value:\"13\\001\" " ETC + " }" + " field { type:TYPE_STRING default_value:\"abc\" " ETC + " }" + " field { type:TYPE_BYTES default_value:\"14\\\\002\" " ETC + " }" + " field { type:TYPE_BYTES default_value:\"abc\" " ETC + " }" + " field { type:TYPE_BOOL default_value:\"true\" " ETC + " }" + " field { type_name:\"Foo\" default_value:\"FOO\" " ETC + " }" + + " field {" + " type:TYPE_INT32 default_value:\"2147483647\" " ETC + " }" + " field {" + " type:TYPE_INT32 default_value:\"-2147483648\" " ETC + " }" + " field {" + " type:TYPE_UINT32 default_value:\"4294967295\" " ETC + " }" + " field {" + " type:TYPE_INT64 default_value:\"9223372036854775807\" " ETC + " }" + " field {" + " type:TYPE_INT64 default_value:\"-9223372036854775808\" " ETC + " }" + " field {" + " type:TYPE_UINT64 default_value:\"18446744073709551615\" " ETC + " }" + " field {" + " type:TYPE_DOUBLE default_value:\"43981\" " ETC + " }" + "}"); #undef ETC } TEST_F(ParseMessageTest, FieldJsonName) { ExpectParsesTo( - "message TestMessage {\n" - " optional string foo = 1 [json_name = \"@type\"];\n" - "}\n", - "message_type {" - " name: \"TestMessage\"" - " field {\n" - " name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" - " json_name: \"@type\"\n" - " }\n" - "}\n"); + "message TestMessage {\n" + " optional string foo = 1 [json_name = \"@type\"];\n" + "}\n", + "message_type {" + " name: \"TestMessage\"" + " field {\n" + " name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" + " json_name: \"@type\"\n" + " }\n" + "}\n"); } TEST_F(ParseMessageTest, FieldOptions) { ExpectParsesTo( - "message TestMessage {\n" - " optional string foo = 1\n" - " [ctype=CORD, (foo)=7, foo.(.bar.baz).qux.quux.(corge)=-33, \n" - " (quux)=\"x\040y\", (baz.qux)=hey];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: 1" - " options { uninterpreted_option: { name { name_part: \"ctype\" " - " is_extension: false } " - " identifier_value: \"CORD\" }" - " uninterpreted_option: { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 7 }" - " uninterpreted_option: { name { name_part: \"foo\" " - " is_extension: false } " - " name { name_part: \".bar.baz\"" - " is_extension: true } " - " name { name_part: \"qux\" " - " is_extension: false } " - " name { name_part: \"quux\" " - " is_extension: false } " - " name { name_part: \"corge\" " - " is_extension: true } " - " negative_int_value: -33 }" - " uninterpreted_option: { name { name_part: \"quux\" " - " is_extension: true } " - " string_value: \"x y\" }" - " uninterpreted_option: { name { name_part: \"baz.qux\" " - " is_extension: true } " - " identifier_value: \"hey\" }" - " }" - " }" - "}"); + "message TestMessage {\n" + " optional string foo = 1\n" + " [ctype=CORD, (foo)=7, foo.(.bar.baz).qux.quux.(corge)=-33, \n" + " (quux)=\"x\040y\", (baz.qux)=hey];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name: \"foo\" label: LABEL_OPTIONAL type: TYPE_STRING number: " + "1" + " options { uninterpreted_option: { name { name_part: \"ctype\" " + " is_extension: false " + "} " + " identifier_value: \"CORD\" " + "}" + " uninterpreted_option: { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 7 }" + " uninterpreted_option: { name { name_part: \"foo\" " + " is_extension: false " + "} " + " name { name_part: " + "\".bar.baz\"" + " is_extension: true } " + " name { name_part: \"qux\" " + " is_extension: false " + "} " + " name { name_part: \"quux\" " + " is_extension: false " + "} " + " name { name_part: \"corge\" " + " is_extension: true } " + " negative_int_value: -33 }" + " uninterpreted_option: { name { name_part: \"quux\" " + " is_extension: true } " + " string_value: \"x y\" }" + " uninterpreted_option: { name { name_part: " + "\"baz.qux\" " + " is_extension: true } " + " identifier_value: \"hey\" }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, Oneof) { ExpectParsesTo( - "message TestMessage {\n" - " oneof foo {\n" - " int32 a = 1;\n" - " string b = 2;\n" - " TestMessage c = 3;\n" - " group D = 4 { optional int32 i = 5; }\n" - " }\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " - " oneof_index:0 }" - " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " - " oneof_index:0 }" - " field { name:\"c\" label:LABEL_OPTIONAL type_name:\"TestMessage\" " - " number:3 oneof_index:0 }" - " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_GROUP " - " type_name:\"D\" number:4 oneof_index:0 }" - " oneof_decl {" - " name: \"foo\"" - " }" - " nested_type {" - " name: \"D\"" - " field { name:\"i\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 }" - " }" - "}"); + "message TestMessage {\n" + " oneof foo {\n" + " int32 a = 1;\n" + " string b = 2;\n" + " TestMessage c = 3;\n" + " group D = 4 { optional int32 i = 5; }\n" + " }\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " + " oneof_index:0 }" + " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " + " oneof_index:0 }" + " field { name:\"c\" label:LABEL_OPTIONAL type_name:\"TestMessage\" " + " number:3 oneof_index:0 }" + " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_GROUP " + " type_name:\"D\" number:4 oneof_index:0 }" + " oneof_decl {" + " name: \"foo\"" + " }" + " nested_type {" + " name: \"D\"" + " field { name:\"i\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 }" + " }" + "}"); } TEST_F(ParseMessageTest, MultipleOneofs) { ExpectParsesTo( - "message TestMessage {\n" - " oneof foo {\n" - " int32 a = 1;\n" - " string b = 2;\n" - " }\n" - " oneof bar {\n" - " int32 c = 3;\n" - " string d = 4;\n" - " }\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " - " oneof_index:0 }" - " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " - " oneof_index:0 }" - " field { name:\"c\" label:LABEL_OPTIONAL type:TYPE_INT32 number:3 " - " oneof_index:1 }" - " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_STRING number:4 " - " oneof_index:1 }" - " oneof_decl {" - " name: \"foo\"" - " }" - " oneof_decl {" - " name: \"bar\"" - " }" - "}"); + "message TestMessage {\n" + " oneof foo {\n" + " int32 a = 1;\n" + " string b = 2;\n" + " }\n" + " oneof bar {\n" + " int32 c = 3;\n" + " string d = 4;\n" + " }\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"a\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 " + " oneof_index:0 }" + " field { name:\"b\" label:LABEL_OPTIONAL type:TYPE_STRING number:2 " + " oneof_index:0 }" + " field { name:\"c\" label:LABEL_OPTIONAL type:TYPE_INT32 number:3 " + " oneof_index:1 }" + " field { name:\"d\" label:LABEL_OPTIONAL type:TYPE_STRING number:4 " + " oneof_index:1 }" + " oneof_decl {" + " name: \"foo\"" + " }" + " oneof_decl {" + " name: \"bar\"" + " }" + "}"); } TEST_F(ParseMessageTest, Maps) { ExpectParsesTo( - "message TestMessage {\n" - " map<int32, string> primitive_type_map = 1;\n" - " map<KeyType, ValueType> composite_type_map = 2;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " nested_type {" - " name: \"PrimitiveTypeMapEntry\"" - " field { " - " name: \"key\" number: 1 label:LABEL_OPTIONAL" - " type:TYPE_INT32" - " }" - " field { " - " name: \"value\" number: 2 label:LABEL_OPTIONAL" - " type:TYPE_STRING" - " }" - " options { map_entry: true }" - " }" - " nested_type {" - " name: \"CompositeTypeMapEntry\"" - " field { " - " name: \"key\" number: 1 label:LABEL_OPTIONAL" - " type_name: \"KeyType\"" - " }" - " field { " - " name: \"value\" number: 2 label:LABEL_OPTIONAL" - " type_name: \"ValueType\"" - " }" - " options { map_entry: true }" - " }" - " field {" - " name: \"primitive_type_map\"" - " label: LABEL_REPEATED" - " type_name: \"PrimitiveTypeMapEntry\"" - " number: 1" - " }" - " field {" - " name: \"composite_type_map\"" - " label: LABEL_REPEATED" - " type_name: \"CompositeTypeMapEntry\"" - " number: 2" - " }" - "}"); + "message TestMessage {\n" + " map<int32, string> primitive_type_map = 1;\n" + " map<KeyType, ValueType> composite_type_map = 2;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " nested_type {" + " name: \"PrimitiveTypeMapEntry\"" + " field { " + " name: \"key\" number: 1 label:LABEL_OPTIONAL" + " type:TYPE_INT32" + " }" + " field { " + " name: \"value\" number: 2 label:LABEL_OPTIONAL" + " type:TYPE_STRING" + " }" + " options { map_entry: true }" + " }" + " nested_type {" + " name: \"CompositeTypeMapEntry\"" + " field { " + " name: \"key\" number: 1 label:LABEL_OPTIONAL" + " type_name: \"KeyType\"" + " }" + " field { " + " name: \"value\" number: 2 label:LABEL_OPTIONAL" + " type_name: \"ValueType\"" + " }" + " options { map_entry: true }" + " }" + " field {" + " name: \"primitive_type_map\"" + " label: LABEL_REPEATED" + " type_name: \"PrimitiveTypeMapEntry\"" + " number: 1" + " }" + " field {" + " name: \"composite_type_map\"" + " label: LABEL_REPEATED" + " type_name: \"CompositeTypeMapEntry\"" + " number: 2" + " }" + "}"); } TEST_F(ParseMessageTest, Group) { ExpectParsesTo( - "message TestMessage {\n" - " optional group TestGroup = 1 {};\n" - "}\n", + "message TestMessage {\n" + " optional group TestGroup = 1 {};\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " nested_type { name: \"TestGroup\" }" - " field { name:\"testgroup\" label:LABEL_OPTIONAL number:1" - " type:TYPE_GROUP type_name: \"TestGroup\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " nested_type { name: \"TestGroup\" }" + " field { name:\"testgroup\" label:LABEL_OPTIONAL number:1" + " type:TYPE_GROUP type_name: \"TestGroup\" }" + "}"); } TEST_F(ParseMessageTest, NestedMessage) { ExpectParsesTo( - "message TestMessage {\n" - " message Nested {}\n" - " optional Nested test_nested = 1;\n" - "}\n", + "message TestMessage {\n" + " message Nested {}\n" + " optional Nested test_nested = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " nested_type { name: \"Nested\" }" - " field { name:\"test_nested\" label:LABEL_OPTIONAL number:1" - " type_name: \"Nested\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " nested_type { name: \"Nested\" }" + " field { name:\"test_nested\" label:LABEL_OPTIONAL number:1" + " type_name: \"Nested\" }" + "}"); } TEST_F(ParseMessageTest, NestedEnum) { ExpectParsesTo( - "message TestMessage {\n" - " enum NestedEnum {}\n" - " optional NestedEnum test_enum = 1;\n" - "}\n", + "message TestMessage {\n" + " enum NestedEnum {}\n" + " optional NestedEnum test_enum = 1;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " enum_type { name: \"NestedEnum\" }" - " field { name:\"test_enum\" label:LABEL_OPTIONAL number:1" - " type_name: \"NestedEnum\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " enum_type { name: \"NestedEnum\" }" + " field { name:\"test_enum\" label:LABEL_OPTIONAL number:1" + " type_name: \"NestedEnum\" }" + "}"); } TEST_F(ParseMessageTest, ReservedRange) { ExpectParsesTo( - "message TestMessage {\n" - " required int32 foo = 1;\n" - " reserved 2, 15, 9 to 11, 3, 20 to max;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" - " reserved_range { start:2 end:3 }" - " reserved_range { start:15 end:16 }" - " reserved_range { start:9 end:12 }" - " reserved_range { start:3 end:4 }" - " reserved_range { start:20 end:536870912 }" - "}"); + "message TestMessage {\n" + " required int32 foo = 1;\n" + " reserved 2, 15, 9 to 11, 3, 20 to max;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }" + " reserved_range { start:2 end:3 }" + " reserved_range { start:15 end:16 }" + " reserved_range { start:9 end:12 }" + " reserved_range { start:3 end:4 }" + " reserved_range { start:20 end:536870912 }" + "}"); } TEST_F(ParseMessageTest, ReservedRangeOnMessageSet) { ExpectParsesTo( - "message TestMessage {\n" - " option message_set_wire_format = true;\n" - " reserved 20 to max;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"message_set_wire_format\"" - " is_extension: false" - " }" - " identifier_value: \"true\"" - " }" - " }" - " reserved_range { start:20 end:2147483647 }" - "}"); + "message TestMessage {\n" + " option message_set_wire_format = true;\n" + " reserved 20 to max;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"message_set_wire_format\"" + " is_extension: false" + " }" + " identifier_value: \"true\"" + " }" + " }" + " reserved_range { start:20 end:2147483647 }" + "}"); } TEST_F(ParseMessageTest, ReservedNames) { ExpectParsesTo( - "message TestMessage {\n" - " reserved \"foo\", \"bar\";\n" - "}\n", + "message TestMessage {\n" + " reserved \"foo\", \"bar\";\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}"); + "message_type {" + " name: \"TestMessage\"" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}"); } TEST_F(ParseMessageTest, ExtensionRange) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 10 to 19;\n" - " extensions 30 to max;\n" - "}\n", + "message TestMessage {\n" + " extensions 10 to 19;\n" + " extensions 30 to max;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:10 end:20 }" - " extension_range { start:30 end:536870912 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:10 end:20 }" + " extension_range { start:30 end:536870912 }" + "}"); } TEST_F(ParseMessageTest, ExtensionRangeWithOptions) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 10 to 19 [(i) = 5];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range {" - " start:10" - " end:20" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - "}"); + "message TestMessage {\n" + " extensions 10 to 19 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:10" + " end:20" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, CompoundExtensionRange) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 2, 15, 9 to 11, 100 to max, 3;\n" - "}\n", + "message TestMessage {\n" + " extensions 2, 15, 9 to 11, 100 to max, 3;\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:2 end:3 }" - " extension_range { start:15 end:16 }" - " extension_range { start:9 end:12 }" - " extension_range { start:100 end:536870912 }" - " extension_range { start:3 end:4 }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:2 end:3 }" + " extension_range { start:15 end:16 }" + " extension_range { start:9 end:12 }" + " extension_range { start:100 end:536870912 }" + " extension_range { start:3 end:4 }" + "}"); } TEST_F(ParseMessageTest, CompoundExtensionRangeWithOptions) { ExpectParsesTo( - "message TestMessage {\n" - " extensions 2, 15, 9 to 11, 100 to max, 3 [(i) = 5];\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range {" - " start:2" - " end:3" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:15" - " end:16" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:9" - " end:12" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:100" - " end:536870912" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - " extension_range {" - " start:3" - " end:4" - " options {" - " uninterpreted_option {" - " name {" - " name_part: \"i\"" - " is_extension: true" - " }" - " positive_int_value: 5" - " }" - " }" - " }" - "}"); + "message TestMessage {\n" + " extensions 2, 15, 9 to 11, 100 to max, 3 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:2" + " end:3" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:15" + " end:16" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:9" + " end:12" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:100" + " end:536870912" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:3" + " end:4" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); } TEST_F(ParseMessageTest, LargerMaxForMessageSetWireFormatMessages) { @@ -844,77 +893,79 @@ TEST_F(ParseMessageTest, LargerMaxForMessageSetWireFormatMessages) { // extension numbers, as the numbers are not encoded as int32 field values // rather than tags. ExpectParsesTo( - "message TestMessage {\n" - " extensions 4 to max;\n" - " option message_set_wire_format = true;\n" - "}\n", - - "message_type {" - " name: \"TestMessage\"" - " extension_range { start:4 end: 0x7fffffff }" - " options {\n" - " uninterpreted_option { \n" - " name {\n" - " name_part: \"message_set_wire_format\"\n" - " is_extension: false\n" - " }\n" - " identifier_value: \"true\"\n" - " }\n" - " }\n" - "}"); + "message TestMessage {\n" + " extensions 4 to max;\n" + " option message_set_wire_format = true;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range { start:4 end: 0x7fffffff }" + " options {\n" + " uninterpreted_option { \n" + " name {\n" + " name_part: \"message_set_wire_format\"\n" + " is_extension: false\n" + " }\n" + " identifier_value: \"true\"\n" + " }\n" + " }\n" + "}"); } TEST_F(ParseMessageTest, Extensions) { ExpectParsesTo( - "extend Extendee1 { optional int32 foo = 12; }\n" - "extend Extendee2 { repeated TestMessage bar = 22; }\n", + "extend Extendee1 { optional int32 foo = 12; }\n" + "extend Extendee2 { repeated TestMessage bar = 22; }\n", - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" } " - "extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee2\" }"); + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" + " extendee: \"Extendee1\" } " + "extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee2\" }"); } TEST_F(ParseMessageTest, ExtensionsInMessageScope) { ExpectParsesTo( - "message TestMessage {\n" - " extend Extendee1 { optional int32 foo = 12; }\n" - " extend Extendee2 { repeated TestMessage bar = 22; }\n" - "}\n", + "message TestMessage {\n" + " extend Extendee1 { optional int32 foo = 12; }\n" + " extend Extendee2 { repeated TestMessage bar = 22; }\n" + "}\n", - "message_type {" - " name: \"TestMessage\"" - " extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" }" - " extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee2\" }" - "}"); + "message_type {" + " name: \"TestMessage\"" + " extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:12" + " extendee: \"Extendee1\" }" + " extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee2\" }" + "}"); } TEST_F(ParseMessageTest, MultipleExtensionsOneExtendee) { ExpectParsesTo( - "extend Extendee1 {\n" - " optional int32 foo = 12;\n" - " repeated TestMessage bar = 22;\n" - "}\n", + "extend Extendee1 {\n" + " optional int32 foo = 12;\n" + " repeated TestMessage bar = 22;\n" + "}\n", - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" - " extendee: \"Extendee1\" } " - "extension { name:\"bar\" label:LABEL_REPEATED number:22" - " type_name:\"TestMessage\" extendee: \"Extendee1\" }"); + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:12" + " extendee: \"Extendee1\" } " + "extension { name:\"bar\" label:LABEL_REPEATED number:22" + " type_name:\"TestMessage\" extendee: \"Extendee1\" }"); } TEST_F(ParseMessageTest, OptionalLabelProto3) { ExpectParsesTo( - "syntax = \"proto3\";\n" - "message TestMessage {\n" - " int32 foo = 1;\n" - "}\n", + "syntax = \"proto3\";\n" + "message TestMessage {\n" + " int32 foo = 1;\n" + "}\n", - "syntax: \"proto3\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 } }"); + "syntax: \"proto3\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 } " + "}"); } // =================================================================== @@ -923,108 +974,109 @@ typedef ParserTest ParseEnumTest; TEST_F(ParseEnumTest, SimpleEnum) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - "}\n", + "enum TestEnum {\n" + " FOO = 0;\n" + "}\n", - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - "}"); + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + "}"); } TEST_F(ParseEnumTest, Values) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 13;\n" - " BAR = -10;\n" - " BAZ = 500;\n" - " HEX_MAX = 0x7FFFFFFF;\n" - " HEX_MIN = -0x80000000;\n" - " INT_MAX = 2147483647;\n" - " INT_MIN = -2147483648;\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:13 }" - " value { name:\"BAR\" number:-10 }" - " value { name:\"BAZ\" number:500 }" - " value { name:\"HEX_MAX\" number:2147483647 }" - " value { name:\"HEX_MIN\" number:-2147483648 }" - " value { name:\"INT_MAX\" number:2147483647 }" - " value { name:\"INT_MIN\" number:-2147483648 }" - "}"); + "enum TestEnum {\n" + " FOO = 13;\n" + " BAR = -10;\n" + " BAZ = 500;\n" + " HEX_MAX = 0x7FFFFFFF;\n" + " HEX_MIN = -0x80000000;\n" + " INT_MAX = 2147483647;\n" + " INT_MIN = -2147483648;\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:13 }" + " value { name:\"BAR\" number:-10 }" + " value { name:\"BAZ\" number:500 }" + " value { name:\"HEX_MAX\" number:2147483647 }" + " value { name:\"HEX_MIN\" number:-2147483648 }" + " value { name:\"INT_MAX\" number:2147483647 }" + " value { name:\"INT_MIN\" number:-2147483648 }" + "}"); } TEST_F(ParseEnumTest, ValueOptions) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 13;\n" - " BAR = -10 [ (something.text) = 'abc' ];\n" - " BAZ = 500 [ (something.text) = 'def', other = 1 ];\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name: \"FOO\" number: 13 }" - " value { name: \"BAR\" number: -10 " - " options { " - " uninterpreted_option { " - " name { name_part: \"something.text\" is_extension: true } " - " string_value: \"abc\" " - " } " - " } " - " } " - " value { name: \"BAZ\" number: 500 " - " options { " - " uninterpreted_option { " - " name { name_part: \"something.text\" is_extension: true } " - " string_value: \"def\" " - " } " - " uninterpreted_option { " - " name { name_part: \"other\" is_extension: false } " - " positive_int_value: 1 " - " } " - " } " - " } " - "}"); + "enum TestEnum {\n" + " FOO = 13;\n" + " BAR = -10 [ (something.text) = 'abc' ];\n" + " BAZ = 500 [ (something.text) = 'def', other = 1 ];\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name: \"FOO\" number: 13 }" + " value { name: \"BAR\" number: -10 " + " options { " + " uninterpreted_option { " + " name { name_part: \"something.text\" is_extension: true } " + " string_value: \"abc\" " + " } " + " } " + " } " + " value { name: \"BAZ\" number: 500 " + " options { " + " uninterpreted_option { " + " name { name_part: \"something.text\" is_extension: true } " + " string_value: \"def\" " + " } " + " uninterpreted_option { " + " name { name_part: \"other\" is_extension: false } " + " positive_int_value: 1 " + " } " + " } " + " } " + "}"); } TEST_F(ParseEnumTest, ReservedRange) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - " reserved -2147483648, -6 to -4, -1 to 1, 2, 15, 9 to 11, 3, 20 to max;\n" - "}\n", - - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - " reserved_range { start:-2147483648 end:-2147483648 }" - " reserved_range { start:-6 end:-4 }" - " reserved_range { start:-1 end:1 }" - " reserved_range { start:2 end:2 }" - " reserved_range { start:15 end:15 }" - " reserved_range { start:9 end:11 }" - " reserved_range { start:3 end:3 }" - " reserved_range { start:20 end:2147483647 }" - "}"); + "enum TestEnum {\n" + " FOO = 0;\n" + " reserved -2147483648, -6 to -4, -1 to 1, 2, 15, 9 to 11, 3, 20 to " + "max;\n" + "}\n", + + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + " reserved_range { start:-2147483648 end:-2147483648 }" + " reserved_range { start:-6 end:-4 }" + " reserved_range { start:-1 end:1 }" + " reserved_range { start:2 end:2 }" + " reserved_range { start:15 end:15 }" + " reserved_range { start:9 end:11 }" + " reserved_range { start:3 end:3 }" + " reserved_range { start:20 end:2147483647 }" + "}"); } TEST_F(ParseEnumTest, ReservedNames) { ExpectParsesTo( - "enum TestEnum {\n" - " FOO = 0;\n" - " reserved \"foo\", \"bar\";\n" - "}\n", + "enum TestEnum {\n" + " FOO = 0;\n" + " reserved \"foo\", \"bar\";\n" + "}\n", - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"FOO\" number:0 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}"); + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"FOO\" number:0 }" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}"); } // =================================================================== @@ -1033,80 +1085,76 @@ typedef ParserTest ParseServiceTest; TEST_F(ParseServiceTest, SimpleService) { ExpectParsesTo( - "service TestService {\n" - " rpc Foo(In) returns (Out);\n" - "}\n", + "service TestService {\n" + " rpc Foo(In) returns (Out);\n" + "}\n", - "service {" - " name: \"TestService\"" - " method { name:\"Foo\" input_type:\"In\" output_type:\"Out\" }" - "}"); + "service {" + " name: \"TestService\"" + " method { name:\"Foo\" input_type:\"In\" output_type:\"Out\" }" + "}"); } TEST_F(ParseServiceTest, MethodsAndStreams) { ExpectParsesTo( - "service TestService {\n" - " rpc Foo(In1) returns (Out1);\n" - " rpc Bar(In2) returns (Out2);\n" - " rpc Baz(In3) returns (Out3);\n" - "}\n", + "service TestService {\n" + " rpc Foo(In1) returns (Out1);\n" + " rpc Bar(In2) returns (Out2);\n" + " rpc Baz(In3) returns (Out3);\n" + "}\n", - "service {" - " name: \"TestService\"" - " method { name:\"Foo\" input_type:\"In1\" output_type:\"Out1\" }" - " method { name:\"Bar\" input_type:\"In2\" output_type:\"Out2\" }" - " method { name:\"Baz\" input_type:\"In3\" output_type:\"Out3\" }" - "}"); + "service {" + " name: \"TestService\"" + " method { name:\"Foo\" input_type:\"In1\" output_type:\"Out1\" }" + " method { name:\"Bar\" input_type:\"In2\" output_type:\"Out2\" }" + " method { name:\"Baz\" input_type:\"In3\" output_type:\"Out3\" }" + "}"); } - // =================================================================== // imports and packages typedef ParserTest ParseMiscTest; TEST_F(ParseMiscTest, ParseImport) { - ExpectParsesTo( - "import \"foo/bar/baz.proto\";\n", - "dependency: \"foo/bar/baz.proto\""); + ExpectParsesTo("import \"foo/bar/baz.proto\";\n", + "dependency: \"foo/bar/baz.proto\""); } TEST_F(ParseMiscTest, ParseMultipleImports) { ExpectParsesTo( - "import \"foo.proto\";\n" - "import \"bar.proto\";\n" - "import \"baz.proto\";\n", - "dependency: \"foo.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\""); + "import \"foo.proto\";\n" + "import \"bar.proto\";\n" + "import \"baz.proto\";\n", + "dependency: \"foo.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\""); } TEST_F(ParseMiscTest, ParsePublicImports) { ExpectParsesTo( - "import \"foo.proto\";\n" - "import public \"bar.proto\";\n" - "import \"baz.proto\";\n" - "import public \"qux.proto\";\n", - "dependency: \"foo.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\"" - "dependency: \"qux.proto\"" - "public_dependency: 1 " - "public_dependency: 3 "); + "import \"foo.proto\";\n" + "import public \"bar.proto\";\n" + "import \"baz.proto\";\n" + "import public \"qux.proto\";\n", + "dependency: \"foo.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\"" + "dependency: \"qux.proto\"" + "public_dependency: 1 " + "public_dependency: 3 "); } TEST_F(ParseMiscTest, ParsePackage) { - ExpectParsesTo( - "package foo.bar.baz;\n", - "package: \"foo.bar.baz\""); + ExpectParsesTo("package foo.bar.baz;\n", "package: \"foo.bar.baz\""); } TEST_F(ParseMiscTest, ParsePackageWithSpaces) { ExpectParsesTo( - "package foo . bar. \n" - " baz;\n", - "package: \"foo.bar.baz\""); + "package foo . bar. \n" + " baz;\n", + "package: \"foo.bar.baz\""); } // =================================================================== @@ -1149,341 +1197,335 @@ TEST_F(ParseErrorTest, MissingSyntaxIdentifier) { TEST_F(ParseErrorTest, UnknownSyntaxIdentifier) { ExpectHasEarlyExitErrors( - "syntax = \"no_such_syntax\";", - "0:9: Unrecognized syntax identifier \"no_such_syntax\". This parser " + "syntax = \"no_such_syntax\";", + "0:9: Unrecognized syntax identifier \"no_such_syntax\". This parser " "only recognizes \"proto2\" and \"proto3\".\n"); EXPECT_EQ("no_such_syntax", parser_->GetSyntaxIdentifier()); } TEST_F(ParseErrorTest, SimpleSyntaxError) { - ExpectHasErrors( - "message TestMessage @#$ { blah }", - "0:20: Expected \"{\".\n"); + ExpectHasErrors("message TestMessage @#$ { blah }", + "0:20: Expected \"{\".\n"); EXPECT_EQ("proto2", parser_->GetSyntaxIdentifier()); } TEST_F(ParseErrorTest, ExpectedTopLevel) { - ExpectHasErrors( - "blah;", - "0:0: Expected top-level statement (e.g. \"message\").\n"); + ExpectHasErrors("blah;", + "0:0: Expected top-level statement (e.g. \"message\").\n"); } TEST_F(ParseErrorTest, UnmatchedCloseBrace) { // This used to cause an infinite loop. Doh. - ExpectHasErrors( - "}", - "0:0: Expected top-level statement (e.g. \"message\").\n" - "0:0: Unmatched \"}\".\n"); + ExpectHasErrors("}", + "0:0: Expected top-level statement (e.g. \"message\").\n" + "0:0: Unmatched \"}\".\n"); } // ------------------------------------------------------------------- // Message errors TEST_F(ParseErrorTest, MessageMissingName) { - ExpectHasErrors( - "message {}", - "0:8: Expected message name.\n"); + ExpectHasErrors("message {}", "0:8: Expected message name.\n"); } TEST_F(ParseErrorTest, MessageMissingBody) { - ExpectHasErrors( - "message TestMessage;", - "0:19: Expected \"{\".\n"); + ExpectHasErrors("message TestMessage;", "0:19: Expected \"{\".\n"); } TEST_F(ParseErrorTest, EofInMessage) { ExpectHasErrors( - "message TestMessage {", - "0:21: Reached end of input in message definition (missing '}').\n"); + "message TestMessage {", + "0:21: Reached end of input in message definition (missing '}').\n"); } TEST_F(ParseErrorTest, MissingFieldNumber) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo;\n" - "}\n", - "1:20: Missing field number.\n"); + "message TestMessage {\n" + " optional int32 foo;\n" + "}\n", + "1:20: Missing field number.\n"); } TEST_F(ParseErrorTest, ExpectedFieldNumber) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = ;\n" - "}\n", - "1:23: Expected field number.\n"); + "message TestMessage {\n" + " optional int32 foo = ;\n" + "}\n", + "1:23: Expected field number.\n"); } TEST_F(ParseErrorTest, FieldNumberOutOfRange) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = 0x100000000;\n" - "}\n", - "1:23: Integer out of range.\n"); + "message TestMessage {\n" + " optional int32 foo = 0x100000000;\n" + "}\n", + "1:23: Integer out of range.\n"); } TEST_F(ParseErrorTest, MissingLabel) { ExpectHasErrors( - "message TestMessage {\n" - " int32 foo = 1;\n" - "}\n", - "1:2: Expected \"required\", \"optional\", or \"repeated\".\n"); + "message TestMessage {\n" + " int32 foo = 1;\n" + "}\n", + "1:2: Expected \"required\", \"optional\", or \"repeated\".\n"); } TEST_F(ParseErrorTest, ExpectedOptionName) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [];\n" - "}\n", - "1:27: Expected identifier.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [];\n" + "}\n", + "1:27: Expected identifier.\n"); } TEST_F(ParseErrorTest, NonExtensionOptionNameBeginningWithDot) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [.foo=1];\n" - "}\n", - "1:27: Expected identifier.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [.foo=1];\n" + "}\n", + "1:27: Expected identifier.\n"); } TEST_F(ParseErrorTest, DefaultValueTypeMismatch) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=true];\n" - "}\n", - "1:35: Expected integer for field default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=true];\n" + "}\n", + "1:35: Expected integer for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueNotBoolean) { ExpectHasErrors( - "message TestMessage {\n" - " optional bool foo = 1 [default=blah];\n" - "}\n", - "1:33: Expected \"true\" or \"false\".\n"); + "message TestMessage {\n" + " optional bool foo = 1 [default=blah];\n" + "}\n", + "1:33: Expected \"true\" or \"false\".\n"); } TEST_F(ParseErrorTest, DefaultValueNotString) { ExpectHasErrors( - "message TestMessage {\n" - " optional string foo = 1 [default=1];\n" - "}\n", - "1:35: Expected string for field default value.\n"); + "message TestMessage {\n" + " optional string foo = 1 [default=1];\n" + "}\n", + "1:35: Expected string for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueUnsignedNegative) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=-1];\n" - "}\n", - "1:36: Unsigned field can't have negative default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=-1];\n" + "}\n", + "1:36: Unsigned field can't have negative default value.\n"); } TEST_F(ParseErrorTest, DefaultValueTooLarge) { ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo = 1 [default= 0x80000000];\n" - " optional int32 foo = 1 [default=-0x80000001];\n" - " optional uint32 foo = 1 [default= 0x100000000];\n" - " optional int64 foo = 1 [default= 0x80000000000000000];\n" - " optional int64 foo = 1 [default=-0x80000000000000001];\n" - " optional uint64 foo = 1 [default= 0x100000000000000000];\n" - "}\n", - "1:36: Integer out of range.\n" - "2:36: Integer out of range.\n" - "3:36: Integer out of range.\n" - "4:36: Integer out of range.\n" - "5:36: Integer out of range.\n" - "6:36: Integer out of range.\n"); + "message TestMessage {\n" + " optional int32 foo = 1 [default= 0x80000000];\n" + " optional int32 foo = 1 [default=-0x80000001];\n" + " optional uint32 foo = 1 [default= 0x100000000];\n" + " optional int64 foo = 1 [default= 0x80000000000000000];\n" + " optional int64 foo = 1 [default=-0x80000000000000001];\n" + " optional uint64 foo = 1 [default= 0x100000000000000000];\n" + "}\n", + "1:36: Integer out of range.\n" + "2:36: Integer out of range.\n" + "3:36: Integer out of range.\n" + "4:36: Integer out of range.\n" + "5:36: Integer out of range.\n" + "6:36: Integer out of range.\n"); } TEST_F(ParseErrorTest, JsonNameNotString) { ExpectHasErrors( - "message TestMessage {\n" - " optional string foo = 1 [json_name=1];\n" - "}\n", - "1:37: Expected string for JSON name.\n"); + "message TestMessage {\n" + " optional string foo = 1 [json_name=1];\n" + "}\n", + "1:37: Expected string for JSON name.\n"); } TEST_F(ParseErrorTest, DuplicateJsonName) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [json_name=\"a\",json_name=\"b\"];\n" - "}\n", - "1:41: Already set option \"json_name\".\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [json_name=\"a\",json_name=\"b\"];\n" + "}\n", + "1:41: Already set option \"json_name\".\n"); } TEST_F(ParseErrorTest, EnumValueOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - " HEX_TOO_BIG = 0x80000000;\n" - " HEX_TOO_SMALL = -0x80000001;\n" - " INT_TOO_BIG = 2147483648;\n" - " INT_TOO_SMALL = -2147483649;\n" - "}\n", - "1:19: Integer out of range.\n" - "2:19: Integer out of range.\n" - "3:19: Integer out of range.\n" - "4:19: Integer out of range.\n"); + "enum TestEnum {\n" + " HEX_TOO_BIG = 0x80000000;\n" + " HEX_TOO_SMALL = -0x80000001;\n" + " INT_TOO_BIG = 2147483648;\n" + " INT_TOO_SMALL = -2147483649;\n" + "}\n", + "1:19: Integer out of range.\n" + "2:19: Integer out of range.\n" + "3:19: Integer out of range.\n" + "4:19: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumAllowAliasFalse) { ExpectHasErrors( - "enum Foo {\n" - " option allow_alias = false;\n" - " BAR = 1;\n" - " BAZ = 2;\n" - "}\n", - "5:0: \"Foo\" declares 'option allow_alias = false;' which has no effect. " - "Please remove the declaration.\n"); + "enum Foo {\n" + " option allow_alias = false;\n" + " BAR = 1;\n" + " BAZ = 2;\n" + "}\n", + "5:0: \"Foo\" declares 'option allow_alias = false;' which has no " + "effect. " + "Please remove the declaration.\n"); } TEST_F(ParseErrorTest, UnnecessaryEnumAllowAlias) { ExpectHasErrors( - "enum Foo {\n" - " option allow_alias = true;\n" - " BAR = 1;\n" - " BAZ = 2;\n" - "}\n", - "5:0: \"Foo\" declares support for enum aliases but no enum values share " - "field numbers. Please remove the unnecessary 'option allow_alias = true;' " - "declaration.\n"); + "enum Foo {\n" + " option allow_alias = true;\n" + " BAR = 1;\n" + " BAZ = 2;\n" + "}\n", + "5:0: \"Foo\" declares support for enum aliases but no enum values share " + "field numbers. Please remove the unnecessary 'option allow_alias = " + "true;' " + "declaration.\n"); } TEST_F(ParseErrorTest, DefaultValueMissing) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=];\n" - "}\n", - "1:35: Expected integer for field default value.\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=];\n" + "}\n", + "1:35: Expected integer for field default value.\n"); } TEST_F(ParseErrorTest, DefaultValueForGroup) { ExpectHasErrors( - "message TestMessage {\n" - " optional group Foo = 1 [default=blah] {}\n" - "}\n", - "1:34: Messages can't have default values.\n"); + "message TestMessage {\n" + " optional group Foo = 1 [default=blah] {}\n" + "}\n", + "1:34: Messages can't have default values.\n"); } TEST_F(ParseErrorTest, DuplicateDefaultValue) { ExpectHasErrors( - "message TestMessage {\n" - " optional uint32 foo = 1 [default=1,default=2];\n" - "}\n", - "1:37: Already set option \"default\".\n"); + "message TestMessage {\n" + " optional uint32 foo = 1 [default=1,default=2];\n" + "}\n", + "1:37: Already set option \"default\".\n"); } TEST_F(ParseErrorTest, MissingOneofName) { ExpectHasErrors( - "message TestMessage {\n" - " oneof {\n" - " int32 bar = 1;\n" - " }\n" - "}\n", - "1:8: Expected oneof name.\n"); + "message TestMessage {\n" + " oneof {\n" + " int32 bar = 1;\n" + " }\n" + "}\n", + "1:8: Expected oneof name.\n"); } TEST_F(ParseErrorTest, LabelInOneof) { ExpectHasErrors( - "message TestMessage {\n" - " oneof foo {\n" - " optional int32 bar = 1;\n" - " }\n" - "}\n", - "2:4: Fields in oneofs must not have labels (required / optional " + "message TestMessage {\n" + " oneof foo {\n" + " optional int32 bar = 1;\n" + " }\n" + "}\n", + "2:4: Fields in oneofs must not have labels (required / optional " "/ repeated).\n"); } TEST_F(ParseErrorTest, MapInOneof) { ExpectHasErrors( - "message TestMessage {\n" - " oneof foo {\n" - " map<int32, int32> foo_map = 1;\n" - " map message_field = 2;\n" // a normal message field is OK - " }\n" - "}\n", - "2:7: Map fields are not allowed in oneofs.\n"); + "message TestMessage {\n" + " oneof foo {\n" + " map<int32, int32> foo_map = 1;\n" + " map message_field = 2;\n" // a normal message field is OK + " }\n" + "}\n", + "2:7: Map fields are not allowed in oneofs.\n"); } TEST_F(ParseErrorTest, LabelForMap) { ExpectHasErrors( - "message TestMessage {\n" - " optional map<int32, int32> int_map = 1;\n" - " required map<int32, int32> int_map2 = 2;\n" - " repeated map<int32, int32> int_map3 = 3;\n" - " optional map map_message = 4;\n" // a normal message field is OK - "}\n", - "1:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n" - "2:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n" - "3:14: Field labels (required/optional/repeated) are not allowed on map " - "fields.\n"); + "message TestMessage {\n" + " optional map<int32, int32> int_map = 1;\n" + " required map<int32, int32> int_map2 = 2;\n" + " repeated map<int32, int32> int_map3 = 3;\n" + " optional map map_message = 4;\n" // a normal message field is OK + "}\n", + "1:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n" + "2:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n" + "3:14: Field labels (required/optional/repeated) are not allowed on map " + "fields.\n"); } TEST_F(ParseErrorTest, MalformedMaps) { ExpectHasErrors( - "message TestMessage {\n" - " map map_message = 1;\n" // a normal message field lacking label - " map<string> str_map = 2;\n" - " map<string,> str_map2 = 3;\n" - " map<,string> str_map3 = 4;\n" - " map<> empty_map = 5;\n" - " map<string,string str_map6 = 6;\n" - "}" - "extend SomeMessage {\n" - " map<int32, int32> int_map = 1;\n" - "}", - "1:6: Expected \"required\", \"optional\", or \"repeated\".\n" - "2:12: Expected \",\".\n" - "3:13: Expected type name.\n" - "4:6: Expected type name.\n" - "5:6: Expected type name.\n" - "6:20: Expected \">\".\n" - "8:5: Map fields are not allowed to be extensions.\n"); + "message TestMessage {\n" + " map map_message = 1;\n" // a normal message field lacking label + " map<string> str_map = 2;\n" + " map<string,> str_map2 = 3;\n" + " map<,string> str_map3 = 4;\n" + " map<> empty_map = 5;\n" + " map<string,string str_map6 = 6;\n" + "}" + "extend SomeMessage {\n" + " map<int32, int32> int_map = 1;\n" + "}", + "1:6: Expected \"required\", \"optional\", or \"repeated\".\n" + "2:12: Expected \",\".\n" + "3:13: Expected type name.\n" + "4:6: Expected type name.\n" + "5:6: Expected type name.\n" + "6:20: Expected \">\".\n" + "8:5: Map fields are not allowed to be extensions.\n"); } TEST_F(ParseErrorTest, GroupNotCapitalized) { ExpectHasErrors( - "message TestMessage {\n" - " optional group foo = 1 {}\n" - "}\n", - "1:17: Group names must start with a capital letter.\n"); + "message TestMessage {\n" + " optional group foo = 1 {}\n" + "}\n", + "1:17: Group names must start with a capital letter.\n"); } TEST_F(ParseErrorTest, GroupMissingBody) { ExpectHasErrors( - "message TestMessage {\n" - " optional group Foo = 1;\n" - "}\n", - "1:24: Missing group body.\n"); + "message TestMessage {\n" + " optional group Foo = 1;\n" + "}\n", + "1:24: Missing group body.\n"); } TEST_F(ParseErrorTest, ExtendingPrimitive) { - ExpectHasErrors( - "extend int32 { optional string foo = 4; }\n", - "0:7: Expected message type.\n"); + ExpectHasErrors("extend int32 { optional string foo = 4; }\n", + "0:7: Expected message type.\n"); } TEST_F(ParseErrorTest, ErrorInExtension) { ExpectHasErrors( - "message Foo { extensions 100 to 199; }\n" - "extend Foo { optional string foo; }\n", - "1:32: Missing field number.\n"); + "message Foo { extensions 100 to 199; }\n" + "extend Foo { optional string foo; }\n", + "1:32: Missing field number.\n"); } TEST_F(ParseErrorTest, MultipleParseErrors) { // When a statement has a parse error, the parser should be able to continue // parsing at the next statement. ExpectHasErrors( - "message TestMessage {\n" - " optional int32 foo;\n" - " !invalid statement ending in a block { blah blah { blah } blah }\n" - " optional int32 bar = 3 {}\n" - "}\n", - "1:20: Missing field number.\n" - "2:2: Expected \"required\", \"optional\", or \"repeated\".\n" - "2:2: Expected type name.\n" - "3:25: Expected \";\".\n"); + "message TestMessage {\n" + " optional int32 foo;\n" + " !invalid statement ending in a block { blah blah { blah } blah }\n" + " optional int32 bar = 3 {}\n" + "}\n", + "1:20: Missing field number.\n" + "2:2: Expected \"required\", \"optional\", or \"repeated\".\n" + "2:2: Expected type name.\n" + "3:25: Expected \";\".\n"); } TEST_F(ParseErrorTest, EofInAggregateValue) { @@ -1503,67 +1545,66 @@ TEST_F(ParseErrorTest, ExplicitOptionalLabelProto3) { "label, as fields are 'optional' by default.\n"); } - // ------------------------------------------------------------------- // Enum errors TEST_F(ParseErrorTest, EofInEnum) { ExpectHasErrors( - "enum TestEnum {", - "0:15: Reached end of input in enum definition (missing '}').\n"); + "enum TestEnum {", + "0:15: Reached end of input in enum definition (missing '}').\n"); } TEST_F(ParseErrorTest, EnumValueMissingNumber) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO;\n" - "}\n", - "1:5: Missing numeric value for enum constant.\n"); + "enum TestEnum {\n" + " FOO;\n" + "}\n", + "1:5: Missing numeric value for enum constant.\n"); } TEST_F(ParseErrorTest, EnumReservedStandaloneMaxNotAllowed) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved max;\n" - "}\n", - "2:11: Expected enum value or number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved max;\n" + "}\n", + "2:11: Expected enum value or number range.\n"); } TEST_F(ParseErrorTest, EnumReservedMixNameAndNumber) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved 10, \"foo\";\n" - "}\n", - "2:15: Expected enum number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved 10, \"foo\";\n" + "}\n", + "2:15: Expected enum number range.\n"); } TEST_F(ParseErrorTest, EnumReservedPositiveNumberOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - "FOO = 1;\n" - " reserved 2147483648;\n" - "}\n", - "2:11: Integer out of range.\n"); + "enum TestEnum {\n" + "FOO = 1;\n" + " reserved 2147483648;\n" + "}\n", + "2:11: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumReservedNegativeNumberOutOfRange) { ExpectHasErrors( - "enum TestEnum {\n" - "FOO = 1;\n" - " reserved -2147483649;\n" - "}\n", - "2:12: Integer out of range.\n"); + "enum TestEnum {\n" + "FOO = 1;\n" + " reserved -2147483649;\n" + "}\n", + "2:12: Integer out of range.\n"); } TEST_F(ParseErrorTest, EnumReservedMissingQuotes) { ExpectHasErrors( - "enum TestEnum {\n" - " FOO = 1;\n" - " reserved foo;\n" - "}\n", - "2:11: Expected enum value or number range.\n"); + "enum TestEnum {\n" + " FOO = 1;\n" + " reserved foo;\n" + "}\n", + "2:11: Expected enum value or number range.\n"); } // ------------------------------------------------------------------- @@ -1571,90 +1612,89 @@ TEST_F(ParseErrorTest, EnumReservedMissingQuotes) { TEST_F(ParseErrorTest, ReservedStandaloneMaxNotAllowed) { ExpectHasErrors( - "message Foo {\n" - " reserved max;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved max;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedMixNameAndNumber) { ExpectHasErrors( - "message Foo {\n" - " reserved 10, \"foo\";\n" - "}\n", - "1:15: Expected field number range.\n"); + "message Foo {\n" + " reserved 10, \"foo\";\n" + "}\n", + "1:15: Expected field number range.\n"); } TEST_F(ParseErrorTest, ReservedMissingQuotes) { ExpectHasErrors( - "message Foo {\n" - " reserved foo;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved foo;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedNegativeNumber) { ExpectHasErrors( - "message Foo {\n" - " reserved -10;\n" - "}\n", - "1:11: Expected field name or number range.\n"); + "message Foo {\n" + " reserved -10;\n" + "}\n", + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedNumberOutOfRange) { ExpectHasErrors( - "message Foo {\n" - " reserved 2147483648;\n" - "}\n", - "1:11: Integer out of range.\n"); + "message Foo {\n" + " reserved 2147483648;\n" + "}\n", + "1:11: Integer out of range.\n"); } - // ------------------------------------------------------------------- // Service errors TEST_F(ParseErrorTest, EofInService) { ExpectHasErrors( - "service TestService {", - "0:21: Reached end of input in service definition (missing '}').\n"); + "service TestService {", + "0:21: Reached end of input in service definition (missing '}').\n"); } TEST_F(ParseErrorTest, ServiceMethodPrimitiveParams) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(int32) returns (string);\n" - "}\n", - "1:10: Expected message type.\n" - "1:26: Expected message type.\n"); + "service TestService {\n" + " rpc Foo(int32) returns (string);\n" + "}\n", + "1:10: Expected message type.\n" + "1:26: Expected message type.\n"); } TEST_F(ParseErrorTest, EofInMethodOptions) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(Bar) returns(Bar) {", - "1:29: Reached end of input in method options (missing '}').\n" - "1:29: Reached end of input in service definition (missing '}').\n"); + "service TestService {\n" + " rpc Foo(Bar) returns(Bar) {", + "1:29: Reached end of input in method options (missing '}').\n" + "1:29: Reached end of input in service definition (missing '}').\n"); } TEST_F(ParseErrorTest, PrimitiveMethodInput) { ExpectHasErrors( - "service TestService {\n" - " rpc Foo(int32) returns(Bar);\n" - "}\n", - "1:10: Expected message type.\n"); + "service TestService {\n" + " rpc Foo(int32) returns(Bar);\n" + "}\n", + "1:10: Expected message type.\n"); } TEST_F(ParseErrorTest, MethodOptionTypeError) { // This used to cause an infinite loop. ExpectHasErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Baz) { option invalid syntax; }\n" - "}\n", - "2:45: Expected \"=\".\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Baz) { option invalid syntax; }\n" + "}\n", + "2:45: Expected \"=\".\n"); } @@ -1662,16 +1702,15 @@ TEST_F(ParseErrorTest, MethodOptionTypeError) { // Import and package errors TEST_F(ParseErrorTest, ImportNotQuoted) { - ExpectHasErrors( - "import foo;\n", - "0:7: Expected a string naming the file to import.\n"); + ExpectHasErrors("import foo;\n", + "0:7: Expected a string naming the file to import.\n"); } TEST_F(ParseErrorTest, MultiplePackagesInFile) { ExpectHasErrors( - "package foo;\n" - "package bar;\n", - "1:0: Multiple package definitions.\n"); + "package foo;\n" + "package bar;\n", + "1:0: Multiple package definitions.\n"); } // =================================================================== @@ -1690,149 +1729,272 @@ TEST_F(ParserValidationErrorTest, PackageNameError) { // Now try to define it as a package. ExpectHasValidationErrors( - "package foo.bar;", - "0:0: \"foo\" is already defined (as something other than a package) " + "package foo.bar;", + "0:0: \"foo\" is already defined (as something other than a package) " "in file \"bar.proto\".\n"); } +TEST_F(ParserValidationErrorTest, ImportUnloadedError) { + ExpectHasValidationErrors( + "package test;\n" + "\n" + "import \"unloaded.proto\";", + "2:0: Import \"unloaded.proto\" has not been loaded.\n"); +} + +TEST_F(ParserValidationErrorTest, ImportTwice) { + FileDescriptorProto other_file; + other_file.set_name("bar.proto"); + other_file.add_message_type()->set_name("foo"); + EXPECT_TRUE(pool_.BuildFile(other_file) != nullptr); + + ExpectHasValidationErrors( + "package test;\n" + "\n" + "import \"bar.proto\";\n" + " import \"bar.proto\";", + "3:2: Import \"bar.proto\" was listed twice.\n"); +} + +TEST_F(ParserValidationErrorTest, DuplicateFileError) { + FileDescriptorProto other_file; + other_file.set_name("foo.proto"); + EXPECT_TRUE(pool_.BuildFile(other_file) != nullptr); + + ExpectHasValidationErrors( + "package test;", "0:0: A file with this name is already in the pool.\n"); +} + TEST_F(ParserValidationErrorTest, MessageNameError) { ExpectHasValidationErrors( - "message Foo {}\n" - "message Foo {}\n", - "1:8: \"Foo\" is already defined.\n"); + "message Foo {}\n" + "message Foo {}\n", + "1:8: \"Foo\" is already defined.\n"); } TEST_F(ParserValidationErrorTest, FieldNameError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 1;\n" - " optional int32 bar = 2;\n" - "}\n", - "2:17: \"bar\" is already defined in \"Foo\".\n"); + "message Foo {\n" + " optional int32 bar = 1;\n" + " optional int32 bar = 2;\n" + "}\n", + "2:17: \"bar\" is already defined in \"Foo\".\n"); } TEST_F(ParserValidationErrorTest, FieldTypeError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional Baz bar = 1;\n" - "}\n", - "1:11: \"Baz\" is not defined.\n"); + "message Foo {\n" + " optional Baz bar = 1;\n" + "}\n", + "1:11: \"Baz\" is not defined.\n"); } TEST_F(ParserValidationErrorTest, FieldNumberError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 0;\n" - "}\n", - "1:23: Field numbers must be positive integers.\n"); + "message Foo {\n" + " optional int32 bar = 0;\n" + "}\n", + "1:23: Field numbers must be positive integers.\n"); } TEST_F(ParserValidationErrorTest, FieldExtendeeError) { + ExpectHasValidationErrors("extend Baz { optional int32 bar = 1; }\n", + "0:7: \"Baz\" is not defined.\n"); +} + +TEST_F(ParserValidationErrorTest, ExtensionJsonNameError) { ExpectHasValidationErrors( - "extend Baz { optional int32 bar = 1; }\n", - "0:7: \"Baz\" is not defined.\n"); + "message TestMessage {\n" + " extensions 1 to 100;\n" + "}\n" + "extend TestMessage {\n" + " optional int32 foo = 12 [json_name = \"bar\"];\n" + "}", + "4:27: option json_name is not allowed on extension fields.\n"); } TEST_F(ParserValidationErrorTest, FieldDefaultValueError) { ExpectHasValidationErrors( - "enum Baz { QUX = 1; }\n" - "message Foo {\n" - " optional Baz bar = 1 [default=NO_SUCH_VALUE];\n" - "}\n", - "2:32: Enum type \"Baz\" has no value named \"NO_SUCH_VALUE\".\n"); + "enum Baz { QUX = 1; }\n" + "message Foo {\n" + " optional Baz bar = 1 [default=NO_SUCH_VALUE];\n" + "}\n", + "2:32: Enum type \"Baz\" has no value named \"NO_SUCH_VALUE\".\n"); } TEST_F(ParserValidationErrorTest, FileOptionNameError) { ExpectHasValidationErrors( - "option foo = 5;", - "0:7: Option \"foo\" unknown. Ensure that your proto definition file " - "imports the proto which defines the option.\n"); + "option foo = 5;", + "0:7: Option \"foo\" unknown. Ensure that your proto definition file " + "imports the proto which defines the option.\n"); } TEST_F(ParserValidationErrorTest, FileOptionValueError) { ExpectHasValidationErrors( - "option java_outer_classname = 5;", - "0:30: Value must be quoted string for string option " - "\"google.protobuf.FileOptions.java_outer_classname\".\n"); + "option java_outer_classname = 5;", + "0:30: Value must be quoted string for string option " + "\"google.protobuf.FileOptions.java_outer_classname\".\n"); } TEST_F(ParserValidationErrorTest, FieldOptionNameError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional bool bar = 1 [foo=1];\n" - "}\n", - "1:25: Option \"foo\" unknown. Ensure that your proto definition file " - "imports the proto which defines the option.\n"); + "message Foo {\n" + " optional bool bar = 1 [foo=1];\n" + "}\n", + "1:25: Option \"foo\" unknown. Ensure that your proto definition file " + "imports the proto which defines the option.\n"); } TEST_F(ParserValidationErrorTest, FieldOptionValueError) { ExpectHasValidationErrors( - "message Foo {\n" - " optional int32 bar = 1 [ctype=1];\n" - "}\n", - "1:32: Value must be identifier for enum-valued option " - "\"google.protobuf.FieldOptions.ctype\".\n"); + "message Foo {\n" + " optional int32 bar = 1 [ctype=1];\n" + "}\n", + "1:32: Value must be identifier for enum-valued option " + "\"google.protobuf.FieldOptions.ctype\".\n"); } TEST_F(ParserValidationErrorTest, ExtensionRangeNumberError) { ExpectHasValidationErrors( - "message Foo {\n" - " extensions 0;\n" - "}\n", - "1:13: Extension numbers must be positive integers.\n"); + "message Foo {\n" + " extensions 0;\n" + "}\n", + "1:13: Extension numbers must be positive integers.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3ExtensionError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " extensions 100 to 199;\n" + "}\n" + "extend Foo { string foo = 101; }\n", + "4:7: Extensions in proto3 are only allowed for defining options.\n" + "2:13: Extension ranges are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3MessageSet) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " option message_set_wire_format = true;\n" + "}\n", + "1:8: MessageSet is not supported in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3Required) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " required int32 field = 1;" + "}\n", + "2:11: Required fields are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3Default) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message Foo { \n" + " int32 field = 1 [default = 12];" + "}\n", + "2:29: Explicit default values are not allowed in proto3.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3JsonConflictError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "message TestMessage {\n" + " uint32 foo = 1;\n" + " uint32 Foo = 2;\n" + "}\n", + "3:9: The JSON camel-case name of field \"Foo\" conflicts with field " + "\"foo\". This is not allowed in proto3.\n"); } TEST_F(ParserValidationErrorTest, EnumNameError) { ExpectHasValidationErrors( - "enum Foo {A = 1;}\n" - "enum Foo {B = 1;}\n", - "1:5: \"Foo\" is already defined.\n"); + "enum Foo {A = 1;}\n" + "enum Foo {B = 1;}\n", + "1:5: \"Foo\" is already defined.\n"); +} + +TEST_F(ParserValidationErrorTest, Proto3EnumError) { + ExpectHasValidationErrors( + "syntax = 'proto3';\n" + "enum Foo {A = 1;}\n", + "1:14: The first enum value must be zero in proto3.\n"); } TEST_F(ParserValidationErrorTest, EnumValueNameError) { ExpectHasValidationErrors( - "enum Foo {\n" - " BAR = 1;\n" - " BAR = 1;\n" - "}\n", - "2:2: \"BAR\" is already defined.\n"); + "enum Foo {\n" + " BAR = 1;\n" + " BAR = 1;\n" + "}\n", + "2:2: \"BAR\" is already defined.\n"); +} + +TEST_F(ParserValidationErrorTest, EnumValueAliasError) { + ExpectHasValidationErrors( + "enum Foo {\n" + " BAR = 1;\n" + " BAZ = 1;\n" + "}\n", + "2:8: \"BAZ\" uses the same enum value as \"BAR\". If this is " + "intended, set 'option allow_alias = true;' to the enum " + "definition.\n"); +} + +TEST_F(ParserValidationErrorTest, ExplicitlyMapEntryError) { + ExpectHasValidationErrors( + "message Foo {\n" + " message ValueEntry {\n" + " option map_entry = true;\n" + " optional int32 key = 1;\n" + " optional int32 value = 2;\n" + " extensions 99 to 999;\n" + " }\n" + " repeated ValueEntry value = 1;\n" + "}", + "7:11: map_entry should not be set explicitly. Use " + "map<KeyType, ValueType> instead.\n"); } TEST_F(ParserValidationErrorTest, ServiceNameError) { ExpectHasValidationErrors( - "service Foo {}\n" - "service Foo {}\n", - "1:8: \"Foo\" is already defined.\n"); + "service Foo {}\n" + "service Foo {}\n", + "1:8: \"Foo\" is already defined.\n"); } TEST_F(ParserValidationErrorTest, MethodNameError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Baz);\n" - " rpc Bar(Baz) returns(Baz);\n" - "}\n", - "3:6: \"Bar\" is already defined in \"Foo\".\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Baz);\n" + " rpc Bar(Baz) returns(Baz);\n" + "}\n", + "3:6: \"Bar\" is already defined in \"Foo\".\n"); } TEST_F(ParserValidationErrorTest, MethodInputTypeError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Qux) returns(Baz);\n" - "}\n", - "2:10: \"Qux\" is not defined.\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Qux) returns(Baz);\n" + "}\n", + "2:10: \"Qux\" is not defined.\n"); } TEST_F(ParserValidationErrorTest, MethodOutputTypeError) { ExpectHasValidationErrors( - "message Baz {}\n" - "service Foo {\n" - " rpc Bar(Baz) returns(Qux);\n" - "}\n", - "2:23: \"Qux\" is not defined.\n"); + "message Baz {}\n" + "service Foo {\n" + " rpc Bar(Baz) returns(Qux);\n" + "}\n", + "2:23: \"Qux\" is not defined.\n"); } @@ -1847,16 +2009,16 @@ TEST_F(ParserValidationErrorTest, ResovledUndefinedError) { // Define "foo.base" and try "base.bar". // "base.bar" is resolved to "foo.base.bar" which is not defined. ExpectHasValidationErrors( - "package foo.base;\n" - "import \"base.proto\";\n" - "message qux {\n" - " optional base.bar baz = 1;\n" - " optional .base.bar quz = 2;\n" - "}\n", - "3:11: \"base.bar\" is resolved to \"foo.base.bar\"," - " which is not defined. The innermost scope is searched first " - "in name resolution. Consider using a leading '.'(i.e., \".base.bar\")" - " to start from the outermost scope.\n"); + "package foo.base;\n" + "import \"base.proto\";\n" + "message qux {\n" + " optional base.bar baz = 1;\n" + " optional .base.bar quz = 2;\n" + "}\n", + "3:11: \"base.bar\" is resolved to \"foo.base.bar\"," + " which is not defined. The innermost scope is searched first " + "in name resolution. Consider using a leading '.'(i.e., \".base.bar\")" + " to start from the outermost scope.\n"); } TEST_F(ParserValidationErrorTest, ResovledUndefinedOptionError) { @@ -1928,26 +2090,26 @@ class CompareDescriptorNames { }; // Sorts nested DescriptorProtos of a DescriptoProto, by name. -void SortMessages(DescriptorProto *descriptor_proto) { +void SortMessages(DescriptorProto* descriptor_proto) { int size = descriptor_proto->nested_type_size(); // recursively sort; we can't guarantee the order of nested messages either for (int i = 0; i < size; ++i) { SortMessages(descriptor_proto->mutable_nested_type(i)); } - DescriptorProto **data = - descriptor_proto->mutable_nested_type()->mutable_data(); + DescriptorProto** data = + descriptor_proto->mutable_nested_type()->mutable_data(); std::sort(data, data + size, CompareDescriptorNames()); } // Sorts DescriptorProtos belonging to a FileDescriptorProto, by name. -void SortMessages(FileDescriptorProto *file_descriptor_proto) { +void SortMessages(FileDescriptorProto* file_descriptor_proto) { int size = file_descriptor_proto->message_type_size(); // recursively sort; we can't guarantee the order of nested messages either for (int i = 0; i < size; ++i) { SortMessages(file_descriptor_proto->mutable_message_type(i)); } - DescriptorProto **data = - file_descriptor_proto->mutable_message_type()->mutable_data(); + DescriptorProto** data = + file_descriptor_proto->mutable_message_type()->mutable_data(); std::sort(data, data + size, CompareDescriptorNames()); } @@ -1974,7 +2136,7 @@ void StripFieldTypeName(FileDescriptorProto* file_proto) { TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { const FileDescriptor* original_file = - protobuf_unittest::TestAllTypes::descriptor()->file(); + protobuf_unittest::TestAllTypes::descriptor()->file(); FileDescriptorProto expected; original_file->CopyTo(&expected); @@ -1987,8 +2149,7 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { FileDescriptorProto parsed; parser_->Parse(input_.get(), &parsed); EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); - ASSERT_EQ("", error_collector_.text_) - << "Failed to parse:\n" << debug_string; + ASSERT_EQ("", error_collector_.text_) << "Failed to parse:\n" << debug_string; // We now have a FileDescriptorProto, but to compare with the expected we // need to link to a FileDecriptor, then output back to a proto. We'll @@ -2002,14 +2163,13 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { public_import->CopyTo(&public_import_proto); ASSERT_TRUE(pool_.BuildFile(public_import_proto) != NULL); const FileDescriptor* import = - protobuf_unittest_import::ImportMessage::descriptor()->file(); + protobuf_unittest_import::ImportMessage::descriptor()->file(); FileDescriptorProto import_proto; import->CopyTo(&import_proto); ASSERT_TRUE(pool_.BuildFile(import_proto) != NULL); const FileDescriptor* actual = pool_.BuildFile(parsed); parsed.Clear(); - ASSERT_TRUE(actual != NULL) - << "Failed to validate:\n" << debug_string; + ASSERT_TRUE(actual != NULL) << "Failed to validate:\n" << debug_string; actual->CopyTo(&parsed); ASSERT_TRUE(actual != NULL); @@ -2027,7 +2187,7 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) { TEST_F(ParseDescriptorDebugTest, TestCustomOptions) { const FileDescriptor* original_file = - protobuf_unittest::AggregateMessage::descriptor()->file(); + protobuf_unittest::AggregateMessage::descriptor()->file(); FileDescriptorProto expected; original_file->CopyTo(&expected); @@ -2139,25 +2299,25 @@ TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) { // We don't test the exact comment placement or formatting, because we do not // want to be too fragile here. const char* expected_comments[] = { - "Detached comment before syntax.", - "Syntax comment.", - "Detached comment before package.", - "Package comment.", - "Detached comment before TestMessage1.", - "Message comment.", - "More detail in message comment.", - "Detached comment before foo.", - "Field comment", - "Detached comment before NestedMessage.", - "Nested-message comment", - "Detached comment before MyEnumType.", - "Enum comment", - "Detached comment before ASDF.", - "Enum-value comment", - "Detached comment before MyService.", - "Service comment", - "Detached comment before MyRPCCall.", - "RPC comment", + "Detached comment before syntax.", + "Syntax comment.", + "Detached comment before package.", + "Package comment.", + "Detached comment before TestMessage1.", + "Message comment.", + "More detail in message comment.", + "Detached comment before foo.", + "Field comment", + "Detached comment before NestedMessage.", + "Nested-message comment", + "Detached comment before MyEnumType.", + "Enum comment", + "Detached comment before ASDF.", + "Enum-value comment", + "Detached comment before MyService.", + "Service comment", + "Detached comment before MyRPCCall.", + "RPC comment", }; DebugStringOptions debug_string_options; @@ -2179,8 +2339,8 @@ TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) { FileDescriptorProto parsed; parser_->Parse(input_.get(), &parsed); EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type); - ASSERT_EQ("", error_collector_.text_) - << "Failed to parse:\n" << debug_string; + ASSERT_EQ("", error_collector_.text_) << "Failed to parse:\n" + << debug_string; } } @@ -2234,11 +2394,9 @@ TEST_F(ParseDescriptorDebugTest, TestMaps) { // *output_field to the descriptor of the field, and *output_index to -1. // Returns true if the path was valid, false otherwise. A gTest failure is // recorded before returning false. -bool FollowPath(const Message& root, - const int* path_begin, const int* path_end, +bool FollowPath(const Message& root, const int* path_begin, const int* path_end, const Message** output_message, - const FieldDescriptor** output_field, - int* output_index) { + const FieldDescriptor** output_field, int* output_index) { if (path_begin == path_end) { // Path refers to this whole message. *output_message = &root; @@ -2253,8 +2411,8 @@ bool FollowPath(const Message& root, const FieldDescriptor* field = descriptor->FindFieldByNumber(*path_begin); if (field == NULL) { - ADD_FAILURE() << descriptor->name() << " has no field number: " - << *path_begin; + ADD_FAILURE() << descriptor->name() + << " has no field number: " << *path_begin; return false; } @@ -2274,16 +2432,16 @@ bool FollowPath(const Message& root, if (index >= size) { ADD_FAILURE() << descriptor->name() << "." << field->name() - << " has size " << size << ", but path contained index: " - << index; + << " has size " << size + << ", but path contained index: " << index; return false; } if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { // Descend into child message. const Message& child = reflection->GetRepeatedMessage(root, field, index); - return FollowPath(child, path_begin, path_end, - output_message, output_field, output_index); + return FollowPath(child, path_begin, path_end, output_message, + output_field, output_index); } else if (path_begin == path_end) { // Path refers to this element. *output_message = &root; @@ -2298,8 +2456,8 @@ bool FollowPath(const Message& root, } else { if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { const Message& child = reflection->GetMessage(root, field); - return FollowPath(child, path_begin, path_end, - output_message, output_field, output_index); + return FollowPath(child, path_begin, path_end, output_message, + output_field, output_index); } else if (path_begin == path_end) { // Path refers to this field. *output_message = &root; @@ -2359,9 +2517,8 @@ class SourceInfoTest : public ParserTest { } virtual void TearDown() { - EXPECT_TRUE(spans_.empty()) - << "Forgot to call HasSpan() for:\n" - << spans_.begin()->second->DebugString(); + EXPECT_TRUE(spans_.empty()) << "Forgot to call HasSpan() for:\n" + << spans_.begin()->second->DebugString(); } // ----------------------------------------------------------------- @@ -2372,8 +2529,8 @@ class SourceInfoTest : public ParserTest { bool HasSpan(char start_marker, char end_marker, const Message& descriptor_proto) { - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, NULL, -1, NULL, NULL, NULL); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, NULL, + -1, NULL, NULL, NULL); } bool HasSpanWithComment(char start_marker, char end_marker, @@ -2381,10 +2538,10 @@ class SourceInfoTest : public ParserTest { const char* expected_leading_comments, const char* expected_trailing_comments, const char* expected_leading_detached_comments) { - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, NULL, -1, - expected_leading_comments, expected_trailing_comments, - expected_leading_detached_comments); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, NULL, + -1, expected_leading_comments, + expected_trailing_comments, + expected_leading_detached_comments); } bool HasSpan(char start_marker, char end_marker, @@ -2395,8 +2552,8 @@ class SourceInfoTest : public ParserTest { bool HasSpan(char start_marker, char end_marker, const Message& descriptor_proto, const std::string& field_name, int index) { - return HasSpan(start_marker, end_marker, descriptor_proto, - field_name, index, NULL, NULL, NULL); + return HasSpan(start_marker, end_marker, descriptor_proto, field_name, + index, NULL, NULL, NULL); } bool HasSpan(char start_marker, char end_marker, @@ -2412,15 +2569,15 @@ class SourceInfoTest : public ParserTest { return false; } - return HasSpanWithComment( - start_marker, end_marker, descriptor_proto, field, index, - expected_leading_comments, expected_trailing_comments, - expected_leading_detached_comments); + return HasSpanWithComment(start_marker, end_marker, descriptor_proto, field, + index, expected_leading_comments, + expected_trailing_comments, + expected_leading_detached_comments); } bool HasSpan(const Message& descriptor_proto) { - return HasSpanWithComment( - '\0', '\0', descriptor_proto, NULL, -1, NULL, NULL, NULL); + return HasSpanWithComment('\0', '\0', descriptor_proto, NULL, -1, NULL, + NULL, NULL); } bool HasSpan(const Message& descriptor_proto, const std::string& field_name) { @@ -2432,12 +2589,12 @@ class SourceInfoTest : public ParserTest { return HasSpan('\0', '\0', descriptor_proto, field_name, index); } - bool HasSpanWithComment( - char start_marker, char end_marker, const Message& descriptor_proto, - const FieldDescriptor* field, int index, - const char* expected_leading_comments, - const char* expected_trailing_comments, - const char* expected_leading_detached_comments) { + bool HasSpanWithComment(char start_marker, char end_marker, + const Message& descriptor_proto, + const FieldDescriptor* field, int index, + const char* expected_leading_comments, + const char* expected_trailing_comments, + const char* expected_leading_detached_comments) { std::pair<SpanMap::iterator, SpanMap::iterator> range = spans_.equal_range(SpanKey(descriptor_proto, field, index)); @@ -2501,9 +2658,9 @@ class SourceInfoTest : public ParserTest { inline SpanKey() {} inline SpanKey(const Message& descriptor_proto_param, - const FieldDescriptor* field_param, - int index_param) - : descriptor_proto(&descriptor_proto_param), field(field_param), + const FieldDescriptor* field_param, int index_param) + : descriptor_proto(&descriptor_proto_param), + field(field_param), index(index_param) {} inline bool operator<(const SpanKey& other) const { @@ -2551,15 +2708,15 @@ class SourceInfoTest : public ParserTest { }; TEST_F(SourceInfoTest, BasicFileDecls) { - EXPECT_TRUE(Parse( - "$a$syntax = \"proto2\";$i$\n" - "$b$package foo.bar;$c$\n" - "$d$import \"baz.proto\";$e$\n" - "$f$import\"qux.proto\";$h$\n" - "$j$import $k$public$l$ \"bar.proto\";$m$\n" - "$n$import $o$weak$p$ \"bar.proto\";$q$\n" - "\n" - "// comment ignored\n")); + EXPECT_TRUE( + Parse("$a$syntax = \"proto2\";$i$\n" + "$b$package foo.bar;$c$\n" + "$d$import \"baz.proto\";$e$\n" + "$f$import\"qux.proto\";$h$\n" + "$j$import $k$public$l$ \"bar.proto\";$m$\n" + "$n$import $o$weak$p$ \"bar.proto\";$q$\n" + "\n" + "// comment ignored\n")); EXPECT_TRUE(HasSpan('a', 'q', file_)); EXPECT_TRUE(HasSpan('b', 'c', file_, "package")); @@ -2573,9 +2730,9 @@ TEST_F(SourceInfoTest, BasicFileDecls) { } TEST_F(SourceInfoTest, Messages) { - EXPECT_TRUE(Parse( - "$a$message $b$Foo$c$ {}$d$\n" - "$e$message $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$message $b$Foo$c$ {}$d$\n" + "$e$message $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.message_type(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.message_type(0), "name")); @@ -2587,11 +2744,11 @@ TEST_F(SourceInfoTest, Messages) { } TEST_F(SourceInfoTest, Fields) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$optional$b$ $c$int32$d$ $e$bar$f$ = $g$1$h$;$i$\n" - " $j$repeated$k$ $l$X.Y$m$ $n$baz$o$ = $p$2$q$;$r$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$optional$b$ $c$int32$d$ $e$bar$f$ = $g$1$h$;$i$\n" + " $j$repeated$k$ $l$X.Y$m$ $n$baz$o$ = $p$2$q$;$r$\n" + "}\n")); const FieldDescriptorProto& field1 = file_.message_type(0).field(0); const FieldDescriptorProto& field2 = file_.message_type(0).field(1); @@ -2615,14 +2772,14 @@ TEST_F(SourceInfoTest, Fields) { } TEST_F(SourceInfoTest, Extensions) { - EXPECT_TRUE(Parse( - "$a$extend $b$Foo$c$ {\n" - " $d$optional$e$ int32 bar = 1;$f$\n" - " $g$repeated$h$ X.Y baz = 2;$i$\n" - "}$j$\n" - "$k$extend $l$Bar$m$ {\n" - " $n$optional int32 qux = 1;$o$\n" - "}$p$\n")); + EXPECT_TRUE( + Parse("$a$extend $b$Foo$c$ {\n" + " $d$optional$e$ int32 bar = 1;$f$\n" + " $g$repeated$h$ X.Y baz = 2;$i$\n" + "}$j$\n" + "$k$extend $l$Bar$m$ {\n" + " $n$optional int32 qux = 1;$o$\n" + "}$p$\n")); const FieldDescriptorProto& field1 = file_.extension(0); const FieldDescriptorProto& field2 = file_.extension(1); @@ -2657,16 +2814,16 @@ TEST_F(SourceInfoTest, Extensions) { } TEST_F(SourceInfoTest, NestedExtensions) { - EXPECT_TRUE(Parse( - "message Message {\n" - " $a$extend $b$Foo$c$ {\n" - " $d$optional$e$ int32 bar = 1;$f$\n" - " $g$repeated$h$ X.Y baz = 2;$i$\n" - " }$j$\n" - " $k$extend $l$Bar$m$ {\n" - " $n$optional int32 qux = 1;$o$\n" - " }$p$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Message {\n" + " $a$extend $b$Foo$c$ {\n" + " $d$optional$e$ int32 bar = 1;$f$\n" + " $g$repeated$h$ X.Y baz = 2;$i$\n" + " }$j$\n" + " $k$extend $l$Bar$m$ {\n" + " $n$optional int32 qux = 1;$o$\n" + " }$p$\n" + "}\n")); const FieldDescriptorProto& field1 = file_.message_type(0).extension(0); const FieldDescriptorProto& field2 = file_.message_type(0).extension(1); @@ -2703,11 +2860,11 @@ TEST_F(SourceInfoTest, NestedExtensions) { } TEST_F(SourceInfoTest, ExtensionRanges) { - EXPECT_TRUE(Parse( - "message Message {\n" - " $a$extensions $b$1$c$ to $d$4$e$, $f$6$g$;$h$\n" - " $i$extensions $j$8$k$ to $l$max$m$;$n$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Message {\n" + " $a$extensions $b$1$c$ to $d$4$e$, $f$6$g$;$h$\n" + " $i$extensions $j$8$k$ to $l$max$m$;$n$\n" + "}\n")); const DescriptorProto::ExtensionRange& range1 = file_.message_type(0).extension_range(0); @@ -2765,12 +2922,12 @@ TEST_F(SourceInfoTest, ReservedRanges) { } TEST_F(SourceInfoTest, Oneofs) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$oneof $c$foo$d$ {\n" - " $e$int32$f$ $g$a$h$ = $i$1$j$;$k$\n" - " }$r$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$oneof $c$foo$d$ {\n" + " $e$int32$f$ $g$a$h$ = $i$1$j$;$k$\n" + " }$r$\n" + "}\n")); const OneofDescriptorProto& oneof_decl = file_.message_type(0).oneof_decl(0); const FieldDescriptorProto& field = file_.message_type(0).field(0); @@ -2790,13 +2947,13 @@ TEST_F(SourceInfoTest, Oneofs) { } TEST_F(SourceInfoTest, NestedMessages) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$message $b$Bar$c$ {\n" - " $d$message $e$Baz$f$ {}$g$\n" - " }$h$\n" - " $i$message $j$Qux$k$ {}$l$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$message $b$Bar$c$ {\n" + " $d$message $e$Baz$f$ {}$g$\n" + " }$h$\n" + " $i$message $j$Qux$k$ {}$l$\n" + "}\n")); const DescriptorProto& bar = file_.message_type(0).nested_type(0); const DescriptorProto& baz = bar.nested_type(0); @@ -2816,13 +2973,13 @@ TEST_F(SourceInfoTest, NestedMessages) { } TEST_F(SourceInfoTest, Groups) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " message Bar {}\n" - " $a$optional$b$ $c$group$d$ $e$Baz$f$ = $g$1$h$ {\n" - " $i$message Qux {}$j$\n" - " }$k$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " message Bar {}\n" + " $a$optional$b$ $c$group$d$ $e$Baz$f$ = $g$1$h$ {\n" + " $i$message Qux {}$j$\n" + " }$k$\n" + "}\n")); const DescriptorProto& bar = file_.message_type(0).nested_type(0); const DescriptorProto& baz = file_.message_type(0).nested_type(1); @@ -2850,9 +3007,9 @@ TEST_F(SourceInfoTest, Groups) { } TEST_F(SourceInfoTest, Enums) { - EXPECT_TRUE(Parse( - "$a$enum $b$Foo$c$ {}$d$\n" - "$e$enum $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$enum $b$Foo$c$ {}$d$\n" + "$e$enum $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.enum_type(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.enum_type(0), "name")); @@ -2864,11 +3021,11 @@ TEST_F(SourceInfoTest, Enums) { } TEST_F(SourceInfoTest, EnumValues) { - EXPECT_TRUE(Parse( - "enum Foo {\n" - " $a$BAR$b$ = $c$1$d$;$e$\n" - " $f$BAZ$g$ = $h$2$i$;$j$\n" - "}")); + EXPECT_TRUE( + Parse("enum Foo {\n" + " $a$BAR$b$ = $c$1$d$;$e$\n" + " $f$BAZ$g$ = $h$2$i$;$j$\n" + "}")); const EnumValueDescriptorProto& bar = file_.enum_type(0).value(0); const EnumValueDescriptorProto& baz = file_.enum_type(0).value(1); @@ -2887,11 +3044,11 @@ TEST_F(SourceInfoTest, EnumValues) { } TEST_F(SourceInfoTest, NestedEnums) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$enum $b$Bar$c$ {}$d$\n" - " $e$enum $f$Baz$g$ {}$h$\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$enum $b$Bar$c$ {}$d$\n" + " $e$enum $f$Baz$g$ {}$h$\n" + "}\n")); const EnumDescriptorProto& bar = file_.message_type(0).enum_type(0); const EnumDescriptorProto& baz = file_.message_type(0).enum_type(1); @@ -2908,9 +3065,9 @@ TEST_F(SourceInfoTest, NestedEnums) { } TEST_F(SourceInfoTest, Services) { - EXPECT_TRUE(Parse( - "$a$service $b$Foo$c$ {}$d$\n" - "$e$service $f$Bar$g$ {}$h$\n")); + EXPECT_TRUE( + Parse("$a$service $b$Foo$c$ {}$d$\n" + "$e$service $f$Bar$g$ {}$h$\n")); EXPECT_TRUE(HasSpan('a', 'd', file_.service(0))); EXPECT_TRUE(HasSpan('b', 'c', file_.service(0), "name")); @@ -2922,11 +3079,11 @@ TEST_F(SourceInfoTest, Services) { } TEST_F(SourceInfoTest, MethodsAndStreams) { - EXPECT_TRUE(Parse( - "service Foo {\n" - " $a$rpc $b$Bar$c$($d$X$e$) returns($f$Y$g$);$h$" - " $i$rpc $j$Baz$k$($l$Z$m$) returns($n$W$o$);$p$" - "}")); + EXPECT_TRUE( + Parse("service Foo {\n" + " $a$rpc $b$Bar$c$($d$X$e$) returns($f$Y$g$);$h$" + " $i$rpc $j$Baz$k$($l$Z$m$) returns($n$W$o$);$p$" + "}")); const MethodDescriptorProto& bar = file_.service(0).method(0); const MethodDescriptorProto& baz = file_.service(0).method(1); @@ -2949,15 +3106,14 @@ TEST_F(SourceInfoTest, MethodsAndStreams) { TEST_F(SourceInfoTest, Options) { - EXPECT_TRUE(Parse( - "$a$option $b$foo$c$.$d$($e$bar.baz$f$)$g$ = " - "$h$123$i$;$j$\n" - "$k$option qux = $l$-123$m$;$n$\n" - "$o$option corge = $p$abc$q$;$r$\n" - "$s$option grault = $t$'blah'$u$;$v$\n" - "$w$option garply = $x${ yadda yadda }$y$;$z$\n" - "$0$option waldo = $1$123.0$2$;$3$\n" - )); + EXPECT_TRUE( + Parse("$a$option $b$foo$c$.$d$($e$bar.baz$f$)$g$ = " + "$h$123$i$;$j$\n" + "$k$option qux = $l$-123$m$;$n$\n" + "$o$option corge = $p$abc$q$;$r$\n" + "$s$option grault = $t$'blah'$u$;$v$\n" + "$w$option garply = $x${ yadda yadda }$y$;$z$\n" + "$0$option waldo = $1$123.0$2$;$3$\n")); const UninterpretedOption& option1 = file_.options().uninterpreted_option(0); const UninterpretedOption& option2 = file_.options().uninterpreted_option(1); @@ -3015,22 +3171,22 @@ TEST_F(SourceInfoTest, Options) { } TEST_F(SourceInfoTest, ScopedOptions) { - EXPECT_TRUE(Parse( - "message Foo {\n" - " $a$option mopt = 1;$b$\n" - "}\n" - "enum Bar {\n" - " $c$option eopt = 1;$d$\n" - "}\n" - "service Baz {\n" - " $e$option sopt = 1;$f$\n" - " rpc M(X) returns(Y) {\n" - " $g$option mopt = 1;$h$\n" - " }\n" - " rpc MS4($1$stream$2$ X) returns($3$stream$4$ Y) {\n" - " $k$option mopt = 1;$l$\n" - " }\n" - "}\n")); + EXPECT_TRUE( + Parse("message Foo {\n" + " $a$option mopt = 1;$b$\n" + "}\n" + "enum Bar {\n" + " $c$option eopt = 1;$d$\n" + "}\n" + "service Baz {\n" + " $e$option sopt = 1;$f$\n" + " rpc M(X) returns(Y) {\n" + " $g$option mopt = 1;$h$\n" + " }\n" + " rpc MS4($1$stream$2$ X) returns($3$stream$4$ Y) {\n" + " $k$option mopt = 1;$l$\n" + " }\n" + "}\n")); EXPECT_TRUE(HasSpan('a', 'b', file_.message_type(0).options())); EXPECT_TRUE(HasSpan('c', 'd', file_.enum_type(0).options())); @@ -3041,87 +3197,89 @@ TEST_F(SourceInfoTest, ScopedOptions) { EXPECT_TRUE(HasSpan(file_)); EXPECT_TRUE(HasSpan(file_.message_type(0))); EXPECT_TRUE(HasSpan(file_.message_type(0), "name")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.message_type(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.message_type(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE( + HasSpan(file_.message_type(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE(HasSpan(file_.message_type(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.enum_type(0))); EXPECT_TRUE(HasSpan(file_.enum_type(0), "name")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.enum_type(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.enum_type(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE( + HasSpan(file_.enum_type(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE(HasSpan(file_.enum_type(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.service(0))); EXPECT_TRUE(HasSpan(file_.service(0), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE(HasSpan(file_.service(0).options().uninterpreted_option(0))); + EXPECT_TRUE( + HasSpan(file_.service(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE( + HasSpan(file_.service(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).options().uninterpreted_option(0).name(0), "name_part")); + EXPECT_TRUE(HasSpan(file_.service(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "input_type")); EXPECT_TRUE(HasSpan(file_.service(0).method(0), "output_type")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).method(0).options() - .uninterpreted_option(0), "positive_int_value")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(0).options().uninterpreted_option(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0), "name")); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(0).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(0).options().uninterpreted_option(0), + "positive_int_value")); EXPECT_TRUE(HasSpan('k', 'l', file_.service(0).method(1).options())); EXPECT_TRUE(HasSpan(file_.service(0).method(1))); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "name")); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "input_type")); EXPECT_TRUE(HasSpan(file_.service(0).method(1), "output_type")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0), "name")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan(file_.service(0).method(1).options() - .uninterpreted_option(0), "positive_int_value")); - EXPECT_TRUE(HasSpan('1', '2', file_.service(0).method(1), - "client_streaming")); - EXPECT_TRUE(HasSpan('3', '4', file_.service(0).method(1), - "server_streaming")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(1).options().uninterpreted_option(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0), "name")); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0).name(0))); + EXPECT_TRUE(HasSpan( + file_.service(0).method(1).options().uninterpreted_option(0).name(0), + "name_part")); + EXPECT_TRUE( + HasSpan(file_.service(0).method(1).options().uninterpreted_option(0), + "positive_int_value")); + EXPECT_TRUE( + HasSpan('1', '2', file_.service(0).method(1), "client_streaming")); + EXPECT_TRUE( + HasSpan('3', '4', file_.service(0).method(1), "server_streaming")); } TEST_F(SourceInfoTest, FieldOptions) { // The actual "name = value" pairs are parsed by the same code as for // top-level options so we won't re-test that -- just make sure that the // syntax used for field options is understood. - EXPECT_TRUE(Parse( - "message Foo {" - " optional int32 bar = 1 " - "$a$[default=$b$123$c$,$d$opt1=123$e$," - "$f$opt2='hi'$g$]$h$;" - "}\n" - )); + EXPECT_TRUE( + Parse("message Foo {" + " optional int32 bar = 1 " + "$a$[default=$b$123$c$,$d$opt1=123$e$," + "$f$opt2='hi'$g$]$h$;" + "}\n")); const FieldDescriptorProto& field = file_.message_type(0).field(0); const UninterpretedOption& option1 = field.options().uninterpreted_option(0); @@ -3155,11 +3313,10 @@ TEST_F(SourceInfoTest, EnumValueOptions) { // The actual "name = value" pairs are parsed by the same code as for // top-level options so we won't re-test that -- just make sure that the // syntax used for enum options is understood. - EXPECT_TRUE(Parse( - "enum Foo {" - " BAR = 1 $a$[$b$opt1=123$c$,$d$opt2='hi'$e$]$f$;" - "}\n" - )); + EXPECT_TRUE( + Parse("enum Foo {" + " BAR = 1 $a$[$b$opt1=123$c$,$d$opt2='hi'$e$]$f$;" + "}\n")); const EnumValueDescriptorProto& value = file_.enum_type(0).value(0); const UninterpretedOption& option1 = value.options().uninterpreted_option(0); @@ -3187,33 +3344,28 @@ TEST_F(SourceInfoTest, EnumValueOptions) { } TEST_F(SourceInfoTest, DocComments) { - EXPECT_TRUE(Parse( - "// Foo leading\n" - "// line 2\n" - "$a$message Foo {\n" - " // Foo trailing\n" - " // line 2\n" - "\n" - " // detached\n" - "\n" - " // bar leading\n" - " $b$optional int32 bar = 1;$c$\n" - " // bar trailing\n" - "}$d$\n" - "// ignored\n" - )); + EXPECT_TRUE( + Parse("// Foo leading\n" + "// line 2\n" + "$a$message Foo {\n" + " // Foo trailing\n" + " // line 2\n" + "\n" + " // detached\n" + "\n" + " // bar leading\n" + " $b$optional int32 bar = 1;$c$\n" + " // bar trailing\n" + "}$d$\n" + "// ignored\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, - " Foo leading\n line 2\n", - " Foo trailing\n line 2\n", - NULL)); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - " detached\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, " Foo leading\n line 2\n", + " Foo trailing\n line 2\n", NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", " detached\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3225,45 +3377,40 @@ TEST_F(SourceInfoTest, DocComments) { } TEST_F(SourceInfoTest, DocComments2) { - EXPECT_TRUE(Parse( - "// detached before message.\n" - "\n" - "// Foo leading\n" - "// line 2\n" - "$a$message Foo {\n" - " /* Foo trailing\n" - " * line 2 */\n" - " // detached\n" - " /* bar leading\n" - " */" - " $b$optional int32 bar = 1;$c$ // bar trailing\n" - " // ignored detached\n" - "}$d$\n" - "// ignored\n" - "\n" - "// detached before option\n" - "\n" - "// option leading\n" - "$e$option baz = 123;$f$\n" - "// option trailing\n" - )); + EXPECT_TRUE( + Parse("// detached before message.\n" + "\n" + "// Foo leading\n" + "// line 2\n" + "$a$message Foo {\n" + " /* Foo trailing\n" + " * line 2 */\n" + " // detached\n" + " /* bar leading\n" + " */" + " $b$optional int32 bar = 1;$c$ // bar trailing\n" + " // ignored detached\n" + "}$d$\n" + "// ignored\n" + "\n" + "// detached before option\n" + "\n" + "// option leading\n" + "$e$option baz = 123;$f$\n" + "// option trailing\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); const UninterpretedOption& baz = file_.options().uninterpreted_option(0); - EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, - " Foo leading\n line 2\n", - " Foo trailing\n line 2 ", - " detached before message.\n")); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - " detached\n")); - EXPECT_TRUE(HasSpanWithComment('e', 'f', baz, - " option leading\n", - " option trailing\n", - " detached before option\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'd', foo, " Foo leading\n line 2\n", + " Foo trailing\n line 2 ", + " detached before message.\n")); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", " detached\n")); + EXPECT_TRUE(HasSpanWithComment('e', 'f', baz, " option leading\n", + " option trailing\n", + " detached before option\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3280,22 +3427,19 @@ TEST_F(SourceInfoTest, DocComments2) { } TEST_F(SourceInfoTest, DocComments3) { - EXPECT_TRUE(Parse( - "$a$message Foo {\n" - " // bar leading\n" - " $b$optional int32 bar = 1 [(baz.qux) = {}];$c$\n" - " // bar trailing\n" - "}$d$\n" - "// ignored\n" - )); + EXPECT_TRUE( + Parse("$a$message Foo {\n" + " // bar leading\n" + " $b$optional int32 bar = 1 [(baz.qux) = {}];$c$\n" + " // bar trailing\n" + "}$d$\n" + "// ignored\n")); const DescriptorProto& foo = file_.message_type(0); const FieldDescriptorProto& bar = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, - " bar leading\n", - " bar trailing\n", - NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'c', bar, " bar leading\n", + " bar trailing\n", NULL)); // Ignore these. EXPECT_TRUE(HasSpan(file_)); @@ -3309,88 +3453,82 @@ TEST_F(SourceInfoTest, DocComments3) { EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0))); EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0), "name")); EXPECT_TRUE(HasSpan(bar.options().uninterpreted_option(0).name(0))); - EXPECT_TRUE(HasSpan( - bar.options().uninterpreted_option(0).name(0), "name_part")); - EXPECT_TRUE(HasSpan( - bar.options().uninterpreted_option(0), "aggregate_value")); + EXPECT_TRUE( + HasSpan(bar.options().uninterpreted_option(0).name(0), "name_part")); + EXPECT_TRUE( + HasSpan(bar.options().uninterpreted_option(0), "aggregate_value")); } TEST_F(SourceInfoTest, DocCommentsTopLevel) { - EXPECT_TRUE(Parse( - "// detached before syntax paragraph 1\n" - "\n" - "// detached before syntax paragraph 2\n" - "\n" - "// syntax leading\n" - "$a$syntax = \"proto2\";$b$\n" - "// syntax trailing\n" - "\n" - "// syntax-package detached comments\n" - "\n" - ";\n" - "\n" - "// detached after empty before package\n" - "\n" - "// package leading\n" - "$c$package foo;$d$\n" - "// package trailing\n" - "\n" - "// ignored detach\n" - "\n")); - - EXPECT_TRUE(HasSpan('a', 'b', file_, "syntax", -1, - " syntax leading\n", - " syntax trailing\n", - " detached before syntax paragraph 1\n" - "\n" - " detached before syntax paragraph 2\n")); - EXPECT_TRUE(HasSpan('c', 'd', file_, "package", -1, - " package leading\n", - " package trailing\n", - " syntax-package detached comments\n" - "\n" - " detached after empty before package\n")); + EXPECT_TRUE( + Parse("// detached before syntax paragraph 1\n" + "\n" + "// detached before syntax paragraph 2\n" + "\n" + "// syntax leading\n" + "$a$syntax = \"proto2\";$b$\n" + "// syntax trailing\n" + "\n" + "// syntax-package detached comments\n" + "\n" + ";\n" + "\n" + "// detached after empty before package\n" + "\n" + "// package leading\n" + "$c$package foo;$d$\n" + "// package trailing\n" + "\n" + "// ignored detach\n" + "\n")); + + EXPECT_TRUE(HasSpan('a', 'b', file_, "syntax", -1, " syntax leading\n", + " syntax trailing\n", + " detached before syntax paragraph 1\n" + "\n" + " detached before syntax paragraph 2\n")); + EXPECT_TRUE(HasSpan('c', 'd', file_, "package", -1, " package leading\n", + " package trailing\n", + " syntax-package detached comments\n" + "\n" + " detached after empty before package\n")); // ignore these. EXPECT_TRUE(HasSpan(file_)); } TEST_F(SourceInfoTest, DocCommentsOneof) { - EXPECT_TRUE(Parse( - "// Foo leading\n" - "$a$message Foo {\n" - " /* Foo trailing\n" - " */\n" - " // detached before oneof\n" - " /* bar leading\n" - " * line 2 */\n" - " $b$oneof bar {\n" - " /* bar trailing\n" - " * line 2 */\n" - " // detached before bar_int\n" - " /* bar_int leading\n" - " */\n" - " $c$int32 bar_int = 1;$d$ // bar_int trailing\n" - " // detach comment ignored\n" - " }$e$\n" - "}$f$\n")); + EXPECT_TRUE( + Parse("// Foo leading\n" + "$a$message Foo {\n" + " /* Foo trailing\n" + " */\n" + " // detached before oneof\n" + " /* bar leading\n" + " * line 2 */\n" + " $b$oneof bar {\n" + " /* bar trailing\n" + " * line 2 */\n" + " // detached before bar_int\n" + " /* bar_int leading\n" + " */\n" + " $c$int32 bar_int = 1;$d$ // bar_int trailing\n" + " // detach comment ignored\n" + " }$e$\n" + "}$f$\n")); const DescriptorProto& foo = file_.message_type(0); const OneofDescriptorProto& bar = foo.oneof_decl(0); const FieldDescriptorProto& bar_int = foo.field(0); - EXPECT_TRUE(HasSpanWithComment('a', 'f', foo, - " Foo leading\n", - " Foo trailing\n", - NULL)); - EXPECT_TRUE(HasSpanWithComment('b', 'e', bar, - " bar leading\n line 2 ", - " bar trailing\n line 2 ", - " detached before oneof\n")); - EXPECT_TRUE(HasSpanWithComment('c', 'd', bar_int, - " bar_int leading\n", - " bar_int trailing\n", - " detached before bar_int\n")); + EXPECT_TRUE(HasSpanWithComment('a', 'f', foo, " Foo leading\n", + " Foo trailing\n", NULL)); + EXPECT_TRUE(HasSpanWithComment('b', 'e', bar, " bar leading\n line 2 ", + " bar trailing\n line 2 ", + " detached before oneof\n")); + EXPECT_TRUE(HasSpanWithComment('c', 'd', bar_int, " bar_int leading\n", + " bar_int trailing\n", + " detached before bar_int\n")); // Ignore these. EXPECT_TRUE(HasSpan(file_)); diff --git a/src/google/protobuf/compiler/plugin.cc b/src/google/protobuf/compiler/plugin.cc index 3504875893..ac383f88ed 100644 --- a/src/google/protobuf/compiler/plugin.cc +++ b/src/google/protobuf/compiler/plugin.cc @@ -45,9 +45,9 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/compiler/plugin.pb.h> #include <google/protobuf/compiler/code_generator.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/descriptor.h> -#include <google/protobuf/stubs/io_win32.h> namespace google { @@ -57,14 +57,13 @@ namespace compiler { #if defined(_WIN32) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::setmode; +using google::protobuf::io::win32::setmode; #endif class GeneratorResponseContext : public GeneratorContext { public: GeneratorResponseContext( - const Version& compiler_version, - CodeGeneratorResponse* response, + const Version& compiler_version, CodeGeneratorResponse* response, const std::vector<const FileDescriptor*>& parsed_files) : compiler_version_(compiler_version), response_(response), @@ -117,24 +116,26 @@ bool GenerateCode(const CodeGeneratorRequest& request, for (int i = 0; i < request.file_to_generate_size(); i++) { parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); if (parsed_files.back() == NULL) { - *error_msg = "protoc asked plugin to generate a file but " - "did not provide a descriptor for the file: " + - request.file_to_generate(i); + *error_msg = + "protoc asked plugin to generate a file but " + "did not provide a descriptor for the file: " + + request.file_to_generate(i); return false; } } - GeneratorResponseContext context( - request.compiler_version(), response, parsed_files); + GeneratorResponseContext context(request.compiler_version(), response, + parsed_files); std::string error; - bool succeeded = generator.GenerateAll( - parsed_files, request.parameter(), &context, &error); + bool succeeded = generator.GenerateAll(parsed_files, request.parameter(), + &context, &error); if (!succeeded && error.empty()) { - error = "Code generator returned false but provided no error " - "description."; + error = + "Code generator returned false but provided no error " + "description."; } if (!error.empty()) { response->set_error(error); diff --git a/src/google/protobuf/compiler/plugin.h b/src/google/protobuf/compiler/plugin.h index 0eed2df37f..69c5d3f537 100644 --- a/src/google/protobuf/compiler/plugin.h +++ b/src/google/protobuf/compiler/plugin.h @@ -70,7 +70,7 @@ namespace google { namespace protobuf { namespace compiler { -class CodeGenerator; // code_generator.h +class CodeGenerator; // code_generator.h class CodeGeneratorRequest; class CodeGeneratorResponse; diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 47594739fa..16ea252c31 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -98,13 +98,6 @@ PROTOC_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_CodeGenerat {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsCodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto}, { &scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base,}}; -void InitDefaults_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fcompiler_2fplugin_2eproto[4]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto = nullptr; @@ -196,8 +189,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2epro "rotobuf/protoc-gen-go/plugin;plugin_go" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto = { - false, InitDefaults_google_2fprotobuf_2fcompiler_2fplugin_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fcompiler_2fplugin_2eproto, "google/protobuf/compiler/plugin.proto", &assign_descriptors_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, 638, }; @@ -206,6 +198,10 @@ void AddDescriptors_google_2fprotobuf_2fcompiler_2fplugin_2eproto() { { ::AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto, }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Version_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorRequest_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_File_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_CodeGeneratorResponse_google_2fprotobuf_2fcompiler_2fplugin_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, deps, 1); } @@ -220,17 +216,18 @@ void Version::InitAsDefaultInstance() { } class Version::HasBitSetters { public: - static void set_has_major(Version* msg) { - msg->_has_bits_[0] |= 0x00000002u; + using HasBits = decltype(std::declval<Version>()._has_bits_); + static void set_has_major(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_minor(Version* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_minor(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_patch(Version* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_patch(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_suffix(Version* msg) { - msg->_has_bits_[0] |= 0x00000001u; + static void set_has_suffix(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; @@ -309,52 +306,63 @@ void Version::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Version::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional int32 major = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_major(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_major(&has_bits); + major_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 minor = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_minor(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_minor(&has_bits); + minor_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 patch = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_patch(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_patch(&has_bits); + patch_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional string suffix = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_suffix(), ptr, ctx, "google.protobuf.compiler.Version.suffix"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Version::MergePartialFromCodedStream( @@ -370,7 +378,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 major = 1; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_major(this); + HasBitSetters::set_has_major(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &major_))); @@ -383,7 +391,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 minor = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_minor(this); + HasBitSetters::set_has_minor(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &minor_))); @@ -396,7 +404,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 patch = 3; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_patch(this); + HasBitSetters::set_has_patch(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &patch_))); @@ -659,12 +667,13 @@ void CodeGeneratorRequest::InitAsDefaultInstance() { } class CodeGeneratorRequest::HasBitSetters { public: - static void set_has_parameter(CodeGeneratorRequest* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<CodeGeneratorRequest>()._has_bits_); + static void set_has_parameter(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } static const PROTOBUF_NAMESPACE_ID::compiler::Version& compiler_version(const CodeGeneratorRequest* msg); - static void set_has_compiler_version(CodeGeneratorRequest* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_compiler_version(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -756,58 +765,70 @@ void CodeGeneratorRequest::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* CodeGeneratorRequest::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated string file_to_generate = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_file_to_generate(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } // optional string parameter = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_parameter(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorRequest.parameter"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.compiler.Version compiler_version = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ctx->ParseMessage(mutable_compiler_version(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.FileDescriptorProto proto_file = 15; case 15: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 122) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_proto_file(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 122 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 122) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorRequest::MergePartialFromCodedStream( @@ -1130,14 +1151,15 @@ void CodeGeneratorResponse_File::InitAsDefaultInstance() { } class CodeGeneratorResponse_File::HasBitSetters { public: - static void set_has_name(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<CodeGeneratorResponse_File>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_insertion_point(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_insertion_point(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_content(CodeGeneratorResponse_File* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_content(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; @@ -1224,45 +1246,53 @@ void CodeGeneratorResponse_File::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* CodeGeneratorResponse_File::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string insertion_point = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_insertion_point(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string content = 15; case 15: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 122) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_content(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.File.content"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorResponse_File::MergePartialFromCodedStream( @@ -1560,8 +1590,9 @@ void CodeGeneratorResponse::InitAsDefaultInstance() { } class CodeGeneratorResponse::HasBitSetters { public: - static void set_has_error(CodeGeneratorResponse* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<CodeGeneratorResponse>()._has_bits_); + static void set_has_error(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; @@ -1629,41 +1660,51 @@ void CodeGeneratorResponse::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* CodeGeneratorResponse::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string error = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_error(), ptr, ctx, "google.protobuf.compiler.CodeGeneratorResponse.error"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; case 15: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 122) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_file(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 122 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 122) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool CodeGeneratorResponse::MergePartialFromCodedStream( diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc index f652ef6584..cf5b7f6579 100644 --- a/src/google/protobuf/compiler/python/python_generator.cc +++ b/src/google/protobuf/compiler/python/python_generator.cc @@ -114,7 +114,7 @@ const char* const kKeywords[] = { "class", "continue", "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", - "return", "try", "while", "with", "yield", + "return", "try", "while", "with", "yield", "print", }; const char* const* kKeywordsEnd = kKeywords + (sizeof(kKeywords) / sizeof(kKeywords[0])); @@ -129,6 +129,17 @@ bool ContainsPythonKeyword(const std::string& module_name) { return false; } +inline bool IsPythonKeyword(const string& name) { + return (std::find(kKeywords, kKeywordsEnd, name) != kKeywordsEnd); +} + +std::string ResolveKeyword(const std::string& name) { + if (IsPythonKeyword(name)) { + return "globals()['" + name + "']"; + } + return name; +} + // Returns the name of all containing types for descriptor, // in order from outermost to innermost, followed by descriptor's // own name. Each name is separated by |separator|. @@ -136,9 +147,17 @@ template <typename DescriptorT> std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, const std::string& separator) { std::string name = descriptor.name(); - for (const Descriptor* current = descriptor.containing_type(); - current != NULL; current = current->containing_type()) { - name = current->name() + separator + name; + const Descriptor* parent = descriptor.containing_type(); + if (parent != nullptr) { + std::string prefix = NamePrefixedWithNestedTypes(*parent, separator); + if (separator == "." && IsPythonKeyword(name)) { + return "getattr(" + prefix + ", '" + name + "')"; + } else { + return prefix + separator + name; + } + } + if (separator == ".") { + name = ResolveKeyword(name); } return name; } @@ -149,30 +168,27 @@ std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, // in proto2/public/reflection.py. const char kDescriptorKey[] = "DESCRIPTOR"; - // Does the file have top-level enums? -inline bool HasTopLevelEnums(const FileDescriptor *file) { +inline bool HasTopLevelEnums(const FileDescriptor* file) { return file->enum_type_count() > 0; } - // Should we generate generic services for this file? -inline bool HasGenericServices(const FileDescriptor *file) { - return file->service_count() > 0 && - file->options().py_generic_services(); +inline bool HasGenericServices(const FileDescriptor* file) { + return file->service_count() > 0 && file->options().py_generic_services(); } - // Prints the common boilerplate needed at the top of every .py // file output by this generator. -void PrintTopBoilerplate( - io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) { +void PrintTopBoilerplate(io::Printer* printer, const FileDescriptor* file, + bool descriptor_proto) { // TODO(robinson): Allow parameterization of Python version? printer->Print( "# -*- coding: utf-8 -*-\n" "# Generated by the protocol buffer compiler. DO NOT EDIT!\n" "# source: $filename$\n" - "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))" //##PY25 + "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda " + "x:x.encode('latin1'))" //##PY25 "\n", "filename", file->name()); if (HasTopLevelEnums(file)) { @@ -197,7 +213,6 @@ void PrintTopBoilerplate( printer->Print("\n\n"); } - // Returns a Python literal giving the default value for a field. // If the field specifies no explicit default value, we'll return // the default default value for the field type (zero for numbers, @@ -259,12 +274,14 @@ std::string StringifyDefaultValue(const FieldDescriptor& field) { case FieldDescriptor::CPPTYPE_ENUM: return StrCat(field.default_value_enum()->number()); case FieldDescriptor::CPPTYPE_STRING: -//##!PY25 return "b\"" + CEscape(field.default_value_string()) + -//##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" : -//##!PY25 "\".decode('utf-8')"); + //##!PY25 return "b\"" + CEscape(field.default_value_string()) + //+ + //##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" + //: + //##!PY25 "\".decode('utf-8')"); return "_b(\"" + CEscape(field.default_value_string()) + //##PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\")" : //##PY25 - "\").decode('utf-8')"); //##PY25 + "\").decode('utf-8')"); //##PY25 case FieldDescriptor::CPPTYPE_MESSAGE: return "None"; } @@ -290,12 +307,9 @@ std::string StringifySyntax(FileDescriptor::Syntax syntax) { } // namespace +Generator::Generator() : file_(NULL) {} -Generator::Generator() : file_(NULL) { -} - -Generator::~Generator() { -} +Generator::~Generator() {} bool Generator::Generate(const FileDescriptor* file, const std::string& parameter, @@ -349,8 +363,7 @@ bool Generator::Generate(const FileDescriptor* file, PrintServices(); } - printer.Print( - "# @@protoc_insertion_point(module_scope)\n"); + printer.Print("# @@protoc_insertion_point(module_scope)\n"); return !printer.failed(); } @@ -363,9 +376,7 @@ bool Generator::Generate(const FileDescriptor* file, void StripThirdPartyPy(std::string* module_name) { const std::string third_party_py_prefix = "google3.third_party.py."; int len = third_party_py_prefix.length(); - if (module_name->compare(0, len, - third_party_py_prefix, 0, - len) == 0) { + if (module_name->compare(0, len, third_party_py_prefix, 0, len) == 0) { *module_name = module_name->erase(0, len); } } @@ -474,11 +485,11 @@ void Generator::PrintTopLevelEnums() const { for (int i = 0; i < file_->enum_type_count(); ++i) { const EnumDescriptor& enum_descriptor = *file_->enum_type(i); PrintEnum(enum_descriptor); - printer_->Print("$name$ = " - "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", - "name", enum_descriptor.name(), - "descriptor_name", - ModuleLevelDescriptorName(enum_descriptor)); + printer_->Print( + "$name$ = " + "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", + "name", ResolveKeyword(enum_descriptor.name()), "descriptor_name", + ModuleLevelDescriptorName(enum_descriptor)); printer_->Print("\n"); for (int j = 0; j < enum_descriptor.value_count(); ++j) { @@ -490,7 +501,7 @@ void Generator::PrintTopLevelEnums() const { for (int i = 0; i < top_level_enum_values.size(); ++i) { printer_->Print("$name$ = $value$\n", "name", - top_level_enum_values[i].first, "value", + ResolveKeyword(top_level_enum_values[i].first), "value", StrCat(top_level_enum_values[i].second)); } printer_->Print("\n"); @@ -533,8 +544,7 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { printer_->Outdent(); printer_->Print("],\n"); printer_->Print("containing_type=None,\n"); - printer_->Print("serialized_options=$options_value$,\n", - "options_value", + printer_->Print("serialized_options=$options_value$,\n", "options_value", OptionsValue(options_string)); EnumDescriptorProto edp; PrintSerializedPbInterval(enum_descriptor, edp); @@ -566,7 +576,8 @@ void Generator::PrintTopLevelExtensions() const { printer_->Print("$constant_name$ = $number$\n", "constant_name", constant_name, "number", StrCat(extension_field.number())); - printer_->Print("$name$ = ", "name", extension_field.name()); + printer_->Print("$name$ = ", "name", + ResolveKeyword(extension_field.name())); PrintFieldDescriptor(extension_field, is_extension); printer_->Print("\n"); } @@ -604,9 +615,8 @@ void Generator::PrintServiceDescriptor( std::string options_string; descriptor.options().SerializeToString(&options_string); - printer_->Print( - "$service_name$ = _descriptor.ServiceDescriptor(\n", - "service_name", service_name); + printer_->Print("$service_name$ = _descriptor.ServiceDescriptor(\n", + "service_name", service_name); printer_->Indent(); std::map<std::string, std::string> m; m["name"] = descriptor.name(); @@ -640,15 +650,14 @@ void Generator::PrintServiceDescriptor( m["options_value"] = OptionsValue(options_string); printer_->Print("_descriptor.MethodDescriptor(\n"); printer_->Indent(); - printer_->Print( - m, - "name='$name$',\n" - "full_name='$full_name$',\n" - "index=$index$,\n" - "containing_service=None,\n" - "input_type=$input_type$,\n" - "output_type=$output_type$,\n" - "serialized_options=$options_value$,\n"); + printer_->Print(m, + "name='$name$',\n" + "full_name='$full_name$',\n" + "index=$index$,\n" + "containing_service=None,\n" + "input_type=$input_type$,\n" + "output_type=$output_type$,\n" + "serialized_options=$options_value$,\n"); printer_->Outdent(); printer_->Print("),\n"); } @@ -660,23 +669,21 @@ void Generator::PrintServiceDescriptor( printer_->Print("\n"); } - void Generator::PrintDescriptorKeyAndModuleName( const ServiceDescriptor& descriptor) const { - printer_->Print( - "$descriptor_key$ = $descriptor_name$,\n", - "descriptor_key", kDescriptorKey, - "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); - printer_->Print( - "__module__ = '$module_name$'\n", - "module_name", ModuleName(file_->name())); + printer_->Print("$descriptor_key$ = $descriptor_name$,\n", "descriptor_key", + kDescriptorKey, "descriptor_name", + ModuleLevelServiceDescriptorName(descriptor)); + printer_->Print("__module__ = '$module_name$'\n", "module_name", + ModuleName(file_->name())); } void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { // Print the service. - printer_->Print("$class_name$ = service_reflection.GeneratedServiceType(" - "'$class_name$', (_service.Service,), dict(\n", - "class_name", descriptor.name()); + printer_->Print( + "$class_name$ = service_reflection.GeneratedServiceType(" + "'$class_name$', (_service.Service,), dict(\n", + "class_name", descriptor.name()); printer_->Indent(); Generator::PrintDescriptorKeyAndModuleName(descriptor); printer_->Print("))\n\n"); @@ -685,10 +692,11 @@ void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const { // Print the service stub. - printer_->Print("$class_name$_Stub = " - "service_reflection.GeneratedServiceStubType(" - "'$class_name$_Stub', ($class_name$,), dict(\n", - "class_name", descriptor.name()); + printer_->Print( + "$class_name$_Stub = " + "service_reflection.GeneratedServiceStubType(" + "'$class_name$_Stub', ($class_name$,), dict(\n", + "class_name", descriptor.name()); printer_->Indent(); Generator::PrintDescriptorKeyAndModuleName(descriptor); printer_->Print("))\n\n"); @@ -747,9 +755,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { "serialized_options=$options_value$,\n" "is_extendable=$extendable$,\n" "syntax='$syntax$'", - "options_value", OptionsValue(options_string), - "extendable", message_descriptor.extension_range_count() > 0 ? - "True" : "False", + "options_value", OptionsValue(options_string), "extendable", + message_descriptor.extension_range_count() > 0 ? "True" : "False", "syntax", StringifySyntax(message_descriptor.file()->syntax())); printer_->Print(",\n"); @@ -777,12 +784,11 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { } else { m["serialized_options"] = ", serialized_options=" + options_string; } - printer_->Print( - m, - "_descriptor.OneofDescriptor(\n" - " name='$name$', full_name='$full_name$',\n" - " index=$index$, containing_type=None, " - "fields=[]$serialized_options$),\n"); + printer_->Print(m, + "_descriptor.OneofDescriptor(\n" + " name='$name$', full_name='$full_name$',\n" + " index=$index$, containing_type=None, " + "fields=[]$serialized_options$),\n"); } printer_->Outdent(); printer_->Print("],\n"); @@ -812,7 +818,7 @@ void Generator::PrintMessages() const { PrintMessage(*file_->message_type(i), "", &to_register, false); for (int j = 0; j < to_register.size(); ++j) { printer_->Print("_sym_db.RegisterMessage($name$)\n", "name", - to_register[j]); + ResolveKeyword(to_register[j])); } printer_->Print("\n"); } @@ -830,28 +836,36 @@ void Generator::PrintMessage(const Descriptor& message_descriptor, const std::string& prefix, std::vector<std::string>* to_register, bool is_nested) const { - std::string qualified_name(prefix + message_descriptor.name()); - to_register->push_back(qualified_name); + std::string qualified_name; if (is_nested) { + if (IsPythonKeyword(message_descriptor.name())) { + qualified_name = + "getattr(" + prefix + ", '" + message_descriptor.name() + "')"; + } else { + qualified_name = prefix + "." + message_descriptor.name(); + } printer_->Print( "'$name$' : _reflection.GeneratedProtocolMessageType('$name$', " "(_message.Message,), {\n", "name", message_descriptor.name()); } else { + qualified_name = ResolveKeyword(message_descriptor.name()); printer_->Print( - "$name$ = _reflection.GeneratedProtocolMessageType('$name$', " + "$qualified_name$ = _reflection.GeneratedProtocolMessageType('$name$', " "(_message.Message,), {\n", - "name", message_descriptor.name()); + "qualified_name", qualified_name, "name", message_descriptor.name()); } printer_->Indent(); - PrintNestedMessages(message_descriptor, qualified_name + ".", to_register); + to_register->push_back(qualified_name); + + PrintNestedMessages(message_descriptor, qualified_name, to_register); std::map<std::string, std::string> m; m["descriptor_key"] = kDescriptorKey; m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); printer_->Print(m, "'$descriptor_key$' : $descriptor_name$,\n"); - printer_->Print("'__module__' : '$module_name$'\n", - "module_name", ModuleName(file_->name())); + printer_->Print("'__module__' : '$module_name$'\n", "module_name", + ModuleName(file_->name())); printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n", "full_name", message_descriptor.full_name()); printer_->Print("})\n"); @@ -954,9 +968,10 @@ void Generator::AddExtensionToFileDescriptor( std::map<std::string, std::string> m; m["descriptor_name"] = kDescriptorKey; m["field_name"] = descriptor.name(); + m["resolved_name"] = ResolveKeyword(descriptor.name()); const char file_descriptor_template[] = "$descriptor_name$.extensions_by_name['$field_name$'] = " - "$field_name$\n"; + "$resolved_name$\n"; printer_->Print(m, file_descriptor_template); } @@ -1002,15 +1017,14 @@ std::string Generator::FieldReferencingExpression( const std::string& python_dict_name) const { // We should only ever be looking up fields in the current file. // The only things we refer to from other files are message descriptors. - GOOGLE_CHECK_EQ(field.file(), file_) << field.file()->name() << " vs. " - << file_->name(); + GOOGLE_CHECK_EQ(field.file(), file_) + << field.file()->name() << " vs. " << file_->name(); if (!containing_type) { - return field.name(); + return ResolveKeyword(field.name()); } - return strings::Substitute( - "$0.$1['$2']", - ModuleLevelDescriptorName(*containing_type), - python_dict_name, field.name()); + return strings::Substitute("$0.$1['$2']", + ModuleLevelDescriptorName(*containing_type), + python_dict_name, field.name()); } // Prints containing_type for nested descriptors or enum descriptors. @@ -1022,10 +1036,8 @@ void Generator::FixContainingTypeInDescriptor( const std::string nested_name = ModuleLevelDescriptorName(descriptor); const std::string parent_name = ModuleLevelDescriptorName(*containing_descriptor); - printer_->Print( - "$nested_name$.containing_type = $parent_name$\n", - "nested_name", nested_name, - "parent_name", parent_name); + printer_->Print("$nested_name$.containing_type = $parent_name$\n", + "nested_name", nested_name, "parent_name", parent_name); } } @@ -1081,11 +1093,10 @@ void Generator::FixForeignFieldsInExtension( // containing_type() means "extended type." // On the other hand, extension_scope() will give us what we normally // mean by containing_type(). - m["extended_message_class"] = ModuleLevelMessageName( - *extension_field.containing_type()); - m["field"] = FieldReferencingExpression(extension_field.extension_scope(), - extension_field, - "extensions_by_name"); + m["extended_message_class"] = + ModuleLevelMessageName(*extension_field.containing_type()); + m["field"] = FieldReferencingExpression( + extension_field.extension_scope(), extension_field, "extensions_by_name"); printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n"); } @@ -1114,12 +1125,11 @@ void Generator::PrintEnumValueDescriptor( m["index"] = StrCat(descriptor.index()); m["number"] = StrCat(descriptor.number()); m["options"] = OptionsValue(options_string); - printer_->Print( - m, - "_descriptor.EnumValueDescriptor(\n" - " name='$name$', index=$index$, number=$number$,\n" - " serialized_options=$options$,\n" - " type=None)"); + printer_->Print(m, + "_descriptor.EnumValueDescriptor(\n" + " name='$name$', index=$index$, number=$number$,\n" + " serialized_options=$options$,\n" + " type=None)"); } // Returns a CEscaped string of serialized_options. @@ -1128,14 +1138,14 @@ std::string Generator::OptionsValue( if (serialized_options.length() == 0 || GeneratingDescriptorProto()) { return "None"; } else { -//##!PY25 return "b'('" + CEscape(serialized_options)+ "')"; - return "_b('"+ CEscape(serialized_options) + "')"; //##PY25 + //##!PY25 return "b'('" + CEscape(serialized_options)+ "')"; + return "_b('" + CEscape(serialized_options) + "')"; //##PY25 } } // Prints an expression for a Python FieldDescriptor for |field|. -void Generator::PrintFieldDescriptor( - const FieldDescriptor& field, bool is_extension) const { +void Generator::PrintFieldDescriptor(const FieldDescriptor& field, + bool is_extension) const { std::string options_string; field.options().SerializeToString(&options_string); std::map<std::string, std::string> m; @@ -1150,19 +1160,20 @@ void Generator::PrintFieldDescriptor( m["default_value"] = StringifyDefaultValue(field); m["is_extension"] = is_extension ? "True" : "False"; m["serialized_options"] = OptionsValue(options_string); - m["json_name"] = field.has_json_name() ? - ", json_name='" + field.json_name() + "'": ""; + m["json_name"] = + field.has_json_name() ? ", json_name='" + field.json_name() + "'" : ""; // We always set message_type and enum_type to None at this point, and then // these fields in correctly after all referenced descriptors have been // defined and/or imported (see FixForeignFieldsInDescriptors()). const char field_descriptor_decl[] = - "_descriptor.FieldDescriptor(\n" - " name='$name$', full_name='$full_name$', index=$index$,\n" - " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" - " has_default_value=$has_default_value$, default_value=$default_value$,\n" - " message_type=None, enum_type=None, containing_type=None,\n" - " is_extension=$is_extension$, extension_scope=None,\n" - " serialized_options=$serialized_options$$json_name$, file=DESCRIPTOR)"; + "_descriptor.FieldDescriptor(\n" + " name='$name$', full_name='$full_name$', index=$index$,\n" + " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" + " has_default_value=$has_default_value$, " + "default_value=$default_value$,\n" + " message_type=None, enum_type=None, containing_type=None,\n" + " is_extension=$is_extension$, extension_scope=None,\n" + " serialized_options=$serialized_options$$json_name$, file=DESCRIPTOR)"; printer_->Print(m, field_descriptor_decl); } @@ -1174,8 +1185,7 @@ void Generator::PrintFieldDescriptorsInDescriptor( printer_->Print("$list$=[\n", "list", list_variable_name); printer_->Indent(); for (int i = 0; i < (message_descriptor.*CountFn)(); ++i) { - PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i), - is_extension); + PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i), is_extension); printer_->Print(",\n"); } printer_->Outdent(); @@ -1187,9 +1197,9 @@ void Generator::PrintFieldDescriptorsInDescriptor( void Generator::PrintFieldsInDescriptor( const Descriptor& message_descriptor) const { const bool is_extension = false; - PrintFieldDescriptorsInDescriptor( - message_descriptor, is_extension, "fields", - &Descriptor::field_count, &Descriptor::field); + PrintFieldDescriptorsInDescriptor(message_descriptor, is_extension, "fields", + &Descriptor::field_count, + &Descriptor::field); } // Prints a statement assigning "extensions" to a list of Python @@ -1197,9 +1207,9 @@ void Generator::PrintFieldsInDescriptor( void Generator::PrintExtensionsInDescriptor( const Descriptor& message_descriptor) const { const bool is_extension = true; - PrintFieldDescriptorsInDescriptor( - message_descriptor, is_extension, "extensions", - &Descriptor::extension_count, &Descriptor::extension); + PrintFieldDescriptorsInDescriptor(message_descriptor, is_extension, + "extensions", &Descriptor::extension_count, + &Descriptor::extension); } bool Generator::GeneratingDescriptorProto() const { @@ -1273,8 +1283,8 @@ std::string Generator::ModuleLevelServiceDescriptorName( // serialized_end=43, // template <typename DescriptorT, typename DescriptorProtoT> -void Generator::PrintSerializedPbInterval( - const DescriptorT& descriptor, DescriptorProtoT& proto) const { +void Generator::PrintSerializedPbInterval(const DescriptorT& descriptor, + DescriptorProtoT& proto) const { descriptor.CopyTo(&proto); std::string sp; proto.SerializeToString(&sp); @@ -1294,9 +1304,7 @@ void PrintDescriptorOptionsFixingCode(const std::string& descriptor, io::Printer* printer) { // Reset the _options to None thus DescriptorBase.GetOptions() can // parse _options again after extensions are registered. - printer->Print( - "$descriptor$._options = None\n", - "descriptor", descriptor); + printer->Print("$descriptor$._options = None\n", "descriptor", descriptor); } } // namespace @@ -1358,8 +1366,7 @@ void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const { // Prints expressions that set the options for field descriptors (including // extensions). -void Generator::FixOptionsForField( - const FieldDescriptor& field) const { +void Generator::FixOptionsForField(const FieldDescriptor& field) const { std::string field_options = OptionsValue(field.options().SerializeAsString()); if (field_options != "None") { std::string field_name; @@ -1368,12 +1375,12 @@ void Generator::FixOptionsForField( // Top level extensions. field_name = field.name(); } else { - field_name = FieldReferencingExpression( - field.extension_scope(), field, "extensions_by_name"); + field_name = FieldReferencingExpression(field.extension_scope(), field, + "extensions_by_name"); } } else { - field_name = FieldReferencingExpression( - field.containing_type(), field, "fields_by_name"); + field_name = FieldReferencingExpression(field.containing_type(), field, + "fields_by_name"); } PrintDescriptorOptionsFixingCode(field_name, field_options, printer_); } @@ -1409,8 +1416,7 @@ void Generator::FixOptionsForMessage(const Descriptor& descriptor) const { OptionsValue(descriptor.options().SerializeAsString()); if (message_options != "None") { std::string descriptor_name = ModuleLevelDescriptorName(descriptor); - PrintDescriptorOptionsFixingCode(descriptor_name, - message_options, + PrintDescriptorOptionsFixingCode(descriptor_name, message_options, printer_); } } @@ -1425,13 +1431,12 @@ void Generator::CopyPublicDependenciesAliases( // There's no module alias in the dependent file if it was generated by // an old protoc (less than 3.0.0-alpha-1). Use module name in this // situation. - printer_->Print("try:\n" - " $alias$ = $copy_from$.$alias$\n" - "except AttributeError:\n" - " $alias$ = $copy_from$.$module$\n", - "alias", module_alias, - "module", module_name, - "copy_from", copy_from); + printer_->Print( + "try:\n" + " $alias$ = $copy_from$.$alias$\n" + "except AttributeError:\n" + " $alias$ = $copy_from$.$module$\n", + "alias", module_alias, "module", module_name, "copy_from", copy_from); CopyPublicDependenciesAliases(copy_from, file->public_dependency(i)); } } diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h index b4959bfcf6..4630671702 100644 --- a/src/google/protobuf/compiler/python/python_generator.h +++ b/src/google/protobuf/compiler/python/python_generator.h @@ -52,7 +52,9 @@ class FieldDescriptor; class OneofDescriptor; class ServiceDescriptor; -namespace io { class Printer; } +namespace io { +class Printer; +} namespace compiler { namespace python { @@ -82,8 +84,8 @@ class PROTOC_EXPORT Generator : public CodeGenerator { void PrintTopLevelExtensions() const; - void PrintFieldDescriptor( - const FieldDescriptor& field, bool is_extension) const; + void PrintFieldDescriptor(const FieldDescriptor& field, + bool is_extension) const; void PrintFieldDescriptorsInDescriptor( const Descriptor& message_descriptor, bool is_extension, const std::string& list_variable_name, int (Descriptor::*CountFn)() const, @@ -146,8 +148,8 @@ class PROTOC_EXPORT Generator : public CodeGenerator { const ServiceDescriptor& descriptor) const; template <typename DescriptorT, typename DescriptorProtoT> - void PrintSerializedPbInterval( - const DescriptorT& descriptor, DescriptorProtoT& proto) const; + void PrintSerializedPbInterval(const DescriptorT& descriptor, + DescriptorProtoT& proto) const; void FixAllDescriptorOptions() const; void FixOptionsForField(const FieldDescriptor& field) const; diff --git a/src/google/protobuf/compiler/python/python_plugin_unittest.cc b/src/google/protobuf/compiler/python/python_plugin_unittest.cc index aa04e8b714..8d7ef98b27 100644 --- a/src/google/protobuf/compiler/python/python_plugin_unittest.cc +++ b/src/google/protobuf/compiler/python/python_plugin_unittest.cc @@ -36,10 +36,10 @@ #include <memory> -#include <google/protobuf/compiler/python/python_generator.h> #include <google/protobuf/compiler/command_line_interface.h> -#include <google/protobuf/io/zero_copy_stream.h> +#include <google/protobuf/compiler/python/python_generator.h> #include <google/protobuf/io/printer.h> +#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/testing/file.h> #include <google/protobuf/testing/file.h> @@ -102,13 +102,8 @@ TEST(PythonPluginTest, PluginTest) { std::string python_out = "--python_out=" + TestTempDir(); std::string test_out = "--test_out=" + TestTempDir(); - const char* argv[] = { - "protoc", - proto_path.c_str(), - python_out.c_str(), - test_out.c_str(), - "test.proto" - }; + const char* argv[] = {"protoc", proto_path.c_str(), python_out.c_str(), + test_out.c_str(), "test.proto"}; EXPECT_EQ(0, cli.Run(5, argv)); } diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc index 42f3d0617f..b920b2ddd4 100644 --- a/src/google/protobuf/compiler/subprocess.cc +++ b/src/google/protobuf/compiler/subprocess.cc @@ -38,9 +38,9 @@ #ifndef _WIN32 #include <errno.h> +#include <signal.h> #include <sys/select.h> #include <sys/wait.h> -#include <signal.h> #endif #include <google/protobuf/stubs/logging.h> @@ -54,7 +54,7 @@ namespace compiler { namespace { char* portable_strdup(const char* s) { - char* ns = (char*) malloc(strlen(s) + 1); + char* ns = (char*)malloc(strlen(s) + 1); if (ns != NULL) { strcpy(ns, s); } @@ -73,7 +73,9 @@ static void CloseHandleOrDie(HANDLE handle) { Subprocess::Subprocess() : process_start_error_(ERROR_SUCCESS), - child_handle_(NULL), child_stdin_(NULL), child_stdout_(NULL) {} + child_handle_(NULL), + child_stdin_(NULL), + child_stdout_(NULL) {} Subprocess::~Subprocess() { if (child_stdin_ != NULL) { @@ -99,13 +101,13 @@ void Subprocess::Start(const std::string& program, SearchMode search_mode) { } // Make child side of the pipes inheritable. - if (!SetHandleInformation(stdin_pipe_read, - HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + if (!SetHandleInformation(stdin_pipe_read, HANDLE_FLAG_INHERIT, + HANDLE_FLAG_INHERIT)) { GOOGLE_LOG(FATAL) << "SetHandleInformation: " << Win32ErrorMessage(GetLastError()); } - if (!SetHandleInformation(stdout_pipe_write, - HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + if (!SetHandleInformation(stdout_pipe_write, HANDLE_FLAG_INHERIT, + HANDLE_FLAG_INHERIT)) { GOOGLE_LOG(FATAL) << "SetHandleInformation: " << Win32ErrorMessage(GetLastError()); } @@ -120,14 +122,13 @@ void Subprocess::Start(const std::string& program, SearchMode search_mode) { startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); if (startup_info.hStdError == INVALID_HANDLE_VALUE) { - GOOGLE_LOG(FATAL) << "GetStdHandle: " - << Win32ErrorMessage(GetLastError()); + GOOGLE_LOG(FATAL) << "GetStdHandle: " << Win32ErrorMessage(GetLastError()); } // Invoking cmd.exe allows for '.bat' files from the path as well as '.exe'. // Using a malloc'ed string because CreateProcess() can mutate its second // parameter. - char *command_line = + char* command_line = portable_strdup(("cmd.exe /c \"" + program + "\"").c_str()); // Create the process. @@ -141,8 +142,7 @@ void Subprocess::Start(const std::string& program, SearchMode search_mode) { 0, // obscure creation flags NULL, // environment (inherit from parent) NULL, // current directory (inherit from parent) - &startup_info, - &process_info)) { + &startup_info, &process_info)) { child_handle_ = process_info.hProcess; CloseHandleOrDie(process_info.hThread); child_stdin_ = stdin_pipe_write; @@ -200,10 +200,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (signaled_handle == child_stdin_) { DWORD n; - if (!WriteFile(child_stdin_, - input_data.data() + input_pos, - input_data.size() - input_pos, - &n, NULL)) { + if (!WriteFile(child_stdin_, input_data.data() + input_pos, + input_data.size() - input_pos, &n, NULL)) { // Child closed pipe. Presumably it will report an error later. // Pretend we're done for now. input_pos = input_data.size(); @@ -257,8 +255,8 @@ bool Subprocess::Communicate(const Message& input, Message* output, child_handle_ = NULL; if (exit_code != 0) { - *error = strings::Substitute( - "Plugin failed with status code $0.", exit_code); + *error = + strings::Substitute("Plugin failed with status code $0.", exit_code); return false; } @@ -312,7 +310,7 @@ void Subprocess::Start(const std::string& program, SearchMode search_mode) { GOOGLE_CHECK(pipe(stdin_pipe) != -1); GOOGLE_CHECK(pipe(stdout_pipe) != -1); - char* argv[2] = { portable_strdup(program.c_str()), NULL }; + char* argv[2] = {portable_strdup(program.c_str()), NULL}; child_pid_ = fork(); if (child_pid_ == -1) { @@ -342,7 +340,7 @@ void Subprocess::Start(const std::string& program, SearchMode search_mode) { ignored = write(STDERR_FILENO, argv[0], strlen(argv[0])); const char* message = ": program not found or is not executable\n"; ignored = write(STDERR_FILENO, message, strlen(message)); - (void) ignored; + (void)ignored; // Must use _exit() rather than exit() to avoid flushing output buffers // that will also be flushed by the parent. @@ -397,7 +395,7 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (child_stdin_ != -1 && FD_ISSET(child_stdin_, &write_fds)) { int n = write(child_stdin_, input_data.data() + input_pos, - input_data.size() - input_pos); + input_data.size() - input_pos); if (n < 0) { // Child closed pipe. Presumably it will report an error later. // Pretend we're done for now. @@ -447,14 +445,13 @@ bool Subprocess::Communicate(const Message& input, Message* output, if (WIFEXITED(status)) { if (WEXITSTATUS(status) != 0) { int error_code = WEXITSTATUS(status); - *error = strings::Substitute( - "Plugin failed with status code $0.", error_code); + *error = + strings::Substitute("Plugin failed with status code $0.", error_code); return false; } } else if (WIFSIGNALED(status)) { int signal = WTERMSIG(status); - *error = strings::Substitute( - "Plugin killed by signal $0.", signal); + *error = strings::Substitute("Plugin killed by signal $0.", signal); return false; } else { *error = "Neither WEXITSTATUS nor WTERMSIG is true?"; diff --git a/src/google/protobuf/compiler/subprocess.h b/src/google/protobuf/compiler/subprocess.h index 977abff7b5..9c4f8b7690 100644 --- a/src/google/protobuf/compiler/subprocess.h +++ b/src/google/protobuf/compiler/subprocess.h @@ -34,7 +34,7 @@ #define GOOGLE_PROTOBUF_COMPILER_SUBPROCESS_H__ #ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN // right... +#define WIN32_LEAN_AND_MEAN // right... #include <windows.h> #else // _WIN32 #include <sys/types.h> @@ -60,8 +60,8 @@ class PROTOC_EXPORT Subprocess { ~Subprocess(); enum SearchMode { - SEARCH_PATH, // Use PATH environment variable. - EXACT_NAME // Program is an exact file name; don't use the PATH. + SEARCH_PATH, // Use PATH environment variable. + EXACT_NAME // Program is an exact file name; don't use the PATH. }; // Start the subprocess. Currently we don't provide a way to specify diff --git a/src/google/protobuf/compiler/test_plugin.cc b/src/google/protobuf/compiler/test_plugin.cc index 4b758764ea..6b56170003 100644 --- a/src/google/protobuf/compiler/test_plugin.cc +++ b/src/google/protobuf/compiler/test_plugin.cc @@ -33,10 +33,10 @@ // This is a dummy code generator plugin used by // command_line_interface_unittest. -#include <string> #include <stdlib.h> -#include <google/protobuf/compiler/plugin.h> +#include <string> #include <google/protobuf/compiler/mock_code_generator.h> +#include <google/protobuf/compiler/plugin.h> namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/zip_writer.cc b/src/google/protobuf/compiler/zip_writer.cc index f03b3a6df7..872dd9edd6 100644 --- a/src/google/protobuf/compiler/zip_writer.cc +++ b/src/google/protobuf/compiler/zip_writer.cc @@ -75,50 +75,49 @@ namespace compiler { static const uint16 kDosEpoch = 1 << 5 | 1; static const uint32 kCRC32Table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -}; + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; static uint32 ComputeCRC32(const std::string& buf) { uint32 x = ~0U; @@ -129,7 +128,7 @@ static uint32 ComputeCRC32(const std::string& buf) { return ~x; } -static void WriteShort(io::CodedOutputStream *out, uint16 val) { +static void WriteShort(io::CodedOutputStream* out, uint16 val) { uint8 p[2]; p[0] = static_cast<uint8>(val); p[1] = static_cast<uint8>(val >> 8); @@ -137,7 +136,7 @@ static void WriteShort(io::CodedOutputStream *out, uint16 val) { } ZipWriter::ZipWriter(io::ZeroCopyOutputStream* raw_output) - : raw_output_(raw_output) {} + : raw_output_(raw_output) {} ZipWriter::~ZipWriter() {} bool ZipWriter::Write(const std::string& filename, @@ -155,18 +154,18 @@ bool ZipWriter::Write(const std::string& filename, // write file header io::CodedOutputStream output(raw_output_); output.WriteLittleEndian32(0x04034b50); // magic - WriteShort(&output, 10); // version needed to extract - WriteShort(&output, 0); // flags - WriteShort(&output, 0); // compression method: stored - WriteShort(&output, 0); // last modified time - WriteShort(&output, kDosEpoch); // last modified date + WriteShort(&output, 10); // version needed to extract + WriteShort(&output, 0); // flags + WriteShort(&output, 0); // compression method: stored + WriteShort(&output, 0); // last modified time + WriteShort(&output, kDosEpoch); // last modified date output.WriteLittleEndian32(info.crc32); // crc-32 - output.WriteLittleEndian32(info.size); // compressed size - output.WriteLittleEndian32(info.size); // uncompressed size - WriteShort(&output, filename_size); // file name length - WriteShort(&output, 0); // extra field length - output.WriteString(filename); // file name - output.WriteString(contents); // file data + output.WriteLittleEndian32(info.size); // compressed size + output.WriteLittleEndian32(info.size); // uncompressed size + WriteShort(&output, filename_size); // file name length + WriteShort(&output, 0); // extra field length + output.WriteString(filename); // file name + output.WriteString(contents); // file data return !output.HadError(); } @@ -185,35 +184,35 @@ bool ZipWriter::WriteDirectory() { uint32 offset = files_[i].offset; output.WriteLittleEndian32(0x02014b50); // magic - WriteShort(&output, 10); // version made by - WriteShort(&output, 10); // version needed to extract - WriteShort(&output, 0); // flags - WriteShort(&output, 0); // compression method: stored - WriteShort(&output, 0); // last modified time - WriteShort(&output, kDosEpoch); // last modified date - output.WriteLittleEndian32(crc32); // crc-32 - output.WriteLittleEndian32(size); // compressed size - output.WriteLittleEndian32(size); // uncompressed size - WriteShort(&output, filename_size); // file name length - WriteShort(&output, 0); // extra field length - WriteShort(&output, 0); // file comment length - WriteShort(&output, 0); // starting disk number - WriteShort(&output, 0); // internal file attributes - output.WriteLittleEndian32(0); // external file attributes - output.WriteLittleEndian32(offset); // local header offset - output.WriteString(filename); // file name + WriteShort(&output, 10); // version made by + WriteShort(&output, 10); // version needed to extract + WriteShort(&output, 0); // flags + WriteShort(&output, 0); // compression method: stored + WriteShort(&output, 0); // last modified time + WriteShort(&output, kDosEpoch); // last modified date + output.WriteLittleEndian32(crc32); // crc-32 + output.WriteLittleEndian32(size); // compressed size + output.WriteLittleEndian32(size); // uncompressed size + WriteShort(&output, filename_size); // file name length + WriteShort(&output, 0); // extra field length + WriteShort(&output, 0); // file comment length + WriteShort(&output, 0); // starting disk number + WriteShort(&output, 0); // internal file attributes + output.WriteLittleEndian32(0); // external file attributes + output.WriteLittleEndian32(offset); // local header offset + output.WriteString(filename); // file name } uint32 dir_len = output.ByteCount(); // write end of central directory marker output.WriteLittleEndian32(0x06054b50); // magic - WriteShort(&output, 0); // disk number - WriteShort(&output, 0); // disk with start of central directory - WriteShort(&output, num_entries); // central directory entries (this disk) - WriteShort(&output, num_entries); // central directory entries (total) + WriteShort(&output, 0); // disk number + WriteShort(&output, 0); // disk with start of central directory + WriteShort(&output, num_entries); // central directory entries (this disk) + WriteShort(&output, num_entries); // central directory entries (total) output.WriteLittleEndian32(dir_len); // central directory byte size output.WriteLittleEndian32(dir_ofs); // central directory offset - WriteShort(&output, 0); // comment length + WriteShort(&output, 0); // comment length return output.HadError(); } diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index d5c20eeba3..cc1c4bf88d 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -150,73 +150,73 @@ struct Symbol { }; const FieldDescriptor::CppType -FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = { - static_cast<CppType>(0), // 0 is reserved for errors - - CPPTYPE_DOUBLE, // TYPE_DOUBLE - CPPTYPE_FLOAT, // TYPE_FLOAT - CPPTYPE_INT64, // TYPE_INT64 - CPPTYPE_UINT64, // TYPE_UINT64 - CPPTYPE_INT32, // TYPE_INT32 - CPPTYPE_UINT64, // TYPE_FIXED64 - CPPTYPE_UINT32, // TYPE_FIXED32 - CPPTYPE_BOOL, // TYPE_BOOL - CPPTYPE_STRING, // TYPE_STRING - CPPTYPE_MESSAGE, // TYPE_GROUP - CPPTYPE_MESSAGE, // TYPE_MESSAGE - CPPTYPE_STRING, // TYPE_BYTES - CPPTYPE_UINT32, // TYPE_UINT32 - CPPTYPE_ENUM, // TYPE_ENUM - CPPTYPE_INT32, // TYPE_SFIXED32 - CPPTYPE_INT64, // TYPE_SFIXED64 - CPPTYPE_INT32, // TYPE_SINT32 - CPPTYPE_INT64, // TYPE_SINT64 + FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = { + static_cast<CppType>(0), // 0 is reserved for errors + + CPPTYPE_DOUBLE, // TYPE_DOUBLE + CPPTYPE_FLOAT, // TYPE_FLOAT + CPPTYPE_INT64, // TYPE_INT64 + CPPTYPE_UINT64, // TYPE_UINT64 + CPPTYPE_INT32, // TYPE_INT32 + CPPTYPE_UINT64, // TYPE_FIXED64 + CPPTYPE_UINT32, // TYPE_FIXED32 + CPPTYPE_BOOL, // TYPE_BOOL + CPPTYPE_STRING, // TYPE_STRING + CPPTYPE_MESSAGE, // TYPE_GROUP + CPPTYPE_MESSAGE, // TYPE_MESSAGE + CPPTYPE_STRING, // TYPE_BYTES + CPPTYPE_UINT32, // TYPE_UINT32 + CPPTYPE_ENUM, // TYPE_ENUM + CPPTYPE_INT32, // TYPE_SFIXED32 + CPPTYPE_INT64, // TYPE_SFIXED64 + CPPTYPE_INT32, // TYPE_SINT32 + CPPTYPE_INT64, // TYPE_SINT64 }; -const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { - "ERROR", // 0 is reserved for errors - - "double", // TYPE_DOUBLE - "float", // TYPE_FLOAT - "int64", // TYPE_INT64 - "uint64", // TYPE_UINT64 - "int32", // TYPE_INT32 - "fixed64", // TYPE_FIXED64 - "fixed32", // TYPE_FIXED32 - "bool", // TYPE_BOOL - "string", // TYPE_STRING - "group", // TYPE_GROUP - "message", // TYPE_MESSAGE - "bytes", // TYPE_BYTES - "uint32", // TYPE_UINT32 - "enum", // TYPE_ENUM - "sfixed32", // TYPE_SFIXED32 - "sfixed64", // TYPE_SFIXED64 - "sint32", // TYPE_SINT32 - "sint64", // TYPE_SINT64 +const char* const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { + "ERROR", // 0 is reserved for errors + + "double", // TYPE_DOUBLE + "float", // TYPE_FLOAT + "int64", // TYPE_INT64 + "uint64", // TYPE_UINT64 + "int32", // TYPE_INT32 + "fixed64", // TYPE_FIXED64 + "fixed32", // TYPE_FIXED32 + "bool", // TYPE_BOOL + "string", // TYPE_STRING + "group", // TYPE_GROUP + "message", // TYPE_MESSAGE + "bytes", // TYPE_BYTES + "uint32", // TYPE_UINT32 + "enum", // TYPE_ENUM + "sfixed32", // TYPE_SFIXED32 + "sfixed64", // TYPE_SFIXED64 + "sint32", // TYPE_SINT32 + "sint64", // TYPE_SINT64 }; -const char * const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = { - "ERROR", // 0 is reserved for errors - - "int32", // CPPTYPE_INT32 - "int64", // CPPTYPE_INT64 - "uint32", // CPPTYPE_UINT32 - "uint64", // CPPTYPE_UINT64 - "double", // CPPTYPE_DOUBLE - "float", // CPPTYPE_FLOAT - "bool", // CPPTYPE_BOOL - "enum", // CPPTYPE_ENUM - "string", // CPPTYPE_STRING - "message", // CPPTYPE_MESSAGE +const char* const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = { + "ERROR", // 0 is reserved for errors + + "int32", // CPPTYPE_INT32 + "int64", // CPPTYPE_INT64 + "uint32", // CPPTYPE_UINT32 + "uint64", // CPPTYPE_UINT64 + "double", // CPPTYPE_DOUBLE + "float", // CPPTYPE_FLOAT + "bool", // CPPTYPE_BOOL + "enum", // CPPTYPE_ENUM + "string", // CPPTYPE_STRING + "message", // CPPTYPE_MESSAGE }; -const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { - "ERROR", // 0 is reserved for errors +const char* const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { + "ERROR", // 0 is reserved for errors - "optional", // LABEL_OPTIONAL - "required", // LABEL_REQUIRED - "repeated", // LABEL_REPEATED + "optional", // LABEL_OPTIONAL + "required", // LABEL_REQUIRED + "repeated", // LABEL_REPEATED }; const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) { @@ -232,7 +232,7 @@ const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) { return NULL; } -static const char * const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty"; +static const char* const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty"; #if !defined(_MSC_VER) || _MSC_VER >= 1900 const int FieldDescriptor::kMaxNumber; @@ -406,7 +406,7 @@ typedef std::pair<const EnumDescriptor*, int> EnumIntPair; #define HASH_SET std::unordered_set #define HASH_FXN hash -template<typename PairType> +template <typename PairType> struct PointerIntegerPairHash { size_t operator()(const PairType& p) const { static const size_t prime1 = 16777499; @@ -421,8 +421,7 @@ struct PointerIntegerPairHash { static const size_t min_buckets = 8; #endif inline bool operator()(const PairType& a, const PairType& b) const { - return a.first < b.first || - (a.first == b.first && a.second < b.second); + return a.first < b.first || (a.first == b.first && a.second < b.second); } }; @@ -467,26 +466,26 @@ typedef HASH_MAP<PointerStringPair, const FieldDescriptor*, typedef HASH_MAP<DescriptorIntPair, const FieldDescriptor*, PointerIntegerPairHash<DescriptorIntPair>, - std::equal_to<DescriptorIntPair> > + std::equal_to<DescriptorIntPair>> FieldsByNumberMap; typedef HASH_MAP<EnumIntPair, const EnumValueDescriptor*, PointerIntegerPairHash<EnumIntPair>, - std::equal_to<EnumIntPair> > + std::equal_to<EnumIntPair>> EnumValuesByNumberMap; // This is a map rather than a hash-map, since we use it to iterate // through all the extensions that extend a given Descriptor, and an // ordered data structure that implements lower_bound is convenient // for that. typedef std::map<DescriptorIntPair, const FieldDescriptor*> - ExtensionsGroupedByDescriptorMap; + ExtensionsGroupedByDescriptorMap; typedef HASH_MAP<std::string, const SourceCodeInfo_Location*> LocationsByPathMap; std::set<std::string>* NewAllowedProto3Extendee() { auto allowed_proto3_extendees = new std::set<std::string>; const char* kOptionNames[] = { - "FileOptions", "MessageOptions", "FieldOptions", "EnumOptions", + "FileOptions", "MessageOptions", "FieldOptions", "EnumOptions", "EnumValueOptions", "ServiceOptions", "MethodOptions", "OneofOptions"}; for (int i = 0; i < GOOGLE_ARRAYSIZE(kOptionNames); ++i) { // descriptor.proto has a different package name in opensource. We allow @@ -620,11 +619,13 @@ class DescriptorPool::Tables { // destroyed. Note that the object's destructor will never be called, // so its fields must be plain old data (primitive data types and // pointers). All of the descriptor types are such objects. - template<typename Type> Type* Allocate(); + template <typename Type> + Type* Allocate(); // Allocate an array of objects which will be reclaimed when the // pool in destroyed. Again, destructors are never called. - template<typename Type> Type* AllocateArray(int count); + template <typename Type> + Type* AllocateArray(int count); // Allocate a string which will be destroyed when the pool is destroyed. // The string is initialized to the given value for convenience. @@ -638,22 +639,23 @@ class DescriptorPool::Tables { // trouble understanding explicit template instantiations in some cases, so // in those cases we have to pass a dummy pointer of the right type as the // parameter instead of specifying the type explicitly. - template<typename Type> Type* AllocateMessage(Type* dummy = NULL); + template <typename Type> + Type* AllocateMessage(Type* dummy = NULL); // Allocate a FileDescriptorTables object. FileDescriptorTables* AllocateFileTables(); private: std::vector<std::string*> strings_; // All strings in the pool. - std::vector<Message*> messages_; // All messages in the pool. + std::vector<Message*> messages_; // All messages in the pool. std::vector<internal::once_flag*> once_dynamics_; // All internal::call_onces in the pool. std::vector<FileDescriptorTables*> file_tables_; // All file tables in the pool. std::vector<void*> allocations_; // All other memory allocated in the pool. - SymbolsByNameMap symbols_by_name_; - FilesByNameMap files_by_name_; + SymbolsByNameMap symbols_by_name_; + FilesByNameMap files_by_name_; ExtensionsGroupedByDescriptorMap extensions_; struct CheckPoint { @@ -679,8 +681,8 @@ class DescriptorPool::Tables { int pending_extensions_before_checkpoint; }; std::vector<CheckPoint> checkpoints_; - std::vector<const char* > symbols_after_checkpoint_; - std::vector<const char* > files_after_checkpoint_; + std::vector<const char*> symbols_after_checkpoint_; + std::vector<const char*> files_after_checkpoint_; std::vector<DescriptorIntPair> extensions_after_checkpoint_; // Allocate some bytes which will be reclaimed when the pool is @@ -717,14 +719,14 @@ class FileDescriptorTables { const Symbol::Type type) const; // These return NULL if not found. - inline const FieldDescriptor* FindFieldByNumber( - const Descriptor* parent, int number) const; + inline const FieldDescriptor* FindFieldByNumber(const Descriptor* parent, + int number) const; inline const FieldDescriptor* FindFieldByLowercaseName( const void* parent, const std::string& lowercase_name) const; inline const FieldDescriptor* FindFieldByCamelcaseName( const void* parent, const std::string& camelcase_name) const; inline const EnumValueDescriptor* FindEnumValueByNumber( - const EnumDescriptor* parent, int number) const; + const EnumDescriptor* parent, int number) const; // This creates a new EnumValueDescriptor if not found, in a thread-safe way. inline const EnumValueDescriptor* FindEnumValueByNumberCreatingIfUnknown( const EnumDescriptor* parent, int number) const; @@ -853,18 +855,15 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { const CheckPoint& checkpoint = checkpoints_.back(); for (int i = checkpoint.pending_symbols_before_checkpoint; - i < symbols_after_checkpoint_.size(); - i++) { + i < symbols_after_checkpoint_.size(); i++) { symbols_by_name_.erase(symbols_after_checkpoint_[i]); } for (int i = checkpoint.pending_files_before_checkpoint; - i < files_after_checkpoint_.size(); - i++) { + i < files_after_checkpoint_.size(); i++) { files_by_name_.erase(files_after_checkpoint_[i]); } for (int i = checkpoint.pending_extensions_before_checkpoint; - i < extensions_after_checkpoint_.size(); - i++) { + i < extensions_after_checkpoint_.size(); i++) { extensions_.erase(extensions_after_checkpoint_[i]); } @@ -886,8 +885,7 @@ void DescriptorPool::Tables::RollbackToLastCheckpoint() { file_tables_.begin() + checkpoint.file_tables_before_checkpoint, file_tables_.end()); for (int i = checkpoint.allocations_before_checkpoint; - i < allocations_.size(); - i++) { + i < allocations_.size(); i++) { operator delete(allocations_[i]); } @@ -940,8 +938,7 @@ Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, if (result.IsNull() && pool->underlay_ != NULL) { // Symbol not found; check the underlay. - result = - pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); + result = pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); } if (result.IsNull()) { @@ -1067,13 +1064,12 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( // later. std::string enum_value_name = StringPrintf("UNKNOWN_ENUM_VALUE_%s_%d", parent->name().c_str(), number); - DescriptorPool::Tables* tables = - const_cast<DescriptorPool::Tables*>(DescriptorPool::generated_pool()-> - tables_.get()); + DescriptorPool::Tables* tables = const_cast<DescriptorPool::Tables*>( + DescriptorPool::generated_pool()->tables_.get()); EnumValueDescriptor* result = tables->Allocate<EnumValueDescriptor>(); result->name_ = tables->AllocateString(enum_value_name); - result->full_name_ = tables->AllocateString(parent->full_name() + - "." + enum_value_name); + result->full_name_ = + tables->AllocateString(parent->full_name() + "." + enum_value_name); result->number_ = number; result->type_ = parent; result->options_ = &EnumValueOptions::default_instance(); @@ -1083,7 +1079,6 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( } } - inline const FieldDescriptor* DescriptorPool::Tables::FindExtension( const Descriptor* extendee, int number) const { return FindPtrOrNull(extensions_, std::make_pair(extendee, number)); @@ -1184,12 +1179,12 @@ bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) { // ------------------------------------------------------------------- -template<typename Type> +template <typename Type> Type* DescriptorPool::Tables::Allocate() { return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type))); } -template<typename Type> +template <typename Type> Type* DescriptorPool::Tables::AllocateArray(int count) { return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count)); } @@ -1206,7 +1201,7 @@ internal::once_flag* DescriptorPool::Tables::AllocateOnceDynamic() { return result; } -template<typename Type> +template <typename Type> Type* DescriptorPool::Tables::AllocateMessage(Type* /* dummy */) { Type* result = new Type; messages_.push_back(result); @@ -1254,42 +1249,41 @@ const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation( DescriptorPool::ErrorCollector::~ErrorCollector() {} DescriptorPool::DescriptorPool() - : mutex_(NULL), - fallback_database_(NULL), - default_error_collector_(NULL), - underlay_(NULL), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) {} + : mutex_(NULL), + fallback_database_(NULL), + default_error_collector_(NULL), + underlay_(NULL), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, ErrorCollector* error_collector) - : mutex_(new internal::WrappedMutex), - fallback_database_(fallback_database), - default_error_collector_(error_collector), - underlay_(NULL), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) { -} + : mutex_(new internal::WrappedMutex), + fallback_database_(fallback_database), + default_error_collector_(error_collector), + underlay_(NULL), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::DescriptorPool(const DescriptorPool* underlay) - : mutex_(NULL), - fallback_database_(NULL), - default_error_collector_(NULL), - underlay_(underlay), - tables_(new Tables), - enforce_dependencies_(true), - lazily_build_dependencies_(false), - allow_unknown_(false), - enforce_weak_(false), - disallow_enforce_utf8_(false) {} + : mutex_(NULL), + fallback_database_(NULL), + default_error_collector_(NULL), + underlay_(underlay), + tables_(new Tables), + enforce_dependencies_(true), + lazily_build_dependencies_(false), + allow_unknown_(false), + enforce_weak_(false), + disallow_enforce_utf8_(false) {} DescriptorPool::~DescriptorPool() { if (mutex_ != NULL) delete mutex_; @@ -1411,7 +1405,7 @@ const FileDescriptor* DescriptorPool::FindFileContainingSymbol( if (!result.IsNull()) return result.GetFile(); if (underlay_ != NULL) { const FileDescriptor* file_result = - underlay_->FindFileContainingSymbol(symbol_name); + underlay_->FindFileContainingSymbol(symbol_name); if (file_result != NULL) return file_result; } if (TryFindSymbolInFallbackDatabase(symbol_name)) { @@ -1441,8 +1435,7 @@ const FieldDescriptor* DescriptorPool::FindFieldByName( const FieldDescriptor* DescriptorPool::FindExtensionByName( const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - if (result.type == Symbol::FIELD && - result.field_descriptor->is_extension()) { + if (result.type == Symbol::FIELD && result.field_descriptor->is_extension()) { return result.field_descriptor; } else { return NULL; @@ -1464,8 +1457,8 @@ const EnumDescriptor* DescriptorPool::FindEnumTypeByName( const EnumValueDescriptor* DescriptorPool::FindEnumValueByName( const std::string& name) const { Symbol result = tables_->FindByNameHelper(this, name); - return (result.type == Symbol::ENUM_VALUE) ? - result.enum_value_descriptor : NULL; + return (result.type == Symbol::ENUM_VALUE) ? result.enum_value_descriptor + : NULL; } const ServiceDescriptor* DescriptorPool::FindServiceByName( @@ -1548,10 +1541,8 @@ void DescriptorPool::FindAllExtensions( // ------------------------------------------------------------------- -const FieldDescriptor* -Descriptor::FindFieldByNumber(int key) const { - const FieldDescriptor* result = - file()->tables_->FindFieldByNumber(this, key); +const FieldDescriptor* Descriptor::FindFieldByNumber(int key) const { + const FieldDescriptor* result = file()->tables_->FindFieldByNumber(this, key); if (result == NULL || result->is_extension()) { return NULL; } else { @@ -1562,7 +1553,7 @@ Descriptor::FindFieldByNumber(int key) const { const FieldDescriptor* Descriptor::FindFieldByLowercaseName( const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByLowercaseName(this, key); + file()->tables_->FindFieldByLowercaseName(this, key); if (result == NULL || result->is_extension()) { return NULL; } else { @@ -1573,7 +1564,7 @@ const FieldDescriptor* Descriptor::FindFieldByLowercaseName( const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByCamelcaseName(this, key); + file()->tables_->FindFieldByCamelcaseName(this, key); if (result == NULL || result->is_extension()) { return NULL; } else { @@ -1584,7 +1575,7 @@ const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( const FieldDescriptor* Descriptor::FindFieldByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); if (!result.IsNull() && !result.field_descriptor->is_extension()) { return result.field_descriptor; } else { @@ -1595,7 +1586,7 @@ const FieldDescriptor* Descriptor::FindFieldByName( const OneofDescriptor* Descriptor::FindOneofByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ONEOF); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ONEOF); if (!result.IsNull()) { return result.oneof_descriptor; } else { @@ -1606,7 +1597,7 @@ const OneofDescriptor* Descriptor::FindOneofByName( const FieldDescriptor* Descriptor::FindExtensionByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); if (!result.IsNull() && result.field_descriptor->is_extension()) { return result.field_descriptor; } else { @@ -1617,7 +1608,7 @@ const FieldDescriptor* Descriptor::FindExtensionByName( const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByLowercaseName(this, key); + file()->tables_->FindFieldByLowercaseName(this, key); if (result == NULL || !result->is_extension()) { return NULL; } else { @@ -1628,7 +1619,7 @@ const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( const std::string& key) const { const FieldDescriptor* result = - file()->tables_->FindFieldByCamelcaseName(this, key); + file()->tables_->FindFieldByCamelcaseName(this, key); if (result == NULL || !result->is_extension()) { return NULL; } else { @@ -1639,7 +1630,7 @@ const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( const Descriptor* Descriptor::FindNestedTypeByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); if (!result.IsNull()) { return result.descriptor; } else { @@ -1650,7 +1641,7 @@ const Descriptor* Descriptor::FindNestedTypeByName( const EnumDescriptor* Descriptor::FindEnumTypeByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); if (!result.IsNull()) { return result.enum_descriptor; } else { @@ -1661,7 +1652,7 @@ const EnumDescriptor* Descriptor::FindEnumTypeByName( const EnumValueDescriptor* Descriptor::FindEnumValueByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { @@ -1672,7 +1663,7 @@ const EnumValueDescriptor* Descriptor::FindEnumValueByName( const EnumValueDescriptor* EnumDescriptor::FindValueByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { @@ -1680,20 +1671,19 @@ const EnumValueDescriptor* EnumDescriptor::FindValueByName( } } -const EnumValueDescriptor* -EnumDescriptor::FindValueByNumber(int key) const { +const EnumValueDescriptor* EnumDescriptor::FindValueByNumber(int key) const { return file()->tables_->FindEnumValueByNumber(this, key); } -const EnumValueDescriptor* -EnumDescriptor::FindValueByNumberCreatingIfUnknown(int key) const { +const EnumValueDescriptor* EnumDescriptor::FindValueByNumberCreatingIfUnknown( + int key) const { return file()->tables_->FindEnumValueByNumberCreatingIfUnknown(this, key); } const MethodDescriptor* ServiceDescriptor::FindMethodByName( const std::string& key) const { Symbol result = - file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); + file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); if (!result.IsNull()) { return result.method_descriptor; } else { @@ -1724,7 +1714,7 @@ const EnumDescriptor* FileDescriptor::FindEnumTypeByName( const EnumValueDescriptor* FileDescriptor::FindEnumValueByName( const std::string& key) const { Symbol result = - tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); + tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); if (!result.IsNull()) { return result.enum_value_descriptor; } else { @@ -1787,19 +1777,18 @@ Descriptor::FindExtensionRangeContainingNumber(int number) const { // more than a couple extension ranges. for (int i = 0; i < extension_range_count(); i++) { if (number >= extension_range(i)->start && - number < extension_range(i)->end) { + number < extension_range(i)->end) { return extension_range(i); } } return NULL; } -const Descriptor::ReservedRange* -Descriptor::FindReservedRangeContainingNumber(int number) const { +const Descriptor::ReservedRange* Descriptor::FindReservedRangeContainingNumber( + int number) const { // TODO(chrisn): Consider a non-linear search. for (int i = 0; i < reserved_range_count(); i++) { - if (number >= reserved_range(i)->start && - number < reserved_range(i)->end) { + if (number >= reserved_range(i)->start && number < reserved_range(i)->end) { return reserved_range(i); } } @@ -1864,22 +1853,22 @@ bool DescriptorPool::TryFindSymbolInFallbackDatabase( if (tables_->known_bad_symbols_.count(name) > 0) return false; FileDescriptorProto file_proto; - if (// We skip looking in the fallback database if the name is a sub-symbol - // of any descriptor that already exists in the descriptor pool (except - // for package descriptors). This is valid because all symbols except - // for packages are defined in a single file, so if the symbol exists - // then we should already have its definition. - // - // The other reason to do this is to support "overriding" type - // definitions by merging two databases that define the same type. (Yes, - // people do this.) The main difficulty with making this work is that - // FindFileContainingSymbol() is allowed to return both false positives - // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and false - // negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase). - // When two such databases are merged, looking up a non-existent - // sub-symbol of a type that already exists in the descriptor pool can - // result in an attempt to load multiple definitions of the same type. - // The check below avoids this. + if ( // We skip looking in the fallback database if the name is a sub-symbol + // of any descriptor that already exists in the descriptor pool (except + // for package descriptors). This is valid because all symbols except + // for packages are defined in a single file, so if the symbol exists + // then we should already have its definition. + // + // The other reason to do this is to support "overriding" type + // definitions by merging two databases that define the same type. (Yes, + // people do this.) The main difficulty with making this work is that + // FindFileContainingSymbol() is allowed to return both false positives + // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and + // false negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase). + // When two such databases are merged, looking up a non-existent + // sub-symbol of a type that already exists in the descriptor pool can + // result in an attempt to load multiple definitions of the same type. + // The check below avoids this. IsSubSymbolOfBuiltType(name) // Look up file containing this symbol in fallback database. @@ -1905,7 +1894,7 @@ bool DescriptorPool::TryFindExtensionInFallbackDatabase( FileDescriptorProto file_proto; if (!fallback_database_->FindFileContainingExtension( - containing_type->full_name(), field_number, &file_proto)) { + containing_type->full_name(), field_number, &file_proto)) { return false; } @@ -2241,8 +2230,8 @@ bool RetrieveOptionsAssumingRightPool( std::string tmp; TextFormat::Printer printer; printer.SetInitialIndentLevel(depth + 1); - printer.PrintFieldValueToString(options, fields[i], - repeated ? j : -1, &tmp); + printer.PrintFieldValueToString(options, fields[i], repeated ? j : -1, + &tmp); fieldval.append("{\n"); fieldval.append(tmp); fieldval.append(depth * 2, ' '); @@ -2313,8 +2302,8 @@ bool FormatLineOptions(int depth, const Message& options, std::vector<std::string> all_options; if (RetrieveOptions(depth, options, pool, &all_options)) { for (int i = 0; i < all_options.size(); i++) { - strings::SubstituteAndAppend(output, "$0option $1;\n", - prefix, all_options[i]); + strings::SubstituteAndAppend(output, "$0option $1;\n", prefix, + all_options[i]); } } return !all_options.empty(); @@ -2328,8 +2317,8 @@ class SourceLocationCommentPrinter { : options_(options), prefix_(prefix) { // Perform the SourceLocation lookup only if we're including user comments, // because the lookup is fairly expensive. - have_source_loc_ = options.include_comments && - desc->GetSourceLocation(&source_loc_); + have_source_loc_ = + options.include_comments && desc->GetSourceLocation(&source_loc_); } SourceLocationCommentPrinter(const FileDescriptor* file, const std::vector<int>& path, @@ -2338,13 +2327,13 @@ class SourceLocationCommentPrinter { : options_(options), prefix_(prefix) { // Perform the SourceLocation lookup only if we're including user comments, // because the lookup is fairly expensive. - have_source_loc_ = options.include_comments && - file->GetSourceLocation(path, &source_loc_); + have_source_loc_ = + options.include_comments && file->GetSourceLocation(path, &source_loc_); } void AddPreComment(std::string* output) { if (have_source_loc_) { // Detached leading comments. - for (int i = 0 ; i < source_loc_.leading_detached_comments.size(); ++i) { + for (int i = 0; i < source_loc_.leading_detached_comments.size(); ++i) { *output += FormatComment(source_loc_.leading_detached_comments[i]); *output += "\n"; } @@ -2395,16 +2384,15 @@ std::string FileDescriptor::DebugStringWithOptions( { std::vector<int> path; path.push_back(FileDescriptorProto::kSyntaxFieldNumber); - SourceLocationCommentPrinter syntax_comment( - this, path, "", debug_string_options); + SourceLocationCommentPrinter syntax_comment(this, path, "", + debug_string_options); syntax_comment.AddPreComment(&contents); strings::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n", SyntaxName(syntax())); syntax_comment.AddPostComment(&contents); } - SourceLocationCommentPrinter - comment_printer(this, "", debug_string_options); + SourceLocationCommentPrinter comment_printer(this, "", debug_string_options); comment_printer.AddPreComment(&contents); std::set<int> public_dependencies; @@ -2430,8 +2418,8 @@ std::string FileDescriptor::DebugStringWithOptions( if (!package().empty()) { std::vector<int> path; path.push_back(FileDescriptorProto::kPackageFieldNumber); - SourceLocationCommentPrinter package_comment( - this, path, "", debug_string_options); + SourceLocationCommentPrinter package_comment(this, path, "", + debug_string_options); package_comment.AddPreComment(&contents); strings::SubstituteAndAppend(&contents, "package $0;\n\n", package()); package_comment.AddPostComment(&contents); @@ -2508,8 +2496,8 @@ void Descriptor::DebugString(int depth, std::string* contents, std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); if (include_opening_clause) { @@ -2555,8 +2543,7 @@ void Descriptor::DebugString(int depth, std::string* contents, } for (int i = 0; i < extension_range_count(); i++) { - strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", - prefix, + strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", prefix, extension_range(i)->start, extension_range(i)->end - 1); } @@ -2567,12 +2554,11 @@ void Descriptor::DebugString(int depth, std::string* contents, if (extension(i)->containing_type() != containing_type) { if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); containing_type = extension(i)->containing_type(); - strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", - prefix, containing_type->full_name()); + strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", prefix, + containing_type->full_name()); } - extension(i)->DebugString( - depth + 1, FieldDescriptor::PRINT_LABEL, contents, - debug_string_options); + extension(i)->DebugString(depth + 1, FieldDescriptor::PRINT_LABEL, contents, + debug_string_options); } if (extension_count() > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); @@ -2584,8 +2570,8 @@ void Descriptor::DebugString(int depth, std::string* contents, if (range->end == range->start + 1) { strings::SubstituteAndAppend(contents, "$0, ", range->start); } else { - strings::SubstituteAndAppend(contents, "$0 to $1, ", - range->start, range->end - 1); + strings::SubstituteAndAppend(contents, "$0 to $1, ", range->start, + range->end - 1); } } contents->replace(contents->size() - 2, 2, ";\n"); @@ -2627,7 +2613,7 @@ std::string FieldDescriptor::DebugStringWithOptions( // The field type string used in FieldDescriptor::DebugString() std::string FieldDescriptor::FieldTypeNameDebugString() const { - switch(type()) { + switch (type()) { case TYPE_MESSAGE: return "." + message_type()->full_name(); case TYPE_ENUM: @@ -2669,17 +2655,13 @@ void FieldDescriptor::DebugString( label.push_back(' '); } - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0$1$2 $3 = $4", - prefix, - label, - field_type, - type() == TYPE_GROUP ? message_type()->name() : - name(), - number()); + strings::SubstituteAndAppend( + contents, "$0$1$2 $3 = $4", prefix, label, field_type, + type() == TYPE_GROUP ? message_type()->name() : name(), number()); bool bracketed = false; if (has_default_value()) { @@ -2742,8 +2724,8 @@ void OneofDescriptor::DebugString( const DebugStringOptions& debug_string_options) const { std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); strings::SubstituteAndAppend(contents, "$0oneof $1 {", prefix, name()); @@ -2781,12 +2763,11 @@ void EnumDescriptor::DebugString( std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0enum $1 {\n", - prefix, name()); + strings::SubstituteAndAppend(contents, "$0enum $1 {\n", prefix, name()); FormatLineOptions(depth, options(), file()->pool(), contents); @@ -2801,8 +2782,8 @@ void EnumDescriptor::DebugString( if (range->end == range->start) { strings::SubstituteAndAppend(contents, "$0, ", range->start); } else { - strings::SubstituteAndAppend(contents, "$0 to $1, ", - range->start, range->end); + strings::SubstituteAndAppend(contents, "$0 to $1, ", range->start, + range->end); } } contents->replace(contents->size() - 2, 2, ";\n"); @@ -2839,12 +2820,11 @@ void EnumValueDescriptor::DebugString( const DebugStringOptions& debug_string_options) const { std::string prefix(depth * 2, ' '); - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0$1 = $2", - prefix, name(), number()); + strings::SubstituteAndAppend(contents, "$0$1 = $2", prefix, name(), number()); std::string formatted_options; if (FormatBracketedOptions(depth, options(), type()->file()->pool(), @@ -2871,8 +2851,8 @@ std::string ServiceDescriptor::DebugStringWithOptions( void ServiceDescriptor::DebugString( std::string* contents, const DebugStringOptions& debug_string_options) const { - SourceLocationCommentPrinter - comment_printer(this, /* prefix */ "", debug_string_options); + SourceLocationCommentPrinter comment_printer(this, /* prefix */ "", + debug_string_options); comment_printer.AddPreComment(contents); strings::SubstituteAndAppend(contents, "service $0 {\n", name()); @@ -2906,22 +2886,20 @@ void MethodDescriptor::DebugString( std::string prefix(depth * 2, ' '); ++depth; - SourceLocationCommentPrinter - comment_printer(this, prefix, debug_string_options); + SourceLocationCommentPrinter comment_printer(this, prefix, + debug_string_options); comment_printer.AddPreComment(contents); - strings::SubstituteAndAppend(contents, "$0rpc $1($4.$2) returns ($5.$3)", - prefix, name(), - input_type()->full_name(), - output_type()->full_name(), - client_streaming() ? "stream " : "", - server_streaming() ? "stream " : ""); + strings::SubstituteAndAppend( + contents, "$0rpc $1($4.$2) returns ($5.$3)", prefix, name(), + input_type()->full_name(), output_type()->full_name(), + client_streaming() ? "stream " : "", server_streaming() ? "stream " : ""); std::string formatted_options; if (FormatLineOptions(depth, options(), service()->file()->pool(), &formatted_options)) { - strings::SubstituteAndAppend(contents, " {\n$0$1}\n", - formatted_options, prefix); + strings::SubstituteAndAppend(contents, " {\n$0$1}\n", formatted_options, + prefix); } else { contents->append(";\n"); } @@ -2937,13 +2915,13 @@ bool FileDescriptor::GetSourceLocation(const std::vector<int>& path, GOOGLE_CHECK(out_location != nullptr); if (source_code_info_) { if (const SourceCodeInfo_Location* loc = - tables_->GetSourceLocation(path, source_code_info_)) { + tables_->GetSourceLocation(path, source_code_info_)) { const RepeatedField<int32>& span = loc->span(); if (span.size() == 3 || span.size() == 4) { - out_location->start_line = span.Get(0); + out_location->start_line = span.Get(0); out_location->start_column = span.Get(1); - out_location->end_line = span.Get(span.size() == 3 ? 0 : 2); - out_location->end_column = span.Get(span.size() - 1); + out_location->end_line = span.Get(span.size() == 3 ? 0 : 2); + out_location->end_column = span.Get(span.size() - 1); out_location->leading_comments = loc->leading_comments(); out_location->trailing_comments = loc->trailing_comments(); @@ -3105,8 +3083,7 @@ struct OptionsToInterpret { class DescriptorBuilder { public: - DescriptorBuilder(const DescriptorPool* pool, - DescriptorPool::Tables* tables, + DescriptorBuilder(const DescriptorPool* pool, DescriptorPool::Tables* tables, DescriptorPool::ErrorCollector* error_collector); ~DescriptorBuilder(); @@ -3212,9 +3189,7 @@ class DescriptorBuilder { // that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode, // if it believes that's all it could refer to. The caller should always // check that it receives the type of symbol it was expecting. - enum ResolveMode { - LOOKUP_ALL, LOOKUP_TYPES - }; + enum ResolveMode { LOOKUP_ALL, LOOKUP_TYPES }; Symbol LookupSymbol(const std::string& name, const std::string& relative_to, DescriptorPool::PlaceholderType placeholder_type = DescriptorPool::PLACEHOLDER_MESSAGE, @@ -3274,49 +3249,41 @@ class DescriptorBuilder { // These methods all have the same signature for the sake of the BUILD_ARRAY // macro, below. - void BuildMessage(const DescriptorProto& proto, - const Descriptor* parent, + void BuildMessage(const DescriptorProto& proto, const Descriptor* parent, Descriptor* result); void BuildFieldOrExtension(const FieldDescriptorProto& proto, - const Descriptor* parent, - FieldDescriptor* result, + const Descriptor* parent, FieldDescriptor* result, bool is_extension); - void BuildField(const FieldDescriptorProto& proto, - const Descriptor* parent, + void BuildField(const FieldDescriptorProto& proto, const Descriptor* parent, FieldDescriptor* result) { BuildFieldOrExtension(proto, parent, result, false); } void BuildExtension(const FieldDescriptorProto& proto, - const Descriptor* parent, - FieldDescriptor* result) { + const Descriptor* parent, FieldDescriptor* result) { BuildFieldOrExtension(proto, parent, result, true); } void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto, const Descriptor* parent, Descriptor::ExtensionRange* result); void BuildReservedRange(const DescriptorProto::ReservedRange& proto, - const Descriptor* parent, - Descriptor::ReservedRange* result); + const Descriptor* parent, + Descriptor::ReservedRange* result); void BuildReservedRange(const EnumDescriptorProto::EnumReservedRange& proto, const EnumDescriptor* parent, EnumDescriptor::ReservedRange* result); - void BuildOneof(const OneofDescriptorProto& proto, - Descriptor* parent, + void BuildOneof(const OneofDescriptorProto& proto, Descriptor* parent, OneofDescriptor* result); void CheckEnumValueUniqueness(const EnumDescriptorProto& proto, const EnumDescriptor* result); - void BuildEnum(const EnumDescriptorProto& proto, - const Descriptor* parent, + void BuildEnum(const EnumDescriptorProto& proto, const Descriptor* parent, EnumDescriptor* result); void BuildEnumValue(const EnumValueDescriptorProto& proto, const EnumDescriptor* parent, EnumValueDescriptor* result); - void BuildService(const ServiceDescriptorProto& proto, - const void* dummy, + void BuildService(const ServiceDescriptorProto& proto, const void* dummy, ServiceDescriptor* result); void BuildMethod(const MethodDescriptorProto& proto, - const ServiceDescriptor* parent, - MethodDescriptor* result); + const ServiceDescriptor* parent, MethodDescriptor* result); void LogUnusedDependency(const FileDescriptorProto& proto, const FileDescriptor* result); @@ -3512,10 +3479,8 @@ class DescriptorBuilder { const ServiceDescriptorProto& proto); void ValidateMethodOptions(MethodDescriptor* method, const MethodDescriptorProto& proto); - void ValidateProto3(FileDescriptor* file, - const FileDescriptorProto& proto); - void ValidateProto3Message(Descriptor* message, - const DescriptorProto& proto); + void ValidateProto3(FileDescriptor* file, const FileDescriptorProto& proto); + void ValidateProto3Message(Descriptor* message, const DescriptorProto& proto); void ValidateProto3Field(FieldDescriptor* field, const FieldDescriptorProto& proto); void ValidateProto3Enum(EnumDescriptor* enm, @@ -3538,27 +3503,26 @@ class DescriptorBuilder { const FileDescriptor* DescriptorPool::BuildFile( const FileDescriptorProto& proto) { GOOGLE_CHECK(fallback_database_ == NULL) - << "Cannot call BuildFile on a DescriptorPool that uses a " - "DescriptorDatabase. You must instead find a way to get your file " - "into the underlying database."; - GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. + << "Cannot call BuildFile on a DescriptorPool that uses a " + "DescriptorDatabase. You must instead find a way to get your file " + "into the underlying database."; + GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto); } const FileDescriptor* DescriptorPool::BuildFileCollectingErrors( - const FileDescriptorProto& proto, - ErrorCollector* error_collector) { + const FileDescriptorProto& proto, ErrorCollector* error_collector) { GOOGLE_CHECK(fallback_database_ == NULL) - << "Cannot call BuildFile on a DescriptorPool that uses a " - "DescriptorDatabase. You must instead find a way to get your file " - "into the underlying database."; - GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. + << "Cannot call BuildFile on a DescriptorPool that uses a " + "DescriptorDatabase. You must instead find a way to get your file " + "into the underlying database."; + GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); - return DescriptorBuilder(this, tables_.get(), - error_collector).BuildFile(proto); + return DescriptorBuilder(this, tables_.get(), error_collector) + .BuildFile(proto); } const FileDescriptor* DescriptorPool::BuildFileFromDatabase( @@ -3568,8 +3532,8 @@ const FileDescriptor* DescriptorPool::BuildFileFromDatabase( return NULL; } const FileDescriptor* result = - DescriptorBuilder(this, tables_.get(), - default_error_collector_).BuildFile(proto); + DescriptorBuilder(this, tables_.get(), default_error_collector_) + .BuildFile(proto); if (result == NULL) { tables_->known_bad_files_.insert(proto.name()); } @@ -3577,15 +3541,14 @@ const FileDescriptor* DescriptorPool::BuildFileFromDatabase( } DescriptorBuilder::DescriptorBuilder( - const DescriptorPool* pool, - DescriptorPool::Tables* tables, + const DescriptorPool* pool, DescriptorPool::Tables* tables, DescriptorPool::ErrorCollector* error_collector) - : pool_(pool), - tables_(tables), - error_collector_(error_collector), - had_errors_(false), - possible_undeclared_dependency_(NULL), - undefine_resolved_name_("") {} + : pool_(pool), + tables_(tables), + error_collector_(error_collector), + had_errors_(false), + possible_undeclared_dependency_(NULL), + undefine_resolved_name_("") {} DescriptorBuilder::~DescriptorBuilder() {} @@ -3600,8 +3563,8 @@ void DescriptorBuilder::AddError( } GOOGLE_LOG(ERROR) << " " << element_name << ": " << error; } else { - error_collector_->AddError(filename_, element_name, - &descriptor, location, error); + error_collector_->AddError(filename_, element_name, &descriptor, location, + error); } had_errors_ = true; } @@ -3624,19 +3587,22 @@ void DescriptorBuilder::AddNotDefinedError( if (possible_undeclared_dependency_ != NULL) { AddError(element_name, descriptor, location, "\"" + possible_undeclared_dependency_name_ + - "\" seems to be defined in \"" + - possible_undeclared_dependency_->name() + "\", which is not " - "imported by \"" + filename_ + "\". To use it here, please " - "add the necessary import."); + "\" seems to be defined in \"" + + possible_undeclared_dependency_->name() + + "\", which is not " + "imported by \"" + + filename_ + + "\". To use it here, please " + "add the necessary import."); } if (!undefine_resolved_name_.empty()) { AddError(element_name, descriptor, location, "\"" + undefined_symbol + "\" is resolved to \"" + - undefine_resolved_name_ + "\", which is not defined. " - "The innermost scope is searched first in name resolution. " - "Consider using a leading '.'(i.e., \"." - + undefined_symbol + - "\") to start from the outermost scope."); + undefine_resolved_name_ + + "\", which is not defined. " + "The innermost scope is searched first in name resolution. " + "Consider using a leading '.'(i.e., \"." + + undefined_symbol + "\") to start from the outermost scope."); } } } @@ -3875,10 +3841,10 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( std::string::size_type dotpos = placeholder_full_name->find_last_of('.'); if (dotpos != std::string::npos) { - placeholder_package = tables_->AllocateString( - placeholder_full_name->substr(0, dotpos)); - placeholder_name = tables_->AllocateString( - placeholder_full_name->substr(dotpos + 1)); + placeholder_package = + tables_->AllocateString(placeholder_full_name->substr(0, dotpos)); + placeholder_name = + tables_->AllocateString(placeholder_full_name->substr(dotpos + 1)); } else { placeholder_package = &internal::GetEmptyString(); placeholder_name = placeholder_full_name; @@ -3891,8 +3857,7 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( if (placeholder_type == PLACEHOLDER_ENUM) { placeholder_file->enum_type_count_ = 1; - placeholder_file->enum_types_ = - tables_->AllocateArray<EnumDescriptor>(1); + placeholder_file->enum_types_ = tables_->AllocateArray<EnumDescriptor>(1); EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0]; memset(placeholder_enum, 0, sizeof(*placeholder_enum)); @@ -3914,8 +3879,10 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE"); // Note that enum value names are siblings of their type, not children. placeholder_value->full_name_ = - placeholder_package->empty() ? placeholder_value->name_ : - tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE"); + placeholder_package->empty() + ? placeholder_value->name_ + : tables_->AllocateString(*placeholder_package + + ".PLACEHOLDER_VALUE"); placeholder_value->number_ = 0; placeholder_value->type_ = placeholder_enum; @@ -3924,8 +3891,7 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( return Symbol(placeholder_enum); } else { placeholder_file->message_type_count_ = 1; - placeholder_file->message_types_ = - tables_->AllocateArray<Descriptor>(1); + placeholder_file->message_types_ = tables_->AllocateArray<Descriptor>(1); Descriptor* placeholder_message = &placeholder_file->message_types_[0]; memset(placeholder_message, 0, sizeof(*placeholder_message)); @@ -3940,11 +3906,11 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) { placeholder_message->extension_range_count_ = 1; placeholder_message->extension_ranges_ = - tables_->AllocateArray<Descriptor::ExtensionRange>(1); + tables_->AllocateArray<Descriptor::ExtensionRange>(1); placeholder_message->extension_ranges_->start = 1; // kMaxNumber + 1 because ExtensionRange::end is exclusive. placeholder_message->extension_ranges_->end = - FieldDescriptor::kMaxNumber + 1; + FieldDescriptor::kMaxNumber + 1; } return Symbol(placeholder_message); @@ -3991,7 +3957,8 @@ bool DescriptorBuilder::AddSymbol(const std::string& full_name, // This is only possible if there was already an error adding something of // the same name. if (!had_errors_) { - GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in " + GOOGLE_LOG(DFATAL) << "\"" << full_name + << "\" not previously defined in " "symbols_by_name_, but was defined in " "symbols_by_parent_; this shouldn't be possible."; } @@ -4008,14 +3975,14 @@ bool DescriptorBuilder::AddSymbol(const std::string& full_name, } else { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + full_name.substr(dot_pos + 1) + - "\" is already defined in \"" + - full_name.substr(0, dot_pos) + "\"."); + "\" is already defined in \"" + + full_name.substr(0, dot_pos) + "\"."); } } else { // Symbol seems to have been defined in a different file. AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + full_name + "\" is already defined in file \"" + - other_file->name() + "\"."); + other_file->name() + "\"."); } return false; } @@ -4043,9 +4010,10 @@ void DescriptorBuilder::AddPackage(const std::string& name, if (existing_symbol.type != Symbol::PACKAGE) { // Symbol seems to have been defined in a different file. AddError(name, proto, DescriptorPool::ErrorCollector::NAME, - "\"" + name + "\" is already defined (as something other than " - "a package) in file \"" + existing_symbol.GetFile()->name() + - "\"."); + "\"" + name + + "\" is already defined (as something other than " + "a package) in file \"" + + existing_symbol.GetFile()->name() + "\"."); } } } @@ -4061,8 +4029,7 @@ void DescriptorBuilder::ValidateSymbolName(const std::string& name, // I don't trust isalnum() due to locales. :( if ((name[i] < 'a' || 'z' < name[i]) && (name[i] < 'A' || 'Z' < name[i]) && - (name[i] < '0' || '9' < name[i]) && - (name[i] != '_')) { + (name[i] < '0' || '9' < name[i]) && (name[i] != '_')) { AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, "\"" + name + "\" is not a valid identifier."); } @@ -4136,11 +4103,11 @@ void DescriptorBuilder::AllocateOptionsImpl( // A common pattern: We want to convert a repeated field in the descriptor // to an array of values, calling some method to build each value. -#define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ - OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ - AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ - for (int i = 0; i < INPUT.NAME##_size(); i++) { \ - METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ +#define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ + OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ + AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ + for (int i = 0; i < INPUT.NAME##_size(); i++) { \ + METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ } void DescriptorBuilder::AddRecursiveImportError( @@ -4152,13 +4119,19 @@ void DescriptorBuilder::AddRecursiveImportError( } error_message.append(proto.name()); - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, - error_message); + if (from_here < tables_->pending_files_.size() - 1) { + AddError(tables_->pending_files_[from_here + 1], proto, + DescriptorPool::ErrorCollector::IMPORT, error_message); + } else { + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::IMPORT, + error_message); + } } void DescriptorBuilder::AddTwiceListedError(const FileDescriptorProto& proto, int index) { - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, + AddError(proto.dependency(index), proto, + DescriptorPool::ErrorCollector::IMPORT, "Import \"" + proto.dependency(index) + "\" was listed twice."); } @@ -4166,13 +4139,13 @@ void DescriptorBuilder::AddImportError(const FileDescriptorProto& proto, int index) { std::string message; if (pool_->fallback_database_ == NULL) { - message = "Import \"" + proto.dependency(index) + - "\" has not been loaded."; + message = "Import \"" + proto.dependency(index) + "\" has not been loaded."; } else { message = "Import \"" + proto.dependency(index) + "\" was not found or had errors."; } - AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, message); + AddError(proto.dependency(index), proto, + DescriptorPool::ErrorCollector::IMPORT, message); } static bool ExistingFileMatchesProto(const FileDescriptor* existing_file, @@ -4389,8 +4362,8 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( // Check public dependencies. int public_dependency_count = 0; - result->public_dependencies_ = tables_->AllocateArray<int>( - proto.public_dependency_size()); + result->public_dependencies_ = + tables_->AllocateArray<int>(proto.public_dependency_size()); for (int i = 0; i < proto.public_dependency_size(); i++) { // Only put valid public dependency indexes. int index = proto.public_dependency(i); @@ -4404,8 +4377,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( unused_dependency_.erase(result->dependency(index)); } } else { - AddError(proto.name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, "Invalid public dependency index."); } } @@ -4424,25 +4396,24 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( // Check weak dependencies. int weak_dependency_count = 0; - result->weak_dependencies_ = tables_->AllocateArray<int>( - proto.weak_dependency_size()); + result->weak_dependencies_ = + tables_->AllocateArray<int>(proto.weak_dependency_size()); for (int i = 0; i < proto.weak_dependency_size(); i++) { int index = proto.weak_dependency(i); if (index >= 0 && index < proto.dependency_size()) { result->weak_dependencies_[weak_dependency_count++] = index; } else { - AddError(proto.name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, "Invalid weak dependency index."); } } result->weak_dependency_count_ = weak_dependency_count; // Convert children. - BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL); - BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL); - BUILD_ARRAY(proto, result, service , BuildService , NULL); - BUILD_ARRAY(proto, result, extension , BuildExtension, NULL); + BUILD_ARRAY(proto, result, message_type, BuildMessage, NULL); + BUILD_ARRAY(proto, result, enum_type, BuildEnum, NULL); + BUILD_ARRAY(proto, result, service, BuildService, NULL); + BUILD_ARRAY(proto, result, extension, BuildExtension, NULL); // Copy options. if (!proto.has_options()) { @@ -4512,21 +4483,21 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; result->containing_type_ = parent; - result->is_placeholder_ = false; + result->is_placeholder_ = false; result->is_unqualified_placeholder_ = false; // Build oneofs first so that fields and extension ranges can refer to them. - BUILD_ARRAY(proto, result, oneof_decl , BuildOneof , result); - BUILD_ARRAY(proto, result, field , BuildField , result); - BUILD_ARRAY(proto, result, nested_type , BuildMessage , result); - BUILD_ARRAY(proto, result, enum_type , BuildEnum , result); + BUILD_ARRAY(proto, result, oneof_decl, BuildOneof, result); + BUILD_ARRAY(proto, result, field, BuildField, result); + BUILD_ARRAY(proto, result, nested_type, BuildMessage, result); + BUILD_ARRAY(proto, result, enum_type, BuildEnum, result); BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result); - BUILD_ARRAY(proto, result, extension , BuildExtension , result); - BUILD_ARRAY(proto, result, reserved_range , BuildReservedRange , result); + BUILD_ARRAY(proto, result, extension, BuildExtension, result); + BUILD_ARRAY(proto, result, reserved_range, BuildReservedRange, result); // Copy reserved names. int reserved_name_count = proto.reserved_name_size(); @@ -4546,8 +4517,7 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, DescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); for (int i = 0; i < proto.reserved_range_size(); i++) { @@ -4573,8 +4543,7 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, } else { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, strings::Substitute( - "Field name \"$0\" is reserved multiple times.", - name)); + "Field name \"$0\" is reserved multiple times.", name)); } } @@ -4583,12 +4552,12 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, for (int j = 0; j < result->extension_range_count(); j++) { const Descriptor::ExtensionRange* range = result->extension_range(j); if (range->start <= field->number() && field->number() < range->end) { - AddError(field->full_name(), proto.extension_range(j), - DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Extension range $0 to $1 includes field \"$2\" ($3).", - range->start, range->end - 1, - field->name(), field->number())); + AddError( + field->full_name(), proto.extension_range(j), + DescriptorPool::ErrorCollector::NUMBER, + strings::Substitute( + "Extension range $0 to $1 includes field \"$2\" ($3).", + range->start, range->end - 1, field->name(), field->number())); } } for (int j = 0; j < result->reserved_range_count(); j++) { @@ -4596,16 +4565,15 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, if (range->start <= field->number() && field->number() < range->end) { AddError(field->full_name(), proto.reserved_range(j), DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Field \"$0\" uses reserved number $1.", - field->name(), field->number())); + strings::Substitute("Field \"$0\" uses reserved number $1.", + field->name(), field->number())); } } if (reserved_name_set.find(field->name()) != reserved_name_set.end()) { - AddError(field->full_name(), proto.field(i), - DescriptorPool::ErrorCollector::NAME, - strings::Substitute( - "Field name \"$0\" is reserved.", field->name())); + AddError( + field->full_name(), proto.field(i), + DescriptorPool::ErrorCollector::NAME, + strings::Substitute("Field name \"$0\" is reserved.", field->name())); } } @@ -4650,10 +4618,10 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; - result->number_ = proto.number(); + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; + result->number_ = proto.number(); result->is_extension_ = is_extension; // If .proto files follow the style guide then the name should already be @@ -4725,19 +4693,19 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, switch (result->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: result->default_value_int32_ = - strtol(proto.default_value().c_str(), &end_pos, 0); + strtol(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_INT64: result->default_value_int64_ = - strto64(proto.default_value().c_str(), &end_pos, 0); + strto64(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_UINT32: result->default_value_uint32_ = - strtoul(proto.default_value().c_str(), &end_pos, 0); + strtoul(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_UINT64: result->default_value_uint64_ = - strtou64(proto.default_value().c_str(), &end_pos, 0); + strtou64(proto.default_value().c_str(), &end_pos, 0); break; case FieldDescriptor::CPPTYPE_FLOAT: if (proto.default_value() == "inf") { @@ -4749,7 +4717,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } else if (proto.default_value() == "nan") { result->default_value_float_ = std::numeric_limits<float>::quiet_NaN(); - } else { + } else { result->default_value_float_ = io::SafeDoubleToFloat( io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos)); } @@ -4764,7 +4732,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } else if (proto.default_value() == "nan") { result->default_value_double_ = std::numeric_limits<double>::quiet_NaN(); - } else { + } else { result->default_value_double_ = io::NoLocaleStrtod(proto.default_value().c_str(), &end_pos); } @@ -4787,7 +4755,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, case FieldDescriptor::CPPTYPE_STRING: if (result->type() == FieldDescriptor::TYPE_BYTES) { result->default_value_string_ = tables_->AllocateString( - UnescapeCEscapeString(proto.default_value())); + UnescapeCEscapeString(proto.default_value())); } else { result->default_value_string_ = tables_->AllocateString(proto.default_value()); @@ -4809,7 +4777,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::DEFAULT_VALUE, "Couldn't parse default value \"" + proto.default_value() + - "\"."); + "\"."); } } } else { @@ -4868,10 +4836,10 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, result->number() <= FieldDescriptor::kLastReservedNumber) { AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute( - "Field numbers $0 through $1 are reserved for the protocol " - "buffer library implementation.", - FieldDescriptor::kFirstReservedNumber, - FieldDescriptor::kLastReservedNumber)); + "Field numbers $0 through $1 are reserved for the protocol " + "buffer library implementation.", + FieldDescriptor::kFirstReservedNumber, + FieldDescriptor::kLastReservedNumber)); } if (is_extension) { @@ -4884,8 +4852,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, result->extension_scope_ = parent; if (proto.has_oneof_index()) { - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "FieldDescriptorProto.oneof_index should not be set for " "extensions."); } @@ -4905,11 +4872,10 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, if (proto.oneof_index() < 0 || proto.oneof_index() >= parent->oneof_decl_count()) { AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + DescriptorPool::ErrorCollector::TYPE, strings::Substitute("FieldDescriptorProto.oneof_index $0 is " "out of range for type \"$1\".", - proto.oneof_index(), - parent->name())); + proto.oneof_index(), parent->name())); result->containing_oneof_ = NULL; } else { result->containing_oneof_ = parent->oneof_decl(proto.oneof_index()); @@ -4928,19 +4894,16 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } void DescriptorBuilder::BuildExtensionRange( - const DescriptorProto::ExtensionRange& proto, - const Descriptor* parent, + const DescriptorProto::ExtensionRange& proto, const Descriptor* parent, Descriptor::ExtensionRange* result) { result->start = proto.start(); result->end = proto.end(); if (result->start <= 0) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Extension numbers must be positive integers."); } @@ -4950,8 +4913,7 @@ void DescriptorBuilder::BuildExtensionRange( // numbers are actually used as int32s in the message_set_wire_format. if (result->start >= result->end) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Extension range end number must be greater than start number."); } @@ -4973,14 +4935,12 @@ void DescriptorBuilder::BuildExtensionRange( } void DescriptorBuilder::BuildReservedRange( - const DescriptorProto::ReservedRange& proto, - const Descriptor* parent, + const DescriptorProto::ReservedRange& proto, const Descriptor* parent, Descriptor::ReservedRange* result) { result->start = proto.start(); result->end = proto.end(); if (result->start <= 0) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Reserved numbers must be positive integers."); } } @@ -4992,8 +4952,7 @@ void DescriptorBuilder::BuildReservedRange( result->end = proto.end(); if (result->start > result->end) { - AddError(parent->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, + AddError(parent->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, "Reserved range end number must be greater than start number."); } } @@ -5024,8 +4983,7 @@ void DescriptorBuilder::BuildOneof(const OneofDescriptorProto& proto, OneofDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } void DescriptorBuilder::CheckEnumValueUniqueness( @@ -5105,18 +5063,17 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); - result->full_name_ = full_name; - result->file_ = file_; + result->name_ = tables_->AllocateString(proto.name()); + result->full_name_ = full_name; + result->file_ = file_; result->containing_type_ = parent; - result->is_placeholder_ = false; + result->is_placeholder_ = false; result->is_unqualified_placeholder_ = false; if (proto.value_size() == 0) { // We cannot allow enums with no values because this would mean there // would be no valid default value for fields of this type. - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Enums must contain at least one value."); } @@ -5143,8 +5100,7 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, EnumDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); for (int i = 0; i < proto.reserved_range_size(); i++) { const EnumDescriptorProto_EnumReservedRange& range1 = @@ -5171,8 +5127,7 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, } else { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, strings::Substitute( - "Enum value \"$0\" is reserved multiple times.", - name)); + "Enum value \"$0\" is reserved multiple times.", name)); } } @@ -5181,18 +5136,18 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, for (int j = 0; j < result->reserved_range_count(); j++) { const EnumDescriptor::ReservedRange* range = result->reserved_range(j); if (range->start <= value->number() && value->number() <= range->end) { - AddError(value->full_name(), proto.reserved_range(j), - DescriptorPool::ErrorCollector::NUMBER, - strings::Substitute( - "Enum value \"$0\" uses reserved number $1.", - value->name(), value->number())); + AddError( + value->full_name(), proto.reserved_range(j), + DescriptorPool::ErrorCollector::NUMBER, + strings::Substitute("Enum value \"$0\" uses reserved number $1.", + value->name(), value->number())); } } if (reserved_name_set.find(value->name()) != reserved_name_set.end()) { - AddError(value->full_name(), proto.value(i), - DescriptorPool::ErrorCollector::NAME, - strings::Substitute( - "Enum value \"$0\" is reserved.", value->name())); + AddError( + value->full_name(), proto.value(i), + DescriptorPool::ErrorCollector::NAME, + strings::Substitute("Enum value \"$0\" is reserved.", value->name())); } } } @@ -5200,9 +5155,9 @@ void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, const EnumDescriptor* parent, EnumValueDescriptor* result) { - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->number_ = proto.number(); - result->type_ = parent; + result->type_ = parent; // Note: full_name for enum values is a sibling to the parent's name, not a // child of it. @@ -5225,15 +5180,15 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, // of the enum type instead of children of it. So, we use // parent->containing_type() as the value's parent. bool added_to_outer_scope = - AddSymbol(result->full_name(), parent->containing_type(), result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent->containing_type(), result->name(), + proto, Symbol(result)); // However, we also want to be able to search for values within a single // enum type, so we add it as a child of the enum type itself, too. // Note: This could fail, but if it does, the error has already been // reported by the above AddSymbol() call, so we ignore the return code. bool added_to_inner_scope = - file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); + file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); if (added_to_inner_scope && !added_to_outer_scope) { // This value did not conflict with any values defined in the same enum, @@ -5252,12 +5207,12 @@ void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, outer_scope = "\"" + outer_scope + "\""; } - AddError(result->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Note that enum values use C++ scoping rules, meaning that " "enum values are siblings of their type, not children of it. " - "Therefore, \"" + result->name() + "\" must be unique within " - + outer_scope + ", not just within \"" + parent->name() + "\"."); + "Therefore, \"" + + result->name() + "\" must be unique within " + outer_scope + + ", not just within \"" + parent->name() + "\"."); } // An enum is allowed to define two numbers that refer to the same value. @@ -5275,9 +5230,9 @@ void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto, ValidateSymbolName(proto.name(), *full_name, proto); - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->full_name_ = full_name; - result->file_ = file_; + result->file_ = file_; BUILD_ARRAY(proto, result, method, BuildMethod, result); @@ -5289,14 +5244,13 @@ void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto, ServiceDescriptorProto::kOptionsFieldNumber); } - AddSymbol(result->full_name(), NULL, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), NULL, result->name(), proto, Symbol(result)); } void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, const ServiceDescriptor* parent, MethodDescriptor* result) { - result->name_ = tables_->AllocateString(proto.name()); + result->name_ = tables_->AllocateString(proto.name()); result->service_ = parent; std::string* full_name = tables_->AllocateString(parent->full_name()); @@ -5321,16 +5275,15 @@ void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, result->client_streaming_ = proto.client_streaming(); result->server_streaming_ = proto.server_streaming(); - AddSymbol(result->full_name(), parent, result->name(), - proto, Symbol(result)); + AddSymbol(result->full_name(), parent, result->name(), proto, Symbol(result)); } #undef BUILD_ARRAY // ------------------------------------------------------------------- -void DescriptorBuilder::CrossLinkFile( - FileDescriptor* file, const FileDescriptorProto& proto) { +void DescriptorBuilder::CrossLinkFile(FileDescriptor* file, + const FileDescriptorProto& proto) { if (file->options_ == NULL) { file->options_ = &FileOptions::default_instance(); } @@ -5352,8 +5305,8 @@ void DescriptorBuilder::CrossLinkFile( } } -void DescriptorBuilder::CrossLinkMessage( - Descriptor* message, const DescriptorProto& proto) { +void DescriptorBuilder::CrossLinkMessage(Descriptor* message, + const DescriptorProto& proto) { if (message->options_ == NULL) { message->options_ = &MessageOptions::default_instance(); } @@ -5393,14 +5346,13 @@ void DescriptorBuilder::CrossLinkMessage( // safe. if (oneof_decl->field_count() > 0 && message->field(i - 1)->containing_oneof() != oneof_decl) { - AddError( - message->full_name() + "." + message->field(i - 1)->name(), - proto.field(i - 1), DescriptorPool::ErrorCollector::OTHER, - strings::Substitute( - "Fields in the same oneof must be defined consecutively. " - "\"$0\" cannot be defined before the completion of the " - "\"$1\" oneof definition.", - message->field(i - 1)->name(), oneof_decl->name())); + AddError(message->full_name() + "." + message->field(i - 1)->name(), + proto.field(i - 1), DescriptorPool::ErrorCollector::TYPE, + strings::Substitute( + "Fields in the same oneof must be defined consecutively. " + "\"$0\" cannot be defined before the completion of the " + "\"$1\" oneof definition.", + message->field(i - 1)->name(), oneof_decl->name())); } // Must go through oneof_decls_ array to get a non-const version of the // OneofDescriptor. @@ -5414,13 +5366,12 @@ void DescriptorBuilder::CrossLinkMessage( if (oneof_decl->field_count() == 0) { AddError(message->full_name() + "." + oneof_decl->name(), - proto.oneof_decl(i), - DescriptorPool::ErrorCollector::NAME, + proto.oneof_decl(i), DescriptorPool::ErrorCollector::NAME, "Oneof must have at least one field."); } - oneof_decl->fields_ = - tables_->AllocateArray<const FieldDescriptor*>(oneof_decl->field_count_); + oneof_decl->fields_ = tables_->AllocateArray<const FieldDescriptor*>( + oneof_decl->field_count_); oneof_decl->field_count_ = 0; if (oneof_decl->options_ == NULL) { @@ -5449,8 +5400,8 @@ void DescriptorBuilder::CrossLinkExtensionRange( } } -void DescriptorBuilder::CrossLinkField( - FieldDescriptor* field, const FieldDescriptorProto& proto) { +void DescriptorBuilder::CrossLinkField(FieldDescriptor* field, + const FieldDescriptorProto& proto) { if (field->options_ == NULL) { field->options_ = &FieldOptions::default_instance(); } @@ -5475,8 +5426,9 @@ void DescriptorBuilder::CrossLinkField( } field->containing_type_ = extendee.descriptor; - const Descriptor::ExtensionRange* extension_range = field->containing_type() - ->FindExtensionRangeContainingNumber(field->number()); + const Descriptor::ExtensionRange* extension_range = + field->containing_type()->FindExtensionRangeContainingNumber( + field->number()); if (extension_range == NULL) { AddError(field->full_name(), proto, @@ -5493,8 +5445,7 @@ void DescriptorBuilder::CrossLinkField( // Note that this error will never happen when parsing .proto files. // It can only happen if you manually construct a FileDescriptorProto // that is incorrect. - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Fields of oneofs must themselves have label LABEL_OPTIONAL."); } } @@ -5615,20 +5566,19 @@ void DescriptorBuilder::CrossLinkField( // We can't just use field->enum_type()->FindValueByName() here // because that locks the pool's mutex, which we have already locked // at this point. - Symbol default_value = - LookupSymbolNoPlaceholder(proto.default_value(), - field->enum_type()->full_name()); + Symbol default_value = LookupSymbolNoPlaceholder( + proto.default_value(), field->enum_type()->full_name()); if (default_value.type == Symbol::ENUM_VALUE && default_value.enum_value_descriptor->type() == - field->enum_type()) { + field->enum_type()) { field->default_value_enum_ = default_value.enum_value_descriptor; } else { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::DEFAULT_VALUE, "Enum type \"" + field->enum_type()->full_name() + - "\" has no value named \"" + proto.default_value() + - "\"."); + "\" has no value named \"" + proto.default_value() + + "\"."); } } } else if (field->enum_type()->value_count() > 0) { @@ -5656,9 +5606,8 @@ void DescriptorBuilder::CrossLinkField( // risk to calling containing_type() or other accessors that will build // dependencies. if (!file_tables_->AddFieldByNumber(field)) { - const FieldDescriptor* conflicting_field = - file_tables_->FindFieldByNumber(field->containing_type(), - field->number()); + const FieldDescriptor* conflicting_field = file_tables_->FindFieldByNumber( + field->containing_type(), field->number()); std::string containing_type_name = field->containing_type() == NULL ? "unknown" @@ -5668,16 +5617,14 @@ void DescriptorBuilder::CrossLinkField( DescriptorPool::ErrorCollector::NUMBER, strings::Substitute("Extension number $0 has already been used " "in \"$1\" by extension \"$2\".", - field->number(), - containing_type_name, + field->number(), containing_type_name, conflicting_field->full_name())); } else { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, strings::Substitute("Field number $0 has already been used in " "\"$1\" by field \"$2\".", - field->number(), - containing_type_name, + field->number(), containing_type_name, conflicting_field->name())); } } else { @@ -5705,8 +5652,8 @@ void DescriptorBuilder::CrossLinkField( } } -void DescriptorBuilder::CrossLinkEnum( - EnumDescriptor* enum_type, const EnumDescriptorProto& proto) { +void DescriptorBuilder::CrossLinkEnum(EnumDescriptor* enum_type, + const EnumDescriptorProto& proto) { if (enum_type->options_ == NULL) { enum_type->options_ = &EnumOptions::default_instance(); } @@ -5724,8 +5671,8 @@ void DescriptorBuilder::CrossLinkEnumValue( } } -void DescriptorBuilder::CrossLinkService( - ServiceDescriptor* service, const ServiceDescriptorProto& proto) { +void DescriptorBuilder::CrossLinkService(ServiceDescriptor* service, + const ServiceDescriptorProto& proto) { if (service->options_ == NULL) { service->options_ = &ServiceOptions::default_instance(); } @@ -5735,8 +5682,8 @@ void DescriptorBuilder::CrossLinkService( } } -void DescriptorBuilder::CrossLinkMethod( - MethodDescriptor* method, const MethodDescriptorProto& proto) { +void DescriptorBuilder::CrossLinkMethod(MethodDescriptor* method, + const MethodDescriptorProto& proto) { if (method->options_ == NULL) { method->options_ = &MethodOptions::default_instance(); } @@ -5784,10 +5731,10 @@ void DescriptorBuilder::CrossLinkMethod( // ------------------------------------------------------------------- -#define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ - for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ - Validate##type##Options(descriptor->array_name##s_ + i, \ - proto.array_name(i)); \ +#define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ + for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ + Validate##type##Options(descriptor->array_name##s_ + i, \ + proto.array_name(i)); \ } // Determine if the file uses optimize_for = LITE_RUNTIME, being careful to @@ -5795,8 +5742,7 @@ void DescriptorBuilder::CrossLinkMethod( static bool IsLite(const FileDescriptor* file) { // TODO(kenton): I don't even remember how many of these conditions are // actually possible. I'm just being super-safe. - return file != NULL && - &file->options() != &FileOptions::default_instance() && + return file != NULL && &file->options() != &FileOptions::default_instance() && file->options().optimize_for() == FileOptions::LITE_RUNTIME; } @@ -5812,11 +5758,12 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, for (int i = 0; i < file->dependency_count(); i++) { if (IsLite(file->dependency(i))) { AddError( - file->name(), proto, - DescriptorPool::ErrorCollector::OTHER, - "Files that do not use optimize_for = LITE_RUNTIME cannot import " - "files which do use this option. This file is not lite, but it " - "imports \"" + file->dependency(i)->name() + "\" which is."); + file->dependency(i)->name(), proto, + DescriptorPool::ErrorCollector::IMPORT, + "Files that do not use optimize_for = LITE_RUNTIME cannot import " + "files which do use this option. This file is not lite, but it " + "imports \"" + + file->dependency(i)->name() + "\" which is."); break; } } @@ -5826,8 +5773,8 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, } } -void DescriptorBuilder::ValidateProto3( - FileDescriptor* file, const FileDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3(FileDescriptor* file, + const FileDescriptorProto& proto) { for (int i = 0; i < file->extension_count(); ++i) { ValidateProto3Field(file->extensions_ + i, proto.extension(i)); } @@ -5853,31 +5800,28 @@ static std::string ToLowercaseWithoutUnderscores(const std::string& name) { return result; } -void DescriptorBuilder::ValidateProto3Message( - Descriptor* message, const DescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Message(Descriptor* message, + const DescriptorProto& proto) { for (int i = 0; i < message->nested_type_count(); ++i) { - ValidateProto3Message(message->nested_types_ + i, - proto.nested_type(i)); + ValidateProto3Message(message->nested_types_ + i, proto.nested_type(i)); } for (int i = 0; i < message->enum_type_count(); ++i) { - ValidateProto3Enum(message->enum_types_ + i, - proto.enum_type(i)); + ValidateProto3Enum(message->enum_types_ + i, proto.enum_type(i)); } for (int i = 0; i < message->field_count(); ++i) { ValidateProto3Field(message->fields_ + i, proto.field(i)); } for (int i = 0; i < message->extension_count(); ++i) { - ValidateProto3Field(message->extensions_ +i, proto.extension(i)); + ValidateProto3Field(message->extensions_ + i, proto.extension(i)); } if (message->extension_range_count() > 0) { - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto.extension_range(0), + DescriptorPool::ErrorCollector::NUMBER, "Extension ranges are not allowed in proto3."); } if (message->options().message_set_wire_format()) { // Using MessageSet doesn't make sense since we disallow extensions. - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "MessageSet is not supported in proto3."); } @@ -5889,61 +5833,58 @@ void DescriptorBuilder::ValidateProto3Message( std::string lowercase_name = ToLowercaseWithoutUnderscores(message->field(i)->name()); if (name_to_field.find(lowercase_name) != name_to_field.end()) { - AddError(message->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(message->full_name(), proto.field(i), + DescriptorPool::ErrorCollector::NAME, "The JSON camel-case name of field \"" + - message->field(i)->name() + "\" conflicts with field \"" + - name_to_field[lowercase_name]->name() + "\". This is not " + - "allowed in proto3."); + message->field(i)->name() + "\" conflicts with field \"" + + name_to_field[lowercase_name]->name() + "\". This is not " + + "allowed in proto3."); } else { name_to_field[lowercase_name] = message->field(i); } } } -void DescriptorBuilder::ValidateProto3Field( - FieldDescriptor* field, const FieldDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Field(FieldDescriptor* field, + const FieldDescriptorProto& proto) { if (field->is_extension() && !AllowedExtendeeInProto3(field->containing_type()->full_name())) { AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + DescriptorPool::ErrorCollector::EXTENDEE, "Extensions in proto3 are only allowed for defining options."); } if (field->is_required()) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Required fields are not allowed in proto3."); } if (field->has_default_value()) { - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::OTHER, - "Explicit default values are not allowed in proto3."); + AddError(field->full_name(), proto, + DescriptorPool::ErrorCollector::DEFAULT_VALUE, + "Explicit default values are not allowed in proto3."); } if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && field->enum_type() && field->enum_type()->file()->syntax() != FileDescriptor::SYNTAX_PROTO3) { // Proto3 messages can only use Proto3 enum types; otherwise we can't // guarantee that the default value is zero. - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Enum type \"" + field->enum_type()->full_name() + - "\" is not a proto3 enum, but is used in \"" + - field->containing_type()->full_name() + - "\" which is a proto3 message type."); + "\" is not a proto3 enum, but is used in \"" + + field->containing_type()->full_name() + + "\" which is a proto3 message type."); } if (field->type() == FieldDescriptor::TYPE_GROUP) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Groups are not supported in proto3 syntax."); } } -void DescriptorBuilder::ValidateProto3Enum( - EnumDescriptor* enm, const EnumDescriptorProto& proto) { +void DescriptorBuilder::ValidateProto3Enum(EnumDescriptor* enm, + const EnumDescriptorProto& proto) { if (enm->value_count() > 0 && enm->value(0)->number() != 0) { - AddError( - enm->full_name(), proto, DescriptorPool::ErrorCollector::OTHER, - "The first enum value must be zero in proto3."); + AddError(enm->full_name(), proto.value(0), + DescriptorPool::ErrorCollector::NUMBER, + "The first enum value must be zero in proto3."); } } @@ -5955,9 +5896,9 @@ void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field); const int64 max_extension_range = - static_cast<int64>(message->options().message_set_wire_format() ? - kint32max : - FieldDescriptor::kMaxNumber); + static_cast<int64>(message->options().message_set_wire_format() + ? kint32max + : FieldDescriptor::kMaxNumber); for (int i = 0; i < message->extension_range_count(); ++i) { if (message->extension_range(i)->end > max_extension_range + 1) { AddError( @@ -5970,16 +5911,15 @@ void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, } -void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, - const FieldDescriptorProto& proto) { +void DescriptorBuilder::ValidateFieldOptions( + FieldDescriptor* field, const FieldDescriptorProto& proto) { if (pool_->lazily_build_dependencies_ && (!field || !field->message_type())) { return; } // Only message type fields may be lazy. if (field->options().lazy()) { if (field->type() != FieldDescriptor::TYPE_MESSAGE) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "[lazy = true] can only be specified for submessage fields."); } } @@ -5987,16 +5927,15 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, // Only repeated primitive fields may be packed. if (field->options().packed() && !field->is_packable()) { AddError( - field->full_name(), proto, - DescriptorPool::ErrorCollector::TYPE, - "[packed = true] can only be specified for repeated primitive fields."); + field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "[packed = true] can only be specified for repeated primitive fields."); } // Note: Default instance may not yet be initialized here, so we have to // avoid reading from it. if (field->containing_type_ != NULL && &field->containing_type()->options() != - &MessageOptions::default_instance() && + &MessageOptions::default_instance() && field->containing_type()->options().message_set_wire_format()) { if (field->is_extension()) { if (!field->is_optional() || @@ -6006,15 +5945,13 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, "Extensions of MessageSets must be optional messages."); } } else { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "MessageSets cannot have fields, only extensions."); } } // Lite extensions can only be of Lite types. - if (IsLite(field->file()) && - field->containing_type_ != NULL && + if (IsLite(field->file()) && field->containing_type_ != NULL && !IsLite(field->containing_type()->file())) { AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::EXTENDEE, @@ -6026,8 +5963,7 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, // Validate map types. if (field->is_map()) { if (!ValidateMapEntry(field, proto)) { - AddError(field->full_name(), proto, - DescriptorPool::ErrorCollector::OTHER, + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "map_entry should not be set explicitly. Use map<KeyType, " "ValueType> instead."); } @@ -6070,13 +6006,8 @@ void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm, "'option allow_alias = true;' to the enum definition."; if (!enm->options().allow_alias()) { // Generate error if duplicated enum values are explicitly disallowed. - AddError(enm->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, - error); - } else { - // Generate warning if duplicated values are found but the option - // isn't set. - GOOGLE_LOG(ERROR) << error; + AddError(enm->full_name(), proto.value(i), + DescriptorPool::ErrorCollector::NUMBER, error); } } else { used_values[enum_value->number()] = enum_value->full_name(); @@ -6090,13 +6021,12 @@ void DescriptorBuilder::ValidateEnumValueOptions( const EnumValueDescriptorProto& /* proto */) { // Nothing to do so far. } -void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service, - const ServiceDescriptorProto& proto) { +void DescriptorBuilder::ValidateServiceOptions( + ServiceDescriptor* service, const ServiceDescriptorProto& proto) { if (IsLite(service->file()) && (service->file()->options().cc_generic_services() || service->file()->options().java_generic_services())) { - AddError(service->full_name(), proto, - DescriptorPool::ErrorCollector::NAME, + AddError(service->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Files with optimize_for = LITE_RUNTIME cannot define services " "unless you set both options cc_generic_services and " "java_generic_sevices to false."); @@ -6105,16 +6035,16 @@ void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service, VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method); } -void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* /* method */, - const MethodDescriptorProto& /* proto */) { +void DescriptorBuilder::ValidateMethodOptions( + MethodDescriptor* /* method */, const MethodDescriptorProto& /* proto */) { // Nothing to do so far. } bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, const FieldDescriptorProto& proto) { const Descriptor* message = field->message_type(); - if (// Must not contain extensions, extension range or nested message or - // enums + if ( // Must not contain extensions, extension range or nested message or + // enums message->extension_count() != 0 || field->label() != FieldDescriptor::LABEL_REPEATED || message->extension_range_count() != 0 || @@ -6142,9 +6072,8 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, // Check key types are legal. switch (key->type()) { case FieldDescriptor::TYPE_ENUM: - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, - "Key in map fields cannot be enum types."); + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "Key in map fields cannot be enum types."); break; case FieldDescriptor::TYPE_FLOAT: case FieldDescriptor::TYPE_DOUBLE: @@ -6169,15 +6098,14 @@ bool DescriptorBuilder::ValidateMapEntry(FieldDescriptor* field, case FieldDescriptor::TYPE_SFIXED64: // Legal cases break; - // Do not add a default, so that the compiler will complain when new types - // are added. + // Do not add a default, so that the compiler will complain when new types + // are added. } if (value->type() == FieldDescriptor::TYPE_ENUM) { if (value->enum_type()->value(0)->number() != 0) { - AddError( - field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, - "Enum value in map must define 0 as the first value."); + AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, + "Enum value in map must define 0 as the first value."); } } @@ -6197,7 +6125,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + nested->name() + - " conflicts with an existing nested message type."); + " conflicts with an existing nested message type."); } } // Recursively test on the nested types. @@ -6212,7 +6140,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing field."); + " conflicts with an existing field."); } } // Check for conflicted enum names. @@ -6224,7 +6152,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing enum type."); + " conflicts with an existing enum type."); } } // Check for conflicted oneof names. @@ -6236,7 +6164,7 @@ void DescriptorBuilder::DetectMapConflicts(const Descriptor* message, AddError(message->full_name(), proto, DescriptorPool::ErrorCollector::NAME, "Expanded map entry type " + iter->second->name() + - " conflicts with an existing oneof type."); + " conflicts with an existing oneof type."); } } } @@ -6264,7 +6192,7 @@ void DescriptorBuilder::ValidateJSType(FieldDescriptor* field, AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, "Illegal jstype for int64, uint64, sint64, fixed64 " "or sfixed64 field: " + - FieldOptions_JSType_descriptor()->value(jstype)->name()); + FieldOptions_JSType_descriptor()->value(jstype)->name()); break; // No other types permit a jstype option. @@ -6281,12 +6209,12 @@ void DescriptorBuilder::ValidateJSType(FieldDescriptor* field, // ------------------------------------------------------------------- DescriptorBuilder::OptionInterpreter::OptionInterpreter( - DescriptorBuilder* builder) : builder_(builder) { + DescriptorBuilder* builder) + : builder_(builder) { GOOGLE_CHECK(builder_); } -DescriptorBuilder::OptionInterpreter::~OptionInterpreter() { -} +DescriptorBuilder::OptionInterpreter::~OptionInterpreter() {} bool DescriptorBuilder::OptionInterpreter::InterpretOptions( OptionsToInterpret* options_to_interpret) { @@ -6311,13 +6239,14 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( // Find the uninterpreted_option field in the original options. const FieldDescriptor* original_uninterpreted_options_field = - original_options->GetDescriptor()-> - FindFieldByName("uninterpreted_option"); + original_options->GetDescriptor()->FindFieldByName( + "uninterpreted_option"); GOOGLE_CHECK(original_uninterpreted_options_field != NULL) << "No field named \"uninterpreted_option\" in the Options proto."; - const int num_uninterpreted_options = original_options->GetReflection()-> - FieldSize(*original_options, original_uninterpreted_options_field); + const int num_uninterpreted_options = + original_options->GetReflection()->FieldSize( + *original_options, original_uninterpreted_options_field); for (int i = 0; i < num_uninterpreted_options; ++i) { src_path.push_back(i); uninterpreted_option_ = down_cast<const UninterpretedOption*>( @@ -6357,8 +6286,11 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptions( DescriptorPool::ErrorCollector::OTHER, "Some options could not be correctly parsed using the proto " "descriptors compiled into this binary.\n" - "Unparsed options: " + unparsed_options->ShortDebugString() + "\n" - "Parsing attempt: " + options->ShortDebugString()); + "Unparsed options: " + + unparsed_options->ShortDebugString() + + "\n" + "Parsing attempt: " + + options->ShortDebugString()); // Restore the unparsed options. options->GetReflection()->Swap(unparsed_options.get(), options); } @@ -6376,8 +6308,9 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( return AddNameError("Option must have a name."); } if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") { - return AddNameError("Option must not use reserved name " - "\"uninterpreted_option\"."); + return AddNameError( + "Option must not use reserved name " + "\"uninterpreted_option\"."); } const Descriptor* options_descriptor = NULL; @@ -6392,7 +6325,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // FindSymbol() because files that use custom options only need to depend on // the file that defines the option, not descriptor.proto itself. Symbol symbol = builder_->FindSymbolNotEnforcingDeps( - options->GetDescriptor()->full_name()); + options->GetDescriptor()->full_name()); if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) { options_descriptor = symbol.descriptor; } else { @@ -6428,8 +6361,8 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows // relative lookups, and 2) because we're already holding the pool's // mutex, and the latter method locks it again. - symbol = builder_->LookupSymbol(name_part, - options_to_interpret_->name_scope); + symbol = + builder_->LookupSymbol(name_part, options_to_interpret_->name_scope); if (!symbol.IsNull() && symbol.type == Symbol::FIELD) { field = symbol.field_descriptor; } @@ -6460,8 +6393,9 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( "\") to start from the outermost scope."); } else { return AddNameError( - "Option \"" + debug_msg_name + "\" unknown. Ensure that your proto" + - " definition file imports the proto which defines the option."); + "Option \"" + debug_msg_name + + "\" unknown. Ensure that your proto" + + " definition file imports the proto which defines the option."); } } else if (field->containing_type() != descriptor) { if (get_is_placeholder(field->containing_type())) { @@ -6510,10 +6444,10 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( // known will populate them correctly. // First see if the option is already set. - if (!field->is_repeated() && !ExamineIfOptionIsSet( - intermediate_fields.begin(), - intermediate_fields.end(), - field, debug_msg_name, + if (!field->is_repeated() && + !ExamineIfOptionIsSet( + intermediate_fields.begin(), intermediate_fields.end(), field, + debug_msg_name, options->GetReflection()->GetUnknownFields(*options))) { return false; // ExamineIfOptionIsSet() already added the error. } @@ -6545,8 +6479,8 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( } case FieldDescriptor::TYPE_GROUP: { - parent_unknown_fields->AddGroup((*iter)->number()) - ->MergeFrom(*unknown_fields); + parent_unknown_fields->AddGroup((*iter)->number()) + ->MergeFrom(*unknown_fields); break; } @@ -6669,11 +6603,12 @@ void DescriptorBuilder::OptionInterpreter::UpdateSourceCodeInfo( void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting( const UninterpretedOption& uninterpreted_option, Message* options) { const FieldDescriptor* field = - options->GetDescriptor()->FindFieldByName("uninterpreted_option"); + options->GetDescriptor()->FindFieldByName("uninterpreted_option"); GOOGLE_CHECK(field != NULL); - options->GetReflection()->AddMessage(options, field) - ->CopyFrom(uninterpreted_option); + options->GetReflection() + ->AddMessage(options, field) + ->CopyFrom(uninterpreted_option); } bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( @@ -6710,8 +6645,8 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( if (intermediate_unknown_fields.ParseFromString( unknown_field->length_delimited()) && !ExamineIfOptionIsSet(intermediate_fields_iter + 1, - intermediate_fields_end, - innermost_field, debug_msg_name, + intermediate_fields_end, innermost_field, + debug_msg_name, intermediate_unknown_fields)) { return false; // Error already added. } @@ -6721,9 +6656,8 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( case FieldDescriptor::TYPE_GROUP: if (unknown_field->type() == UnknownField::TYPE_GROUP) { if (!ExamineIfOptionIsSet(intermediate_fields_iter + 1, - intermediate_fields_end, - innermost_field, debug_msg_name, - unknown_field->group())) { + intermediate_fields_end, innermost_field, + debug_msg_name, unknown_field->group())) { return false; // Error already added. } } @@ -6739,11 +6673,9 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( } bool DescriptorBuilder::OptionInterpreter::SetOptionValue( - const FieldDescriptor* option_field, - UnknownFieldSet* unknown_fields) { + const FieldDescriptor* option_field, UnknownFieldSet* unknown_fields) { // We switch on the CppType to validate. switch (option_field->cpp_type()) { - case FieldDescriptor::CPPTYPE_INT32: if (uninterpreted_option_->has_positive_int_value()) { if (uninterpreted_option_->positive_int_value() > @@ -6803,8 +6735,10 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( option_field->type(), unknown_fields); } } else { - return AddValueError("Value must be non-negative integer for uint32 " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be non-negative integer for uint32 " + "option \"" + + option_field->full_name() + "\"."); } break; @@ -6814,8 +6748,10 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( uninterpreted_option_->positive_int_value(), option_field->type(), unknown_fields); } else { - return AddValueError("Value must be non-negative integer for uint64 " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be non-negative integer for uint64 " + "option \"" + + option_field->full_name() + "\"."); } break; @@ -6856,24 +6792,30 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( case FieldDescriptor::CPPTYPE_BOOL: uint64 value; if (!uninterpreted_option_->has_identifier_value()) { - return AddValueError("Value must be identifier for boolean option " - "\"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be identifier for boolean option " + "\"" + + option_field->full_name() + "\"."); } if (uninterpreted_option_->identifier_value() == "true") { value = 1; } else if (uninterpreted_option_->identifier_value() == "false") { value = 0; } else { - return AddValueError("Value must be \"true\" or \"false\" for boolean " - "option \"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be \"true\" or \"false\" for boolean " + "option \"" + + option_field->full_name() + "\"."); } unknown_fields->AddVarint(option_field->number(), value); break; case FieldDescriptor::CPPTYPE_ENUM: { if (!uninterpreted_option_->has_identifier_value()) { - return AddValueError("Value must be identifier for enum-valued option " - "\"" + option_field->full_name() + "\"."); + return AddValueError( + "Value must be identifier for enum-valued option " + "\"" + + option_field->full_name() + "\"."); } const EnumDescriptor* enum_type = option_field->enum_type(); const std::string& value_name = uninterpreted_option_->identifier_value(); @@ -6892,10 +6834,11 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( // DescriptorPool::FindEnumValueByName() because we're already holding // the pool's mutex, and the latter method locks it again. Symbol symbol = - builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); + builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) { if (symbol.enum_value_descriptor->type() != enum_type) { - return AddValueError("Enum type \"" + enum_type->full_name() + + return AddValueError( + "Enum type \"" + enum_type->full_name() + "\" has no value named \"" + value_name + "\" for option \"" + option_field->full_name() + "\". This appears to be a value from a sibling type."); @@ -6912,13 +6855,16 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( if (enum_value == NULL) { return AddValueError("Enum type \"" + option_field->enum_type()->full_name() + - "\" has no value named \"" + value_name + "\" for " - "option \"" + option_field->full_name() + "\"."); + "\" has no value named \"" + value_name + + "\" for " + "option \"" + + option_field->full_name() + "\"."); } else { // Sign-extension is not a problem, since we cast directly from int32 to // uint64, without first going through uint32. - unknown_fields->AddVarint(option_field->number(), - static_cast<uint64>(static_cast<int64>(enum_value->number()))); + unknown_fields->AddVarint( + option_field->number(), + static_cast<uint64>(static_cast<int64>(enum_value->number()))); } break; } @@ -6932,7 +6878,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue( } // The string has already been unquoted and unescaped by the parser. unknown_fields->AddLengthDelimited(option_field->number(), - uninterpreted_option_->string_value()); + uninterpreted_option_->string_value()); break; case FieldDescriptor::CPPTYPE_MESSAGE: @@ -6954,8 +6900,8 @@ class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder const std::string& name) const override { assert_mutex_held(builder_->pool_); const Descriptor* descriptor = message->GetDescriptor(); - Symbol result = builder_->LookupSymbolNoPlaceholder( - name, descriptor->full_name()); + Symbol result = + builder_->LookupSymbolNoPlaceholder(name, descriptor->full_name()); if (result.type == Symbol::FIELD && result.field_descriptor->is_extension()) { return result.field_descriptor; @@ -7007,16 +6953,16 @@ class AggregateErrorCollector : public io::ErrorCollector { // option_field, parse the supplied text-format string into this // message, and serialize the resulting message to produce the value. bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( - const FieldDescriptor* option_field, - UnknownFieldSet* unknown_fields) { + const FieldDescriptor* option_field, UnknownFieldSet* unknown_fields) { if (!uninterpreted_option_->has_aggregate_value()) { return AddValueError("Option \"" + option_field->full_name() + "\" is a message. To set the entire message, use " - "syntax like \"" + option_field->name() + + "syntax like \"" + + option_field->name() + " = { <proto text format> }\". " "To set fields within it, use " - "syntax like \"" + option_field->name() + - ".foo = value\"."); + "syntax like \"" + + option_field->name() + ".foo = value\"."); } const Descriptor* type = option_field->message_type(); @@ -7041,7 +6987,7 @@ bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( if (option_field->type() == FieldDescriptor::TYPE_MESSAGE) { unknown_fields->AddLengthDelimited(option_field->number(), serial); } else { - GOOGLE_CHECK_EQ(option_field->type(), FieldDescriptor::TYPE_GROUP); + GOOGLE_CHECK_EQ(option_field->type(), FieldDescriptor::TYPE_GROUP); UnknownFieldSet* group = unknown_fields->AddGroup(option_field->number()); group->ParseFromString(serial); } @@ -7049,12 +6995,13 @@ bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( } } -void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetInt32( + int number, int32 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_INT32: unknown_fields->AddVarint(number, - static_cast<uint64>(static_cast<int64>(value))); + static_cast<uint64>(static_cast<int64>(value))); break; case FieldDescriptor::TYPE_SFIXED32: @@ -7072,8 +7019,9 @@ void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, } } -void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetInt64( + int number, int64 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_INT64: unknown_fields->AddVarint(number, static_cast<uint64>(value)); @@ -7094,8 +7042,9 @@ void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, } } -void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetUInt32( + int number, uint32 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_UINT32: unknown_fields->AddVarint(number, static_cast<uint64>(value)); @@ -7111,8 +7060,9 @@ void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, } } -void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value, - FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { +void DescriptorBuilder::OptionInterpreter::SetUInt64( + int number, uint64 value, FieldDescriptor::Type type, + UnknownFieldSet* unknown_fields) { switch (type) { case FieldDescriptor::TYPE_UINT64: unknown_fields->AddVarint(number, value); @@ -7142,15 +7092,15 @@ void DescriptorBuilder::LogUnusedDependency(const FileDescriptorProto& proto, annotation_extensions.insert("google.protobuf.ServiceOptions"); annotation_extensions.insert("google.protobuf.MethodOptions"); annotation_extensions.insert("google.protobuf.StreamOptions"); - for (std::set<const FileDescriptor*>::const_iterator - it = unused_dependency_.begin(); + for (std::set<const FileDescriptor*>::const_iterator it = + unused_dependency_.begin(); it != unused_dependency_.end(); ++it) { // Do not log warnings for proto files which extend annotations. int i; - for (i = 0 ; i < (*it)->extension_count(); ++i) { + for (i = 0; i < (*it)->extension_count(); ++i) { if (annotation_extensions.find( - (*it)->extension(i)->containing_type()->full_name()) - != annotation_extensions.end()) { + (*it)->extension(i)->containing_type()->full_name()) != + annotation_extensions.end()) { break; } } @@ -7158,7 +7108,7 @@ void DescriptorBuilder::LogUnusedDependency(const FileDescriptorProto& proto, if (i == (*it)->extension_count()) { std::string error_message = "Import " + (*it)->name() + " but not used."; - AddWarning((*it)->name(), proto, DescriptorPool::ErrorCollector::OTHER, + AddWarning((*it)->name(), proto, DescriptorPool::ErrorCollector::IMPORT, error_message); } } diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index add275c55e..424bd84c19 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -174,7 +174,8 @@ struct DebugStringOptions { DebugStringOptions() : include_comments(false), elide_group_body(false), - elide_oneof_body(false) {} + elide_oneof_body(false) { + } }; // A class to handle the simplest cases of a lazily linked descriptor @@ -523,62 +524,62 @@ class PROTOBUF_EXPORT FieldDescriptor { // Identifies a field type. 0 is reserved for errors. The order is weird // for historical reasons. Types 12 and up are new in proto2. enum Type { - TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. - TYPE_FLOAT = 2, // float, exactly four bytes on the wire. - TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT64 if negative - // values are likely. - TYPE_UINT64 = 4, // uint64, varint on the wire. - TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT32 if negative - // values are likely. - TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. - TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. - TYPE_BOOL = 8, // bool, varint on the wire. - TYPE_STRING = 9, // UTF-8 text. - TYPE_GROUP = 10, // Tag-delimited message. Deprecated. - TYPE_MESSAGE = 11, // Length-delimited message. - - TYPE_BYTES = 12, // Arbitrary byte array. - TYPE_UINT32 = 13, // uint32, varint on the wire - TYPE_ENUM = 14, // Enum, varint on the wire - TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire - TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire - TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire - TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire - - MAX_TYPE = 18, // Constant useful for defining lookup tables - // indexed by Type. + TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. + TYPE_FLOAT = 2, // float, exactly four bytes on the wire. + TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers + // take 10 bytes. Use TYPE_SINT64 if negative + // values are likely. + TYPE_UINT64 = 4, // uint64, varint on the wire. + TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers + // take 10 bytes. Use TYPE_SINT32 if negative + // values are likely. + TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. + TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. + TYPE_BOOL = 8, // bool, varint on the wire. + TYPE_STRING = 9, // UTF-8 text. + TYPE_GROUP = 10, // Tag-delimited message. Deprecated. + TYPE_MESSAGE = 11, // Length-delimited message. + + TYPE_BYTES = 12, // Arbitrary byte array. + TYPE_UINT32 = 13, // uint32, varint on the wire + TYPE_ENUM = 14, // Enum, varint on the wire + TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire + TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire + TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire + TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire + + MAX_TYPE = 18, // Constant useful for defining lookup tables + // indexed by Type. }; // Specifies the C++ data type used to represent the field. There is a // fixed mapping from Type to CppType where each Type maps to exactly one // CppType. 0 is reserved for errors. enum CppType { - CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 - CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 - CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 - CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 - CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE - CPPTYPE_FLOAT = 6, // TYPE_FLOAT - CPPTYPE_BOOL = 7, // TYPE_BOOL - CPPTYPE_ENUM = 8, // TYPE_ENUM - CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES - CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP - - MAX_CPPTYPE = 10, // Constant useful for defining lookup tables - // indexed by CppType. + CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 + CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 + CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 + CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 + CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE + CPPTYPE_FLOAT = 6, // TYPE_FLOAT + CPPTYPE_BOOL = 7, // TYPE_BOOL + CPPTYPE_ENUM = 8, // TYPE_ENUM + CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES + CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP + + MAX_CPPTYPE = 10, // Constant useful for defining lookup tables + // indexed by CppType. }; // Identifies whether the field is optional, required, or repeated. 0 is // reserved for errors. enum Label { - LABEL_OPTIONAL = 1, // optional - LABEL_REQUIRED = 2, // required - LABEL_REPEATED = 3, // repeated + LABEL_OPTIONAL = 1, // optional + LABEL_REQUIRED = 2, // required + LABEL_REPEATED = 3, // repeated - MAX_LABEL = 3, // Constant useful for defining lookup tables - // indexed by Label. + MAX_LABEL = 3, // Constant useful for defining lookup tables + // indexed by Label. }; // Valid field numbers are positive integers up to kMaxNumber. @@ -589,14 +590,14 @@ class PROTOBUF_EXPORT FieldDescriptor { static const int kFirstReservedNumber = 19000; // Last field number reserved for the protocol buffer library implementation. // Users may not declare fields that use reserved numbers. - static const int kLastReservedNumber = 19999; + static const int kLastReservedNumber = 19999; const std::string& name() const; // Name of this field within the message. const std::string& full_name() const; // Fully-qualified name of the field. const std::string& json_name() const; // JSON name of this field. - const FileDescriptor* file() const;// File in which this field was defined. - bool is_extension() const; // Is this an extension field? - int number() const; // Declared tag number. + const FileDescriptor* file() const; // File in which this field was defined. + bool is_extension() const; // Is this an extension field? + int number() const; // Declared tag number. // Same as name() except converted to lower-case. This (and especially the // FindFieldByLowercaseName() method) can be useful when parsing formats @@ -623,15 +624,15 @@ class PROTOBUF_EXPORT FieldDescriptor { const char* cpp_type_name() const; // Name of the C++ type. Label label() const; // optional/required/repeated - bool is_required() const; // shorthand for label() == LABEL_REQUIRED - bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL - bool is_repeated() const; // shorthand for label() == LABEL_REPEATED - bool is_packable() const; // shorthand for is_repeated() && - // IsTypePackable(type()) - bool is_packed() const; // shorthand for is_packable() && - // options().packed() - bool is_map() const; // shorthand for type() == TYPE_MESSAGE && - // message_type()->options().map_entry() + bool is_required() const; // shorthand for label() == LABEL_REQUIRED + bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL + bool is_repeated() const; // shorthand for label() == LABEL_REPEATED + bool is_packable() const; // shorthand for is_repeated() && + // IsTypePackable(type()) + bool is_packed() const; // shorthand for is_packable() && + // options().packed() + bool is_map() const; // shorthand for type() == TYPE_MESSAGE && + // message_type()->options().map_entry() // Index of this field within the message's field array, or the file or // extension scope's extensions array. @@ -798,13 +799,13 @@ class PROTOBUF_EXPORT FieldDescriptor { // descriptor.cc and update them to initialize the field. union { - int32 default_value_int32_; - int64 default_value_int64_; + int32 default_value_int32_; + int64 default_value_int64_; uint32 default_value_uint32_; uint64 default_value_uint64_; - float default_value_float_; + float default_value_float_; double default_value_double_; - bool default_value_bool_; + bool default_value_bool_; mutable const EnumValueDescriptor* default_value_enum_; const std::string* default_value_string_; @@ -812,11 +813,11 @@ class PROTOBUF_EXPORT FieldDescriptor { static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; - static const char * const kTypeToName[MAX_TYPE + 1]; + static const char* const kTypeToName[MAX_TYPE + 1]; - static const char * const kCppTypeToName[MAX_CPPTYPE + 1]; + static const char* const kCppTypeToName[MAX_CPPTYPE + 1]; - static const char * const kLabelToName[MAX_LABEL + 1]; + static const char* const kLabelToName[MAX_LABEL + 1]; // Must be constructed using DescriptorPool. FieldDescriptor() {} @@ -977,8 +978,8 @@ class PROTOBUF_EXPORT EnumDescriptor { bool IsReservedNumber(int number) const; // Returns NULL if no reserved range contains the given number. - const EnumDescriptor::ReservedRange* - FindReservedRangeContainingNumber(int number) const; + const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber( + int number) const; // The number of reserved field names in this message type. int reserved_name_count() const; @@ -1011,9 +1012,8 @@ class PROTOBUF_EXPORT EnumDescriptor { // This is private but is used by GeneratedMessageReflection (which is // friended below) to return a valid EnumValueDescriptor from GetEnum() when // this feature is enabled. - const EnumValueDescriptor* - FindValueByNumberCreatingIfUnknown(int number) const; - + const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown( + int number) const; // See Descriptor::DebugString(). void DebugString(int depth, std::string* contents, @@ -1068,8 +1068,8 @@ class PROTOBUF_EXPORT EnumValueDescriptor { typedef EnumValueDescriptorProto Proto; const std::string& name() const; // Name of this enum constant. - int index() const; // Index within the enums's Descriptor. - int number() const; // Numeric value of this enum constant. + int index() const; // Index within the enums's Descriptor. + int number() const; // Numeric value of this enum constant. // The full_name of an enum value is a sibling symbol of the enum type. // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually @@ -1381,8 +1381,8 @@ class PROTOBUF_EXPORT FileDescriptor { // Syntax of this file. enum Syntax { SYNTAX_UNKNOWN = 0, - SYNTAX_PROTO2 = 2, - SYNTAX_PROTO3 = 3, + SYNTAX_PROTO2 = 2, + SYNTAX_PROTO3 = 3, }; Syntax syntax() const; static const char* SyntaxName(Syntax syntax); @@ -1618,16 +1618,17 @@ class PROTOBUF_EXPORT DescriptorPool { // This is useful e.g. for mapping the error back to an exact location // in a .proto file. enum ErrorLocation { - NAME, // the symbol name, or the package name for files - NUMBER, // field or extension range number - TYPE, // field type - EXTENDEE, // field extendee - DEFAULT_VALUE, // field default value - INPUT_TYPE, // method input type - OUTPUT_TYPE, // method output type - OPTION_NAME, // name in assignment - OPTION_VALUE, // value in option assignment - OTHER // some other problem + NAME, // the symbol name, or the package name for files + NUMBER, // field or extension range number + TYPE, // field type + EXTENDEE, // field extendee + DEFAULT_VALUE, // field default value + INPUT_TYPE, // method input type + OUTPUT_TYPE, // method output type + OPTION_NAME, // name in assignment + OPTION_VALUE, // value in option assignment + IMPORT, // import error + OTHER // some other problem }; // Reports an error in the FileDescriptorProto. Use this function if the @@ -1665,8 +1666,7 @@ class PROTOBUF_EXPORT DescriptorPool { // Same as BuildFile() except errors are sent to the given ErrorCollector. const FileDescriptor* BuildFileCollectingErrors( - const FileDescriptorProto& proto, - ErrorCollector* error_collector); + const FileDescriptorProto& proto, ErrorCollector* error_collector); // By default, it is an error if a FileDescriptorProto contains references // to types or other files that are not found in the DescriptorPool (or its @@ -1721,8 +1721,8 @@ class PROTOBUF_EXPORT DescriptorPool { // Called by generated classes at init time to add their descriptors to // generated_pool. Do NOT call this in your own code! filename must be a // permanent string (e.g. a string literal). - static void InternalAddGeneratedFile( - const void* encoded_file_descriptor, int size); + static void InternalAddGeneratedFile(const void* encoded_file_descriptor, + int size); // Disallow [enforce_utf8 = false] in .proto files. void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; } @@ -1799,7 +1799,7 @@ class PROTOBUF_EXPORT DescriptorPool { // fallback_database_. Declared const because it is called by (semantically) // const methods. const FileDescriptor* BuildFileFromDatabase( - const FileDescriptorProto& proto) const; + const FileDescriptorProto& proto) const; // Helper for when lazily_build_dependencies_ is set, can look up a symbol // after the file's descriptor is built, and can build the file where that @@ -1886,8 +1886,7 @@ PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int) PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int) PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, const Descriptor::ExtensionRange*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, - const FieldDescriptor*) +PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*) PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int) PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, @@ -1914,13 +1913,13 @@ PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 ) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 ) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32, int32) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64, int64) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float ) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float, float) PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool ) +PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool) PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name) @@ -2151,14 +2150,11 @@ inline const FileDescriptor* FileDescriptor::public_dependency( return dependency(public_dependencies_[index]); } -inline const FileDescriptor* FileDescriptor::weak_dependency( - int index) const { +inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const { return dependency(weak_dependencies_[index]); } -inline FileDescriptor::Syntax FileDescriptor::syntax() const { - return syntax_; -} +inline FileDescriptor::Syntax FileDescriptor::syntax() const { return syntax_; } // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array // of pointers rather than the usual array of objects. diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 06d6ea0a81..f85087e624 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -564,36 +564,6 @@ PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Generated {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsGeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto}, { &scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base,}}; -void InitDefaults_google_2fprotobuf_2fdescriptor_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto[27]; static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto[6]; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto = nullptr; @@ -1203,8 +1173,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto[] = "le.Protobuf.Reflection" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = { - false, InitDefaults_google_2fprotobuf_2fdescriptor_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", &assign_descriptors_table_google_2fprotobuf_2fdescriptor_2eproto, 6022, }; @@ -1212,6 +1181,33 @@ void AddDescriptors_google_2fprotobuf_2fdescriptor_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorSet_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_ExtensionRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_ReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ExtensionRangeOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumDescriptorProto_EnumReservedRange_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodDescriptorProto_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FileOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MessageOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_OneofOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValueOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ServiceOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_MethodOptions_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_NamePart_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UninterpretedOption_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_Location_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_Annotation_google_2fprotobuf_2fdescriptor_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_GeneratedCodeInfo_google_2fprotobuf_2fdescriptor_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, deps, 0); } @@ -1393,6 +1389,7 @@ void FileDescriptorSet::InitAsDefaultInstance() { } class FileDescriptorSet::HasBitSetters { public: + using HasBits = decltype(std::declval<FileDescriptorSet>()._has_bits_); }; #if !defined(_MSC_VER) || _MSC_VER >= 1900 @@ -1463,35 +1460,43 @@ void FileDescriptorSet::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FileDescriptorSet::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.FileDescriptorProto file = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_file(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileDescriptorSet::MergePartialFromCodedStream( @@ -1695,22 +1700,23 @@ void FileDescriptorProto::InitAsDefaultInstance() { } class FileDescriptorProto::HasBitSetters { public: - static void set_has_name(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<FileDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_package(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_package(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } static const PROTOBUF_NAMESPACE_ID::FileOptions& options(const FileDescriptorProto* msg); - static void set_has_options(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } static const PROTOBUF_NAMESPACE_ID::SourceCodeInfo& source_code_info(const FileDescriptorProto* msg); - static void set_has_source_code_info(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_source_code_info(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_syntax(FileDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_syntax(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; @@ -1901,138 +1907,160 @@ void FileDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FileDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.FileDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string package = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_package(), ptr, ctx, "google.protobuf.FileDescriptorProto.package"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated string dependency = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_dependency(), ptr, ctx, "google.protobuf.FileDescriptorProto.dependency"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } // repeated .google.protobuf.DescriptorProto message_type = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_message_type(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 34 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 34) break; + ptr += 1; + } break; } // repeated .google.protobuf.EnumDescriptorProto enum_type = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_enum_type(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 42 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 42) break; + ptr += 1; + } break; } // repeated .google.protobuf.ServiceDescriptorProto service = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_service(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 50 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 50) break; + ptr += 1; + } break; } // repeated .google.protobuf.FieldDescriptorProto extension = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_extension(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 58 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 58) break; + ptr += 1; + } break; } // optional .google.protobuf.FileOptions options = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.SourceCodeInfo source_code_info = 9; case 9: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 74) goto handle_unusual; ptr = ctx->ParseMessage(mutable_source_code_info(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated int32 public_dependency = 10; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80) { - do { + while (true) { add_public_dependency(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 80 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 80) break; + ptr += 1; + } break; } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 82) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_public_dependency(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated int32 weak_dependency = 11; case 11: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 88) { - do { + while (true) { add_weak_dependency(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 88 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 88) break; + ptr += 1; + } break; } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 90) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_weak_dependency(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string syntax = 12; case 12: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 98) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_syntax(), ptr, ctx, "google.protobuf.FileDescriptorProto.syntax"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileDescriptorProto::MergePartialFromCodedStream( @@ -2705,15 +2733,16 @@ void DescriptorProto_ExtensionRange::InitAsDefaultInstance() { } class DescriptorProto_ExtensionRange::HasBitSetters { public: - static void set_has_start(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + using HasBits = decltype(std::declval<DescriptorProto_ExtensionRange>()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_end(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } static const PROTOBUF_NAMESPACE_ID::ExtensionRangeOptions& options(const DescriptorProto_ExtensionRange* msg); - static void set_has_options(DescriptorProto_ExtensionRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; @@ -2823,46 +2852,56 @@ void DescriptorProto_ExtensionRange::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* DescriptorProto_ExtensionRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional int32 start = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_start(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 end = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_end(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional .google.protobuf.ExtensionRangeOptions options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( @@ -2878,7 +2917,7 @@ bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( // optional int32 start = 1; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); + HasBitSetters::set_has_start(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); @@ -2891,7 +2930,7 @@ bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); + HasBitSetters::set_has_end(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); @@ -3135,11 +3174,12 @@ void DescriptorProto_ReservedRange::InitAsDefaultInstance() { } class DescriptorProto_ReservedRange::HasBitSetters { public: - static void set_has_start(DescriptorProto_ReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<DescriptorProto_ReservedRange>()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_end(DescriptorProto_ReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -3219,39 +3259,49 @@ void DescriptorProto_ReservedRange::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* DescriptorProto_ReservedRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional int32 start = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_start(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 end = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_end(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( @@ -3267,7 +3317,7 @@ bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( // optional int32 start = 1; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); + HasBitSetters::set_has_start(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); @@ -3280,7 +3330,7 @@ bool DescriptorProto_ReservedRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); + HasBitSetters::set_has_end(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); @@ -3488,12 +3538,13 @@ void DescriptorProto::InitAsDefaultInstance() { } class DescriptorProto::HasBitSetters { public: - static void set_has_name(DescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<DescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } static const PROTOBUF_NAMESPACE_ID::MessageOptions& options(const DescriptorProto* msg); - static void set_has_options(DescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -3636,119 +3687,143 @@ void DescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* DescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.DescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.FieldDescriptorProto field = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_field(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // repeated .google.protobuf.DescriptorProto nested_type = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_nested_type(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } // repeated .google.protobuf.EnumDescriptorProto enum_type = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_enum_type(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 34 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 34) break; + ptr += 1; + } break; } // repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_extension_range(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 42 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 42) break; + ptr += 1; + } break; } // repeated .google.protobuf.FieldDescriptorProto extension = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_extension(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 50 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 50) break; + ptr += 1; + } break; } // optional .google.protobuf.MessageOptions options = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_oneof_decl(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 66 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 66) break; + ptr += 1; + } break; } // repeated .google.protobuf.DescriptorProto.ReservedRange reserved_range = 9; case 9: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 74) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_reserved_range(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 74 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 74) break; + ptr += 1; + } break; } // repeated string reserved_name = 10; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 82) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_reserved_name(), ptr, ctx, "google.protobuf.DescriptorProto.reserved_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 82 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 82) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DescriptorProto::MergePartialFromCodedStream( @@ -4339,6 +4414,7 @@ void ExtensionRangeOptions::InitAsDefaultInstance() { } class ExtensionRangeOptions::HasBitSetters { public: + using HasBits = decltype(std::declval<ExtensionRangeOptions>()._has_bits_); }; #if !defined(_MSC_VER) || _MSC_VER >= 1900 @@ -4412,41 +4488,49 @@ void ExtensionRangeOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* ExtensionRangeOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ExtensionRangeOptions::MergePartialFromCodedStream( @@ -4669,36 +4753,37 @@ void FieldDescriptorProto::InitAsDefaultInstance() { } class FieldDescriptorProto::HasBitSetters { public: - static void set_has_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<FieldDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_number(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000040u; + static void set_has_number(HasBits* has_bits) { + (*has_bits)[0] |= 64u; } - static void set_has_label(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000100u; + static void set_has_label(HasBits* has_bits) { + (*has_bits)[0] |= 256u; } - static void set_has_type(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000200u; + static void set_has_type(HasBits* has_bits) { + (*has_bits)[0] |= 512u; } - static void set_has_type_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_type_name(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_extendee(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_extendee(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_default_value(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_default_value(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_oneof_index(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000080u; + static void set_has_oneof_index(HasBits* has_bits) { + (*has_bits)[0] |= 128u; } - static void set_has_json_name(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_json_name(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } static const PROTOBUF_NAMESPACE_ID::FieldOptions& options(const FieldDescriptorProto* msg); - static void set_has_options(FieldDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } }; @@ -4873,38 +4958,41 @@ void FieldDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string extendee = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_extendee(), ptr, ctx, "google.protobuf.FieldDescriptorProto.extendee"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional int32 number = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_number(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_number(&has_bits); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional .google.protobuf.FieldDescriptorProto.Label label = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 32) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Label_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(4, val, mutable_unknown_fields()); break; @@ -4916,7 +5004,7 @@ const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::FieldDescriptorProto_Type_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(5, val, mutable_unknown_fields()); break; @@ -4928,50 +5016,57 @@ const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_type_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.type_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string default_value = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_default_value(), ptr, ctx, "google.protobuf.FieldDescriptorProto.default_value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.FieldOptions options = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional int32 oneof_index = 9; case 9: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 72) goto handle_unusual; - set_oneof_index(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_oneof_index(&has_bits); + oneof_index_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional string json_name = 10; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 82) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_json_name(), ptr, ctx, "google.protobuf.FieldDescriptorProto.json_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldDescriptorProto::MergePartialFromCodedStream( @@ -5017,7 +5112,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional int32 number = 3; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_number(this); + HasBitSetters::set_has_number(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &number_))); @@ -5109,7 +5204,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream( // optional int32 oneof_index = 9; case 9: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (72 & 0xFF)) { - HasBitSetters::set_has_oneof_index(this); + HasBitSetters::set_has_oneof_index(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &oneof_index_))); @@ -5573,12 +5668,13 @@ void OneofDescriptorProto::InitAsDefaultInstance() { } class OneofDescriptorProto::HasBitSetters { public: - static void set_has_name(OneofDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<OneofDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } static const PROTOBUF_NAMESPACE_ID::OneofOptions& options(const OneofDescriptorProto* msg); - static void set_has_options(OneofDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -5689,39 +5785,47 @@ void OneofDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* OneofDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.OneofDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.OneofOptions options = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool OneofDescriptorProto::MergePartialFromCodedStream( @@ -5973,11 +6077,12 @@ void EnumDescriptorProto_EnumReservedRange::InitAsDefaultInstance() { } class EnumDescriptorProto_EnumReservedRange::HasBitSetters { public: - static void set_has_start(EnumDescriptorProto_EnumReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<EnumDescriptorProto_EnumReservedRange>()._has_bits_); + static void set_has_start(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_end(EnumDescriptorProto_EnumReservedRange* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -6057,39 +6162,49 @@ void EnumDescriptorProto_EnumReservedRange::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumDescriptorProto_EnumReservedRange::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional int32 start = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_start(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_start(&has_bits); + start_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 end = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_end(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( @@ -6105,7 +6220,7 @@ bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( // optional int32 start = 1; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_start(this); + HasBitSetters::set_has_start(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &start_))); @@ -6118,7 +6233,7 @@ bool EnumDescriptorProto_EnumReservedRange::MergePartialFromCodedStream( // optional int32 end = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_end(this); + HasBitSetters::set_has_end(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); @@ -6326,12 +6441,13 @@ void EnumDescriptorProto::InitAsDefaultInstance() { } class EnumDescriptorProto::HasBitSetters { public: - static void set_has_name(EnumDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<EnumDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } static const PROTOBUF_NAMESPACE_ID::EnumOptions& options(const EnumDescriptorProto* msg); - static void set_has_options(EnumDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -6454,69 +6570,83 @@ void EnumDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.EnumDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.EnumValueDescriptorProto value = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_value(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // optional .google.protobuf.EnumOptions options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.EnumDescriptorProto.EnumReservedRange reserved_range = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_reserved_range(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 34 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 34) break; + ptr += 1; + } break; } // repeated string reserved_name = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_reserved_name(), ptr, ctx, "google.protobuf.EnumDescriptorProto.reserved_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 42 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 42) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumDescriptorProto::MergePartialFromCodedStream( @@ -6899,15 +7029,16 @@ void EnumValueDescriptorProto::InitAsDefaultInstance() { } class EnumValueDescriptorProto::HasBitSetters { public: - static void set_has_name(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<EnumValueDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_number(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_number(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } static const PROTOBUF_NAMESPACE_ID::EnumValueOptions& options(const EnumValueDescriptorProto* msg); - static void set_has_options(EnumValueDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -7023,46 +7154,55 @@ void EnumValueDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumValueDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.EnumValueDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional int32 number = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_number(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_number(&has_bits); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional .google.protobuf.EnumValueOptions options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumValueDescriptorProto::MergePartialFromCodedStream( @@ -7093,7 +7233,7 @@ bool EnumValueDescriptorProto::MergePartialFromCodedStream( // optional int32 number = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_number(this); + HasBitSetters::set_has_number(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &number_))); @@ -7351,12 +7491,13 @@ void ServiceDescriptorProto::InitAsDefaultInstance() { } class ServiceDescriptorProto::HasBitSetters { public: - static void set_has_name(ServiceDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<ServiceDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } static const PROTOBUF_NAMESPACE_ID::ServiceOptions& options(const ServiceDescriptorProto* msg); - static void set_has_options(ServiceDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -7471,49 +7612,59 @@ void ServiceDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* ServiceDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.ServiceDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.MethodDescriptorProto method = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_method(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // optional .google.protobuf.ServiceOptions options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ServiceDescriptorProto::MergePartialFromCodedStream( @@ -7809,24 +7960,25 @@ void MethodDescriptorProto::InitAsDefaultInstance() { } class MethodDescriptorProto::HasBitSetters { public: - static void set_has_name(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<MethodDescriptorProto>()._has_bits_); + static void set_has_name(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_input_type(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_input_type(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_output_type(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_output_type(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } static const PROTOBUF_NAMESPACE_ID::MethodOptions& options(const MethodDescriptorProto* msg); - static void set_has_options(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_options(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_client_streaming(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_client_streaming(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_server_streaming(MethodDescriptorProto* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_server_streaming(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } }; @@ -7969,67 +8121,77 @@ void MethodDescriptorProto::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* MethodDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name(), ptr, ctx, "google.protobuf.MethodDescriptorProto.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string input_type = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_input_type(), ptr, ctx, "google.protobuf.MethodDescriptorProto.input_type"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string output_type = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_output_type(), ptr, ctx, "google.protobuf.MethodDescriptorProto.output_type"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.MethodOptions options = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ctx->ParseMessage(mutable_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional bool client_streaming = 5 [default = false]; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; - set_client_streaming(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_client_streaming(&has_bits); + client_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool server_streaming = 6 [default = false]; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 48) goto handle_unusual; - set_server_streaming(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_server_streaming(&has_bits); + server_streaming_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MethodDescriptorProto::MergePartialFromCodedStream( @@ -8101,7 +8263,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional bool client_streaming = 5 [default = false]; case 5: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_client_streaming(this); + HasBitSetters::set_has_client_streaming(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &client_streaming_))); @@ -8114,7 +8276,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream( // optional bool server_streaming = 6 [default = false]; case 6: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (48 & 0xFF)) { - HasBitSetters::set_has_server_streaming(this); + HasBitSetters::set_has_server_streaming(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &server_streaming_))); @@ -8442,65 +8604,66 @@ void FileOptions::InitAsDefaultInstance() { } class FileOptions::HasBitSetters { public: - static void set_has_java_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<FileOptions>()._has_bits_); + static void set_has_java_package(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_java_outer_classname(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_java_outer_classname(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_java_multiple_files(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000400u; + static void set_has_java_multiple_files(HasBits* has_bits) { + (*has_bits)[0] |= 1024u; } - static void set_has_java_generate_equals_and_hash(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000800u; + static void set_has_java_generate_equals_and_hash(HasBits* has_bits) { + (*has_bits)[0] |= 2048u; } - static void set_has_java_string_check_utf8(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00001000u; + static void set_has_java_string_check_utf8(HasBits* has_bits) { + (*has_bits)[0] |= 4096u; } - static void set_has_optimize_for(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00080000u; + static void set_has_optimize_for(HasBits* has_bits) { + (*has_bits)[0] |= 524288u; } - static void set_has_go_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_go_package(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_cc_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00002000u; + static void set_has_cc_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 8192u; } - static void set_has_java_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00004000u; + static void set_has_java_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 16384u; } - static void set_has_py_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00008000u; + static void set_has_py_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 32768u; } - static void set_has_php_generic_services(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00010000u; + static void set_has_php_generic_services(HasBits* has_bits) { + (*has_bits)[0] |= 65536u; } - static void set_has_deprecated(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00020000u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 131072u; } - static void set_has_cc_enable_arenas(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00040000u; + static void set_has_cc_enable_arenas(HasBits* has_bits) { + (*has_bits)[0] |= 262144u; } - static void set_has_objc_class_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_objc_class_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_csharp_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_csharp_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_swift_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_swift_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_php_class_prefix(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000040u; + static void set_has_php_class_prefix(HasBits* has_bits) { + (*has_bits)[0] |= 64u; } - static void set_has_php_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000080u; + static void set_has_php_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 128u; } - static void set_has_php_metadata_namespace(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000100u; + static void set_has_php_metadata_namespace(HasBits* has_bits) { + (*has_bits)[0] |= 256u; } - static void set_has_ruby_package(FileOptions* msg) { - msg->_has_bits_[0] |= 0x00000200u; + static void set_has_ruby_package(HasBits* has_bits) { + (*has_bits)[0] |= 512u; } }; @@ -8718,31 +8881,33 @@ void FileOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FileOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional string java_package = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_java_package(), ptr, ctx, "google.protobuf.FileOptions.java_package"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string java_outer_classname = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_java_outer_classname(), ptr, ctx, "google.protobuf.FileOptions.java_outer_classname"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional .google.protobuf.FileOptions.OptimizeMode optimize_for = 9 [default = SPEED]; case 9: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 72) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::FileOptions_OptimizeMode_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(9, val, mutable_unknown_fields()); break; @@ -8753,151 +8918,168 @@ const char* FileOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID // optional bool java_multiple_files = 10 [default = false]; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 80) goto handle_unusual; - set_java_multiple_files(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_java_multiple_files(&has_bits); + java_multiple_files_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional string go_package = 11; case 11: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 90) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_go_package(), ptr, ctx, "google.protobuf.FileOptions.go_package"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional bool cc_generic_services = 16 [default = false]; case 16: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 128) goto handle_unusual; - set_cc_generic_services(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_cc_generic_services(&has_bits); + cc_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool java_generic_services = 17 [default = false]; case 17: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 136) goto handle_unusual; - set_java_generic_services(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_java_generic_services(&has_bits); + java_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool py_generic_services = 18 [default = false]; case 18: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 144) goto handle_unusual; - set_py_generic_services(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_py_generic_services(&has_bits); + py_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; case 20: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 160) goto handle_unusual; - set_java_generate_equals_and_hash(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_java_generate_equals_and_hash(&has_bits); + java_generate_equals_and_hash_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool deprecated = 23 [default = false]; case 23: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 184) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool java_string_check_utf8 = 27 [default = false]; case 27: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 216) goto handle_unusual; - set_java_string_check_utf8(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_java_string_check_utf8(&has_bits); + java_string_check_utf8_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool cc_enable_arenas = 31 [default = false]; case 31: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 248) goto handle_unusual; - set_cc_enable_arenas(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_cc_enable_arenas(&has_bits); + cc_enable_arenas_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional string objc_class_prefix = 36; case 36: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_objc_class_prefix(), ptr, ctx, "google.protobuf.FileOptions.objc_class_prefix"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string csharp_namespace = 37; case 37: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_csharp_namespace(), ptr, ctx, "google.protobuf.FileOptions.csharp_namespace"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string swift_prefix = 39; case 39: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_swift_prefix(), ptr, ctx, "google.protobuf.FileOptions.swift_prefix"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string php_class_prefix = 40; case 40: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_class_prefix(), ptr, ctx, "google.protobuf.FileOptions.php_class_prefix"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string php_namespace = 41; case 41: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 74) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_namespace(), ptr, ctx, "google.protobuf.FileOptions.php_namespace"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional bool php_generic_services = 42 [default = false]; case 42: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 80) goto handle_unusual; - set_php_generic_services(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_php_generic_services(&has_bits); + php_generic_services_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional string php_metadata_namespace = 44; case 44: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 98) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_php_metadata_namespace(), ptr, ctx, "google.protobuf.FileOptions.php_metadata_namespace"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string ruby_package = 45; case 45: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 106) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_ruby_package(), ptr, ctx, "google.protobuf.FileOptions.ruby_package"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FileOptions::MergePartialFromCodedStream( @@ -8962,7 +9144,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_multiple_files = 10 [default = false]; case 10: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (80 & 0xFF)) { - HasBitSetters::set_has_java_multiple_files(this); + HasBitSetters::set_has_java_multiple_files(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_multiple_files_))); @@ -8990,7 +9172,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool cc_generic_services = 16 [default = false]; case 16: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (128 & 0xFF)) { - HasBitSetters::set_has_cc_generic_services(this); + HasBitSetters::set_has_cc_generic_services(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &cc_generic_services_))); @@ -9003,7 +9185,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_generic_services = 17 [default = false]; case 17: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (136 & 0xFF)) { - HasBitSetters::set_has_java_generic_services(this); + HasBitSetters::set_has_java_generic_services(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_generic_services_))); @@ -9016,7 +9198,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool py_generic_services = 18 [default = false]; case 18: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (144 & 0xFF)) { - HasBitSetters::set_has_py_generic_services(this); + HasBitSetters::set_has_py_generic_services(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &py_generic_services_))); @@ -9029,7 +9211,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_generate_equals_and_hash = 20 [deprecated = true]; case 20: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (160 & 0xFF)) { - HasBitSetters::set_has_java_generate_equals_and_hash(this); + HasBitSetters::set_has_java_generate_equals_and_hash(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_generate_equals_and_hash_))); @@ -9042,7 +9224,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool deprecated = 23 [default = false]; case 23: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (184 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -9055,7 +9237,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool java_string_check_utf8 = 27 [default = false]; case 27: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (216 & 0xFF)) { - HasBitSetters::set_has_java_string_check_utf8(this); + HasBitSetters::set_has_java_string_check_utf8(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &java_string_check_utf8_))); @@ -9068,7 +9250,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool cc_enable_arenas = 31 [default = false]; case 31: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (248 & 0xFF)) { - HasBitSetters::set_has_cc_enable_arenas(this); + HasBitSetters::set_has_cc_enable_arenas(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &cc_enable_arenas_))); @@ -9156,7 +9338,7 @@ bool FileOptions::MergePartialFromCodedStream( // optional bool php_generic_services = 42 [default = false]; case 42: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (336 & 0xFF)) { - HasBitSetters::set_has_php_generic_services(this); + HasBitSetters::set_has_php_generic_services(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &php_generic_services_))); @@ -9945,17 +10127,18 @@ void MessageOptions::InitAsDefaultInstance() { } class MessageOptions::HasBitSetters { public: - static void set_has_message_set_wire_format(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<MessageOptions>()._has_bits_); + static void set_has_message_set_wire_format(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_no_standard_descriptor_accessor(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_no_standard_descriptor_accessor(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_deprecated(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_map_entry(MessageOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_map_entry(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } }; @@ -10043,69 +10226,83 @@ void MessageOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* MessageOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional bool message_set_wire_format = 1 [default = false]; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_message_set_wire_format(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_message_set_wire_format(&has_bits); + message_set_wire_format_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool no_standard_descriptor_accessor = 2 [default = false]; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_no_standard_descriptor_accessor(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_no_standard_descriptor_accessor(&has_bits); + no_standard_descriptor_accessor_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool map_entry = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 56) goto handle_unusual; - set_map_entry(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_map_entry(&has_bits); + map_entry_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MessageOptions::MergePartialFromCodedStream( @@ -10121,7 +10318,7 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool message_set_wire_format = 1 [default = false]; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_message_set_wire_format(this); + HasBitSetters::set_has_message_set_wire_format(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &message_set_wire_format_))); @@ -10134,7 +10331,7 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool no_standard_descriptor_accessor = 2 [default = false]; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_no_standard_descriptor_accessor(this); + HasBitSetters::set_has_no_standard_descriptor_accessor(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &no_standard_descriptor_accessor_))); @@ -10147,7 +10344,7 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -10160,7 +10357,7 @@ bool MessageOptions::MergePartialFromCodedStream( // optional bool map_entry = 7; case 7: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (56 & 0xFF)) { - HasBitSetters::set_has_map_entry(this); + HasBitSetters::set_has_map_entry(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &map_entry_))); @@ -10463,23 +10660,24 @@ void FieldOptions::InitAsDefaultInstance() { } class FieldOptions::HasBitSetters { public: - static void set_has_ctype(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<FieldOptions>()._has_bits_); + static void set_has_ctype(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_packed(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_packed(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_jstype(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_jstype(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_lazy(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_lazy(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } - static void set_has_deprecated(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_weak(FieldOptions* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_weak(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } }; @@ -10572,17 +10770,19 @@ void FieldOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING]; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::FieldOptions_CType_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(1, val, mutable_unknown_fields()); break; @@ -10593,29 +10793,32 @@ const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I // optional bool packed = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_packed(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_packed(&has_bits); + packed_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool lazy = 5 [default = false]; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; - set_lazy(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_lazy(&has_bits); + lazy_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional .google.protobuf.FieldOptions.JSType jstype = 6 [default = JS_NORMAL]; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 48) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::FieldOptions_JSType_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(6, val, mutable_unknown_fields()); break; @@ -10626,39 +10829,48 @@ const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I // optional bool weak = 10 [default = false]; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 80) goto handle_unusual; - set_weak(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_weak(&has_bits); + weak_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldOptions::MergePartialFromCodedStream( @@ -10693,7 +10905,7 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool packed = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_packed(this); + HasBitSetters::set_has_packed(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &packed_))); @@ -10706,7 +10918,7 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -10719,7 +10931,7 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool lazy = 5 [default = false]; case 5: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_lazy(this); + HasBitSetters::set_has_lazy(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &lazy_))); @@ -10751,7 +10963,7 @@ bool FieldOptions::MergePartialFromCodedStream( // optional bool weak = 10 [default = false]; case 10: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (80 & 0xFF)) { - HasBitSetters::set_has_weak(this); + HasBitSetters::set_has_weak(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &weak_))); @@ -11098,6 +11310,7 @@ void OneofOptions::InitAsDefaultInstance() { } class OneofOptions::HasBitSetters { public: + using HasBits = decltype(std::declval<OneofOptions>()._has_bits_); }; #if !defined(_MSC_VER) || _MSC_VER >= 1900 @@ -11171,41 +11384,49 @@ void OneofOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* OneofOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool OneofOptions::MergePartialFromCodedStream( @@ -11426,11 +11647,12 @@ void EnumOptions::InitAsDefaultInstance() { } class EnumOptions::HasBitSetters { public: - static void set_has_allow_alias(EnumOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<EnumOptions>()._has_bits_); + static void set_has_allow_alias(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_deprecated(EnumOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -11516,55 +11738,67 @@ void EnumOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional bool allow_alias = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_allow_alias(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_allow_alias(&has_bits); + allow_alias_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumOptions::MergePartialFromCodedStream( @@ -11580,7 +11814,7 @@ bool EnumOptions::MergePartialFromCodedStream( // optional bool allow_alias = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_allow_alias(this); + HasBitSetters::set_has_allow_alias(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &allow_alias_))); @@ -11593,7 +11827,7 @@ bool EnumOptions::MergePartialFromCodedStream( // optional bool deprecated = 3 [default = false]; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -11858,8 +12092,9 @@ void EnumValueOptions::InitAsDefaultInstance() { } class EnumValueOptions::HasBitSetters { public: - static void set_has_deprecated(EnumValueOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<EnumValueOptions>()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; @@ -11938,48 +12173,59 @@ void EnumValueOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumValueOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional bool deprecated = 1 [default = false]; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumValueOptions::MergePartialFromCodedStream( @@ -11995,7 +12241,7 @@ bool EnumValueOptions::MergePartialFromCodedStream( // optional bool deprecated = 1 [default = false]; case 1: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (8 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -12235,8 +12481,9 @@ void ServiceOptions::InitAsDefaultInstance() { } class ServiceOptions::HasBitSetters { public: - static void set_has_deprecated(ServiceOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<ServiceOptions>()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } }; @@ -12315,48 +12562,59 @@ void ServiceOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* ServiceOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional bool deprecated = 33 [default = false]; case 33: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ServiceOptions::MergePartialFromCodedStream( @@ -12372,7 +12630,7 @@ bool ServiceOptions::MergePartialFromCodedStream( // optional bool deprecated = 33 [default = false]; case 33: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (264 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -12612,11 +12870,12 @@ void MethodOptions::InitAsDefaultInstance() { } class MethodOptions::HasBitSetters { public: - static void set_has_deprecated(MethodOptions* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<MethodOptions>()._has_bits_); + static void set_has_deprecated(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_idempotency_level(MethodOptions* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_idempotency_level(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -12705,24 +12964,27 @@ void MethodOptions::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* MethodOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // optional bool deprecated = 33 [default = false]; case 33: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_deprecated(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_deprecated(&has_bits); + deprecated_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional .google.protobuf.MethodOptions.IdempotencyLevel idempotency_level = 34 [default = IDEMPOTENCY_UNKNOWN]; case 34: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); if (!PROTOBUF_NAMESPACE_ID::MethodOptions_IdempotencyLevel_IsValid(val)) { ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(34, val, mutable_unknown_fields()); break; @@ -12733,32 +12995,40 @@ const char* MethodOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999; case 999: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_uninterpreted_option(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 65535) == 16058 && (ptr += 2)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint16>(ptr) != 16058) break; + ptr += 2; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } if ((8000u <= tag)) { ptr = _extensions_.ParseField(tag, ptr, internal_default_instance(), &_internal_metadata_, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool MethodOptions::MergePartialFromCodedStream( @@ -12774,7 +13044,7 @@ bool MethodOptions::MergePartialFromCodedStream( // optional bool deprecated = 33 [default = false]; case 33: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (264 & 0xFF)) { - HasBitSetters::set_has_deprecated(this); + HasBitSetters::set_has_deprecated(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &deprecated_))); @@ -13061,11 +13331,12 @@ void UninterpretedOption_NamePart::InitAsDefaultInstance() { } class UninterpretedOption_NamePart::HasBitSetters { public: - static void set_has_name_part(UninterpretedOption_NamePart* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<UninterpretedOption_NamePart>()._has_bits_); + static void set_has_name_part(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_is_extension(UninterpretedOption_NamePart* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_is_extension(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -13149,39 +13420,48 @@ void UninterpretedOption_NamePart::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* UninterpretedOption_NamePart::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // required string name_part = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_name_part(), ptr, ctx, "google.protobuf.UninterpretedOption.NamePart.name_part"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // required bool is_extension = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_is_extension(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_is_extension(&has_bits); + is_extension_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UninterpretedOption_NamePart::MergePartialFromCodedStream( @@ -13212,7 +13492,7 @@ bool UninterpretedOption_NamePart::MergePartialFromCodedStream( // required bool is_extension = 2; case 2: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (16 & 0xFF)) { - HasBitSetters::set_has_is_extension(this); + HasBitSetters::set_has_is_extension(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< bool, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_BOOL>( input, &is_extension_))); @@ -13444,23 +13724,24 @@ void UninterpretedOption::InitAsDefaultInstance() { } class UninterpretedOption::HasBitSetters { public: - static void set_has_identifier_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<UninterpretedOption>()._has_bits_); + static void set_has_identifier_value(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_positive_int_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000008u; + static void set_has_positive_int_value(HasBits* has_bits) { + (*has_bits)[0] |= 8u; } - static void set_has_negative_int_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000010u; + static void set_has_negative_int_value(HasBits* has_bits) { + (*has_bits)[0] |= 16u; } - static void set_has_double_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000020u; + static void set_has_double_value(HasBits* has_bits) { + (*has_bits)[0] |= 32u; } - static void set_has_string_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_string_value(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_aggregate_value(UninterpretedOption* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_aggregate_value(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; @@ -13582,47 +13863,54 @@ void UninterpretedOption::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* UninterpretedOption::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.UninterpretedOption.NamePart name = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_name(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // optional string identifier_value = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_identifier_value(), ptr, ctx, "google.protobuf.UninterpretedOption.identifier_value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional uint64 positive_int_value = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 32) goto handle_unusual; - set_positive_int_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_positive_int_value(&has_bits); + positive_int_value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int64 negative_int_value = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; - set_negative_int_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_negative_int_value(&has_bits); + negative_int_value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional double double_value = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 49) goto handle_unusual; - set_double_value(::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<double>(ptr)); + HasBitSetters::set_has_double_value(&has_bits); + double_value_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<double>(ptr); ptr += sizeof(double); break; } @@ -13630,29 +13918,35 @@ const char* UninterpretedOption::_InternalParse(const char* ptr, ::PROTOBUF_NAME case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 58) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(mutable_string_value(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string aggregate_value = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 66) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_aggregate_value(), ptr, ctx, "google.protobuf.UninterpretedOption.aggregate_value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UninterpretedOption::MergePartialFromCodedStream( @@ -13694,7 +13988,7 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional uint64 positive_int_value = 4; case 4: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { - HasBitSetters::set_has_positive_int_value(this); + HasBitSetters::set_has_positive_int_value(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::uint64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_UINT64>( input, &positive_int_value_))); @@ -13707,7 +14001,7 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional int64 negative_int_value = 5; case 5: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (40 & 0xFF)) { - HasBitSetters::set_has_negative_int_value(this); + HasBitSetters::set_has_negative_int_value(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int64, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT64>( input, &negative_int_value_))); @@ -13720,7 +14014,7 @@ bool UninterpretedOption::MergePartialFromCodedStream( // optional double double_value = 6; case 6: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (49 & 0xFF)) { - HasBitSetters::set_has_double_value(this); + HasBitSetters::set_has_double_value(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< double, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_DOUBLE>( input, &double_value_))); @@ -14093,11 +14387,12 @@ void SourceCodeInfo_Location::InitAsDefaultInstance() { } class SourceCodeInfo_Location::HasBitSetters { public: - static void set_has_leading_comments(SourceCodeInfo_Location* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<SourceCodeInfo_Location>()._has_bits_); + static void set_has_leading_comments(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_trailing_comments(SourceCodeInfo_Location* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_trailing_comments(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } }; @@ -14202,77 +14497,91 @@ void SourceCodeInfo_Location::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* SourceCodeInfo_Location::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated int32 path = 1 [packed = true]; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_path(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - do { + while (true) { add_path(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 8 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 8) break; + ptr += 1; + } break; } // repeated int32 span = 2 [packed = true]; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_span(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - do { + while (true) { add_span(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 16 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 16) break; + ptr += 1; + } break; } // optional string leading_comments = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_leading_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.leading_comments"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional string trailing_comments = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_trailing_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.trailing_comments"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated string leading_detached_comments = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(add_leading_detached_comments(), ptr, ctx, "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 50 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 50) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceCodeInfo_Location::MergePartialFromCodedStream( @@ -14694,6 +15003,7 @@ void SourceCodeInfo::InitAsDefaultInstance() { } class SourceCodeInfo::HasBitSetters { public: + using HasBits = decltype(std::declval<SourceCodeInfo>()._has_bits_); }; #if !defined(_MSC_VER) || _MSC_VER >= 1900 @@ -14764,35 +15074,43 @@ void SourceCodeInfo::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* SourceCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.SourceCodeInfo.Location location = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_location(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceCodeInfo::MergePartialFromCodedStream( @@ -14991,14 +15309,15 @@ void GeneratedCodeInfo_Annotation::InitAsDefaultInstance() { } class GeneratedCodeInfo_Annotation::HasBitSetters { public: - static void set_has_source_file(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000001u; + using HasBits = decltype(std::declval<GeneratedCodeInfo_Annotation>()._has_bits_); + static void set_has_source_file(HasBits* has_bits) { + (*has_bits)[0] |= 1u; } - static void set_has_begin(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000002u; + static void set_has_begin(HasBits* has_bits) { + (*has_bits)[0] |= 2u; } - static void set_has_end(GeneratedCodeInfo_Annotation* msg) { - msg->_has_bits_[0] |= 0x00000004u; + static void set_has_end(HasBits* has_bits) { + (*has_bits)[0] |= 4u; } }; @@ -15095,60 +15414,72 @@ void GeneratedCodeInfo_Annotation::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* GeneratedCodeInfo_Annotation::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure + HasBitSetters::HasBits has_bits{}; ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated int32 path = 1 [packed = true]; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::PackedInt32Parser(mutable_path(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } else if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - do { + while (true) { add_path(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 8 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 8) break; + ptr += 1; + } break; } // optional string source_file = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8Verify(mutable_source_file(), ptr, ctx, "google.protobuf.GeneratedCodeInfo.Annotation.source_file"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // optional int32 begin = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_begin(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_begin(&has_bits); + begin_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // optional int32 end = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 32) goto handle_unusual; - set_end(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + HasBitSetters::set_has_end(&has_bits); + end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: + _has_bits_.Or(has_bits); return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( @@ -15195,7 +15526,7 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( // optional int32 begin = 3; case 3: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (24 & 0xFF)) { - HasBitSetters::set_has_begin(this); + HasBitSetters::set_has_begin(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &begin_))); @@ -15208,7 +15539,7 @@ bool GeneratedCodeInfo_Annotation::MergePartialFromCodedStream( // optional int32 end = 4; case 4: { if (static_cast< ::PROTOBUF_NAMESPACE_ID::uint8>(tag) == (32 & 0xFF)) { - HasBitSetters::set_has_end(this); + HasBitSetters::set_has_end(&_has_bits_); DO_((::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::ReadPrimitive< ::PROTOBUF_NAMESPACE_ID::int32, ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::TYPE_INT32>( input, &end_))); @@ -15488,6 +15819,7 @@ void GeneratedCodeInfo::InitAsDefaultInstance() { } class GeneratedCodeInfo::HasBitSetters { public: + using HasBits = decltype(std::declval<GeneratedCodeInfo>()._has_bits_); }; #if !defined(_MSC_VER) || _MSC_VER >= 1900 @@ -15558,35 +15890,43 @@ void GeneratedCodeInfo::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* GeneratedCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_annotation(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool GeneratedCodeInfo::MergePartialFromCodedStream( diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index bff027e369..261b064ce7 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -194,9 +194,13 @@ constexpr FieldDescriptorProto_Type FieldDescriptorProto_Type_Type_MAX = FieldDe constexpr int FieldDescriptorProto_Type_Type_ARRAYSIZE = FieldDescriptorProto_Type_Type_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Type_descriptor(); -inline const std::string& FieldDescriptorProto_Type_Name(FieldDescriptorProto_Type value) { +template<typename T> +inline const std::string& FieldDescriptorProto_Type_Name(T enum_t_value) { + static_assert(::std::is_same<T, FieldDescriptorProto_Type>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function FieldDescriptorProto_Type_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - FieldDescriptorProto_Type_descriptor(), value); + FieldDescriptorProto_Type_descriptor(), enum_t_value); } inline bool FieldDescriptorProto_Type_Parse( const std::string& name, FieldDescriptorProto_Type* value) { @@ -214,9 +218,13 @@ constexpr FieldDescriptorProto_Label FieldDescriptorProto_Label_Label_MAX = Fiel constexpr int FieldDescriptorProto_Label_Label_ARRAYSIZE = FieldDescriptorProto_Label_Label_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldDescriptorProto_Label_descriptor(); -inline const std::string& FieldDescriptorProto_Label_Name(FieldDescriptorProto_Label value) { +template<typename T> +inline const std::string& FieldDescriptorProto_Label_Name(T enum_t_value) { + static_assert(::std::is_same<T, FieldDescriptorProto_Label>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function FieldDescriptorProto_Label_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - FieldDescriptorProto_Label_descriptor(), value); + FieldDescriptorProto_Label_descriptor(), enum_t_value); } inline bool FieldDescriptorProto_Label_Parse( const std::string& name, FieldDescriptorProto_Label* value) { @@ -234,9 +242,13 @@ constexpr FileOptions_OptimizeMode FileOptions_OptimizeMode_OptimizeMode_MAX = F constexpr int FileOptions_OptimizeMode_OptimizeMode_ARRAYSIZE = FileOptions_OptimizeMode_OptimizeMode_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FileOptions_OptimizeMode_descriptor(); -inline const std::string& FileOptions_OptimizeMode_Name(FileOptions_OptimizeMode value) { +template<typename T> +inline const std::string& FileOptions_OptimizeMode_Name(T enum_t_value) { + static_assert(::std::is_same<T, FileOptions_OptimizeMode>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function FileOptions_OptimizeMode_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - FileOptions_OptimizeMode_descriptor(), value); + FileOptions_OptimizeMode_descriptor(), enum_t_value); } inline bool FileOptions_OptimizeMode_Parse( const std::string& name, FileOptions_OptimizeMode* value) { @@ -254,9 +266,13 @@ constexpr FieldOptions_CType FieldOptions_CType_CType_MAX = FieldOptions_CType_S constexpr int FieldOptions_CType_CType_ARRAYSIZE = FieldOptions_CType_CType_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_CType_descriptor(); -inline const std::string& FieldOptions_CType_Name(FieldOptions_CType value) { +template<typename T> +inline const std::string& FieldOptions_CType_Name(T enum_t_value) { + static_assert(::std::is_same<T, FieldOptions_CType>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function FieldOptions_CType_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - FieldOptions_CType_descriptor(), value); + FieldOptions_CType_descriptor(), enum_t_value); } inline bool FieldOptions_CType_Parse( const std::string& name, FieldOptions_CType* value) { @@ -274,9 +290,13 @@ constexpr FieldOptions_JSType FieldOptions_JSType_JSType_MAX = FieldOptions_JSTy constexpr int FieldOptions_JSType_JSType_ARRAYSIZE = FieldOptions_JSType_JSType_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* FieldOptions_JSType_descriptor(); -inline const std::string& FieldOptions_JSType_Name(FieldOptions_JSType value) { +template<typename T> +inline const std::string& FieldOptions_JSType_Name(T enum_t_value) { + static_assert(::std::is_same<T, FieldOptions_JSType>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function FieldOptions_JSType_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - FieldOptions_JSType_descriptor(), value); + FieldOptions_JSType_descriptor(), enum_t_value); } inline bool FieldOptions_JSType_Parse( const std::string& name, FieldOptions_JSType* value) { @@ -294,9 +314,13 @@ constexpr MethodOptions_IdempotencyLevel MethodOptions_IdempotencyLevel_Idempote constexpr int MethodOptions_IdempotencyLevel_IdempotencyLevel_ARRAYSIZE = MethodOptions_IdempotencyLevel_IdempotencyLevel_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* MethodOptions_IdempotencyLevel_descriptor(); -inline const std::string& MethodOptions_IdempotencyLevel_Name(MethodOptions_IdempotencyLevel value) { +template<typename T> +inline const std::string& MethodOptions_IdempotencyLevel_Name(T enum_t_value) { + static_assert(::std::is_same<T, MethodOptions_IdempotencyLevel>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function MethodOptions_IdempotencyLevel_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - MethodOptions_IdempotencyLevel_descriptor(), value); + MethodOptions_IdempotencyLevel_descriptor(), enum_t_value); } inline bool MethodOptions_IdempotencyLevel_Parse( const std::string& name, MethodOptions_IdempotencyLevel* value) { @@ -1657,8 +1681,12 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : Type_descriptor() { return FieldDescriptorProto_Type_descriptor(); } - static inline const std::string& Type_Name(Type value) { - return FieldDescriptorProto_Type_Name(value); + template<typename T> + static inline const std::string& Type_Name(T enum_t_value) { + static_assert(::std::is_same<T, Type>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Type_Name."); + return FieldDescriptorProto_Type_Name(enum_t_value); } static inline bool Type_Parse(const std::string& name, Type* value) { @@ -1685,8 +1713,12 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : Label_descriptor() { return FieldDescriptorProto_Label_descriptor(); } - static inline const std::string& Label_Name(Label value) { - return FieldDescriptorProto_Label_Name(value); + template<typename T> + static inline const std::string& Label_Name(T enum_t_value) { + static_assert(::std::is_same<T, Label>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Label_Name."); + return FieldDescriptorProto_Label_Name(enum_t_value); } static inline bool Label_Parse(const std::string& name, Label* value) { @@ -3102,8 +3134,12 @@ class PROTOBUF_EXPORT FileOptions final : OptimizeMode_descriptor() { return FileOptions_OptimizeMode_descriptor(); } - static inline const std::string& OptimizeMode_Name(OptimizeMode value) { - return FileOptions_OptimizeMode_Name(value); + template<typename T> + static inline const std::string& OptimizeMode_Name(T enum_t_value) { + static_assert(::std::is_same<T, OptimizeMode>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function OptimizeMode_Name."); + return FileOptions_OptimizeMode_Name(enum_t_value); } static inline bool OptimizeMode_Parse(const std::string& name, OptimizeMode* value) { @@ -3762,8 +3798,12 @@ class PROTOBUF_EXPORT FieldOptions final : CType_descriptor() { return FieldOptions_CType_descriptor(); } - static inline const std::string& CType_Name(CType value) { - return FieldOptions_CType_Name(value); + template<typename T> + static inline const std::string& CType_Name(T enum_t_value) { + static_assert(::std::is_same<T, CType>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function CType_Name."); + return FieldOptions_CType_Name(enum_t_value); } static inline bool CType_Parse(const std::string& name, CType* value) { @@ -3790,8 +3830,12 @@ class PROTOBUF_EXPORT FieldOptions final : JSType_descriptor() { return FieldOptions_JSType_descriptor(); } - static inline const std::string& JSType_Name(JSType value) { - return FieldOptions_JSType_Name(value); + template<typename T> + static inline const std::string& JSType_Name(T enum_t_value) { + static_assert(::std::is_same<T, JSType>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function JSType_Name."); + return FieldOptions_JSType_Name(enum_t_value); } static inline bool JSType_Parse(const std::string& name, JSType* value) { @@ -4623,8 +4667,12 @@ class PROTOBUF_EXPORT MethodOptions final : IdempotencyLevel_descriptor() { return MethodOptions_IdempotencyLevel_descriptor(); } - static inline const std::string& IdempotencyLevel_Name(IdempotencyLevel value) { - return MethodOptions_IdempotencyLevel_Name(value); + template<typename T> + static inline const std::string& IdempotencyLevel_Name(T enum_t_value) { + static_assert(::std::is_same<T, IdempotencyLevel>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function IdempotencyLevel_Name."); + return MethodOptions_IdempotencyLevel_Name(enum_t_value); } static inline bool IdempotencyLevel_Parse(const std::string& name, IdempotencyLevel* value) { diff --git a/src/google/protobuf/descriptor_database.cc b/src/google/protobuf/descriptor_database.cc index 55d6bb1f9c..dbd0f4a313 100644 --- a/src/google/protobuf/descriptor_database.cc +++ b/src/google/protobuf/descriptor_database.cc @@ -51,8 +51,7 @@ DescriptorDatabase::~DescriptorDatabase() {} template <typename Value> bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddFile( - const FileDescriptorProto& file, - Value value) { + const FileDescriptorProto& file, Value value) { if (!InsertIfNotPresent(&by_name_, file.name(), value)) { GOOGLE_LOG(ERROR) << "File already exists in database: " << file.name(); return false; @@ -108,8 +107,10 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddSymbol( } if (IsSubSymbol(iter->first, name)) { - GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the existing " - "symbol \"" << iter->first << "\"."; + GOOGLE_LOG(ERROR) << "Symbol name \"" << name + << "\" conflicts with the existing " + "symbol \"" + << iter->first << "\"."; return false; } @@ -121,8 +122,10 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddSymbol( ++iter; if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) { - GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the existing " - "symbol \"" << iter->first << "\"."; + GOOGLE_LOG(ERROR) << "Symbol name \"" << name + << "\" conflicts with the existing " + "symbol \"" + << iter->first << "\"."; return false; } @@ -138,8 +141,7 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddSymbol( template <typename Value> bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions( - const DescriptorProto& message_type, - Value value) { + const DescriptorProto& message_type, Value value) { for (int i = 0; i < message_type.nested_type_size(); i++) { if (!AddNestedExtensions(message_type.nested_type(i), value)) return false; } @@ -151,8 +153,7 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions( template <typename Value> bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddExtension( - const FieldDescriptorProto& field, - Value value) { + const FieldDescriptorProto& field, Value value) { if (!field.extendee().empty() && field.extendee()[0] == '.') { // The extension is fully-qualified. We can use it as a lookup key in // the by_symbol_ table. @@ -161,8 +162,9 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddExtension( std::make_pair(field.extendee().substr(1), field.number()), value)) { GOOGLE_LOG(ERROR) << "Extension conflicts with extension already in database: " - "extend " << field.extendee() << " { " - << field.name() << " = " << field.number() << " }"; + "extend " + << field.extendee() << " { " << field.name() << " = " + << field.number() << " }"; return false; } } else { @@ -185,8 +187,9 @@ Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindSymbol( typename std::map<std::string, Value>::iterator iter = FindLastLessOrEqual(name); - return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ? - iter->second : Value(); + return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) + ? iter->second + : Value(); } template <typename Value> @@ -249,10 +252,8 @@ bool SimpleDescriptorDatabase::DescriptorIndex<Value>::ValidateSymbolName( const std::string& name) { for (int i = 0; i < name.size(); i++) { // I don't trust ctype.h due to locales. :( - if (name[i] != '.' && name[i] != '_' && - (name[i] < '0' || name[i] > '9') && - (name[i] < 'A' || name[i] > 'Z') && - (name[i] < 'a' || name[i] > 'z')) { + if (name[i] != '.' && name[i] != '_' && (name[i] < '0' || name[i] > '9') && + (name[i] < 'A' || name[i] > 'Z') && (name[i] < 'a' || name[i] > 'z')) { return false; } } @@ -321,8 +322,8 @@ EncodedDescriptorDatabase::~EncodedDescriptorDatabase() { } } -bool EncodedDescriptorDatabase::Add( - const void* encoded_file_descriptor, int size) { +bool EncodedDescriptorDatabase::Add(const void* encoded_file_descriptor, + int size) { FileDescriptorProto file; if (file.ParseFromArray(encoded_file_descriptor, size)) { return index_.AddFile(file, std::make_pair(encoded_file_descriptor, size)); @@ -333,8 +334,8 @@ bool EncodedDescriptorDatabase::Add( } } -bool EncodedDescriptorDatabase::AddCopy( - const void* encoded_file_descriptor, int size) { +bool EncodedDescriptorDatabase::AddCopy(const void* encoded_file_descriptor, + int size) { void* copy = operator new(size); memcpy(copy, encoded_file_descriptor, size); files_to_delete_.push_back(copy); @@ -392,8 +393,7 @@ bool EncodedDescriptorDatabase::FindAllExtensionNumbers( } bool EncodedDescriptorDatabase::MaybeParse( - std::pair<const void*, int> encoded_file, - FileDescriptorProto* output) { + std::pair<const void*, int> encoded_file, FileDescriptorProto* output) { if (encoded_file.first == NULL) return false; return output->ParseFromArray(encoded_file.first, encoded_file.second); } @@ -401,7 +401,7 @@ bool EncodedDescriptorDatabase::MaybeParse( // =================================================================== DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool) - : pool_(pool) {} + : pool_(pool) {} DescriptorPoolDatabase::~DescriptorPoolDatabase() {} bool DescriptorPoolDatabase::FindFileByName(const std::string& filename, @@ -429,7 +429,7 @@ bool DescriptorPoolDatabase::FindFileContainingExtension( if (extendee == NULL) return false; const FieldDescriptor* extension = - pool_.FindExtensionByNumber(extendee, field_number); + pool_.FindExtensionByNumber(extendee, field_number); if (extension == NULL) return false; output->Clear(); @@ -455,14 +455,13 @@ bool DescriptorPoolDatabase::FindAllExtensionNumbers( // =================================================================== MergedDescriptorDatabase::MergedDescriptorDatabase( - DescriptorDatabase* source1, - DescriptorDatabase* source2) { + DescriptorDatabase* source1, DescriptorDatabase* source2) { sources_.push_back(source1); sources_.push_back(source2); } MergedDescriptorDatabase::MergedDescriptorDatabase( const std::vector<DescriptorDatabase*>& sources) - : sources_(sources) {} + : sources_(sources) {} MergedDescriptorDatabase::~MergedDescriptorDatabase() {} bool MergedDescriptorDatabase::FindFileByName(const std::string& filename, @@ -500,8 +499,8 @@ bool MergedDescriptorDatabase::FindFileContainingExtension( const std::string& containing_type, int field_number, FileDescriptorProto* output) { for (int i = 0; i < sources_.size(); i++) { - if (sources_[i]->FindFileContainingExtension( - containing_type, field_number, output)) { + if (sources_[i]->FindFileContainingExtension(containing_type, field_number, + output)) { // The symbol was found in source i. However, if one of the previous // sources defines a file with the same name (which presumably doesn't // contain the symbol, since it wasn't found in that source), then we diff --git a/src/google/protobuf/descriptor_database.h b/src/google/protobuf/descriptor_database.h index 5ac0383010..8fc8ae3e19 100644 --- a/src/google/protobuf/descriptor_database.h +++ b/src/google/protobuf/descriptor_database.h @@ -183,13 +183,10 @@ class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { public: // Helpers to recursively add particular descriptors and all their contents // to the index. - bool AddFile(const FileDescriptorProto& file, - Value value); + bool AddFile(const FileDescriptorProto& file, Value value); bool AddSymbol(const std::string& name, Value value); - bool AddNestedExtensions(const DescriptorProto& message_type, - Value value); - bool AddExtension(const FieldDescriptorProto& field, - Value value); + bool AddNestedExtensions(const DescriptorProto& message_type, Value value); + bool AddExtension(const FieldDescriptorProto& field, Value value); Value FindFile(const std::string& filename); Value FindSymbol(const std::string& name); @@ -270,14 +267,12 @@ class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase { bool ValidateSymbolName(const std::string& name); }; - DescriptorIndex<const FileDescriptorProto*> index_; std::vector<const FileDescriptorProto*> files_to_delete_; // If file is non-NULL, copy it into *output and return true, otherwise // return false. - bool MaybeCopy(const FileDescriptorProto* file, - FileDescriptorProto* output); + bool MaybeCopy(const FileDescriptorProto* file, FileDescriptorProto* output); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleDescriptorDatabase); }; diff --git a/src/google/protobuf/descriptor_database_unittest.cc b/src/google/protobuf/descriptor_database_unittest.cc index 423a7002e9..58ccf4e22e 100644 --- a/src/google/protobuf/descriptor_database_unittest.cc +++ b/src/google/protobuf/descriptor_database_unittest.cc @@ -64,8 +64,7 @@ static void ExpectContainsType(const FileDescriptorProto& proto, for (int i = 0; i < proto.message_type_size(); i++) { if (proto.message_type(i).name() == type_name) return; } - ADD_FAILURE() << "\"" << proto.name() - << "\" did not contain expected type \"" + ADD_FAILURE() << "\"" << proto.name() << "\" did not contain expected type \"" << type_name << "\"."; } @@ -100,9 +99,7 @@ class SimpleDescriptorDatabaseTestCase : public DescriptorDatabaseTestCase { virtual ~SimpleDescriptorDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { return database_.Add(file); } @@ -120,9 +117,7 @@ class EncodedDescriptorDatabaseTestCase : public DescriptorDatabaseTestCase { virtual ~EncodedDescriptorDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { std::string data; file.SerializeToString(&data); @@ -143,9 +138,7 @@ class DescriptorPoolDatabaseTestCase : public DescriptorDatabaseTestCase { DescriptorPoolDatabaseTestCase() : database_(pool_) {} virtual ~DescriptorPoolDatabaseTestCase() {} - virtual DescriptorDatabase* GetDatabase() { - return &database_; - } + virtual DescriptorDatabase* GetDatabase() { return &database_; } virtual bool AddToDatabase(const FileDescriptorProto& file) { return pool_.BuildFile(file); } @@ -183,11 +176,11 @@ class DescriptorDatabaseTest TEST_P(DescriptorDatabaseTest, FindFileByName) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { name:\"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name:\"Foo\" }"); AddToDatabase( - "name: \"bar.proto\" " - "message_type { name:\"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name:\"Bar\" }"); { FileDescriptorProto file; @@ -212,27 +205,26 @@ TEST_P(DescriptorDatabaseTest, FindFileByName) { TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " field { name:\"qux\" }" - " nested_type { name: \"Grault\" } " - " enum_type { name: \"Garply\" } " - "} " - "enum_type { " - " name: \"Waldo\" " - " value { name:\"FRED\" } " - "} " - "extension { name: \"plugh\" } " - "service { " - " name: \"Xyzzy\" " - " method { name: \"Thud\" } " - "}" - ); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " field { name:\"qux\" }" + " nested_type { name: \"Grault\" } " + " enum_type { name: \"Garply\" } " + "} " + "enum_type { " + " name: \"Waldo\" " + " value { name:\"FRED\" } " + "} " + "extension { name: \"plugh\" } " + "service { " + " name: \"Xyzzy\" " + " method { name: \"Thud\" } " + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "package: \"corge\" " + "message_type { name: \"Bar\" }"); { FileDescriptorProto file; @@ -247,8 +239,8 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { EXPECT_EQ("foo.proto", file.name()); // Non-existent field under a valid top level symbol can also be // found. - EXPECT_TRUE(database_->FindFileContainingSymbol("Foo.none_field.none", - &file)); + EXPECT_TRUE( + database_->FindFileContainingSymbol("Foo.none_field.none", &file)); } { @@ -322,24 +314,25 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingSymbol) { TEST_P(DescriptorDatabaseTest, FindFileContainingExtension) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " extension_range { start: 1 end: 1000 } " - " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }" - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " extension_range { start: 1 end: 1000 } " + " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:5 " + " extendee: \".Foo\" }" + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "dependency: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - " extension_range { start: 1 end: 1000 } " - "} " - "extension { name:\"grault\" extendee: \".Foo\" number:32 } " - "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " - "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); + "name: \"bar.proto\" " + "package: \"corge\" " + "dependency: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + " extension_range { start: 1 end: 1000 } " + "} " + "extension { name:\"grault\" extendee: \".Foo\" number:32 } " + "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " + "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); { FileDescriptorProto file; @@ -391,24 +384,25 @@ TEST_P(DescriptorDatabaseTest, FindFileContainingExtension) { TEST_P(DescriptorDatabaseTest, FindAllExtensionNumbers) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - " extension_range { start: 1 end: 1000 } " - " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }" - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + " extension_range { start: 1 end: 1000 } " + " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 " + "number:5 " + " extendee: \".Foo\" }" + "}"); AddToDatabase( - "name: \"bar.proto\" " - "package: \"corge\" " - "dependency: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - " extension_range { start: 1 end: 1000 } " - "} " - "extension { name:\"grault\" extendee: \".Foo\" number:32 } " - "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " - "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); + "name: \"bar.proto\" " + "package: \"corge\" " + "dependency: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + " extension_range { start: 1 end: 1000 } " + "} " + "extension { name:\"grault\" extendee: \".Foo\" number:32 } " + "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } " + "extension { name:\"waldo\" extendee: \"Bar\" number:56 } "); { std::vector<int> numbers; @@ -442,47 +436,49 @@ TEST_P(DescriptorDatabaseTest, FindAllExtensionNumbers) { TEST_P(DescriptorDatabaseTest, ConflictingFileError) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); AddToDatabaseWithError( - "name: \"foo.proto\" " - "message_type { " - " name: \"Bar\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Bar\" " + "}"); } TEST_P(DescriptorDatabaseTest, ConflictingTypeError) { AddToDatabase( - "name: \"foo.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"foo.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); AddToDatabaseWithError( - "name: \"bar.proto\" " - "message_type { " - " name: \"Foo\" " - "}"); + "name: \"bar.proto\" " + "message_type { " + " name: \"Foo\" " + "}"); } TEST_P(DescriptorDatabaseTest, ConflictingExtensionError) { AddToDatabase( - "name: \"foo.proto\" " - "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }"); + "name: \"foo.proto\" " + "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " + " extendee: \".Foo\" }"); AddToDatabaseWithError( - "name: \"bar.proto\" " - "extension { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " - " extendee: \".Foo\" }"); + "name: \"bar.proto\" " + "extension { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 " + " extendee: \".Foo\" }"); } -INSTANTIATE_TEST_CASE_P(Simple, DescriptorDatabaseTest, +INSTANTIATE_TEST_CASE_P( + Simple, DescriptorDatabaseTest, testing::Values(&SimpleDescriptorDatabaseTestCase::New)); -INSTANTIATE_TEST_CASE_P(MemoryConserving, DescriptorDatabaseTest, +INSTANTIATE_TEST_CASE_P( + MemoryConserving, DescriptorDatabaseTest, testing::Values(&EncodedDescriptorDatabaseTestCase::New)); INSTANTIATE_TEST_CASE_P(Pool, DescriptorDatabaseTest, - testing::Values(&DescriptorPoolDatabaseTestCase::New)); + testing::Values(&DescriptorPoolDatabaseTestCase::New)); #endif // GTEST_HAS_PARAM_TEST @@ -540,36 +536,40 @@ TEST(SimpleDescriptorDatabaseExtraTest, FindAllFileNames) { class MergedDescriptorDatabaseTest : public testing::Test { protected: MergedDescriptorDatabaseTest() - : forward_merged_(&database1_, &database2_), - reverse_merged_(&database2_, &database1_) {} + : forward_merged_(&database1_, &database2_), + reverse_merged_(&database2_, &database1_) {} virtual void SetUp() { - AddToDatabase(&database1_, - "name: \"foo.proto\" " - "message_type { name:\"Foo\" extension_range { start: 1 end: 100 } } " - "extension { name:\"foo_ext\" extendee: \".Foo\" number:3 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); - AddToDatabase(&database2_, - "name: \"bar.proto\" " - "message_type { name:\"Bar\" extension_range { start: 1 end: 100 } } " - "extension { name:\"bar_ext\" extendee: \".Bar\" number:5 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database1_, + "name: \"foo.proto\" " + "message_type { name:\"Foo\" extension_range { start: 1 end: 100 } } " + "extension { name:\"foo_ext\" extendee: \".Foo\" number:3 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database2_, + "name: \"bar.proto\" " + "message_type { name:\"Bar\" extension_range { start: 1 end: 100 } } " + "extension { name:\"bar_ext\" extendee: \".Bar\" number:5 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); // baz.proto exists in both pools, with different definitions. - AddToDatabase(&database1_, - "name: \"baz.proto\" " - "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " - "message_type { name:\"FromPool1\" } " - "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } " - "extension { name:\"database1_only_ext\" extendee: \".Baz\" number:13 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); - AddToDatabase(&database2_, - "name: \"baz.proto\" " - "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " - "message_type { name:\"FromPool2\" } " - "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database1_, + "name: \"baz.proto\" " + "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " + "message_type { name:\"FromPool1\" } " + "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } " + "extension { name:\"database1_only_ext\" extendee: \".Baz\" number:13 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + AddToDatabase( + &database2_, + "name: \"baz.proto\" " + "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } " + "message_type { name:\"FromPool2\" } " + "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); } SimpleDescriptorDatabase database1_; @@ -663,8 +663,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingSymbol) { { // Can't find non-existent symbol. FileDescriptorProto file; - EXPECT_FALSE( - forward_merged_.FindFileContainingSymbol("NoSuchType", &file)); + EXPECT_FALSE(forward_merged_.FindFileContainingSymbol("NoSuchType", &file)); } } @@ -672,8 +671,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can find file that is only in database1_. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Foo", 3, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Foo", 3, &file)); EXPECT_EQ("foo.proto", file.name()); ExpectContainsType(file, "Foo"); } @@ -681,8 +679,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can find file that is only in database2_. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Bar", 5, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Bar", 5, &file)); EXPECT_EQ("bar.proto", file.name()); ExpectContainsType(file, "Bar"); } @@ -690,8 +687,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // In forward_merged_, database1_'s baz.proto takes precedence. FileDescriptorProto file; - EXPECT_TRUE( - forward_merged_.FindFileContainingExtension("Baz", 12, &file)); + EXPECT_TRUE(forward_merged_.FindFileContainingExtension("Baz", 12, &file)); EXPECT_EQ("baz.proto", file.name()); ExpectContainsType(file, "FromPool1"); } @@ -699,8 +695,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // In reverse_merged_, database2_'s baz.proto takes precedence. FileDescriptorProto file; - EXPECT_TRUE( - reverse_merged_.FindFileContainingExtension("Baz", 12, &file)); + EXPECT_TRUE(reverse_merged_.FindFileContainingExtension("Baz", 12, &file)); EXPECT_EQ("baz.proto", file.name()); ExpectContainsType(file, "FromPool2"); } @@ -716,8 +711,7 @@ TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) { { // Can't find non-existent extension. FileDescriptorProto file; - EXPECT_FALSE( - forward_merged_.FindFileContainingExtension("Foo", 6, &file)); + EXPECT_FALSE(forward_merged_.FindFileContainingExtension("Foo", 6, &file)); } } diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index e93d01bdc0..f87947ff03 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -208,21 +208,43 @@ class MockErrorCollector : public DescriptorPool::ErrorCollector { const std::string& message) { const char* location_name = NULL; switch (location) { - case NAME : location_name = "NAME" ; break; - case NUMBER : location_name = "NUMBER" ; break; - case TYPE : location_name = "TYPE" ; break; - case EXTENDEE : location_name = "EXTENDEE" ; break; - case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break; - case OPTION_NAME : location_name = "OPTION_NAME" ; break; - case OPTION_VALUE : location_name = "OPTION_VALUE" ; break; - case INPUT_TYPE : location_name = "INPUT_TYPE" ; break; - case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break; - case OTHER : location_name = "OTHER" ; break; + case NAME: + location_name = "NAME"; + break; + case NUMBER: + location_name = "NUMBER"; + break; + case TYPE: + location_name = "TYPE"; + break; + case EXTENDEE: + location_name = "EXTENDEE"; + break; + case DEFAULT_VALUE: + location_name = "DEFAULT_VALUE"; + break; + case OPTION_NAME: + location_name = "OPTION_NAME"; + break; + case OPTION_VALUE: + location_name = "OPTION_VALUE"; + break; + case INPUT_TYPE: + location_name = "INPUT_TYPE"; + break; + case OUTPUT_TYPE: + location_name = "OUTPUT_TYPE"; + break; + case IMPORT: + location_name = "IMPORT"; + break; + case OTHER: + location_name = "OTHER"; + break; } - strings::SubstituteAndAppend( - &text_, "$0: $1: $2: $3\n", - filename, element_name, location_name, message); + strings::SubstituteAndAppend(&text_, "$0: $1: $2: $3\n", filename, + element_name, location_name, message); } // implements ErrorCollector --------------------------------------- @@ -231,21 +253,43 @@ class MockErrorCollector : public DescriptorPool::ErrorCollector { const std::string& message) { const char* location_name = NULL; switch (location) { - case NAME : location_name = "NAME" ; break; - case NUMBER : location_name = "NUMBER" ; break; - case TYPE : location_name = "TYPE" ; break; - case EXTENDEE : location_name = "EXTENDEE" ; break; - case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break; - case OPTION_NAME : location_name = "OPTION_NAME" ; break; - case OPTION_VALUE : location_name = "OPTION_VALUE" ; break; - case INPUT_TYPE : location_name = "INPUT_TYPE" ; break; - case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break; - case OTHER : location_name = "OTHER" ; break; + case NAME: + location_name = "NAME"; + break; + case NUMBER: + location_name = "NUMBER"; + break; + case TYPE: + location_name = "TYPE"; + break; + case EXTENDEE: + location_name = "EXTENDEE"; + break; + case DEFAULT_VALUE: + location_name = "DEFAULT_VALUE"; + break; + case OPTION_NAME: + location_name = "OPTION_NAME"; + break; + case OPTION_VALUE: + location_name = "OPTION_VALUE"; + break; + case INPUT_TYPE: + location_name = "INPUT_TYPE"; + break; + case OUTPUT_TYPE: + location_name = "OUTPUT_TYPE"; + break; + case IMPORT: + location_name = "IMPORT"; + break; + case OTHER: + location_name = "OTHER"; + break; } - strings::SubstituteAndAppend( - &warning_text_, "$0: $1: $2: $3\n", - filename, element_name, location_name, message); + strings::SubstituteAndAppend(&warning_text_, "$0: $1: $2: $3\n", filename, + element_name, location_name, message); } }; @@ -336,17 +380,17 @@ class FileDescriptorTest : public testing::Test { const FileDescriptor* bar_file_; const FileDescriptor* baz_file_; - const Descriptor* foo_message_; - const EnumDescriptor* foo_enum_; + const Descriptor* foo_message_; + const EnumDescriptor* foo_enum_; const EnumValueDescriptor* foo_enum_value_; - const ServiceDescriptor* foo_service_; - const FieldDescriptor* foo_extension_; + const ServiceDescriptor* foo_service_; + const FieldDescriptor* foo_extension_; - const Descriptor* bar_message_; - const EnumDescriptor* bar_enum_; + const Descriptor* bar_message_; + const EnumDescriptor* bar_enum_; const EnumValueDescriptor* bar_enum_value_; - const ServiceDescriptor* bar_service_; - const FieldDescriptor* bar_extension_; + const ServiceDescriptor* bar_service_; + const FieldDescriptor* bar_extension_; }; TEST_F(FileDescriptorTest, Name) { @@ -622,35 +666,28 @@ class DescriptorTest : public testing::Test { AddEmptyEnum(&foo_file, "TestEnum"); DescriptorProto* message = AddMessage(&foo_file, "TestMessage"); - AddField(message, "foo", 1, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message, "bar", 6, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "bar", 6, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_ENUM) - ->set_type_name("TestEnum"); - AddField(message, "baz", 500000000, - FieldDescriptorProto::LABEL_REPEATED, + ->set_type_name("TestEnum"); + AddField(message, "baz", 500000000, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_MESSAGE) - ->set_type_name("TestForeign"); - AddField(message, "qux", 15, - FieldDescriptorProto::LABEL_OPTIONAL, + ->set_type_name("TestForeign"); + AddField(message, "qux", 15, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_GROUP) - ->set_type_name("TestForeign"); + ->set_type_name("TestForeign"); FileDescriptorProto bar_file; bar_file.set_name("bar.proto"); bar_file.set_package("corge.grault"); DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2"); - AddField(message2, "foo", 1, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message2, "bar", 2, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "bar", 2, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); - AddField(message2, "quux", 6, - FieldDescriptorProto::LABEL_REQUIRED, + AddField(message2, "quux", 6, FieldDescriptorProto::LABEL_REQUIRED, FieldDescriptorProto::TYPE_STRING); FileDescriptorProto map_file; @@ -658,11 +695,9 @@ class DescriptorTest : public testing::Test { DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3"); DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry"); - AddField(entry, "key", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(entry, "key", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(entry, "value", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(entry, "value", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); entry->mutable_options()->set_map_entry(true); @@ -675,23 +710,17 @@ class DescriptorTest : public testing::Test { json_file.set_name("json.proto"); json_file.set_syntax("proto3"); DescriptorProto* message4 = AddMessage(&json_file, "TestMessage4"); - AddField(message4, "field_name1", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "field_name1", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "fieldName2", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "fieldName2", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "FieldName3", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "FieldName3", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "_field_name4", 4, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "_field_name4", 4, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "FIELD_NAME5", 5, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "FIELD_NAME5", 5, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message4, "field_name6", 6, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message4, "field_name6", 6, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32) ->set_json_name("@type"); @@ -725,15 +754,15 @@ class DescriptorTest : public testing::Test { message2_ = bar_file_->message_type(0); ASSERT_EQ(3, message2_->field_count()); - foo2_ = message2_->field(0); - bar2_ = message2_->field(1); + foo2_ = message2_->field(0); + bar2_ = message2_->field(1); quux2_ = message2_->field(2); ASSERT_EQ(1, map_file_->message_type_count()); message3_ = map_file_->message_type(0); ASSERT_EQ(1, message3_->field_count()); - map_ = message3_->field(0); + map_ = message3_->field(0); ASSERT_EQ(1, json_file_->message_type_count()); message4_ = json_file_->message_type(0); @@ -806,8 +835,8 @@ TEST_F(DescriptorTest, FindFieldByName) { EXPECT_TRUE(message_->FindFieldByName("no_such_field") == NULL); EXPECT_TRUE(message_->FindFieldByName("quux") == NULL); - EXPECT_EQ(foo2_ , message2_->FindFieldByName("foo" )); - EXPECT_EQ(bar2_ , message2_->FindFieldByName("bar" )); + EXPECT_EQ(foo2_, message2_->FindFieldByName("foo")); + EXPECT_EQ(bar2_, message2_->FindFieldByName("bar")); EXPECT_EQ(quux2_, message2_->FindFieldByName("quux")); EXPECT_TRUE(message2_->FindFieldByName("baz") == NULL); EXPECT_TRUE(message2_->FindFieldByName("qux") == NULL); @@ -821,8 +850,8 @@ TEST_F(DescriptorTest, FindFieldByNumber) { EXPECT_TRUE(message_->FindFieldByNumber(837592) == NULL); EXPECT_TRUE(message_->FindFieldByNumber(2) == NULL); - EXPECT_EQ(foo2_ , message2_->FindFieldByNumber(1)); - EXPECT_EQ(bar2_ , message2_->FindFieldByNumber(2)); + EXPECT_EQ(foo2_, message2_->FindFieldByNumber(1)); + EXPECT_EQ(bar2_, message2_->FindFieldByNumber(2)); EXPECT_EQ(quux2_, message2_->FindFieldByNumber(6)); EXPECT_TRUE(message2_->FindFieldByNumber(15) == NULL); EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == NULL); @@ -904,17 +933,17 @@ TEST_F(DescriptorTest, FieldIndex) { } TEST_F(DescriptorTest, FieldNumber) { - EXPECT_EQ( 1, foo_->number()); - EXPECT_EQ( 6, bar_->number()); + EXPECT_EQ(1, foo_->number()); + EXPECT_EQ(6, bar_->number()); EXPECT_EQ(500000000, baz_->number()); - EXPECT_EQ( 15, qux_->number()); + EXPECT_EQ(15, qux_->number()); } TEST_F(DescriptorTest, FieldType) { - EXPECT_EQ(FieldDescriptor::TYPE_STRING , foo_->type()); - EXPECT_EQ(FieldDescriptor::TYPE_ENUM , bar_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_STRING, foo_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_ENUM, bar_->type()); EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_->type()); - EXPECT_EQ(FieldDescriptor::TYPE_GROUP , qux_->type()); + EXPECT_EQ(FieldDescriptor::TYPE_GROUP, qux_->type()); } TEST_F(DescriptorTest, FieldLabel) { @@ -923,17 +952,17 @@ TEST_F(DescriptorTest, FieldLabel) { EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, baz_->label()); EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, qux_->label()); - EXPECT_TRUE (foo_->is_required()); + EXPECT_TRUE(foo_->is_required()); EXPECT_FALSE(foo_->is_optional()); EXPECT_FALSE(foo_->is_repeated()); EXPECT_FALSE(bar_->is_required()); - EXPECT_TRUE (bar_->is_optional()); + EXPECT_TRUE(bar_->is_optional()); EXPECT_FALSE(bar_->is_repeated()); EXPECT_FALSE(baz_->is_required()); EXPECT_FALSE(baz_->is_optional()); - EXPECT_TRUE (baz_->is_repeated()); + EXPECT_TRUE(baz_->is_repeated()); } TEST_F(DescriptorTest, IsMap) { @@ -955,8 +984,8 @@ TEST_F(DescriptorTest, FieldContainingType) { EXPECT_EQ(message_, baz_->containing_type()); EXPECT_EQ(message_, qux_->containing_type()); - EXPECT_EQ(message2_, foo2_ ->containing_type()); - EXPECT_EQ(message2_, bar2_ ->containing_type()); + EXPECT_EQ(message2_, foo2_->containing_type()); + EXPECT_EQ(message2_, bar2_->containing_type()); EXPECT_EQ(message2_, quux2_->containing_type()); } @@ -1005,21 +1034,17 @@ class OneofDescriptorTest : public testing::Test { oneof_message->add_oneof_decl()->set_name("foo"); oneof_message->add_oneof_decl()->set_name("bar"); - AddField(oneof_message, "a", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "a", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(oneof_message, "b", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "b", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_STRING); oneof_message->mutable_field(1)->set_oneof_index(0); - AddField(oneof_message, "c", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "c", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_MESSAGE); oneof_message->mutable_field(2)->set_oneof_index(0); oneof_message->mutable_field(2)->set_type_name("TestOneof"); - AddField(oneof_message, "d", 4, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(oneof_message, "d", 4, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_FLOAT); oneof_message->mutable_field(3)->set_oneof_index(1); @@ -1086,14 +1111,11 @@ class StylizedFieldNamesTest : public testing::Test { AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000); DescriptorProto* message = AddMessage(&file, "TestMessage"); - AddField(message, "foo_foo", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "foo_foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message, "FooBar", 2, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "FooBar", 2, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); - AddField(message, "fooBaz", 3, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message, "fooBaz", 3, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); AddField(message, "fooFoo", 4, // Camel-case conflict with foo_foo. FieldDescriptorProto::LABEL_OPTIONAL, @@ -1151,22 +1173,22 @@ class StylizedFieldNamesTest : public testing::Test { TEST_F(StylizedFieldNamesTest, LowercaseName) { EXPECT_EQ("foo_foo", message_->field(0)->lowercase_name()); - EXPECT_EQ("foobar" , message_->field(1)->lowercase_name()); - EXPECT_EQ("foobaz" , message_->field(2)->lowercase_name()); - EXPECT_EQ("foofoo" , message_->field(3)->lowercase_name()); - EXPECT_EQ("foobar" , message_->field(4)->lowercase_name()); + EXPECT_EQ("foobar", message_->field(1)->lowercase_name()); + EXPECT_EQ("foobaz", message_->field(2)->lowercase_name()); + EXPECT_EQ("foofoo", message_->field(3)->lowercase_name()); + EXPECT_EQ("foobar", message_->field(4)->lowercase_name()); EXPECT_EQ("bar_foo", message_->extension(0)->lowercase_name()); - EXPECT_EQ("barbar" , message_->extension(1)->lowercase_name()); - EXPECT_EQ("barbaz" , message_->extension(2)->lowercase_name()); - EXPECT_EQ("barfoo" , message_->extension(3)->lowercase_name()); - EXPECT_EQ("barbar" , message_->extension(4)->lowercase_name()); + EXPECT_EQ("barbar", message_->extension(1)->lowercase_name()); + EXPECT_EQ("barbaz", message_->extension(2)->lowercase_name()); + EXPECT_EQ("barfoo", message_->extension(3)->lowercase_name()); + EXPECT_EQ("barbar", message_->extension(4)->lowercase_name()); EXPECT_EQ("baz_foo", file_->extension(0)->lowercase_name()); - EXPECT_EQ("bazbar" , file_->extension(1)->lowercase_name()); - EXPECT_EQ("bazbaz" , file_->extension(2)->lowercase_name()); - EXPECT_EQ("bazfoo" , file_->extension(3)->lowercase_name()); - EXPECT_EQ("bazbar" , file_->extension(4)->lowercase_name()); + EXPECT_EQ("bazbar", file_->extension(1)->lowercase_name()); + EXPECT_EQ("bazbaz", file_->extension(2)->lowercase_name()); + EXPECT_EQ("bazfoo", file_->extension(3)->lowercase_name()); + EXPECT_EQ("bazbar", file_->extension(4)->lowercase_name()); } TEST_F(StylizedFieldNamesTest, CamelcaseName) { @@ -1190,12 +1212,9 @@ TEST_F(StylizedFieldNamesTest, CamelcaseName) { } TEST_F(StylizedFieldNamesTest, FindByLowercaseName) { - EXPECT_EQ(message_->field(0), - message_->FindFieldByLowercaseName("foo_foo")); - EXPECT_EQ(message_->field(1), - message_->FindFieldByLowercaseName("foobar")); - EXPECT_EQ(message_->field(2), - message_->FindFieldByLowercaseName("foobaz")); + EXPECT_EQ(message_->field(0), message_->FindFieldByLowercaseName("foo_foo")); + EXPECT_EQ(message_->field(1), message_->FindFieldByLowercaseName("foobar")); + EXPECT_EQ(message_->field(2), message_->FindFieldByLowercaseName("foobaz")); EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == NULL); EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == NULL); EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == NULL); @@ -1214,22 +1233,17 @@ TEST_F(StylizedFieldNamesTest, FindByLowercaseName) { EXPECT_EQ(file_->extension(0), file_->FindExtensionByLowercaseName("baz_foo")); - EXPECT_EQ(file_->extension(1), - file_->FindExtensionByLowercaseName("bazbar")); - EXPECT_EQ(file_->extension(2), - file_->FindExtensionByLowercaseName("bazbaz")); + EXPECT_EQ(file_->extension(1), file_->FindExtensionByLowercaseName("bazbar")); + EXPECT_EQ(file_->extension(2), file_->FindExtensionByLowercaseName("bazbaz")); EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == NULL); EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == NULL); EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == NULL); } TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) { - EXPECT_EQ(message_->field(0), - message_->FindFieldByCamelcaseName("fooFoo")); - EXPECT_EQ(message_->field(1), - message_->FindFieldByCamelcaseName("fooBar")); - EXPECT_EQ(message_->field(2), - message_->FindFieldByCamelcaseName("fooBaz")); + EXPECT_EQ(message_->field(0), message_->FindFieldByCamelcaseName("fooFoo")); + EXPECT_EQ(message_->field(1), message_->FindFieldByCamelcaseName("fooBar")); + EXPECT_EQ(message_->field(2), message_->FindFieldByCamelcaseName("fooBaz")); EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == NULL); EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == NULL); EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == NULL); @@ -1246,12 +1260,9 @@ TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) { EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == NULL); EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == NULL); - EXPECT_EQ(file_->extension(0), - file_->FindExtensionByCamelcaseName("bazFoo")); - EXPECT_EQ(file_->extension(1), - file_->FindExtensionByCamelcaseName("bazBar")); - EXPECT_EQ(file_->extension(2), - file_->FindExtensionByCamelcaseName("bazBaz")); + EXPECT_EQ(file_->extension(0), file_->FindExtensionByCamelcaseName("bazFoo")); + EXPECT_EQ(file_->extension(1), file_->FindExtensionByCamelcaseName("bazBar")); + EXPECT_EQ(file_->extension(2), file_->FindExtensionByCamelcaseName("bazBaz")); EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == NULL); EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == NULL); EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == NULL); @@ -1358,24 +1369,24 @@ TEST_F(EnumDescriptorTest, ValuesByIndex) { } TEST_F(EnumDescriptorTest, FindValueByName) { - EXPECT_EQ(foo_ , enum_ ->FindValueByName("FOO")); - EXPECT_EQ(bar_ , enum_ ->FindValueByName("BAR")); + EXPECT_EQ(foo_, enum_->FindValueByName("FOO")); + EXPECT_EQ(bar_, enum_->FindValueByName("BAR")); EXPECT_EQ(foo2_, enum2_->FindValueByName("FOO")); EXPECT_EQ(baz2_, enum2_->FindValueByName("BAZ")); - EXPECT_TRUE(enum_ ->FindValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(enum_ ->FindValueByName("BAZ" ) == NULL); - EXPECT_TRUE(enum2_->FindValueByName("BAR" ) == NULL); + EXPECT_TRUE(enum_->FindValueByName("NO_SUCH_VALUE") == NULL); + EXPECT_TRUE(enum_->FindValueByName("BAZ") == NULL); + EXPECT_TRUE(enum2_->FindValueByName("BAR") == NULL); } TEST_F(EnumDescriptorTest, FindValueByNumber) { - EXPECT_EQ(foo_ , enum_ ->FindValueByNumber(1)); - EXPECT_EQ(bar_ , enum_ ->FindValueByNumber(2)); + EXPECT_EQ(foo_, enum_->FindValueByNumber(1)); + EXPECT_EQ(bar_, enum_->FindValueByNumber(2)); EXPECT_EQ(foo2_, enum2_->FindValueByNumber(1)); EXPECT_EQ(baz2_, enum2_->FindValueByNumber(3)); - EXPECT_TRUE(enum_ ->FindValueByNumber(416) == NULL); - EXPECT_TRUE(enum_ ->FindValueByNumber(3) == NULL); + EXPECT_TRUE(enum_->FindValueByNumber(416) == NULL); + EXPECT_TRUE(enum_->FindValueByNumber(3) == NULL); EXPECT_TRUE(enum2_->FindValueByNumber(2) == NULL); } @@ -1402,8 +1413,8 @@ TEST_F(EnumDescriptorTest, ValueNumber) { } TEST_F(EnumDescriptorTest, ValueType) { - EXPECT_EQ(enum_ , foo_ ->type()); - EXPECT_EQ(enum_ , bar_ ->type()); + EXPECT_EQ(enum_, foo_->type()); + EXPECT_EQ(enum_, bar_->type()); EXPECT_EQ(enum2_, foo2_->type()); EXPECT_EQ(enum2_, baz2_->type()); } @@ -1466,11 +1477,11 @@ class ServiceDescriptorTest : public testing::Test { ASSERT_TRUE(bar_file_ != NULL); ASSERT_EQ(6, foo_file_->message_type_count()); - foo_request_ = foo_file_->message_type(0); + foo_request_ = foo_file_->message_type(0); foo_response_ = foo_file_->message_type(1); - bar_request_ = foo_file_->message_type(2); + bar_request_ = foo_file_->message_type(2); bar_response_ = foo_file_->message_type(3); - baz_request_ = foo_file_->message_type(4); + baz_request_ = foo_file_->message_type(4); baz_response_ = foo_file_->message_type(5); ASSERT_EQ(1, foo_file_->service_count()); @@ -1527,14 +1538,14 @@ TEST_F(ServiceDescriptorTest, MethodsByIndex) { } TEST_F(ServiceDescriptorTest, FindMethodByName) { - EXPECT_EQ(foo_ , service_ ->FindMethodByName("Foo")); - EXPECT_EQ(bar_ , service_ ->FindMethodByName("Bar")); + EXPECT_EQ(foo_, service_->FindMethodByName("Foo")); + EXPECT_EQ(bar_, service_->FindMethodByName("Bar")); EXPECT_EQ(foo2_, service2_->FindMethodByName("Foo")); EXPECT_EQ(baz2_, service2_->FindMethodByName("Baz")); - EXPECT_TRUE(service_ ->FindMethodByName("NoSuchMethod") == NULL); - EXPECT_TRUE(service_ ->FindMethodByName("Baz" ) == NULL); - EXPECT_TRUE(service2_->FindMethodByName("Bar" ) == NULL); + EXPECT_TRUE(service_->FindMethodByName("NoSuchMethod") == NULL); + EXPECT_TRUE(service_->FindMethodByName("Baz") == NULL); + EXPECT_TRUE(service2_->FindMethodByName("Bar") == NULL); } TEST_F(ServiceDescriptorTest, MethodName) { @@ -1688,8 +1699,8 @@ class NestedDescriptorTest : public testing::Test { }; TEST_F(NestedDescriptorTest, MessageName) { - EXPECT_EQ("Foo", foo_ ->name()); - EXPECT_EQ("Bar", bar_ ->name()); + EXPECT_EQ("Foo", foo_->name()); + EXPECT_EQ("Bar", bar_->name()); EXPECT_EQ("Foo", foo2_->name()); EXPECT_EQ("Baz", baz2_->name()); @@ -1700,8 +1711,8 @@ TEST_F(NestedDescriptorTest, MessageName) { } TEST_F(NestedDescriptorTest, MessageContainingType) { - EXPECT_EQ(message_ , foo_ ->containing_type()); - EXPECT_EQ(message_ , bar_ ->containing_type()); + EXPECT_EQ(message_, foo_->containing_type()); + EXPECT_EQ(message_, bar_->containing_type()); EXPECT_EQ(message2_, foo2_->containing_type()); EXPECT_EQ(message2_, baz2_->containing_type()); } @@ -1720,34 +1731,34 @@ TEST_F(NestedDescriptorTest, FindFieldByNameDoesntFindNestedTypes) { } TEST_F(NestedDescriptorTest, FindNestedTypeByName) { - EXPECT_EQ(foo_ , message_ ->FindNestedTypeByName("Foo")); - EXPECT_EQ(bar_ , message_ ->FindNestedTypeByName("Bar")); + EXPECT_EQ(foo_, message_->FindNestedTypeByName("Foo")); + EXPECT_EQ(bar_, message_->FindNestedTypeByName("Bar")); EXPECT_EQ(foo2_, message2_->FindNestedTypeByName("Foo")); EXPECT_EQ(baz2_, message2_->FindNestedTypeByName("Baz")); - EXPECT_TRUE(message_ ->FindNestedTypeByName("NoSuchType") == NULL); - EXPECT_TRUE(message_ ->FindNestedTypeByName("Baz" ) == NULL); - EXPECT_TRUE(message2_->FindNestedTypeByName("Bar" ) == NULL); + EXPECT_TRUE(message_->FindNestedTypeByName("NoSuchType") == NULL); + EXPECT_TRUE(message_->FindNestedTypeByName("Baz") == NULL); + EXPECT_TRUE(message2_->FindNestedTypeByName("Bar") == NULL); EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == NULL); } TEST_F(NestedDescriptorTest, EnumName) { - EXPECT_EQ("Baz" , baz_ ->name()); - EXPECT_EQ("Qux" , qux_ ->name()); - EXPECT_EQ("Qux" , qux2_->name()); + EXPECT_EQ("Baz", baz_->name()); + EXPECT_EQ("Qux", qux_->name()); + EXPECT_EQ("Qux", qux2_->name()); EXPECT_EQ("Quux", quux2_->name()); EXPECT_EQ("TestMessage.Baz", baz_->full_name()); EXPECT_EQ("TestMessage.Qux", qux_->full_name()); - EXPECT_EQ("corge.grault.TestMessage2.Qux" , qux2_ ->full_name()); + EXPECT_EQ("corge.grault.TestMessage2.Qux", qux2_->full_name()); EXPECT_EQ("corge.grault.TestMessage2.Quux", quux2_->full_name()); } TEST_F(NestedDescriptorTest, EnumContainingType) { - EXPECT_EQ(message_ , baz_ ->containing_type()); - EXPECT_EQ(message_ , qux_ ->containing_type()); - EXPECT_EQ(message2_, qux2_ ->containing_type()); + EXPECT_EQ(message_, baz_->containing_type()); + EXPECT_EQ(message_, qux_->containing_type()); + EXPECT_EQ(message2_, qux2_->containing_type()); EXPECT_EQ(message2_, quux2_->containing_type()); } @@ -1758,27 +1769,27 @@ TEST_F(NestedDescriptorTest, NestedEnumsByIndex) { } TEST_F(NestedDescriptorTest, FindEnumTypeByName) { - EXPECT_EQ(baz_ , message_ ->FindEnumTypeByName("Baz" )); - EXPECT_EQ(qux_ , message_ ->FindEnumTypeByName("Qux" )); - EXPECT_EQ(qux2_ , message2_->FindEnumTypeByName("Qux" )); + EXPECT_EQ(baz_, message_->FindEnumTypeByName("Baz")); + EXPECT_EQ(qux_, message_->FindEnumTypeByName("Qux")); + EXPECT_EQ(qux2_, message2_->FindEnumTypeByName("Qux")); EXPECT_EQ(quux2_, message2_->FindEnumTypeByName("Quux")); - EXPECT_TRUE(message_ ->FindEnumTypeByName("NoSuchType") == NULL); - EXPECT_TRUE(message_ ->FindEnumTypeByName("Quux" ) == NULL); - EXPECT_TRUE(message2_->FindEnumTypeByName("Baz" ) == NULL); + EXPECT_TRUE(message_->FindEnumTypeByName("NoSuchType") == NULL); + EXPECT_TRUE(message_->FindEnumTypeByName("Quux") == NULL); + EXPECT_TRUE(message2_->FindEnumTypeByName("Baz") == NULL); EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == NULL); } TEST_F(NestedDescriptorTest, FindEnumValueByName) { - EXPECT_EQ(a_ , message_ ->FindEnumValueByName("A")); - EXPECT_EQ(b_ , message_ ->FindEnumValueByName("B")); + EXPECT_EQ(a_, message_->FindEnumValueByName("A")); + EXPECT_EQ(b_, message_->FindEnumValueByName("B")); EXPECT_EQ(a2_, message2_->FindEnumValueByName("A")); EXPECT_EQ(c2_, message2_->FindEnumValueByName("C")); - EXPECT_TRUE(message_ ->FindEnumValueByName("NO_SUCH_VALUE") == NULL); - EXPECT_TRUE(message_ ->FindEnumValueByName("C" ) == NULL); - EXPECT_TRUE(message2_->FindEnumValueByName("B" ) == NULL); + EXPECT_TRUE(message_->FindEnumValueByName("NO_SUCH_VALUE") == NULL); + EXPECT_TRUE(message_->FindEnumValueByName("C") == NULL); + EXPECT_TRUE(message2_->FindEnumValueByName("B") == NULL); EXPECT_TRUE(message_->FindEnumValueByName("Foo") == NULL); } @@ -1822,17 +1833,17 @@ class ExtensionDescriptorTest : public testing::Test { AddExtension(&foo_file, "Foo", "foo_enum", 19, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_ENUM) - ->set_type_name("Baz"); + ->set_type_name("Baz"); DescriptorProto* bar = AddMessage(&foo_file, "Bar"); AddNestedExtension(bar, "Foo", "foo_message", 30, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_MESSAGE) - ->set_type_name("Qux"); + ->set_type_name("Qux"); AddNestedExtension(bar, "Foo", "foo_group", 39, FieldDescriptorProto::LABEL_REPEATED, FieldDescriptorProto::TYPE_GROUP) - ->set_type_name("Qux"); + ->set_type_name("Qux"); // Build the descriptors and get the pointers. foo_file_ = pool_.BuildFile(foo_file); @@ -1878,20 +1889,20 @@ TEST_F(ExtensionDescriptorTest, Extensions) { EXPECT_TRUE(bar_->extension(0)->is_extension()); EXPECT_TRUE(bar_->extension(1)->is_extension()); - EXPECT_EQ("foo_int32" , foo_file_->extension(0)->name()); - EXPECT_EQ("foo_enum" , foo_file_->extension(1)->name()); + EXPECT_EQ("foo_int32", foo_file_->extension(0)->name()); + EXPECT_EQ("foo_enum", foo_file_->extension(1)->name()); EXPECT_EQ("foo_message", bar_->extension(0)->name()); - EXPECT_EQ("foo_group" , bar_->extension(1)->name()); + EXPECT_EQ("foo_group", bar_->extension(1)->name()); EXPECT_EQ(10, foo_file_->extension(0)->number()); EXPECT_EQ(19, foo_file_->extension(1)->number()); EXPECT_EQ(30, bar_->extension(0)->number()); EXPECT_EQ(39, bar_->extension(1)->number()); - EXPECT_EQ(FieldDescriptor::TYPE_INT32 , foo_file_->extension(0)->type()); - EXPECT_EQ(FieldDescriptor::TYPE_ENUM , foo_file_->extension(1)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_INT32, foo_file_->extension(0)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_ENUM, foo_file_->extension(1)->type()); EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_->extension(0)->type()); - EXPECT_EQ(FieldDescriptor::TYPE_GROUP , bar_->extension(1)->type()); + EXPECT_EQ(FieldDescriptor::TYPE_GROUP, bar_->extension(1)->type()); EXPECT_EQ(baz_, foo_file_->extension(1)->enum_type()); EXPECT_EQ(qux_, bar_->extension(0)->message_type()); @@ -1914,13 +1925,13 @@ TEST_F(ExtensionDescriptorTest, Extensions) { }; TEST_F(ExtensionDescriptorTest, IsExtensionNumber) { - EXPECT_FALSE(foo_->IsExtensionNumber( 9)); - EXPECT_TRUE (foo_->IsExtensionNumber(10)); - EXPECT_TRUE (foo_->IsExtensionNumber(19)); + EXPECT_FALSE(foo_->IsExtensionNumber(9)); + EXPECT_TRUE(foo_->IsExtensionNumber(10)); + EXPECT_TRUE(foo_->IsExtensionNumber(19)); EXPECT_FALSE(foo_->IsExtensionNumber(20)); EXPECT_FALSE(foo_->IsExtensionNumber(29)); - EXPECT_TRUE (foo_->IsExtensionNumber(30)); - EXPECT_TRUE (foo_->IsExtensionNumber(39)); + EXPECT_TRUE(foo_->IsExtensionNumber(30)); + EXPECT_TRUE(foo_->IsExtensionNumber(39)); EXPECT_FALSE(foo_->IsExtensionNumber(40)); } @@ -1930,7 +1941,7 @@ TEST_F(ExtensionDescriptorTest, FindExtensionByName) { ASSERT_EQ(2, bar_->extension_count()); EXPECT_EQ(bar_->extension(0), bar_->FindExtensionByName("foo_message")); - EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group" )); + EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group")); EXPECT_TRUE(bar_->FindExtensionByName("no_such_extension") == NULL); EXPECT_TRUE(foo_->FindExtensionByName("foo_int32") == NULL); @@ -2034,16 +2045,16 @@ TEST_F(ReservedDescriptorTest, ReservedRanges) { TEST_F(ReservedDescriptorTest, IsReservedNumber) { EXPECT_FALSE(foo_->IsReservedNumber(1)); - EXPECT_TRUE (foo_->IsReservedNumber(2)); + EXPECT_TRUE(foo_->IsReservedNumber(2)); EXPECT_FALSE(foo_->IsReservedNumber(3)); EXPECT_FALSE(foo_->IsReservedNumber(8)); - EXPECT_TRUE (foo_->IsReservedNumber(9)); - EXPECT_TRUE (foo_->IsReservedNumber(10)); - EXPECT_TRUE (foo_->IsReservedNumber(11)); + EXPECT_TRUE(foo_->IsReservedNumber(9)); + EXPECT_TRUE(foo_->IsReservedNumber(10)); + EXPECT_TRUE(foo_->IsReservedNumber(11)); EXPECT_FALSE(foo_->IsReservedNumber(12)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(14)); - EXPECT_TRUE (foo_->IsReservedNumber(15)); + EXPECT_TRUE(foo_->IsReservedNumber(15)); EXPECT_FALSE(foo_->IsReservedNumber(16)); }; @@ -2055,8 +2066,8 @@ TEST_F(ReservedDescriptorTest, ReservedNames) { }; TEST_F(ReservedDescriptorTest, IsReservedName) { - EXPECT_TRUE (foo_->IsReservedName("foo")); - EXPECT_TRUE (foo_->IsReservedName("bar")); + EXPECT_TRUE(foo_->IsReservedName("foo")); + EXPECT_TRUE(foo_->IsReservedName("bar")); EXPECT_FALSE(foo_->IsReservedName("baz")); }; @@ -2153,17 +2164,17 @@ TEST_F(ReservedEnumDescriptorTest, IsReservedNumber) { EXPECT_TRUE(foo_->IsReservedNumber(-1)); EXPECT_TRUE(foo_->IsReservedNumber(0)); EXPECT_TRUE(foo_->IsReservedNumber(1)); - EXPECT_TRUE (foo_->IsReservedNumber(2)); + EXPECT_TRUE(foo_->IsReservedNumber(2)); EXPECT_TRUE(foo_->IsReservedNumber(3)); EXPECT_FALSE(foo_->IsReservedNumber(8)); - EXPECT_TRUE (foo_->IsReservedNumber(9)); - EXPECT_TRUE (foo_->IsReservedNumber(10)); - EXPECT_TRUE (foo_->IsReservedNumber(11)); + EXPECT_TRUE(foo_->IsReservedNumber(9)); + EXPECT_TRUE(foo_->IsReservedNumber(10)); + EXPECT_TRUE(foo_->IsReservedNumber(11)); EXPECT_TRUE(foo_->IsReservedNumber(12)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(13)); EXPECT_FALSE(foo_->IsReservedNumber(14)); - EXPECT_TRUE (foo_->IsReservedNumber(15)); + EXPECT_TRUE(foo_->IsReservedNumber(15)); EXPECT_TRUE(foo_->IsReservedNumber(16)); EXPECT_FALSE(foo_->IsReservedNumber(17)); @@ -2186,8 +2197,8 @@ TEST_F(ReservedEnumDescriptorTest, ReservedNames) { } TEST_F(ReservedEnumDescriptorTest, IsReservedName) { - EXPECT_TRUE (foo_->IsReservedName("foo")); - EXPECT_TRUE (foo_->IsReservedName("bar")); + EXPECT_TRUE(foo_->IsReservedName("foo")); + EXPECT_TRUE(foo_->IsReservedName("bar")); EXPECT_FALSE(foo_->IsReservedName("baz")); } @@ -2202,9 +2213,9 @@ class MiscTest : public testing::Test { AddEmptyEnum(&file_proto, "DummyEnum"); DescriptorProto* message = AddMessage(&file_proto, "TestMessage"); - FieldDescriptorProto* field = - AddField(message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, - static_cast<FieldDescriptorProto::Type>(static_cast<int>(type))); + FieldDescriptorProto* field = AddField( + message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, + static_cast<FieldDescriptorProto::Type>(static_cast<int>(type))); if (type == FieldDescriptor::TYPE_MESSAGE || type == FieldDescriptor::TYPE_GROUP) { @@ -2217,8 +2228,7 @@ class MiscTest : public testing::Test { pool_.reset(new DescriptorPool()); const FileDescriptor* file = pool_->BuildFile(file_proto); - if (file != NULL && - file->message_type_count() == 1 && + if (file != NULL && file->message_type_count() == 1 && file->message_type(0)->field_count() == 1) { return file->message_type(0)->field(0); } else { @@ -2233,8 +2243,8 @@ class MiscTest : public testing::Test { FieldDescriptor::CppType GetCppTypeForFieldType(FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); - return field != NULL ? field->cpp_type() : - static_cast<FieldDescriptor::CppType>(0); + return field != NULL ? field->cpp_type() + : static_cast<FieldDescriptor::CppType>(0); } const char* GetCppTypeNameForFieldType(FieldDescriptor::Type type) { @@ -2249,7 +2259,7 @@ class MiscTest : public testing::Test { } const EnumDescriptor* GetEnumDescriptorForFieldType( - FieldDescriptor::Type type) { + FieldDescriptor::Type type) { const FieldDescriptor* field = GetFieldDescriptorOfType(type); return field != NULL ? field->enum_type() : NULL; } @@ -2262,24 +2272,24 @@ TEST_F(MiscTest, TypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , GetTypeNameForFieldType(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , GetTypeNameForFieldType(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , GetTypeNameForFieldType(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , GetTypeNameForFieldType(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , GetTypeNameForFieldType(FD::TYPE_INT32 )); - EXPECT_STREQ("fixed64" , GetTypeNameForFieldType(FD::TYPE_FIXED64 )); - EXPECT_STREQ("fixed32" , GetTypeNameForFieldType(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , GetTypeNameForFieldType(FD::TYPE_BOOL )); + EXPECT_STREQ("double", GetTypeNameForFieldType(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", GetTypeNameForFieldType(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", GetTypeNameForFieldType(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", GetTypeNameForFieldType(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", GetTypeNameForFieldType(FD::TYPE_INT32)); + EXPECT_STREQ("fixed64", GetTypeNameForFieldType(FD::TYPE_FIXED64)); + EXPECT_STREQ("fixed32", GetTypeNameForFieldType(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", GetTypeNameForFieldType(FD::TYPE_BOOL)); EXPECT_STREQ("string", GetTypeNameForFieldType(FD::TYPE_STRING)); - EXPECT_STREQ("group" , GetTypeNameForFieldType(FD::TYPE_GROUP )); - EXPECT_STREQ("message" , GetTypeNameForFieldType(FD::TYPE_MESSAGE )); - EXPECT_STREQ("bytes" , GetTypeNameForFieldType(FD::TYPE_BYTES )); - EXPECT_STREQ("uint32" , GetTypeNameForFieldType(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , GetTypeNameForFieldType(FD::TYPE_ENUM )); + EXPECT_STREQ("group", GetTypeNameForFieldType(FD::TYPE_GROUP)); + EXPECT_STREQ("message", GetTypeNameForFieldType(FD::TYPE_MESSAGE)); + EXPECT_STREQ("bytes", GetTypeNameForFieldType(FD::TYPE_BYTES)); + EXPECT_STREQ("uint32", GetTypeNameForFieldType(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", GetTypeNameForFieldType(FD::TYPE_ENUM)); EXPECT_STREQ("sfixed32", GetTypeNameForFieldType(FD::TYPE_SFIXED32)); EXPECT_STREQ("sfixed64", GetTypeNameForFieldType(FD::TYPE_SFIXED64)); - EXPECT_STREQ("sint32" , GetTypeNameForFieldType(FD::TYPE_SINT32 )); - EXPECT_STREQ("sint64" , GetTypeNameForFieldType(FD::TYPE_SINT64 )); + EXPECT_STREQ("sint32", GetTypeNameForFieldType(FD::TYPE_SINT32)); + EXPECT_STREQ("sint64", GetTypeNameForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, StaticTypeNames) { @@ -2287,24 +2297,24 @@ TEST_F(MiscTest, StaticTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , FD::TypeName(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , FD::TypeName(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , FD::TypeName(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , FD::TypeName(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , FD::TypeName(FD::TYPE_INT32 )); - EXPECT_STREQ("fixed64" , FD::TypeName(FD::TYPE_FIXED64 )); - EXPECT_STREQ("fixed32" , FD::TypeName(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , FD::TypeName(FD::TYPE_BOOL )); + EXPECT_STREQ("double", FD::TypeName(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", FD::TypeName(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", FD::TypeName(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", FD::TypeName(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", FD::TypeName(FD::TYPE_INT32)); + EXPECT_STREQ("fixed64", FD::TypeName(FD::TYPE_FIXED64)); + EXPECT_STREQ("fixed32", FD::TypeName(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", FD::TypeName(FD::TYPE_BOOL)); EXPECT_STREQ("string", FD::TypeName(FD::TYPE_STRING)); - EXPECT_STREQ("group" , FD::TypeName(FD::TYPE_GROUP )); - EXPECT_STREQ("message" , FD::TypeName(FD::TYPE_MESSAGE )); - EXPECT_STREQ("bytes" , FD::TypeName(FD::TYPE_BYTES )); - EXPECT_STREQ("uint32" , FD::TypeName(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , FD::TypeName(FD::TYPE_ENUM )); + EXPECT_STREQ("group", FD::TypeName(FD::TYPE_GROUP)); + EXPECT_STREQ("message", FD::TypeName(FD::TYPE_MESSAGE)); + EXPECT_STREQ("bytes", FD::TypeName(FD::TYPE_BYTES)); + EXPECT_STREQ("uint32", FD::TypeName(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", FD::TypeName(FD::TYPE_ENUM)); EXPECT_STREQ("sfixed32", FD::TypeName(FD::TYPE_SFIXED32)); EXPECT_STREQ("sfixed64", FD::TypeName(FD::TYPE_SFIXED64)); - EXPECT_STREQ("sint32" , FD::TypeName(FD::TYPE_SINT32 )); - EXPECT_STREQ("sint64" , FD::TypeName(FD::TYPE_SINT64 )); + EXPECT_STREQ("sint32", FD::TypeName(FD::TYPE_SINT32)); + EXPECT_STREQ("sint64", FD::TypeName(FD::TYPE_SINT64)); } TEST_F(MiscTest, CppTypes) { @@ -2312,24 +2322,24 @@ TEST_F(MiscTest, CppTypes) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_EQ(FD::CPPTYPE_DOUBLE , GetCppTypeForFieldType(FD::TYPE_DOUBLE )); - EXPECT_EQ(FD::CPPTYPE_FLOAT , GetCppTypeForFieldType(FD::TYPE_FLOAT )); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_INT64 )); - EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_UINT64 )); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_INT32 )); - EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_FIXED64 )); - EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_FIXED32 )); - EXPECT_EQ(FD::CPPTYPE_BOOL , GetCppTypeForFieldType(FD::TYPE_BOOL )); - EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_STRING )); - EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP )); - EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE )); - EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_BYTES )); - EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_UINT32 )); - EXPECT_EQ(FD::CPPTYPE_ENUM , GetCppTypeForFieldType(FD::TYPE_ENUM )); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SFIXED32)); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SFIXED64)); - EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SINT32 )); - EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SINT64 )); + EXPECT_EQ(FD::CPPTYPE_DOUBLE, GetCppTypeForFieldType(FD::TYPE_DOUBLE)); + EXPECT_EQ(FD::CPPTYPE_FLOAT, GetCppTypeForFieldType(FD::TYPE_FLOAT)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_INT64)); + EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_UINT64)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_INT32)); + EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_FIXED64)); + EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_FIXED32)); + EXPECT_EQ(FD::CPPTYPE_BOOL, GetCppTypeForFieldType(FD::TYPE_BOOL)); + EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_STRING)); + EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP)); + EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE)); + EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_BYTES)); + EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_UINT32)); + EXPECT_EQ(FD::CPPTYPE_ENUM, GetCppTypeForFieldType(FD::TYPE_ENUM)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SFIXED32)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SFIXED64)); + EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SINT32)); + EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, CppTypeNames) { @@ -2337,24 +2347,24 @@ TEST_F(MiscTest, CppTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("double" , GetCppTypeNameForFieldType(FD::TYPE_DOUBLE )); - EXPECT_STREQ("float" , GetCppTypeNameForFieldType(FD::TYPE_FLOAT )); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_INT64 )); - EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_UINT64 )); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_INT32 )); - EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_FIXED64 )); - EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_FIXED32 )); - EXPECT_STREQ("bool" , GetCppTypeNameForFieldType(FD::TYPE_BOOL )); + EXPECT_STREQ("double", GetCppTypeNameForFieldType(FD::TYPE_DOUBLE)); + EXPECT_STREQ("float", GetCppTypeNameForFieldType(FD::TYPE_FLOAT)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_INT64)); + EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_UINT64)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_INT32)); + EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_FIXED64)); + EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_FIXED32)); + EXPECT_STREQ("bool", GetCppTypeNameForFieldType(FD::TYPE_BOOL)); EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_STRING)); - EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP )); - EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE )); + EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP)); + EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE)); EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_BYTES)); - EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_UINT32 )); - EXPECT_STREQ("enum" , GetCppTypeNameForFieldType(FD::TYPE_ENUM )); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED32)); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED64)); - EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SINT32 )); - EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SINT64 )); + EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_UINT32)); + EXPECT_STREQ("enum", GetCppTypeNameForFieldType(FD::TYPE_ENUM)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SFIXED32)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SFIXED64)); + EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SINT32)); + EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, StaticCppTypeNames) { @@ -2362,14 +2372,14 @@ TEST_F(MiscTest, StaticCppTypeNames) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_STREQ("int32" , FD::CppTypeName(FD::CPPTYPE_INT32 )); - EXPECT_STREQ("int64" , FD::CppTypeName(FD::CPPTYPE_INT64 )); - EXPECT_STREQ("uint32" , FD::CppTypeName(FD::CPPTYPE_UINT32 )); - EXPECT_STREQ("uint64" , FD::CppTypeName(FD::CPPTYPE_UINT64 )); - EXPECT_STREQ("double" , FD::CppTypeName(FD::CPPTYPE_DOUBLE )); - EXPECT_STREQ("float" , FD::CppTypeName(FD::CPPTYPE_FLOAT )); - EXPECT_STREQ("bool" , FD::CppTypeName(FD::CPPTYPE_BOOL )); - EXPECT_STREQ("enum" , FD::CppTypeName(FD::CPPTYPE_ENUM )); + EXPECT_STREQ("int32", FD::CppTypeName(FD::CPPTYPE_INT32)); + EXPECT_STREQ("int64", FD::CppTypeName(FD::CPPTYPE_INT64)); + EXPECT_STREQ("uint32", FD::CppTypeName(FD::CPPTYPE_UINT32)); + EXPECT_STREQ("uint64", FD::CppTypeName(FD::CPPTYPE_UINT64)); + EXPECT_STREQ("double", FD::CppTypeName(FD::CPPTYPE_DOUBLE)); + EXPECT_STREQ("float", FD::CppTypeName(FD::CPPTYPE_FLOAT)); + EXPECT_STREQ("bool", FD::CppTypeName(FD::CPPTYPE_BOOL)); + EXPECT_STREQ("enum", FD::CppTypeName(FD::CPPTYPE_ENUM)); EXPECT_STREQ("string", FD::CppTypeName(FD::CPPTYPE_STRING)); EXPECT_STREQ("message", FD::CppTypeName(FD::CPPTYPE_MESSAGE)); } @@ -2379,24 +2389,24 @@ TEST_F(MiscTest, MessageType) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BOOL )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_STRING )); - EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_GROUP )); - EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BYTES )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_ENUM )); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT64)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT64)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT32)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BOOL)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_STRING)); + EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_GROUP)); + EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BYTES)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT32)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_ENUM)); EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED32)); EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED64)); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT32 )); - EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT64 )); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT32)); + EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT64)); } TEST_F(MiscTest, EnumType) { @@ -2404,27 +2414,26 @@ TEST_F(MiscTest, EnumType) { typedef FieldDescriptor FD; // avoid ugly line wrapping - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BOOL )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_STRING )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_GROUP )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BYTES )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT32 )); - EXPECT_TRUE(NULL != GetEnumDescriptorForFieldType(FD::TYPE_ENUM )); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT64)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT64)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT32)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BOOL)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_STRING)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_GROUP)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BYTES)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT32)); + EXPECT_TRUE(NULL != GetEnumDescriptorForFieldType(FD::TYPE_ENUM)); EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED32)); EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED64)); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT32 )); - EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT64 )); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT32)); + EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT64)); } - TEST_F(MiscTest, DefaultValues) { // Test that setting default values works. FileDescriptorProto file_proto; @@ -2440,47 +2449,47 @@ TEST_F(MiscTest, DefaultValues) { const FD::Label label = FD::LABEL_OPTIONAL; // Create fields of every CPP type with default values. - AddField(message_proto, "int32" , 1, label, FD::TYPE_INT32 ) - ->set_default_value("-1"); - AddField(message_proto, "int64" , 2, label, FD::TYPE_INT64 ) - ->set_default_value("-1000000000000"); + AddField(message_proto, "int32", 1, label, FD::TYPE_INT32) + ->set_default_value("-1"); + AddField(message_proto, "int64", 2, label, FD::TYPE_INT64) + ->set_default_value("-1000000000000"); AddField(message_proto, "uint32", 3, label, FD::TYPE_UINT32) - ->set_default_value("42"); + ->set_default_value("42"); AddField(message_proto, "uint64", 4, label, FD::TYPE_UINT64) - ->set_default_value("2000000000000"); - AddField(message_proto, "float" , 5, label, FD::TYPE_FLOAT ) - ->set_default_value("4.5"); + ->set_default_value("2000000000000"); + AddField(message_proto, "float", 5, label, FD::TYPE_FLOAT) + ->set_default_value("4.5"); AddField(message_proto, "double", 6, label, FD::TYPE_DOUBLE) - ->set_default_value("10e100"); - AddField(message_proto, "bool" , 7, label, FD::TYPE_BOOL ) - ->set_default_value("true"); + ->set_default_value("10e100"); + AddField(message_proto, "bool", 7, label, FD::TYPE_BOOL) + ->set_default_value("true"); AddField(message_proto, "string", 8, label, FD::TYPE_STRING) ->set_default_value("hello"); - AddField(message_proto, "data" , 9, label, FD::TYPE_BYTES ) - ->set_default_value("\\001\\002\\003"); + AddField(message_proto, "data", 9, label, FD::TYPE_BYTES) + ->set_default_value("\\001\\002\\003"); FieldDescriptorProto* enum_field = - AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM); + AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM); enum_field->set_type_name("DummyEnum"); enum_field->set_default_value("B"); // Strings are allowed to have empty defaults. (At one point, due to // a bug, empty defaults for strings were rejected. Oops.) AddField(message_proto, "empty_string", 11, label, FD::TYPE_STRING) - ->set_default_value(""); + ->set_default_value(""); // Add a second set of fields with implicit defalut values. - AddField(message_proto, "implicit_int32" , 21, label, FD::TYPE_INT32 ); - AddField(message_proto, "implicit_int64" , 22, label, FD::TYPE_INT64 ); + AddField(message_proto, "implicit_int32", 21, label, FD::TYPE_INT32); + AddField(message_proto, "implicit_int64", 22, label, FD::TYPE_INT64); AddField(message_proto, "implicit_uint32", 23, label, FD::TYPE_UINT32); AddField(message_proto, "implicit_uint64", 24, label, FD::TYPE_UINT64); - AddField(message_proto, "implicit_float" , 25, label, FD::TYPE_FLOAT ); + AddField(message_proto, "implicit_float", 25, label, FD::TYPE_FLOAT); AddField(message_proto, "implicit_double", 26, label, FD::TYPE_DOUBLE); - AddField(message_proto, "implicit_bool" , 27, label, FD::TYPE_BOOL ); + AddField(message_proto, "implicit_bool", 27, label, FD::TYPE_BOOL); AddField(message_proto, "implicit_string", 28, label, FD::TYPE_STRING); - AddField(message_proto, "implicit_data" , 29, label, FD::TYPE_BYTES ); - AddField(message_proto, "implicit_enum" , 30, label, FD::TYPE_ENUM) - ->set_type_name("DummyEnum"); + AddField(message_proto, "implicit_data", 29, label, FD::TYPE_BYTES); + AddField(message_proto, "implicit_enum", 30, label, FD::TYPE_ENUM) + ->set_type_name("DummyEnum"); // Build it. DescriptorPool pool; @@ -2517,13 +2526,13 @@ TEST_F(MiscTest, DefaultValues) { EXPECT_EQ(42, message->field(2)->default_value_uint32()); EXPECT_EQ(PROTOBUF_ULONGLONG(2000000000000), message->field(3)->default_value_uint64()); - EXPECT_EQ(4.5 , message->field(4)->default_value_float ()); - EXPECT_EQ(10e100 , message->field(5)->default_value_double()); - EXPECT_TRUE( message->field(6)->default_value_bool ()); - EXPECT_EQ("hello" , message->field(7)->default_value_string()); - EXPECT_EQ("\001\002\003" , message->field(8)->default_value_string()); - EXPECT_EQ(enum_value_b , message->field(9)->default_value_enum ()); - EXPECT_EQ("" , message->field(10)->default_value_string()); + EXPECT_EQ(4.5, message->field(4)->default_value_float()); + EXPECT_EQ(10e100, message->field(5)->default_value_double()); + EXPECT_TRUE(message->field(6)->default_value_bool()); + EXPECT_EQ("hello", message->field(7)->default_value_string()); + EXPECT_EQ("\001\002\003", message->field(8)->default_value_string()); + EXPECT_EQ(enum_value_b, message->field(9)->default_value_enum()); + EXPECT_EQ("", message->field(10)->default_value_string()); ASSERT_FALSE(message->field(11)->has_default_value()); ASSERT_FALSE(message->field(12)->has_default_value()); @@ -2536,15 +2545,15 @@ TEST_F(MiscTest, DefaultValues) { ASSERT_FALSE(message->field(19)->has_default_value()); ASSERT_FALSE(message->field(20)->has_default_value()); - EXPECT_EQ(0 , message->field(11)->default_value_int32 ()); - EXPECT_EQ(0 , message->field(12)->default_value_int64 ()); - EXPECT_EQ(0 , message->field(13)->default_value_uint32()); - EXPECT_EQ(0 , message->field(14)->default_value_uint64()); - EXPECT_EQ(0.0f , message->field(15)->default_value_float ()); - EXPECT_EQ(0.0 , message->field(16)->default_value_double()); - EXPECT_FALSE( message->field(17)->default_value_bool ()); - EXPECT_EQ("" , message->field(18)->default_value_string()); - EXPECT_EQ("" , message->field(19)->default_value_string()); + EXPECT_EQ(0, message->field(11)->default_value_int32()); + EXPECT_EQ(0, message->field(12)->default_value_int64()); + EXPECT_EQ(0, message->field(13)->default_value_uint32()); + EXPECT_EQ(0, message->field(14)->default_value_uint64()); + EXPECT_EQ(0.0f, message->field(15)->default_value_float()); + EXPECT_EQ(0.0, message->field(16)->default_value_double()); + EXPECT_FALSE(message->field(17)->default_value_bool()); + EXPECT_EQ("", message->field(18)->default_value_string()); + EXPECT_EQ("", message->field(19)->default_value_string()); EXPECT_EQ(enum_value_a, message->field(20)->default_value_enum()); } @@ -2555,13 +2564,11 @@ TEST_F(MiscTest, FieldOptions) { file_proto.set_name("foo.proto"); DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage"); - AddField(message_proto, "foo", 1, - FieldDescriptorProto::LABEL_OPTIONAL, + AddField(message_proto, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL, FieldDescriptorProto::TYPE_INT32); FieldDescriptorProto* bar_proto = - AddField(message_proto, "bar", 2, - FieldDescriptorProto::LABEL_OPTIONAL, - FieldDescriptorProto::TYPE_INT32); + AddField(message_proto, "bar", 2, FieldDescriptorProto::LABEL_OPTIONAL, + FieldDescriptorProto::TYPE_INT32); FieldOptions* options = bar_proto->mutable_options(); options->set_ctype(FieldOptions::CORD); @@ -2588,17 +2595,12 @@ TEST_F(MiscTest, FieldOptions) { } // =================================================================== -enum DescriptorPoolMode { - NO_DATABASE, - FALLBACK_DATABASE -}; +enum DescriptorPoolMode { NO_DATABASE, FALLBACK_DATABASE }; class AllowUnknownDependenciesTest : public testing::TestWithParam<DescriptorPoolMode> { protected: - DescriptorPoolMode mode() { - return GetParam(); - } + DescriptorPoolMode mode() { return GetParam(); } virtual void SetUp() { FileDescriptorProto foo_proto, bar_proto; @@ -2615,32 +2617,32 @@ class AllowUnknownDependenciesTest pool_->AllowUnknownDependencies(); ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'foo.proto'" - "dependency: 'bar.proto'" - "dependency: 'baz.proto'" - "message_type {" - " name: 'Foo'" - " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }" - " field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }" - " field { name:'qux' number:3 label:LABEL_OPTIONAL" - " type_name: '.corge.Qux'" - " type: TYPE_ENUM" - " options {" - " uninterpreted_option {" - " name {" - " name_part: 'grault'" - " is_extension: true" - " }" - " positive_int_value: 1234" - " }" - " }" - " }" - "}", - &foo_proto)); - ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'bar.proto'" - "message_type { name: 'Bar' }", - &bar_proto)); + "name: 'foo.proto'" + "dependency: 'bar.proto'" + "dependency: 'baz.proto'" + "message_type {" + " name: 'Foo'" + " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }" + " field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }" + " field { name:'qux' number:3 label:LABEL_OPTIONAL" + " type_name: '.corge.Qux'" + " type: TYPE_ENUM" + " options {" + " uninterpreted_option {" + " name {" + " name_part: 'grault'" + " is_extension: true" + " }" + " positive_int_value: 1234" + " }" + " }" + " }" + "}", + &foo_proto)); + ASSERT_TRUE( + TextFormat::ParseFromString("name: 'bar.proto'" + "message_type { name: 'Bar' }", + &bar_proto)); // Collect pointers to stuff. bar_file_ = BuildFile(bar_proto); @@ -2683,7 +2685,7 @@ class AllowUnknownDependenciesTest const FieldDescriptor* baz_field_; const FieldDescriptor* qux_field_; - SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode. + SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode. std::unique_ptr<DescriptorPool> pool_; }; @@ -2759,7 +2761,7 @@ TEST_P(AllowUnknownDependenciesTest, CustomOptions) { // Qux should still have the uninterpreted option attached. ASSERT_EQ(1, qux_field_->options().uninterpreted_option_size()); const UninterpretedOption& option = - qux_field_->options().uninterpreted_option(0); + qux_field_->options().uninterpreted_option(0); ASSERT_EQ(1, option.name_size()); EXPECT_EQ("grault", option.name(0).name_part()); } @@ -2771,10 +2773,10 @@ TEST_P(AllowUnknownDependenciesTest, UnknownExtendee) { FileDescriptorProto extension_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: 'extension.proto'" - "extension { extendee: 'UnknownType' name:'some_extension' number:123" - " label:LABEL_OPTIONAL type:TYPE_INT32 }", - &extension_proto)); + "name: 'extension.proto'" + "extension { extendee: 'UnknownType' name:'some_extension' number:123" + " label:LABEL_OPTIONAL type:TYPE_INT32 }", + &extension_proto)); const FileDescriptor* file = BuildFile(extension_proto); ASSERT_TRUE(file != NULL); @@ -2795,39 +2797,39 @@ TEST_P(AllowUnknownDependenciesTest, CustomOption) { FileDescriptorProto option_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"unknown_custom_options.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { " - " extendee: \"google.protobuf.FileOptions\" " - " name: \"some_option\" " - " number: 123456 " - " label: LABEL_OPTIONAL " - " type: TYPE_INT32 " - "} " - "options { " - " uninterpreted_option { " - " name { " - " name_part: \"some_option\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"unknown_option\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"optimize_for\" " - " is_extension: false " - " } " - " identifier_value: \"SPEED\" " - " } " - "}", - &option_proto)); + "name: \"unknown_custom_options.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { " + " extendee: \"google.protobuf.FileOptions\" " + " name: \"some_option\" " + " number: 123456 " + " label: LABEL_OPTIONAL " + " type: TYPE_INT32 " + "} " + "options { " + " uninterpreted_option { " + " name { " + " name_part: \"some_option\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"unknown_option\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"optimize_for\" " + " is_extension: false " + " } " + " identifier_value: \"SPEED\" " + " } " + "}", + &option_proto)); const FileDescriptor* file = BuildFile(option_proto); ASSERT_TRUE(file != NULL); @@ -2854,18 +2856,18 @@ TEST_P(AllowUnknownDependenciesTest, FileDescriptorProto undeclared_dep_proto; // We make this file fail to build by giving it two fields with tag 1. ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"invalid_file_as_undeclared_dep.proto\" " - "package: \"undeclared\" " - "message_type: { " - " name: \"Quux\" " - " field { " - " name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 " - " }" - " field { " - " name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 " - " }" - "}", - &undeclared_dep_proto)); + "name: \"invalid_file_as_undeclared_dep.proto\" " + "package: \"undeclared\" " + "message_type: { " + " name: \"Quux\" " + " field { " + " name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 " + " }" + " field { " + " name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 " + " }" + "}", + &undeclared_dep_proto)); // We can't use the BuildFile() helper because we don't actually want to build // it into the descriptor pool in the fallback database case: it just needs to // be sitting in the database so that it gets built during the building of @@ -2882,15 +2884,15 @@ TEST_P(AllowUnknownDependenciesTest, FileDescriptorProto test_proto; ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"test.proto\" " - "message_type: { " - " name: \"Corge\" " - " field { " - " name:'quux' number:1 label: LABEL_OPTIONAL " - " type_name:'undeclared.Quux' type: TYPE_MESSAGE " - " }" - "}", - &test_proto)); + "name: \"test.proto\" " + "message_type: { " + " name: \"Corge\" " + " field { " + " name:'quux' number:1 label: LABEL_OPTIONAL " + " type_name:'undeclared.Quux' type: TYPE_MESSAGE " + " }" + "}", + &test_proto)); const FileDescriptor* file = BuildFile(test_proto); ASSERT_TRUE(file != NULL); @@ -2937,13 +2939,10 @@ TEST(CustomOptions, OptionLocations) { field->options().GetExtension(protobuf_unittest::field_opt1)); EXPECT_EQ(42, // Check that we get the default for an option we don't set. field->options().GetExtension(protobuf_unittest::field_opt2)); - EXPECT_EQ(-99, - oneof->options().GetExtension(protobuf_unittest::oneof_opt1)); - EXPECT_EQ(-789, - enm->options().GetExtension(protobuf_unittest::enum_opt1)); - EXPECT_EQ(123, - enm->value(1)->options().GetExtension( - protobuf_unittest::enum_value_opt1)); + EXPECT_EQ(-99, oneof->options().GetExtension(protobuf_unittest::oneof_opt1)); + EXPECT_EQ(-789, enm->options().GetExtension(protobuf_unittest::enum_opt1)); + EXPECT_EQ(123, enm->value(1)->options().GetExtension( + protobuf_unittest::enum_value_opt1)); EXPECT_EQ(PROTOBUF_LONGLONG(-9876543210), service->options().GetExtension(protobuf_unittest::service_opt1)); EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2, @@ -2959,34 +2958,33 @@ TEST(CustomOptions, OptionTypes) { options = &protobuf_unittest::CustomOptionMinIntegerValues::descriptor()->options(); - EXPECT_EQ(false , options->GetExtension(protobuf_unittest::bool_opt)); + EXPECT_EQ(false, options->GetExtension(protobuf_unittest::bool_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::int32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::int64_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::uint32_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::uint64_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint32_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint64_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sint32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sint64_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::fixed32_opt)); - EXPECT_EQ(0 , options->GetExtension(protobuf_unittest::fixed64_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed32_opt)); + EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed64_opt)); EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sfixed32_opt)); EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sfixed64_opt)); options = &protobuf_unittest::CustomOptionMaxIntegerValues::descriptor()->options(); - EXPECT_EQ(true , options->GetExtension(protobuf_unittest::bool_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::int32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::int64_opt)); + EXPECT_EQ(true, options->GetExtension(protobuf_unittest::bool_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::int32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::int64_opt)); EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::uint32_opt)); EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::uint64_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::sint32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::sint64_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sint32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sint64_opt)); EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::fixed32_opt)); EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::fixed64_opt)); - EXPECT_EQ(kint32max , options->GetExtension(protobuf_unittest::sfixed32_opt)); - EXPECT_EQ(kint64max , options->GetExtension(protobuf_unittest::sfixed64_opt)); + EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sfixed32_opt)); + EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sfixed64_opt)); - options = - &protobuf_unittest::CustomOptionOtherValues::descriptor()->options(); + options = &protobuf_unittest::CustomOptionOtherValues::descriptor()->options(); EXPECT_EQ(-100, options->GetExtension(protobuf_unittest::int32_opt)); EXPECT_FLOAT_EQ(12.3456789, options->GetExtension(protobuf_unittest::float_opt)); @@ -3016,35 +3014,52 @@ TEST(CustomOptions, ComplexExtensionOptions) { const MessageOptions* options = &protobuf_unittest::VariousComplexOptions::descriptor()->options(); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1).foo(), 42); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1). - GetExtension(protobuf_unittest::quux), 324); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1). - GetExtension(protobuf_unittest::corge).qux(), 876); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1) + .GetExtension(protobuf_unittest::quux), + 324); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1) + .GetExtension(protobuf_unittest::corge) + .qux(), + 876); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).baz(), 987); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::grault), 654); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::grault), + 654); EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar().foo(), 743); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar(). - GetExtension(protobuf_unittest::quux), 1999); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar(). - GetExtension(protobuf_unittest::corge).qux(), 2008); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply).foo(), 741); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply). - GetExtension(protobuf_unittest::quux), 1998); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - GetExtension(protobuf_unittest::garply). - GetExtension(protobuf_unittest::corge).qux(), 2121); - EXPECT_EQ(options->GetExtension( - protobuf_unittest::ComplexOptionType2::ComplexOptionType4::complex_opt4). - waldo(), 1971); - EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2). - fred().waldo(), 321); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .bar() + .GetExtension(protobuf_unittest::quux), + 1999); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .bar() + .GetExtension(protobuf_unittest::corge) + .qux(), + 2008); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .foo(), + 741); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .GetExtension(protobuf_unittest::quux), + 1998); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2) + .GetExtension(protobuf_unittest::garply) + .GetExtension(protobuf_unittest::corge) + .qux(), + 2121); + EXPECT_EQ(options + ->GetExtension(protobuf_unittest::ComplexOptionType2:: + ComplexOptionType4::complex_opt4) + .waldo(), + 1971); + EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).fred().waldo(), + 321); EXPECT_EQ(9, options->GetExtension(protobuf_unittest::complex_opt3).qux()); - EXPECT_EQ(22, options->GetExtension(protobuf_unittest::complex_opt3). - complexoptiontype5().plugh()); + EXPECT_EQ(22, options->GetExtension(protobuf_unittest::complex_opt3) + .complexoptiontype5() + .plugh()); EXPECT_EQ(24, options->GetExtension(protobuf_unittest::complexopt6).xyzzy()); } @@ -3057,43 +3072,42 @@ TEST(CustomOptions, OptionsFromOtherFile) { FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "options { " - " uninterpreted_option { " - " name { " - " name_part: \"file_opt1\" " - " is_extension: true " - " } " - " positive_int_value: 1234 " - " } " - // Test a non-extension option too. (At one point this failed due to a - // bug.) - " uninterpreted_option { " - " name { " - " name_part: \"java_package\" " - " is_extension: false " - " } " - " string_value: \"foo\" " - " } " - // Test that enum-typed options still work too. (At one point this also - // failed due to a bug.) - " uninterpreted_option { " - " name { " - " name_part: \"optimize_for\" " - " is_extension: false " - " } " - " identifier_value: \"SPEED\" " - " } " - "}" - , - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "options { " + " uninterpreted_option { " + " name { " + " name_part: \"file_opt1\" " + " is_extension: true " + " } " + " positive_int_value: 1234 " + " } " + // Test a non-extension option too. (At one point this failed due to a + // bug.) + " uninterpreted_option { " + " name { " + " name_part: \"java_package\" " + " is_extension: false " + " } " + " string_value: \"foo\" " + " } " + // Test that enum-typed options still work too. (At one point this also + // failed due to a bug.) + " uninterpreted_option { " + " name { " + " name_part: \"optimize_for\" " + " is_extension: false " + " } " + " identifier_value: \"SPEED\" " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); ASSERT_TRUE(file != NULL); @@ -3116,8 +3130,8 @@ TEST(CustomOptions, MessageOptionThreeFieldsSet) { FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); // The following represents the definition: @@ -3130,48 +3144,48 @@ TEST(CustomOptions, MessageOptionThreeFieldsSet) { // option (complex_opt1).foo3 = 1234; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo2\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo3\" " - " is_extension: false " - " } " - " positive_int_value: 1234 " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo2\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo3\" " + " is_extension: false " + " } " + " positive_int_value: 1234 " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); ASSERT_TRUE(file != NULL); @@ -3193,8 +3207,8 @@ TEST(CustomOptions, MessageOptionRepeatedLeafFieldSet) { FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); // The following represents the definition: @@ -3207,48 +3221,48 @@ TEST(CustomOptions, MessageOptionRepeatedLeafFieldSet) { // option (complex_opt1).foo4 = 56; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 12 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 34 " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt1\" " - " is_extension: true " - " } " - " name { " - " name_part: \"foo4\" " - " is_extension: false " - " } " - " positive_int_value: 56 " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 12 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 34 " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt1\" " + " is_extension: true " + " } " + " name { " + " name_part: \"foo4\" " + " is_extension: false " + " } " + " positive_int_value: 56 " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); ASSERT_TRUE(file != NULL); @@ -3273,8 +3287,8 @@ TEST(CustomOptions, MessageOptionRepeatedMsgFieldSet) { FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); // The following represents the definition: @@ -3287,62 +3301,64 @@ TEST(CustomOptions, MessageOptionRepeatedMsgFieldSet) { // option (complex_opt2).barney = {waldo: 100}; // } ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" " - "message_type { " - " name: \"Foo\" " - " options { " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 1\" " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 10\" " - " } " - " uninterpreted_option { " - " name { " - " name_part: \"complex_opt2\" " - " is_extension: true " - " } " - " name { " - " name_part: \"barney\" " - " is_extension: false " - " } " - " aggregate_value: \"waldo: 100\" " - " } " - " } " - "}", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" " + "message_type { " + " name: \"Foo\" " + " options { " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 1\" " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 10\" " + " } " + " uninterpreted_option { " + " name { " + " name_part: \"complex_opt2\" " + " is_extension: true " + " } " + " name { " + " name_part: \"barney\" " + " is_extension: false " + " } " + " aggregate_value: \"waldo: 100\" " + " } " + " } " + "}", + &file_proto)); const FileDescriptor* file = pool.BuildFile(file_proto); ASSERT_TRUE(file != NULL); ASSERT_EQ(1, file->message_type_count()); const MessageOptions& options = file->message_type(0)->options(); - EXPECT_EQ(3, options.GetExtension( - protobuf_unittest::complex_opt2).barney_size()); - EXPECT_EQ(1,options.GetExtension( - protobuf_unittest::complex_opt2).barney(0).waldo()); - EXPECT_EQ(10, options.GetExtension( - protobuf_unittest::complex_opt2).barney(1).waldo()); - EXPECT_EQ(100, options.GetExtension( - protobuf_unittest::complex_opt2).barney(2).waldo()); + EXPECT_EQ(3, + options.GetExtension(protobuf_unittest::complex_opt2).barney_size()); + EXPECT_EQ( + 1, options.GetExtension(protobuf_unittest::complex_opt2).barney(0).waldo()); + EXPECT_EQ( + 10, + options.GetExtension(protobuf_unittest::complex_opt2).barney(1).waldo()); + EXPECT_EQ( + 100, + options.GetExtension(protobuf_unittest::complex_opt2).barney(2).waldo()); } // Check that aggregate options were parsed and saved correctly in @@ -3353,8 +3369,8 @@ TEST(CustomOptions, AggregateOptions) { const FieldDescriptor* field = msg->FindFieldByName("fieldname"); const EnumDescriptor* enumd = file->FindEnumTypeByName("AggregateEnum"); const EnumValueDescriptor* enumv = enumd->FindValueByName("VALUE"); - const ServiceDescriptor* service = file->FindServiceByName( - "AggregateService"); + const ServiceDescriptor* service = + file->FindServiceByName("AggregateService"); const MethodDescriptor* method = service->FindMethodByName("Method"); // Tests for the different types of data embedded in fileopt @@ -3366,9 +3382,10 @@ TEST(CustomOptions, AggregateOptions) { EXPECT_EQ("FileExtensionAnnotation", file_options.file().GetExtension(protobuf_unittest::fileopt).s()); EXPECT_EQ("EmbeddedMessageSetElement", - file_options.mset().GetExtension( - protobuf_unittest::AggregateMessageSetElement - ::message_set_extension).s()); + file_options.mset() + .GetExtension(protobuf_unittest::AggregateMessageSetElement :: + message_set_extension) + .s()); // Simple tests for all the other types of annotations EXPECT_EQ("MessageAnnotation", @@ -3392,16 +3409,16 @@ TEST(CustomOptions, UnusedImportWarning) { FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); - protobuf_unittest::TestMessageWithCustomOptions::descriptor() - ->file()->CopyTo(&file_proto); + protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo( + &file_proto); ASSERT_TRUE(pool.BuildFile(file_proto) != NULL); pool.AddUnusedImportTrackFile("custom_options_import.proto"); ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"custom_options_import.proto\" " - "package: \"protobuf_unittest\" " - "dependency: \"google/protobuf/unittest_custom_options.proto\" ", - &file_proto)); + "name: \"custom_options_import.proto\" " + "package: \"protobuf_unittest\" " + "dependency: \"google/protobuf/unittest_custom_options.proto\" ", + &file_proto)); MockErrorCollector error_collector; EXPECT_TRUE(pool.BuildFileCollectingErrors(file_proto, &error_collector)); @@ -3427,32 +3444,33 @@ TEST(CustomOptions, OptionsWithIncompatibleDescriptors) { // Add the "required_enum_opt" extension. FieldDescriptorProto* extension = file_proto.add_extension(); - protobuf_unittest::OldOptionType::descriptor()->file() - ->FindExtensionByName("required_enum_opt")->CopyTo(extension); + protobuf_unittest::OldOptionType::descriptor() + ->file() + ->FindExtensionByName("required_enum_opt") + ->CopyTo(extension); // Add a test message that uses the "required_enum_opt" option. DescriptorProto* test_message_type = file_proto.add_message_type(); - protobuf_unittest::TestMessageWithRequiredEnumOption::descriptor() - ->CopyTo(test_message_type); + protobuf_unittest::TestMessageWithRequiredEnumOption::descriptor()->CopyTo( + test_message_type); // Instruct the extension to use NewOptionType instead of // OldOptionType, and add the descriptor of NewOptionType. extension->set_type_name(".protobuf_unittest.NewOptionType"); DescriptorProto* new_option_type = file_proto.add_message_type(); - protobuf_unittest::NewOptionType::descriptor() - ->CopyTo(new_option_type); + protobuf_unittest::NewOptionType::descriptor()->CopyTo(new_option_type); // Replace the value of the "required_enum_opt" option used in the // test message with an enum value that only exists in NewOptionType. - ASSERT_TRUE(TextFormat::ParseFromString( - "uninterpreted_option { " - " name { " - " name_part: 'required_enum_opt' " - " is_extension: true " - " } " - " aggregate_value: 'value: NEW_VALUE'" - "}", - test_message_type->mutable_options())); + ASSERT_TRUE( + TextFormat::ParseFromString("uninterpreted_option { " + " name { " + " name_part: 'required_enum_opt' " + " is_extension: true " + " } " + " aggregate_value: 'value: NEW_VALUE'" + "}", + test_message_type->mutable_options())); // Adding the file descriptor to the pool should fail. EXPECT_TRUE(pool.BuildFile(file_proto) == NULL); @@ -3558,8 +3576,8 @@ class ValidationErrorTest : public testing::Test { ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto)); MockErrorCollector error_collector; - EXPECT_TRUE( - pool_.BuildFileCollectingErrors(file_proto, &error_collector) == NULL); + EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector) == + NULL); EXPECT_EQ(expected_errors, error_collector.text_); } @@ -3594,57 +3612,57 @@ class ValidationErrorTest : public testing::Test { TEST_F(ValidationErrorTest, AlreadyDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }" - "message_type { name: \"Foo\" }", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }" + "message_type { name: \"Foo\" }", - "foo.proto: Foo: NAME: \"Foo\" is already defined.\n"); + "foo.proto: Foo: NAME: \"Foo\" is already defined.\n"); } TEST_F(ValidationErrorTest, AlreadyDefinedInPackage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo.bar\" " - "message_type { name: \"Foo\" }" - "message_type { name: \"Foo\" }", + "name: \"foo.proto\" " + "package: \"foo.bar\" " + "message_type { name: \"Foo\" }" + "message_type { name: \"Foo\" }", - "foo.proto: foo.bar.Foo: NAME: \"Foo\" is already defined in " + "foo.proto: foo.bar.Foo: NAME: \"Foo\" is already defined in " "\"foo.bar\".\n"); } TEST_F(ValidationErrorTest, AlreadyDefinedInOtherFile) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }"); BuildFileWithErrors( - "name: \"bar.proto\" " - "message_type { name: \"Foo\" }", + "name: \"bar.proto\" " + "message_type { name: \"Foo\" }", - "bar.proto: Foo: NAME: \"Foo\" is already defined in file " + "bar.proto: Foo: NAME: \"Foo\" is already defined in file " "\"foo.proto\".\n"); } TEST_F(ValidationErrorTest, PackageAlreadyDefined) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"foo\" }"); BuildFileWithErrors( - "name: \"bar.proto\" " - "package: \"foo.bar\"", + "name: \"bar.proto\" " + "package: \"foo.bar\"", - "bar.proto: foo: NAME: \"foo\" is already defined (as something other " + "bar.proto: foo: NAME: \"foo\" is already defined (as something other " "than a package) in file \"foo.proto\".\n"); } TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParent) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " - "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", + "name: \"foo.proto\" " + "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " + "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", - "foo.proto: FOO: NAME: \"FOO\" is already defined.\n" - "foo.proto: FOO: NAME: Note that enum values use C++ scoping rules, " + "foo.proto: FOO: NAME: \"FOO\" is already defined.\n" + "foo.proto: FOO: NAME: Note that enum values use C++ scoping rules, " "meaning that enum values are siblings of their type, not children of " "it. Therefore, \"FOO\" must be unique within the global scope, not " "just within \"Bar\".\n"); @@ -3652,13 +3670,13 @@ TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParent) { TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParentNonGlobal) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"pkg\" " - "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " - "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", + "name: \"foo.proto\" " + "package: \"pkg\" " + "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } " + "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ", - "foo.proto: pkg.FOO: NAME: \"FOO\" is already defined in \"pkg\".\n" - "foo.proto: pkg.FOO: NAME: Note that enum values use C++ scoping rules, " + "foo.proto: pkg.FOO: NAME: \"FOO\" is already defined in \"pkg\".\n" + "foo.proto: pkg.FOO: NAME: Note that enum values use C++ scoping rules, " "meaning that enum values are siblings of their type, not children of " "it. Therefore, \"FOO\" must be unique within \"pkg\", not just within " "\"Bar\".\n"); @@ -3666,60 +3684,60 @@ TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParentNonGlobal) { TEST_F(ValidationErrorTest, MissingName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { }", + "name: \"foo.proto\" " + "message_type { }", - "foo.proto: : NAME: Missing name.\n"); + "foo.proto: : NAME: Missing name.\n"); } TEST_F(ValidationErrorTest, InvalidName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"$\" }", + "name: \"foo.proto\" " + "message_type { name: \"$\" }", - "foo.proto: $: NAME: \"$\" is not a valid identifier.\n"); + "foo.proto: $: NAME: \"$\" is not a valid identifier.\n"); } TEST_F(ValidationErrorTest, InvalidPackageName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo.$\"", + "name: \"foo.proto\" " + "package: \"foo.$\"", - "foo.proto: foo.$: NAME: \"$\" is not a valid identifier.\n"); + "foo.proto: foo.$: NAME: \"$\" is not a valid identifier.\n"); } TEST_F(ValidationErrorTest, MissingFileName) { - BuildFileWithErrors( - "", + BuildFileWithErrors("", - ": : OTHER: Missing field: FileDescriptorProto.name.\n"); + ": : OTHER: Missing field: FileDescriptorProto.name.\n"); } TEST_F(ValidationErrorTest, DupeDependency) { BuildFile("name: \"foo.proto\""); BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" " - "dependency: \"foo.proto\" ", + "name: \"bar.proto\" " + "dependency: \"foo.proto\" " + "dependency: \"foo.proto\" ", - "bar.proto: bar.proto: OTHER: Import \"foo.proto\" was listed twice.\n"); + "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" was listed twice.\n"); } TEST_F(ValidationErrorTest, UnknownDependency) { BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" ", + "name: \"bar.proto\" " + "dependency: \"foo.proto\" ", - "bar.proto: bar.proto: OTHER: Import \"foo.proto\" has not been loaded.\n"); + "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" has not been " + "loaded.\n"); } TEST_F(ValidationErrorTest, InvalidPublicDependencyIndex) { BuildFile("name: \"foo.proto\""); BuildFileWithErrors( - "name: \"bar.proto\" " - "dependency: \"foo.proto\" " - "public_dependency: 1", - "bar.proto: bar.proto: OTHER: Invalid public dependency index.\n"); + "name: \"bar.proto\" " + "dependency: \"foo.proto\" " + "public_dependency: 1", + "bar.proto: bar.proto: OTHER: Invalid public dependency index.\n"); } TEST_F(ValidationErrorTest, ForeignUnimportedPackageNoCrash) { @@ -3729,475 +3747,494 @@ TEST_F(ValidationErrorTest, ForeignUnimportedPackageNoCrash) { // include that parent package in the name (i.e. we do a relative lookup)... // Yes, really. BuildFile( - "name: 'foo.proto' " - "package: 'outer.foo' "); - BuildFileWithErrors( - "name: 'bar.proto' " - "dependency: 'baz.proto' " - "package: 'outer.bar' " - "message_type { " - " name: 'Bar' " - " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'foo.Foo' }" - "}", - - "bar.proto: bar.proto: OTHER: Import \"baz.proto\" has not been loaded.\n" - "bar.proto: outer.bar.Bar.bar: TYPE: \"outer.foo\" seems to be defined in " + "name: 'foo.proto' " + "package: 'outer.foo' "); + BuildFileWithErrors( + "name: 'bar.proto' " + "dependency: 'baz.proto' " + "package: 'outer.bar' " + "message_type { " + " name: 'Bar' " + " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'foo.Foo' }" + "}", + + "bar.proto: baz.proto: IMPORT: Import \"baz.proto\" has not been " + "loaded.\n" + "bar.proto: outer.bar.Bar.bar: TYPE: \"outer.foo\" seems to be defined " + "in " "\"foo.proto\", which is not imported by \"bar.proto\". To use it here, " "please add the necessary import.\n"); } TEST_F(ValidationErrorTest, DupeFile) { BuildFile( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" }"); + "name: \"foo.proto\" " + "message_type { name: \"Foo\" }"); // Note: We should *not* get redundant errors about "Foo" already being // defined. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - // Add another type so that the files aren't identical (in which case there - // would be no error). - "enum_type { name: \"Bar\" }", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + // Add another type so that the files aren't identical (in which case + // there would be no error). + "enum_type { name: \"Bar\" }", - "foo.proto: foo.proto: OTHER: A file with this name is already in the " + "foo.proto: foo.proto: OTHER: A file with this name is already in the " "pool.\n"); } TEST_F(ValidationErrorTest, FieldInExtensionRange) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 9 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 10 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"baz\" number: 19 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"qux\" number: 20 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " extension_range { start: 10 end: 20 }" - "}", - - "foo.proto: Foo.bar: NUMBER: Extension range 10 to 19 includes field " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 9 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"bar\" number: 10 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"baz\" number: 19 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"qux\" number: 20 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " extension_range { start: 10 end: 20 }" + "}", + + "foo.proto: Foo.bar: NUMBER: Extension range 10 to 19 includes field " "\"bar\" (10).\n" - "foo.proto: Foo.baz: NUMBER: Extension range 10 to 19 includes field " + "foo.proto: Foo.baz: NUMBER: Extension range 10 to 19 includes field " "\"baz\" (19).\n"); } TEST_F(ValidationErrorTest, OverlappingExtensionRanges) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " extension_range { start: 20 end: 30 }" - " extension_range { start: 19 end: 21 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " extension_range { start: 20 end: 30 }" + " extension_range { start: 19 end: 21 }" + "}", - "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " + "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " "already-defined range 10 to 19.\n" - "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " + "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with " "already-defined range 20 to 29.\n"); } TEST_F(ValidationErrorTest, ReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " reserved_range { start: 10 end: 20 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " reserved_range { start: 10 end: 20 }" + "}", - "foo.proto: Foo.foo: NUMBER: Field \"foo\" uses reserved number 15.\n"); + "foo.proto: Foo.foo: NUMBER: Field \"foo\" uses reserved number 15.\n"); } TEST_F(ValidationErrorTest, ReservedExtensionRangeError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Extension range 10 to 19" - " overlaps with reserved range 5 to 14.\n"); + "foo.proto: Foo: NUMBER: Extension range 10 to 19" + " overlaps with reserved range 5 to 14.\n"); } TEST_F(ValidationErrorTest, ReservedExtensionRangeAdjacent) { BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 10 }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 10 }" + "}"); } TEST_F(ValidationErrorTest, ReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 14" - " overlaps with already-defined range 10 to 19.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 14" + " overlaps with already-defined range 10 to 19.\n"); } TEST_F(ValidationErrorTest, ReservedNameError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 16 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"baz\" number: 17 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"bar\" number: 16 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " field { name: \"baz\" number: 17 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + "}", - "foo.proto: Foo.foo: NAME: Field name \"foo\" is reserved.\n" - "foo.proto: Foo.bar: NAME: Field name \"bar\" is reserved.\n"); + "foo.proto: Foo.foo: NAME: Field name \"foo\" is reserved.\n" + "foo.proto: Foo.bar: NAME: Field name \"bar\" is reserved.\n"); } TEST_F(ValidationErrorTest, ReservedNameRedundant) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_name: \"foo\"" - " reserved_name: \"foo\"" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_name: \"foo\"" + " reserved_name: \"foo\"" + "}", - "foo.proto: foo: NAME: Field name \"foo\" is reserved multiple times.\n"); + "foo.proto: foo: NAME: Field name \"foo\" is reserved multiple times.\n"); } TEST_F(ValidationErrorTest, ReservedFieldsDebugString) { const FileDescriptor* file = BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - " reserved_range { start: 5 end: 6 }" - " reserved_range { start: 10 end: 20 }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + " reserved_range { start: 5 end: 6 }" + " reserved_range { start: 10 end: 20 }" + "}"); ASSERT_EQ( - "syntax = \"proto2\";\n\n" - "message Foo {\n" - " reserved 5, 10 to 19;\n" - " reserved \"foo\", \"bar\";\n" - "}\n\n", - file->DebugString()); + "syntax = \"proto2\";\n\n" + "message Foo {\n" + " reserved 5, 10 to 19;\n" + " reserved \"foo\", \"bar\";\n" + "}\n\n", + file->DebugString()); } TEST_F(ValidationErrorTest, EnumReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:15 }" - " reserved_range { start: 10 end: 20 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:15 }" + " reserved_range { start: 10 end: 20 }" + "}", - "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number 15.\n"); + "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number 15.\n"); } TEST_F(ValidationErrorTest, EnumNegativeReservedFieldError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:-15 }" - " reserved_range { start: -20 end: -10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:-15 }" + " reserved_range { start: -20 end: -10 }" + "}", - "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number -15.\n"); + "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number -15.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 15 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 15 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 15" - " overlaps with already-defined range 10 to 20.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 15" + " overlaps with already-defined range 10 to 20.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeOverlapByOne) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: 10 end: 20 }" - " reserved_range { start: 5 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: 10 end: 20 }" + " reserved_range { start: 5 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 5 to 10" - " overlaps with already-defined range 10 to 20.\n"); + "foo.proto: Foo: NUMBER: Reserved range 5 to 10" + " overlaps with already-defined range 10 to 20.\n"); } TEST_F(ValidationErrorTest, EnumNegativeReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:0 }" - " reserved_range { start: -20 end: -10 }" - " reserved_range { start: -15 end: -5 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:0 }" + " reserved_range { start: -20 end: -10 }" + " reserved_range { start: -15 end: -5 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range -15 to -5" - " overlaps with already-defined range -20 to -10.\n"); + "foo.proto: Foo: NUMBER: Reserved range -15 to -5" + " overlaps with already-defined range -20 to -10.\n"); } TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: -20 end: 10 }" - " reserved_range { start: -15 end: 5 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: -20 end: 10 }" + " reserved_range { start: -15 end: 5 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range -15 to 5" - " overlaps with already-defined range -20 to 10.\n"); + "foo.proto: Foo: NUMBER: Reserved range -15 to 5" + " overlaps with already-defined range -20 to 10.\n"); } TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap2) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: -20 end: 10 }" - " reserved_range { start: 10 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: -20 end: 10 }" + " reserved_range { start: 10 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range 10 to 10" - " overlaps with already-defined range -20 to 10.\n"); + "foo.proto: Foo: NUMBER: Reserved range 10 to 10" + " overlaps with already-defined range -20 to 10.\n"); } TEST_F(ValidationErrorTest, EnumReservedRangeStartGreaterThanEnd) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"BAR\" number:20 }" - " reserved_range { start: 11 end: 10 }" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"BAR\" number:20 }" + " reserved_range { start: 11 end: 10 }" + "}", - "foo.proto: Foo: NUMBER: Reserved range end number must be greater" - " than start number.\n"); + "foo.proto: Foo: NUMBER: Reserved range end number must be greater" + " than start number.\n"); } TEST_F(ValidationErrorTest, EnumReservedNameError) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:15 }" - " value { name:\"BAR\" number:15 }" - " reserved_name: \"FOO\"" - " reserved_name: \"BAR\"" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:15 }" + " value { name:\"BAR\" number:15 }" + " reserved_name: \"FOO\"" + " reserved_name: \"BAR\"" + "}", - "foo.proto: FOO: NAME: Enum value \"FOO\" is reserved.\n" - "foo.proto: BAR: NAME: Enum value \"BAR\" is reserved.\n"); + "foo.proto: FOO: NAME: Enum value \"FOO\" is reserved.\n" + "foo.proto: BAR: NAME: Enum value \"BAR\" is reserved.\n"); } TEST_F(ValidationErrorTest, EnumReservedNameRedundant) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:15 }" - " reserved_name: \"foo\"" - " reserved_name: \"foo\"" - "}", + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:15 }" + " reserved_name: \"foo\"" + " reserved_name: \"foo\"" + "}", - "foo.proto: foo: NAME: Enum value \"foo\" is reserved multiple times.\n"); + "foo.proto: foo: NAME: Enum value \"foo\" is reserved multiple times.\n"); } TEST_F(ValidationErrorTest, EnumReservedFieldsDebugString) { const FileDescriptor* file = BuildFile( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Foo\"" - " value { name:\"FOO\" number:3 }" - " reserved_name: \"foo\"" - " reserved_name: \"bar\"" - " reserved_range { start: -6 end: -6 }" - " reserved_range { start: -5 end: -4 }" - " reserved_range { start: -1 end: 1 }" - " reserved_range { start: 5 end: 5 }" - " reserved_range { start: 10 end: 19 }" - "}"); - + "name: \"foo.proto\" " + "enum_type {" + " name: \"Foo\"" + " value { name:\"FOO\" number:3 }" + " reserved_name: \"foo\"" + " reserved_name: \"bar\"" + " reserved_range { start: -6 end: -6 }" + " reserved_range { start: -5 end: -4 }" + " reserved_range { start: -1 end: 1 }" + " reserved_range { start: 5 end: 5 }" + " reserved_range { start: 10 end: 19 }" + "}"); + ASSERT_EQ( - "syntax = \"proto2\";\n\n" - "enum Foo {\n" - " FOO = 3;\n" - " reserved -6, -5 to -4, -1 to 1, 5, 10 to 19;\n" - " reserved \"foo\", \"bar\";\n" - "}\n\n", - file->DebugString()); + "syntax = \"proto2\";\n\n" + "enum Foo {\n" + " FOO = 3;\n" + " reserved -6, -5 to -4, -1 to 1, 5, 10 to 19;\n" + " reserved \"foo\", \"bar\";\n" + "}\n\n", + file->DebugString()); } TEST_F(ValidationErrorTest, InvalidDefaults) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" - // Invalid number. - " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32" - " default_value: \"abc\" }" + // Invalid number. + " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32" + " default_value: \"abc\" }" - // Empty default value. - " field { name: \"bar\" number: 2 label: LABEL_OPTIONAL type: TYPE_INT32" - " default_value: \"\" }" + // Empty default value. + " field { name: \"bar\" number: 2 label: LABEL_OPTIONAL type: TYPE_INT32" + " default_value: \"\" }" - // Invalid boolean. - " field { name: \"baz\" number: 3 label: LABEL_OPTIONAL type: TYPE_BOOL" - " default_value: \"abc\" }" + // Invalid boolean. + " field { name: \"baz\" number: 3 label: LABEL_OPTIONAL type: TYPE_BOOL" + " default_value: \"abc\" }" - // Messages can't have defaults. - " field { name: \"qux\" number: 4 label: LABEL_OPTIONAL type: TYPE_MESSAGE" - " default_value: \"abc\" type_name: \"Foo\" }" + // Messages can't have defaults. + " field { name: \"qux\" number: 4 label: LABEL_OPTIONAL type: " + "TYPE_MESSAGE" + " default_value: \"abc\" type_name: \"Foo\" }" - // Same thing, but we don't know that this field has message type until - // we look up the type name. - " field { name: \"quux\" number: 5 label: LABEL_OPTIONAL" - " default_value: \"abc\" type_name: \"Foo\" }" + // Same thing, but we don't know that this field has message type until + // we look up the type name. + " field { name: \"quux\" number: 5 label: LABEL_OPTIONAL" + " default_value: \"abc\" type_name: \"Foo\" }" - // Repeateds can't have defaults. - " field { name: \"corge\" number: 6 label: LABEL_REPEATED type: TYPE_INT32" - " default_value: \"1\" }" - "}", + // Repeateds can't have defaults. + " field { name: \"corge\" number: 6 label: LABEL_REPEATED type: " + "TYPE_INT32" + " default_value: \"1\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Couldn't parse default value \"abc\".\n" - "foo.proto: Foo.bar: DEFAULT_VALUE: Couldn't parse default value \"\".\n" - "foo.proto: Foo.baz: DEFAULT_VALUE: Boolean default must be true or " + "foo.proto: Foo.foo: DEFAULT_VALUE: Couldn't parse default value " + "\"abc\".\n" + "foo.proto: Foo.bar: DEFAULT_VALUE: Couldn't parse default value \"\".\n" + "foo.proto: Foo.baz: DEFAULT_VALUE: Boolean default must be true or " "false.\n" - "foo.proto: Foo.qux: DEFAULT_VALUE: Messages can't have default values.\n" - "foo.proto: Foo.corge: DEFAULT_VALUE: Repeated fields can't have default " + "foo.proto: Foo.qux: DEFAULT_VALUE: Messages can't have default values.\n" + "foo.proto: Foo.corge: DEFAULT_VALUE: Repeated fields can't have default " "values.\n" - // This ends up being reported later because the error is detected at - // cross-linking time. - "foo.proto: Foo.quux: DEFAULT_VALUE: Messages can't have default " + // This ends up being reported later because the error is detected at + // cross-linking time. + "foo.proto: Foo.quux: DEFAULT_VALUE: Messages can't have default " "values.\n"); } TEST_F(ValidationErrorTest, NegativeFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: -1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: -1 label:LABEL_OPTIONAL type:TYPE_INT32 " + "}" + "}", - "foo.proto: Foo.foo: NUMBER: Field numbers must be positive integers.\n"); + "foo.proto: Foo.foo: NUMBER: Field numbers must be positive integers.\n"); } TEST_F(ValidationErrorTest, HugeFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 0x70000000 " - " label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 0x70000000 " + " label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: NUMBER: Field numbers cannot be greater than " + "foo.proto: Foo.foo: NUMBER: Field numbers cannot be greater than " "536870911.\n"); } TEST_F(ValidationErrorTest, ReservedFieldNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field {name:\"qux\" number: 20000 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + " field {name:\"qux\" number: 20000 label:LABEL_OPTIONAL " + "type:TYPE_INT32 }" + "}", - "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are " + "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are " "reserved for the protocol buffer library implementation.\n" - "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are " + "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are " "reserved for the protocol buffer library implementation.\n"); } TEST_F(ValidationErrorTest, ExtensionMissingExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension { name: \"foo\" number: 1 label: LABEL_OPTIONAL" - " type_name: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension { name: \"foo\" number: 1 label: LABEL_OPTIONAL" + " type_name: \"Foo\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee not set for " + "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee not set for " "extension field.\n"); } TEST_F(ValidationErrorTest, NonExtensionWithExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " extension_range { start: 1 end: 2 }" - "}" - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL" - " type_name: \"Foo\" extendee: \"Bar\" }" - "}", - - "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee set for " + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " extension_range { start: 1 end: 2 }" + "}" + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL" + " type_name: \"Foo\" extendee: \"Bar\" }" + "}", + + "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee set for " "non-extension field.\n"); } TEST_F(ValidationErrorTest, FieldOneofIndexTooLarge) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 1 }" - " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 0 }" - " oneof_decl { name:\"bar\" }" - "}", - - "foo.proto: Foo.foo: OTHER: FieldDescriptorProto.oneof_index 1 is out of " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 1 }" + " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 0 }" + " oneof_decl { name:\"bar\" }" + "}", + + "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index 1 is out of " "range for type \"Foo\".\n"); } TEST_F(ValidationErrorTest, FieldOneofIndexNegative) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: -1 }" - " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " - " oneof_index: 0 }" - " oneof_decl { name:\"bar\" }" - "}", - - "foo.proto: Foo.foo: OTHER: FieldDescriptorProto.oneof_index -1 is out of " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: -1 }" + " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 " + " oneof_index: 0 }" + " oneof_decl { name:\"bar\" }" + "}", + + "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index -1 is out " + "of " "range for type \"Foo\".\n"); } @@ -4215,7 +4252,7 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" "}", - "foo.proto: Foo.bar: OTHER: Fields in the same oneof must be defined " + "foo.proto: Foo.bar: TYPE: Fields in the same oneof must be defined " "consecutively. \"bar\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n"); @@ -4235,10 +4272,10 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" " oneof_decl { name:\"bars\" }" "}", - "foo2.proto: Foo2.bar1: OTHER: Fields in the same oneof must be defined " + "foo2.proto: Foo2.bar1: TYPE: Fields in the same oneof must be defined " "consecutively. \"bar1\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n" - "foo2.proto: Foo2.foo2: OTHER: Fields in the same oneof must be defined " + "foo2.proto: Foo2.foo2: TYPE: Fields in the same oneof must be defined " "consecutively. \"foo2\" cannot be defined before the completion of the " "\"bars\" oneof definition.\n"); @@ -4257,202 +4294,205 @@ TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) { " oneof_decl { name:\"foos\" }" " oneof_decl { name:\"bars\" }" "}", - "foo3.proto: Foo3.baz: OTHER: Fields in the same oneof must be defined " + "foo3.proto: Foo3.baz: TYPE: Fields in the same oneof must be defined " "consecutively. \"baz\" cannot be defined before the completion of the " "\"foos\" oneof definition.\n"); } TEST_F(ValidationErrorTest, FieldNumberConflict) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " field { name: \"bar\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + " field { name: \"bar\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.bar: NUMBER: Field number 1 has already been used in " + "foo.proto: Foo.bar: NUMBER: Field number 1 has already been used in " "\"Foo\" by field \"foo\".\n"); } TEST_F(ValidationErrorTest, BadMessageSetExtensionType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"MessageSet\"" - " options { message_set_wire_format: true }" - " extension_range { start: 4 end: 5 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:4 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"MessageSet\" }" - "}", - - "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " + "name: \"foo.proto\" " + "message_type {" + " name: \"MessageSet\"" + " options { message_set_wire_format: true }" + " extension_range { start: 4 end: 5 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:4 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"MessageSet\" }" + "}", + + "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " "messages.\n"); } TEST_F(ValidationErrorTest, BadMessageSetExtensionLabel) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"MessageSet\"" - " options { message_set_wire_format: true }" - " extension_range { start: 4 end: 5 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE" - " type_name: \"Foo\" extendee: \"MessageSet\" }" - "}", - - "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " + "name: \"foo.proto\" " + "message_type {" + " name: \"MessageSet\"" + " options { message_set_wire_format: true }" + " extension_range { start: 4 end: 5 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:4 label:LABEL_REPEATED " + "type:TYPE_MESSAGE" + " type_name: \"Foo\" extendee: \"MessageSet\" }" + "}", + + "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional " "messages.\n"); } TEST_F(ValidationErrorTest, FieldInMessageSet) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " options { message_set_wire_format: true }" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " options { message_set_wire_format: true }" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: NAME: MessageSets cannot have fields, only " + "foo.proto: Foo.foo: NAME: MessageSets cannot have fields, only " "extensions.\n"); } TEST_F(ValidationErrorTest, NegativeExtensionRangeNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: -10 end: -1 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: -10 end: -1 }" + "}", - "foo.proto: Foo: NUMBER: Extension numbers must be positive integers.\n"); + "foo.proto: Foo: NUMBER: Extension numbers must be positive integers.\n"); } TEST_F(ValidationErrorTest, HugeExtensionRangeNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 1 end: 0x70000000 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 1 end: 0x70000000 }" + "}", - "foo.proto: Foo: NUMBER: Extension numbers cannot be greater than " + "foo.proto: Foo: NUMBER: Extension numbers cannot be greater than " "536870911.\n"); } TEST_F(ValidationErrorTest, ExtensionRangeEndBeforeStart) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 10 }" - " extension_range { start: 10 end: 5 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 10 }" + " extension_range { start: 10 end: 5 }" + "}", - "foo.proto: Foo: NUMBER: Extension range end number must be greater than " + "foo.proto: Foo: NUMBER: Extension range end number must be greater than " "start number.\n" - "foo.proto: Foo: NUMBER: Extension range end number must be greater than " + "foo.proto: Foo: NUMBER: Extension range end number must be greater than " "start number.\n"); } TEST_F(ValidationErrorTest, EmptyEnum) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Foo\" }" - // Also use the empty enum in a message to make sure there are no crashes - // during validation (possible if the code attempts to derive a default - // value for the field). - "message_type {" - " name: \"Bar\"" - " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type_name:\"Foo\" }" - " field { name: \"bar\" number: 2 label:LABEL_OPTIONAL type_name:\"Foo\" " - " default_value: \"NO_SUCH_VALUE\" }" - "}", - - "foo.proto: Foo: NAME: Enums must contain at least one value.\n" - "foo.proto: Bar.bar: DEFAULT_VALUE: Enum type \"Foo\" has no value named " + "name: \"foo.proto\" " + "enum_type { name: \"Foo\" }" + // Also use the empty enum in a message to make sure there are no crashes + // during validation (possible if the code attempts to derive a default + // value for the field). + "message_type {" + " name: \"Bar\"" + " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL " + "type_name:\"Foo\" }" + " field { name: \"bar\" number: 2 label:LABEL_OPTIONAL " + "type_name:\"Foo\" " + " default_value: \"NO_SUCH_VALUE\" }" + "}", + + "foo.proto: Foo: NAME: Enums must contain at least one value.\n" + "foo.proto: Bar.bar: DEFAULT_VALUE: Enum type \"Foo\" has no value named " "\"NO_SUCH_VALUE\".\n"); } TEST_F(ValidationErrorTest, UndefinedExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not defined.\n"); + "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, NonMessageExtendee) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } }" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } }" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", - "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not a message type.\n"); + "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not a message type.\n"); } TEST_F(ValidationErrorTest, NotAnExtensionNumber) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - "}" - "message_type {" - " name: \"Foo\"" - " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " extendee: \"Bar\" }" - "}", - - "foo.proto: Foo.foo: NUMBER: \"Bar\" does not declare 1 as an extension " + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + "}" + "message_type {" + " name: \"Foo\"" + " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " extendee: \"Bar\" }" + "}", + + "foo.proto: Foo.foo: NUMBER: \"Bar\" does not declare 1 as an extension " "number.\n"); } TEST_F(ValidationErrorTest, RequiredExtension) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " extension_range { start: 1000 end: 10000 }" - "}" - "message_type {" - " name: \"Foo\"" - " extension {" - " name:\"foo\"" - " number:1000" - " label:LABEL_REQUIRED" - " type:TYPE_INT32" - " extendee: \"Bar\"" - " }" - "}", - - "foo.proto: Foo.foo: TYPE: Message extensions cannot have required " - "fields.\n"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " extension_range { start: 1000 end: 10000 }" + "}" + "message_type {" + " name: \"Foo\"" + " extension {" + " name:\"foo\"" + " number:1000" + " label:LABEL_REQUIRED" + " type:TYPE_INT32" + " extendee: \"Bar\"" + " }" + "}", + + "foo.proto: Foo.foo: TYPE: Message extensions cannot have required " + "fields.\n"); } TEST_F(ValidationErrorTest, UndefinedFieldType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, UndefinedFieldTypeWithDefault) { @@ -4461,42 +4501,42 @@ TEST_F(ValidationErrorTest, UndefinedFieldTypeWithDefault) { // error message. We want this input to yield a validation error instead, // since the unknown type is the primary problem. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"int\" " - " default_value:\"1\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"int\" " + " default_value:\"1\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"int\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"int\" is not defined.\n"); } TEST_F(ValidationErrorTest, UndefinedNestedFieldType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " nested_type { name:\"Baz\" }" - " field { name:\"foo\" number:1" - " label:LABEL_OPTIONAL" - " type_name:\"Foo.Baz.Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " nested_type { name:\"Baz\" }" + " field { name:\"foo\" number:1" + " label:LABEL_OPTIONAL" + " type_name:\"Foo.Baz.Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Foo.Baz.Bar\" is not defined.\n"); + "foo.proto: Foo.foo: TYPE: \"Foo.Baz.Bar\" is not defined.\n"); } TEST_F(ValidationErrorTest, FieldTypeDefinedInUndeclaredDependency) { BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" } "); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" } "); BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4517,21 +4557,21 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInIndirectDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\""); + "name: \"forward.proto\"" + "dependency: \"bar.proto\""); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"forward.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"foo.proto\" " + "dependency: \"forward.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4554,21 +4594,21 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInPublicDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\" " - "public_dependency: 0"); + "name: \"forward.proto\"" + "dependency: \"bar.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"foo.proto\" " - "dependency: \"forward.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "dependency: \"forward.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, FieldTypeDefinedInTransitivePublicDependency) { @@ -4590,26 +4630,26 @@ TEST_F(ValidationErrorTest, FieldTypeDefinedInTransitivePublicDependency) { // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\" " - "public_dependency: 0"); + "name: \"forward.proto\"" + "dependency: \"bar.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"forward2.proto\"" - "dependency: \"forward.proto\" " - "public_dependency: 0"); + "name: \"forward2.proto\"" + "dependency: \"forward.proto\" " + "public_dependency: 0"); BuildFile( - "name: \"foo.proto\" " - "dependency: \"forward2.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "dependency: \"forward2.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, @@ -4634,26 +4674,26 @@ TEST_F(ValidationErrorTest, // } // BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"forward.proto\"" - "dependency: \"bar.proto\""); + "name: \"forward.proto\"" + "dependency: \"bar.proto\""); BuildFile( - "name: \"forward2.proto\"" - "dependency: \"forward.proto\" " - "public_dependency: 0"); - - BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"forward2.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " + "name: \"forward2.proto\"" + "dependency: \"forward.proto\" " + "public_dependency: 0"); + + BuildFileWithErrors( + "name: \"foo.proto\" " + "dependency: \"forward2.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", " "which is not imported by \"foo.proto\". To use it here, please add the " "necessary import.\n"); } @@ -4675,22 +4715,22 @@ TEST_F(ValidationErrorTest, SearchMostLocalFirst) { // fail, and ten try "Bar.Baz" and succeed, even though "Bar" should actually // refer to the inner Bar, not the outer one. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " nested_type { name: \"Baz\" }" - "}" - "message_type {" - " name: \"Foo\"" - " nested_type { name: \"Bar\" }" - " field { name:\"baz\" number:1 label:LABEL_OPTIONAL" - " type_name:\"Bar.Baz\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " nested_type { name: \"Baz\" }" + "}" + "message_type {" + " name: \"Foo\"" + " nested_type { name: \"Bar\" }" + " field { name:\"baz\" number:1 label:LABEL_OPTIONAL" + " type_name:\"Bar.Baz\" }" + "}", - "foo.proto: Foo.baz: TYPE: \"Bar.Baz\" is resolved to \"Foo.Bar.Baz\"," - " which is not defined. The innermost scope is searched first in name " - "resolution. Consider using a leading '.'(i.e., \".Bar.Baz\") to start " - "from the outermost scope.\n"); + "foo.proto: Foo.baz: TYPE: \"Bar.Baz\" is resolved to \"Foo.Bar.Baz\"," + " which is not defined. The innermost scope is searched first in name " + "resolution. Consider using a leading '.'(i.e., \".Bar.Baz\") to start " + "from the outermost scope.\n"); } TEST_F(ValidationErrorTest, SearchMostLocalFirst2) { @@ -4698,17 +4738,17 @@ TEST_F(ValidationErrorTest, SearchMostLocalFirst2) { // proceeds to find the outer one because the inner one's not an // aggregate. BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - " nested_type { name: \"Baz\" }" - "}" - "message_type {" - " name: \"Foo\"" - " field { name: \"Bar\" number:1 type:TYPE_BYTES } " - " field { name:\"baz\" number:2 label:LABEL_OPTIONAL" - " type_name:\"Bar.Baz\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + " nested_type { name: \"Baz\" }" + "}" + "message_type {" + " name: \"Foo\"" + " field { name: \"Bar\" number:1 type:TYPE_BYTES } " + " field { name:\"baz\" number:2 label:LABEL_OPTIONAL" + " type_name:\"Bar.Baz\" }" + "}"); } TEST_F(ValidationErrorTest, PackageOriginallyDeclaredInTransitiveDependent) { @@ -4734,349 +4774,348 @@ TEST_F(ValidationErrorTest, PackageOriginallyDeclaredInTransitiveDependent) { // prevent it from finding "foo.bar". BuildFile( - "name: \"foo.proto\" " - "package: \"foo.bar\" "); + "name: \"foo.proto\" " + "package: \"foo.bar\" "); BuildFile( - "name: \"bar.proto\" " - "package: \"foo.bar\" " - "dependency: \"foo.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "package: \"foo.bar\" " + "dependency: \"foo.proto\" " + "message_type { name: \"Bar\" }"); BuildFile( - "name: \"baz.proto\" " - "package: \"foo\" " - "dependency: \"bar.proto\" " - "message_type { " - " name: \"Baz\" " - " field { name:\"qux\" number:1 label:LABEL_OPTIONAL " - " type_name:\"bar.Bar\" }" - "}"); + "name: \"baz.proto\" " + "package: \"foo\" " + "dependency: \"bar.proto\" " + "message_type { " + " name: \"Baz\" " + " field { name:\"qux\" number:1 label:LABEL_OPTIONAL " + " type_name:\"bar.Bar\" }" + "}"); } TEST_F(ValidationErrorTest, FieldTypeNotAType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " - " type_name:\".Foo.bar\" }" - " field { name:\"bar\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " + " type_name:\".Foo.bar\" }" + " field { name:\"bar\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" + "}", - "foo.proto: Foo.foo: TYPE: \".Foo.bar\" is not a type.\n"); + "foo.proto: Foo.foo: TYPE: \".Foo.bar\" is not a type.\n"); } TEST_F(ValidationErrorTest, RelativeFieldTypeNotAType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " nested_type {" - " name: \"Bar\"" - " field { name:\"Baz\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" - " }" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " - " type_name:\"Bar.Baz\" }" - "}", - "foo.proto: Foo.foo: TYPE: \"Bar.Baz\" is not a type.\n"); + "name: \"foo.proto\" " + "message_type {" + " nested_type {" + " name: \"Bar\"" + " field { name:\"Baz\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" + " }" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL " + " type_name:\"Bar.Baz\" }" + "}", + "foo.proto: Foo.foo: TYPE: \"Bar.Baz\" is not a type.\n"); } TEST_F(ValidationErrorTest, FieldTypeMayBeItsName) { BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"Bar\"" - "}" - "message_type {" - " name: \"Foo\"" - " field { name:\"Bar\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"Bar\"" + "}" + "message_type {" + " name: \"Foo\"" + " field { name:\"Bar\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }" + "}"); } TEST_F(ValidationErrorTest, EnumFieldTypeIsMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Bar\" } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM" - " type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Bar\" } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM" + " type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not an enum type.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not an enum type.\n"); } TEST_F(ValidationErrorTest, MessageFieldTypeIsEnum) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE" - " type_name:\"Bar\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE" + " type_name:\"Bar\" }" + "}", - "foo.proto: Foo.foo: TYPE: \"Bar\" is not a message type.\n"); + "foo.proto: Foo.foo: TYPE: \"Bar\" is not a message type.\n"); } TEST_F(ValidationErrorTest, BadEnumDefaultValue) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" - " default_value:\"NO_SUCH_VALUE\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" + " default_value:\"NO_SUCH_VALUE\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Enum type \"Bar\" has no value named " + "foo.proto: Foo.foo: DEFAULT_VALUE: Enum type \"Bar\" has no value named " "\"NO_SUCH_VALUE\".\n"); } TEST_F(ValidationErrorTest, EnumDefaultValueIsInteger) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" - " default_value:\"0\" }" - "}", + "name: \"foo.proto\" " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\"" + " default_value:\"0\" }" + "}", - "foo.proto: Foo.foo: DEFAULT_VALUE: Default value for an enum field must " - "be an identifier.\n"); + "foo.proto: Foo.foo: DEFAULT_VALUE: Default value for an enum field must " + "be an identifier.\n"); } TEST_F(ValidationErrorTest, PrimitiveWithTypeName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" - " type_name:\"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32" + " type_name:\"Foo\" }" + "}", - "foo.proto: Foo.foo: TYPE: Field with primitive type has type_name.\n"); + "foo.proto: Foo.foo: TYPE: Field with primitive type has type_name.\n"); } TEST_F(ValidationErrorTest, NonPrimitiveWithoutTypeName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE }" + "}", - "foo.proto: Foo.foo: TYPE: Field with message or enum type missing " + "foo.proto: Foo.foo: TYPE: Field with message or enum type missing " "type_name.\n"); } TEST_F(ValidationErrorTest, OneofWithNoFields) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " oneof_decl { name:\"bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " oneof_decl { name:\"bar\" }" + "}", - "foo.proto: Foo.bar: NAME: Oneof must have at least one field.\n"); + "foo.proto: Foo.bar: NAME: Oneof must have at least one field.\n"); } TEST_F(ValidationErrorTest, OneofLabelMismatch) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"Foo\"" - " field { name:\"foo\" number:1 label:LABEL_REPEATED type:TYPE_INT32 " - " oneof_index:0 }" - " oneof_decl { name:\"bar\" }" - "}", + "name: \"foo.proto\" " + "message_type {" + " name: \"Foo\"" + " field { name:\"foo\" number:1 label:LABEL_REPEATED type:TYPE_INT32 " + " oneof_index:0 }" + " oneof_decl { name:\"bar\" }" + "}", - "foo.proto: Foo.foo: NAME: Fields of oneofs must themselves have label " + "foo.proto: Foo.foo: NAME: Fields of oneofs must themselves have label " "LABEL_OPTIONAL.\n"); } TEST_F(ValidationErrorTest, InputTypeNotDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" + "}", - "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not defined.\n" - ); + "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not defined.\n" + ); } TEST_F(ValidationErrorTest, InputTypeNotAMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }" + "}", - "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not a message type.\n" - ); + "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not a message type.\n" + ); } TEST_F(ValidationErrorTest, OutputTypeNotDefined) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" + "}", - "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not defined.\n" - ); + "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not defined.\n" + ); } TEST_F(ValidationErrorTest, OutputTypeNotAMessage) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " - "service {" - " name: \"TestService\"" - " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" - "}", + "name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } " + "service {" + " name: \"TestService\"" + " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }" + "}", - "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not a message type.\n" - ); + "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not a message type.\n" + ); } TEST_F(ValidationErrorTest, IllegalPackedField) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {\n" - " name: \"Foo\"" - " field { name:\"packed_string\" number:1 label:LABEL_REPEATED " - " type:TYPE_STRING " - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - " field { name:\"packed_message\" number:3 label:LABEL_REPEATED " - " type_name: \"Foo\"" - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - " field { name:\"optional_int32\" number: 4 label: LABEL_OPTIONAL " - " type:TYPE_INT32 " - " options { uninterpreted_option {" - " name { name_part: \"packed\" is_extension: false }" - " identifier_value: \"true\" }}}\n" - "}", - - "foo.proto: Foo.packed_string: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - "foo.proto: Foo.packed_message: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - "foo.proto: Foo.optional_int32: TYPE: [packed = true] can only be " - "specified for repeated primitive fields.\n" - ); + "name: \"foo.proto\" " + "message_type {\n" + " name: \"Foo\"" + " field { name:\"packed_string\" number:1 label:LABEL_REPEATED " + " type:TYPE_STRING " + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + " field { name:\"packed_message\" number:3 label:LABEL_REPEATED " + " type_name: \"Foo\"" + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + " field { name:\"optional_int32\" number: 4 label: LABEL_OPTIONAL " + " type:TYPE_INT32 " + " options { uninterpreted_option {" + " name { name_part: \"packed\" is_extension: false }" + " identifier_value: \"true\" }}}\n" + "}", + + "foo.proto: Foo.packed_string: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n" + "foo.proto: Foo.packed_message: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n" + "foo.proto: Foo.optional_int32: TYPE: [packed = true] can only be " + "specified for repeated primitive fields.\n"); } TEST_F(ValidationErrorTest, OptionWrongType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " positive_int_value: 1 }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " positive_int_value: 1 }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_VALUE: Value must be identifier for " - "enum-valued option \"google.protobuf.FieldOptions.ctype\".\n"); + "foo.proto: TestMessage.foo: OPTION_VALUE: Value must be identifier for " + "enum-valued option \"google.protobuf.FieldOptions.ctype\".\n"); } TEST_F(ValidationErrorTest, OptionExtendsAtomicType) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " name { name_part: \"foo\" " - " is_extension: true }" - " positive_int_value: 1 }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " name { name_part: \"foo\" " + " is_extension: true }" + " positive_int_value: 1 }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" is an " - "atomic type, not a message.\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" is an " + "atomic type, not a message.\n"); } TEST_F(ValidationErrorTest, DupOption) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32 " - " options { uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " identifier_value: \"CORD\" }" - " uninterpreted_option { name { name_part: \"ctype\" " - " is_extension: false }" - " identifier_value: \"CORD\" }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32 " + " options { uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " identifier_value: \"CORD\" }" + " uninterpreted_option { name { name_part: \"ctype\" " + " is_extension: false }" + " identifier_value: \"CORD\" }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" was " - "already set.\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" was " + "already set.\n"); } TEST_F(ValidationErrorTest, InvalidOptionName) { BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type { " - " name: \"TestMessage\" " - " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL " - " options { uninterpreted_option { " - " name { name_part: \"uninterpreted_option\" " - " is_extension: false }" - " positive_int_value: 1 " - " }" - " }" - " }" - "}\n", + "name: \"foo.proto\" " + "message_type { " + " name: \"TestMessage\" " + " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL " + " options { uninterpreted_option { " + " name { name_part: \"uninterpreted_option\" " + " is_extension: false }" + " positive_int_value: 1 " + " }" + " }" + " }" + "}\n", - "foo.proto: TestMessage.foo: OPTION_NAME: Option must not use " - "reserved name \"uninterpreted_option\".\n"); + "foo.proto: TestMessage.foo: OPTION_NAME: Option must not use " + "reserved name \"uninterpreted_option\".\n"); } TEST_F(ValidationErrorTest, RepeatedMessageOption) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "message_type: { name: \"Bar\" field: { " - " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " - "} " - "extension { name: \"bar\" number: 7672757 label: LABEL_REPEATED " - " type: TYPE_MESSAGE type_name: \"Bar\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "message_type: { name: \"Bar\" field: { " + " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " + "} " + "extension { name: \"bar\" number: 7672757 label: LABEL_REPEATED " + " type: TYPE_MESSAGE type_name: \"Bar\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", - "foo.proto: foo.proto: OPTION_NAME: Option field \"(bar)\" is a " - "repeated message. Repeated message options must be initialized " - "using an aggregate value.\n"); + "foo.proto: foo.proto: OPTION_NAME: Option field \"(bar)\" is a " + "repeated message. Repeated message options must be initialized " + "using an aggregate value.\n"); } TEST_F(ValidationErrorTest, ResolveUndefinedOption) { @@ -5098,299 +5137,301 @@ TEST_F(ValidationErrorTest, ResolveUndefinedOption) { BuildDescriptorMessagesInTestPool(); BuildFile( - "name: \"foo.proto\" " - "package: \"baz\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "message_type: { name: \"Bar\" field: { " - " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " - "} " - "extension { name: \"bar\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_MESSAGE type_name: \"Bar\" " - " extendee: \"google.protobuf.FileOptions\" }"); - - BuildFileWithErrors( - "name: \"qux.proto\" " - "package: \"qux.baz\" " - "options { uninterpreted_option { name { name_part: \"baz.bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", - - "qux.proto: qux.proto: OPTION_NAME: Option \"(baz.bar)\" is resolved to " - "\"(qux.baz.bar)\"," - " which is not defined. The innermost scope is searched first in name " - "resolution. Consider using a leading '.'(i.e., \"(.baz.bar)\") to start " - "from the outermost scope.\n"); + "name: \"foo.proto\" " + "package: \"baz\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "message_type: { name: \"Bar\" field: { " + " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } " + "} " + "extension { name: \"bar\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_MESSAGE type_name: \"Bar\" " + " extendee: \"google.protobuf.FileOptions\" }"); + + BuildFileWithErrors( + "name: \"qux.proto\" " + "package: \"qux.baz\" " + "options { uninterpreted_option { name { name_part: \"baz.bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", + + "qux.proto: qux.proto: OPTION_NAME: Option \"(baz.bar)\" is resolved to " + "\"(qux.baz.bar)\"," + " which is not defined. The innermost scope is searched first in name " + "resolution. Consider using a leading '.'(i.e., \"(.baz.bar)\") to start " + "from the outermost scope.\n"); } TEST_F(ValidationErrorTest, UnknownOption) { BuildFileWithErrors( - "name: \"qux.proto\" " - "package: \"qux.baz\" " - "options { uninterpreted_option { name { name_part: \"baaz.bar\" " - " is_extension: true } " - " name { name_part: \"foo\" " - " is_extension: false } " - " positive_int_value: 1 } }", + "name: \"qux.proto\" " + "package: \"qux.baz\" " + "options { uninterpreted_option { name { name_part: \"baaz.bar\" " + " is_extension: true } " + " name { name_part: \"foo\" " + " is_extension: false } " + " positive_int_value: 1 } }", - "qux.proto: qux.proto: OPTION_NAME: Option \"(baaz.bar)\" unknown. Ensure " - "that your proto definition file imports the proto which defines the " - "option.\n"); + "qux.proto: qux.proto: OPTION_NAME: Option \"(baaz.bar)\" unknown. " + "Ensure " + "that your proto definition file imports the proto which defines the " + "option.\n"); } TEST_F(ValidationErrorTest, CustomOptionConflictingFieldNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo1\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }" - "extension { name: \"foo2\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo1\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }" + "extension { name: \"foo2\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }", - "foo.proto: foo2: NUMBER: Extension number 7672757 has already been used " - "in \"google.protobuf.FieldOptions\" by extension \"foo1\".\n"); + "foo.proto: foo2: NUMBER: Extension number 7672757 has already been used " + "in \"google.protobuf.FieldOptions\" by extension \"foo1\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueOutOfPositiveRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x80000000 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x80000000 } " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " negative_int_value: -0x80000001 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " negative_int_value: -0x80000001 } " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"5\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"5\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " - "for int32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " + "for int32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x8000000000000000 } " - "}", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x8000000000000000 " + "} " + "}", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for int64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for int64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"5\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"5\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " - "for int64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be integer " + "for int64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " positive_int_value: 0x100000000 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " positive_int_value: 0x100000000 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value out of range " - "for uint32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value out of range " + "for uint32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " double_value: -5.6 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " double_value: -5.6 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " - "for uint32 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " + "for uint32 option \"foo\".\n"); } TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_UINT64 extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " negative_int_value: -5 } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_UINT64 extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " negative_int_value: -5 } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " - "for uint64 option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer " + "for uint64 option \"foo\".\n"); } TEST_F(ValidationErrorTest, FloatOptionValueIsNotNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_FLOAT extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_FLOAT extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be number " - "for float option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be number " + "for float option \"foo\".\n"); } TEST_F(ValidationErrorTest, DoubleOptionValueIsNotNumber) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_DOUBLE extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_DOUBLE extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be number " - "for double option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be number " + "for double option \"foo\".\n"); } TEST_F(ValidationErrorTest, BoolOptionValueIsNotTrueOrFalse) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_BOOL extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"bar\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_BOOL extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"bar\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be \"true\" or \"false\" " - "for boolean option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be \"true\" or \"false\" " + "for boolean option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsNotIdentifier) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " string_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " string_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Value must be identifier for " - "enum-valued option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Value must be identifier for " + "enum-valued option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsNotEnumValueName) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum\" has no value " - "named \"QUUX\" for option \"foo\".\n"); + "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum\" has no value " + "named \"QUUX\" for option \"foo\".\n"); } TEST_F(ValidationErrorTest, EnumOptionValueIsSiblingEnumValueName) { BuildDescriptorMessagesInTestPool(); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"google/protobuf/descriptor.proto\" " - "enum_type { name: \"FooEnum1\" value { name: \"BAR\" number: 1 } " - " value { name: \"BAZ\" number: 2 } }" - "enum_type { name: \"FooEnum2\" value { name: \"QUX\" number: 1 } " - " value { name: \"QUUX\" number: 2 } }" - "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " - " type: TYPE_ENUM type_name: \"FooEnum1\" " - " extendee: \"google.protobuf.FileOptions\" }" - "options { uninterpreted_option { name { name_part: \"foo\" " - " is_extension: true } " - " identifier_value: \"QUUX\" } }", + "name: \"foo.proto\" " + "dependency: \"google/protobuf/descriptor.proto\" " + "enum_type { name: \"FooEnum1\" value { name: \"BAR\" number: 1 } " + " value { name: \"BAZ\" number: 2 } }" + "enum_type { name: \"FooEnum2\" value { name: \"QUX\" number: 1 } " + " value { name: \"QUUX\" number: 2 } }" + "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL " + " type: TYPE_ENUM type_name: \"FooEnum1\" " + " extendee: \"google.protobuf.FileOptions\" }" + "options { uninterpreted_option { name { name_part: \"foo\" " + " is_extension: true } " + " identifier_value: \"QUUX\" } }", - "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum1\" has no value " - "named \"QUUX\" for option \"foo\". This appears to be a value from a " - "sibling type.\n"); + "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum1\" has no value " + "named \"QUUX\" for option \"foo\". This appears to be a value from a " + "sibling type.\n"); } TEST_F(ValidationErrorTest, StringOptionValueIsNotString) { @@ -5412,22 +5453,22 @@ TEST_F(ValidationErrorTest, StringOptionValueIsNotString) { TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) { BuildFileWithErrors( - "name: \"foo.proto\" " - "package: \"foo\" " - "message_type {" - " name: \"Foo\"" - " extension_range { start: 10 end: 20 }" - "}" - "extension {" - " name: \"value\"" - " number: 10" - " label: LABEL_OPTIONAL" - " type: TYPE_INT32" - " extendee: \"foo.Foo\"" - " json_name: \"myName\"" - "}", - "foo.proto: foo.value: OPTION_NAME: option json_name is not allowed on " - "extension fields.\n"); + "name: \"foo.proto\" " + "package: \"foo\" " + "message_type {" + " name: \"Foo\"" + " extension_range { start: 10 end: 20 }" + "}" + "extension {" + " name: \"value\"" + " number: 10" + " label: LABEL_OPTIONAL" + " type: TYPE_INT32" + " extendee: \"foo.Foo\"" + " json_name: \"myName\"" + "}", + "foo.proto: foo.value: OPTION_NAME: option json_name is not allowed on " + "extension fields.\n"); } TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) { @@ -5497,60 +5538,60 @@ TEST_F(ValidationErrorTest, AggregateValueUnknownFields) { TEST_F(ValidationErrorTest, NotLiteImportsLite) { BuildFile( - "name: \"bar.proto\" " - "options { optimize_for: LITE_RUNTIME } "); + "name: \"bar.proto\" " + "options { optimize_for: LITE_RUNTIME } "); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"bar.proto\" ", + "name: \"foo.proto\" " + "dependency: \"bar.proto\" ", - "foo.proto: foo.proto: OTHER: Files that do not use optimize_for = " + "foo.proto: bar.proto: IMPORT: Files that do not use optimize_for = " "LITE_RUNTIME cannot import files which do use this option. This file " "is not lite, but it imports \"bar.proto\" which is.\n"); } TEST_F(ValidationErrorTest, LiteExtendsNotLite) { BuildFile( - "name: \"bar.proto\" " - "message_type: {" - " name: \"Bar\"" - " extension_range { start: 1 end: 1000 }" - "}"); + "name: \"bar.proto\" " + "message_type: {" + " name: \"Bar\"" + " extension_range { start: 1 end: 1000 }" + "}"); BuildFileWithErrors( - "name: \"foo.proto\" " - "dependency: \"bar.proto\" " - "options { optimize_for: LITE_RUNTIME } " - "extension { name: \"ext\" number: 123 label: LABEL_OPTIONAL " - " type: TYPE_INT32 extendee: \"Bar\" }", + "name: \"foo.proto\" " + "dependency: \"bar.proto\" " + "options { optimize_for: LITE_RUNTIME } " + "extension { name: \"ext\" number: 123 label: LABEL_OPTIONAL " + " type: TYPE_INT32 extendee: \"Bar\" }", - "foo.proto: ext: EXTENDEE: Extensions to non-lite types can only be " + "foo.proto: ext: EXTENDEE: Extensions to non-lite types can only be " "declared in non-lite files. Note that you cannot extend a non-lite " "type to contain a lite type, but the reverse is allowed.\n"); } TEST_F(ValidationErrorTest, NoLiteServices) { BuildFileWithErrors( - "name: \"foo.proto\" " - "options {" - " optimize_for: LITE_RUNTIME" - " cc_generic_services: true" - " java_generic_services: true" - "} " - "service { name: \"Foo\" }", + "name: \"foo.proto\" " + "options {" + " optimize_for: LITE_RUNTIME" + " cc_generic_services: true" + " java_generic_services: true" + "} " + "service { name: \"Foo\" }", - "foo.proto: Foo: NAME: Files with optimize_for = LITE_RUNTIME cannot " - "define services unless you set both options cc_generic_services and " - "java_generic_sevices to false.\n"); + "foo.proto: Foo: NAME: Files with optimize_for = LITE_RUNTIME cannot " + "define services unless you set both options cc_generic_services and " + "java_generic_sevices to false.\n"); BuildFile( - "name: \"bar.proto\" " - "options {" - " optimize_for: LITE_RUNTIME" - " cc_generic_services: false" - " java_generic_services: false" - "} " - "service { name: \"Bar\" }"); + "name: \"bar.proto\" " + "options {" + " optimize_for: LITE_RUNTIME" + " cc_generic_services: false" + " java_generic_services: false" + "} " + "service { name: \"Bar\" }"); } TEST_F(ValidationErrorTest, RollbackAfterError) { @@ -5559,47 +5600,47 @@ TEST_F(ValidationErrorTest, RollbackAfterError) { // before the undefined type error is noticed. The DescriptorPool will then // have to roll everything back. BuildFileWithErrors( - "name: \"foo.proto\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" - "} " - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"BAR\" number:1 }" - "} " - "service {" - " name: \"TestService\"" - " method {" - " name: \"Baz\"" - " input_type: \"NoSuchType\"" // error - " output_type: \"TestMessage\"" - " }" - "}", - - "foo.proto: TestService.Baz: INPUT_TYPE: \"NoSuchType\" is not defined.\n" - ); + "name: \"foo.proto\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" + "} " + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"BAR\" number:1 }" + "} " + "service {" + " name: \"TestService\"" + " method {" + " name: \"Baz\"" + " input_type: \"NoSuchType\"" // error + " output_type: \"TestMessage\"" + " }" + "}", + + "foo.proto: TestService.Baz: INPUT_TYPE: \"NoSuchType\" is not defined.\n" + ); // Make sure that if we build the same file again with the error fixed, // it works. If the above rollback was incomplete, then some symbols will // be left defined, and this second attempt will fail since it tries to // re-define the same symbols. BuildFile( - "name: \"foo.proto\" " - "message_type {" - " name: \"TestMessage\"" - " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" - "} " - "enum_type {" - " name: \"TestEnum\"" - " value { name:\"BAR\" number:1 }" - "} " - "service {" - " name: \"TestService\"" - " method { name:\"Baz\"" - " input_type:\"TestMessage\"" - " output_type:\"TestMessage\" }" - "}"); + "name: \"foo.proto\" " + "message_type {" + " name: \"TestMessage\"" + " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }" + "} " + "enum_type {" + " name: \"TestEnum\"" + " value { name:\"BAR\" number:1 }" + "} " + "service {" + " name: \"TestService\"" + " method { name:\"Baz\"" + " input_type:\"TestMessage\"" + " output_type:\"TestMessage\" }" + "}"); } TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { @@ -5607,11 +5648,11 @@ TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { // provided. FileDescriptorProto file_proto; - ASSERT_TRUE(TextFormat::ParseFromString( - "name: \"foo.proto\" " - "message_type { name: \"Foo\" } " - "message_type { name: \"Foo\" } ", - &file_proto)); + ASSERT_TRUE( + TextFormat::ParseFromString("name: \"foo.proto\" " + "message_type { name: \"Foo\" } " + "message_type { name: \"Foo\" } ", + &file_proto)); std::vector<std::string> errors; @@ -5629,50 +5670,50 @@ TEST_F(ValidationErrorTest, ErrorsReportedToLogError) { TEST_F(ValidationErrorTest, DisallowEnumAlias) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:0 }" - " value { name:\"ENUM_B\" number:0 }" - "}", - "foo.proto: Bar: NUMBER: " - "\"ENUM_B\" uses the same enum value as \"ENUM_A\". " - "If this is intended, set 'option allow_alias = true;' to the enum " - "definition.\n"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:0 }" + " value { name:\"ENUM_B\" number:0 }" + "}", + "foo.proto: Bar: NUMBER: " + "\"ENUM_B\" uses the same enum value as \"ENUM_A\". " + "If this is intended, set 'option allow_alias = true;' to the enum " + "definition.\n"); } TEST_F(ValidationErrorTest, AllowEnumAlias) { BuildFile( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:0 }" - " value { name:\"ENUM_B\" number:0 }" - " options { allow_alias: true }" - "}"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:0 }" + " value { name:\"ENUM_B\" number:0 }" + " options { allow_alias: true }" + "}"); } TEST_F(ValidationErrorTest, UnusedImportWarning) { pool_.AddUnusedImportTrackFile("bar.proto"); BuildFile( - "name: \"bar.proto\" " - "message_type { name: \"Bar\" }"); + "name: \"bar.proto\" " + "message_type { name: \"Bar\" }"); pool_.AddUnusedImportTrackFile("base.proto"); BuildFile( - "name: \"base.proto\" " - "message_type { name: \"Base\" }"); + "name: \"base.proto\" " + "message_type { name: \"Base\" }"); pool_.AddUnusedImportTrackFile("baz.proto"); BuildFile( - "name: \"baz.proto\" " - "message_type { name: \"Baz\" }"); + "name: \"baz.proto\" " + "message_type { name: \"Baz\" }"); pool_.AddUnusedImportTrackFile("public.proto"); BuildFile( - "name: \"public.proto\" " - "dependency: \"bar.proto\"" - "public_dependency: 0"); + "name: \"public.proto\" " + "dependency: \"bar.proto\"" + "public_dependency: 0"); // // forward.proto // import "base.proto" // No warning: Base message is used. @@ -5685,17 +5726,18 @@ TEST_F(ValidationErrorTest, UnusedImportWarning) { // pool_.AddUnusedImportTrackFile("forward.proto"); BuildFileWithWarnings( - "name: \"forward.proto\"" - "dependency: \"base.proto\"" - "dependency: \"bar.proto\"" - "dependency: \"baz.proto\"" - "dependency: \"public.proto\"" - "public_dependency: 2 " - "message_type {" - " name: \"Forward\"" - " field { name:\"base\" number:1 label:LABEL_OPTIONAL type_name:\"Base\" }" - "}", - "forward.proto: bar.proto: OTHER: Import bar.proto but not used.\n"); + "name: \"forward.proto\"" + "dependency: \"base.proto\"" + "dependency: \"bar.proto\"" + "dependency: \"baz.proto\"" + "dependency: \"public.proto\"" + "public_dependency: 2 " + "message_type {" + " name: \"Forward\"" + " field { name:\"base\" number:1 label:LABEL_OPTIONAL " + "type_name:\"Base\" }" + "}", + "forward.proto: bar.proto: IMPORT: Import bar.proto but not used.\n"); } namespace { @@ -5726,7 +5768,7 @@ void FillValidMapEntry(FileDescriptorProto* file_proto) { file_proto)); } static const char* kMapEntryErrorMessage = - "foo.proto: Foo.foo_map: OTHER: map_entry should not be set explicitly. " + "foo.proto: Foo.foo_map: TYPE: map_entry should not be set explicitly. " "Use map<KeyType, ValueType> instead.\n"; static const char* kMapEntryKeyTypeErrorMessage = "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot be float/double, " @@ -5830,9 +5872,9 @@ TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) { TEST_F(ValidationErrorTest, MapEntryKeyName) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_name("Key"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5840,9 +5882,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyName) { TEST_F(ValidationErrorTest, MapEntryKeyLabel) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_label(FieldDescriptorProto::LABEL_REQUIRED); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5850,9 +5892,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyLabel) { TEST_F(ValidationErrorTest, MapEntryKeyNumber) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_number(3); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5860,9 +5902,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyNumber) { TEST_F(ValidationErrorTest, MapEntryValueName) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_name("Value"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5870,9 +5912,9 @@ TEST_F(ValidationErrorTest, MapEntryValueName) { TEST_F(ValidationErrorTest, MapEntryValueLabel) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_label(FieldDescriptorProto::LABEL_REQUIRED); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5880,9 +5922,9 @@ TEST_F(ValidationErrorTest, MapEntryValueLabel) { TEST_F(ValidationErrorTest, MapEntryValueNumber) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* value = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(1); + FieldDescriptorProto* value = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 1); value->set_number(3); BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage); } @@ -5890,9 +5932,9 @@ TEST_F(ValidationErrorTest, MapEntryValueNumber) { TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_FLOAT); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5900,9 +5942,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) { TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_DOUBLE); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5910,9 +5952,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) { TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->set_type(FieldDescriptorProto::TYPE_BYTES); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); } @@ -5920,9 +5962,9 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) { TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->clear_type(); key->set_type_name("BarEnum"); EnumDescriptorProto* enum_proto = file_proto.add_enum_type(); @@ -5942,13 +5984,12 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) { "be enum types.\n"); } - TEST_F(ValidationErrorTest, MapEntryKeyTypeMessage) { FileDescriptorProto file_proto; FillValidMapEntry(&file_proto); - FieldDescriptorProto* key = file_proto.mutable_message_type(0) - ->mutable_nested_type(0) - ->mutable_field(0); + FieldDescriptorProto* key = + file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field( + 0); key->clear_type(); key->set_type_name(".Bar"); BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage); @@ -6130,31 +6171,31 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) { TEST_F(ValidationErrorTest, MapEntryUsesNoneZeroEnumDefaultValue) { BuildFileWithErrors( - "name: \"foo.proto\" " - "enum_type {" - " name: \"Bar\"" - " value { name:\"ENUM_A\" number:1 }" - " value { name:\"ENUM_B\" number:2 }" - "}" - "message_type {" - " name: 'Foo' " - " field { " - " name: 'foo_map' number: 1 label:LABEL_REPEATED " - " type_name: 'FooMapEntry' " - " } " - " nested_type { " - " name: 'FooMapEntry' " - " options { map_entry: true } " - " field { " - " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL " - " } " - " field { " - " name: 'value' number: 2 type_name:\"Bar\" label:LABEL_OPTIONAL " - " } " - " } " - "}", - "foo.proto: Foo.foo_map: " - "TYPE: Enum value in map must define 0 as the first value.\n"); + "name: \"foo.proto\" " + "enum_type {" + " name: \"Bar\"" + " value { name:\"ENUM_A\" number:1 }" + " value { name:\"ENUM_B\" number:2 }" + "}" + "message_type {" + " name: 'Foo' " + " field { " + " name: 'foo_map' number: 1 label:LABEL_REPEATED " + " type_name: 'FooMapEntry' " + " } " + " nested_type { " + " name: 'FooMapEntry' " + " options { map_entry: true } " + " field { " + " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL " + " } " + " field { " + " name: 'value' number: 2 type_name:\"Bar\" label:LABEL_OPTIONAL " + " } " + " } " + "}", + "foo.proto: Foo.foo_map: " + "TYPE: Enum value in map must define 0 as the first value.\n"); } TEST_F(ValidationErrorTest, Proto3RequiredFields) { @@ -6165,7 +6206,7 @@ TEST_F(ValidationErrorTest, Proto3RequiredFields) { " name: 'Foo' " " field { name:'foo' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } " "}", - "foo.proto: Foo.foo: OTHER: Required fields are not allowed in " + "foo.proto: Foo.foo: TYPE: Required fields are not allowed in " "proto3.\n"); // applied to nested types as well. @@ -6179,7 +6220,7 @@ TEST_F(ValidationErrorTest, Proto3RequiredFields) { " field { name:'bar' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } " " } " "}", - "foo.proto: Foo.Bar.bar: OTHER: Required fields are not allowed in " + "foo.proto: Foo.Bar.bar: TYPE: Required fields are not allowed in " "proto3.\n"); // optional and repeated fields are OK. @@ -6202,8 +6243,8 @@ TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) { " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 " " default_value: '1' }" "}", - "foo.proto: Foo.foo: OTHER: Explicit default values are not allowed in " - "proto3.\n"); + "foo.proto: Foo.foo: DEFAULT_VALUE: Explicit default values are not " + "allowed in proto3.\n"); BuildFileWithErrors( "name: 'foo.proto' " @@ -6216,8 +6257,8 @@ TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) { " default_value: '1' }" " } " "}", - "foo.proto: Foo.Bar.bar: OTHER: Explicit default values are not allowed " - "in proto3.\n"); + "foo.proto: Foo.Bar.bar: DEFAULT_VALUE: Explicit default values are not " + "allowed in proto3.\n"); } TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { @@ -6229,7 +6270,7 @@ TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } " " extension_range { start:10 end:100 } " "}", - "foo.proto: Foo: OTHER: Extension ranges are not allowed in " + "foo.proto: Foo: NUMBER: Extension ranges are not allowed in " "proto3.\n"); BuildFileWithErrors( @@ -6243,7 +6284,7 @@ TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) { " extension_range { start:10 end:100 } " " } " "}", - "foo.proto: Foo.Bar: OTHER: Extension ranges are not allowed in " + "foo.proto: Foo.Bar: NUMBER: Extension ranges are not allowed in " "proto3.\n"); } @@ -6255,7 +6296,7 @@ TEST_F(ValidationErrorTest, ValidateProto3MessageSetWireFormat) { " name: 'Foo' " " options { message_set_wire_format: true } " "}", - "foo.proto: Foo: OTHER: MessageSet is not supported " + "foo.proto: Foo: NAME: MessageSet is not supported " "in proto3.\n"); } @@ -6267,7 +6308,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) { " name: 'FooEnum' " " value { name: 'FOO_FOO' number:1 } " "}", - "foo.proto: FooEnum: OTHER: The first enum value must be " + "foo.proto: FooEnum: NUMBER: The first enum value must be " "zero in proto3.\n"); BuildFileWithErrors( @@ -6280,7 +6321,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) { " value { name: 'FOO_FOO' number:1 } " " } " "}", - "foo.proto: Foo.FooEnum: OTHER: The first enum value must be " + "foo.proto: Foo.FooEnum: NUMBER: The first enum value must be " "zero in proto3.\n"); // valid case. @@ -6372,7 +6413,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Extension) { " name: 'bar' number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 " " extendee: 'Container' " "}", - "bar.proto: bar: OTHER: Extensions in proto3 are only allowed for " + "bar.proto: bar: EXTENDEE: Extensions in proto3 are only allowed for " "defining options.\n"); } @@ -6387,7 +6428,7 @@ TEST_F(ValidationErrorTest, ValidateProto3JsonName) { " field { name:'name' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" " field { name:'Name' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" "}", - "foo.proto: Foo: OTHER: The JSON camel-case name of field \"Name\" " + "foo.proto: Foo: NAME: The JSON camel-case name of field \"Name\" " "conflicts with field \"name\". This is not allowed in proto3.\n"); // Underscores are ignored. BuildFileWithErrors( @@ -6398,7 +6439,7 @@ TEST_F(ValidationErrorTest, ValidateProto3JsonName) { " field { name:'ab' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" " field { name:'_a__b_' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" "}", - "foo.proto: Foo: OTHER: The JSON camel-case name of field \"_a__b_\" " + "foo.proto: Foo: NAME: The JSON camel-case name of field \"_a__b_\" " "conflicts with field \"ab\". This is not allowed in proto3.\n"); } @@ -6420,24 +6461,26 @@ class DatabaseBackedPoolTest : public testing::Test { SimpleDescriptorDatabase database_; virtual void SetUp() { + AddToDatabase( + &database_, + "name: 'foo.proto' " + "message_type { name:'Foo' extension_range { start: 1 end: 100 } } " + "enum_type { name:'TestEnum' value { name:'DUMMY' number:0 } } " + "service { name:'TestService' } "); AddToDatabase(&database_, - "name: 'foo.proto' " - "message_type { name:'Foo' extension_range { start: 1 end: 100 } } " - "enum_type { name:'TestEnum' value { name:'DUMMY' number:0 } } " - "service { name:'TestService' } "); - AddToDatabase(&database_, - "name: 'bar.proto' " - "dependency: 'foo.proto' " - "message_type { name:'Bar' } " - "extension { name:'foo_ext' extendee: '.Foo' number:5 " - " label:LABEL_OPTIONAL type:TYPE_INT32 } "); + "name: 'bar.proto' " + "dependency: 'foo.proto' " + "message_type { name:'Bar' } " + "extension { name:'foo_ext' extendee: '.Foo' number:5 " + " label:LABEL_OPTIONAL type:TYPE_INT32 } "); // Baz has an undeclared dependency on Foo. - AddToDatabase(&database_, - "name: 'baz.proto' " - "message_type { " - " name:'Baz' " - " field { name:'foo' number:1 label:LABEL_OPTIONAL type_name:'Foo' } " - "}"); + AddToDatabase( + &database_, + "name: 'baz.proto' " + "message_type { " + " name:'Baz' " + " field { name:'foo' number:1 label:LABEL_OPTIONAL type_name:'Foo' } " + "}"); } // We can't inject a file containing errors into a DescriptorPool, so we @@ -6481,7 +6524,7 @@ class DatabaseBackedPoolTest : public testing::Test { class CallCountingDatabase : public DescriptorDatabase { public: CallCountingDatabase(DescriptorDatabase* wrapped_db) - : wrapped_db_(wrapped_db) { + : wrapped_db_(wrapped_db) { Clear(); } ~CallCountingDatabase() {} @@ -6490,9 +6533,7 @@ class DatabaseBackedPoolTest : public testing::Test { int call_count_; - void Clear() { - call_count_ = 0; - } + void Clear() { call_count_ = 0; } // implements DescriptorDatabase --------------------------------- bool FindFileByName(const std::string& filename, @@ -6509,8 +6550,8 @@ class DatabaseBackedPoolTest : public testing::Test { int field_number, FileDescriptorProto* output) { ++call_count_; - return wrapped_db_->FindFileContainingExtension( - containing_type, field_number, output); + return wrapped_db_->FindFileContainingExtension(containing_type, + field_number, output); } }; @@ -6520,7 +6561,7 @@ class DatabaseBackedPoolTest : public testing::Test { class FalsePositiveDatabase : public DescriptorDatabase { public: FalsePositiveDatabase(DescriptorDatabase* wrapped_db) - : wrapped_db_(wrapped_db) {} + : wrapped_db_(wrapped_db) {} ~FalsePositiveDatabase() {} DescriptorDatabase* wrapped_db_; @@ -6667,13 +6708,13 @@ TEST_F(DatabaseBackedPoolTest, ErrorWithErrorCollector) { EXPECT_TRUE(pool.FindFileByName("error.proto") == NULL); EXPECT_EQ( - "error.proto: error.proto: OTHER: File recursively imports itself: " + "error.proto: error2.proto: IMPORT: File recursively imports itself: " "error.proto -> error2.proto -> error.proto\n" - "error2.proto: error2.proto: OTHER: Import \"error.proto\" was not " + "error2.proto: error.proto: IMPORT: Import \"error.proto\" was not " "found or had errors.\n" - "error.proto: error.proto: OTHER: Import \"error2.proto\" was not " + "error.proto: error2.proto: IMPORT: Import \"error2.proto\" was not " "found or had errors.\n", - error_collector.text_); + error_collector.text_); } TEST_F(DatabaseBackedPoolTest, UndeclaredDependencyOnUnbuiltType) { @@ -6683,10 +6724,10 @@ TEST_F(DatabaseBackedPoolTest, UndeclaredDependencyOnUnbuiltType) { DescriptorPool pool(&database_, &error_collector); EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == NULL); EXPECT_EQ( - "baz.proto: Baz.foo: TYPE: \"Foo\" seems to be defined in \"foo.proto\", " - "which is not imported by \"baz.proto\". To use it here, please add " - "the necessary import.\n", - error_collector.text_); + "baz.proto: Baz.foo: TYPE: \"Foo\" seems to be defined in \"foo.proto\", " + "which is not imported by \"baz.proto\". To use it here, please add " + "the necessary import.\n", + error_collector.text_); } TEST_F(DatabaseBackedPoolTest, RollbackAfterError) { @@ -6708,12 +6749,12 @@ TEST_F(DatabaseBackedPoolTest, UnittestProto) { // thoroughly test all paths through DescriptorBuilder to insure that there // are no deadlocking problems when pool_->mutex_ is non-NULL. const FileDescriptor* original_file = - protobuf_unittest::TestAllTypes::descriptor()->file(); + protobuf_unittest::TestAllTypes::descriptor()->file(); DescriptorPoolDatabase database(*DescriptorPool::generated_pool()); DescriptorPool pool(&database); const FileDescriptor* file_from_database = - pool.FindFileByName(original_file->name()); + pool.FindFileByName(original_file->name()); ASSERT_TRUE(file_from_database != NULL); @@ -6727,8 +6768,7 @@ TEST_F(DatabaseBackedPoolTest, UnittestProto) { file_from_database_proto.DebugString()); // Also verify that CopyTo() did not omit any information. - EXPECT_EQ(original_file->DebugString(), - file_from_database->DebugString()); + EXPECT_EQ(original_file->DebugString(), file_from_database->DebugString()); } TEST_F(DatabaseBackedPoolTest, DoesntRetryDbUnnecessarily) { @@ -6844,7 +6884,7 @@ class ExponentialErrorDatabase : public DescriptorDatabase { int begin_size = begin_with.size(); int end_size = end_with.size(); if (name.substr(0, begin_size) != begin_with || - name.substr(name.size()- end_size, end_size) != end_with) { + name.substr(name.size() - end_size, end_size) != end_with) { return; } safe_strto32( @@ -6937,8 +6977,9 @@ class SingletonSourceTree : public compiler::SourceTree { : filename_(filename), contents_(contents) {} virtual io::ZeroCopyInputStream* Open(const std::string& filename) { - return filename == filename_ ? - new io::ArrayInputStream(contents_.data(), contents_.size()) : NULL; + return filename == filename_ + ? new io::ArrayInputStream(contents_.data(), contents_.size()) + : NULL; } private: @@ -7040,10 +7081,8 @@ class SourceLocationTest : public testing::Test { } static std::string PrintSourceLocation(const SourceLocation& loc) { - return strings::Substitute("$0:$1-$2:$3", - 1 + loc.start_line, - 1 + loc.start_column, - 1 + loc.end_line, + return strings::Substitute("$0:$1-$2:$3", 1 + loc.start_line, + 1 + loc.start_column, 1 + loc.end_line, 1 + loc.end_column); } @@ -7073,27 +7112,27 @@ TEST_F(SourceLocationTest, GetSourceLocation) { const FileDescriptor* file_desc = GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto")); - const Descriptor *a_desc = file_desc->FindMessageTypeByName("A"); + const Descriptor* a_desc = file_desc->FindMessageTypeByName("A"); EXPECT_TRUE(a_desc->GetSourceLocation(&loc)); EXPECT_EQ("4:1-16:2", PrintSourceLocation(loc)); - const Descriptor *a_b_desc = a_desc->FindNestedTypeByName("B"); + const Descriptor* a_b_desc = a_desc->FindNestedTypeByName("B"); EXPECT_TRUE(a_b_desc->GetSourceLocation(&loc)); EXPECT_EQ("7:3-9:4", PrintSourceLocation(loc)); - const EnumDescriptor *e_desc = file_desc->FindEnumTypeByName("Indecision"); + const EnumDescriptor* e_desc = file_desc->FindEnumTypeByName("Indecision"); EXPECT_TRUE(e_desc->GetSourceLocation(&loc)); EXPECT_EQ("17:1-24:2", PrintSourceLocation(loc)); - const EnumValueDescriptor *yes_desc = e_desc->FindValueByName("YES"); + const EnumValueDescriptor* yes_desc = e_desc->FindValueByName("YES"); EXPECT_TRUE(yes_desc->GetSourceLocation(&loc)); EXPECT_EQ("21:3-21:42", PrintSourceLocation(loc)); - const ServiceDescriptor *s_desc = file_desc->FindServiceByName("S"); + const ServiceDescriptor* s_desc = file_desc->FindServiceByName("S"); EXPECT_TRUE(s_desc->GetSourceLocation(&loc)); EXPECT_EQ("25:1-35:2", PrintSourceLocation(loc)); - const MethodDescriptor *m_desc = s_desc->FindMethodByName("Method"); + const MethodDescriptor* m_desc = s_desc->FindMethodByName("Method"); EXPECT_TRUE(m_desc->GetSourceLocation(&loc)); EXPECT_EQ("29:3-29:31", PrintSourceLocation(loc)); @@ -7105,16 +7144,16 @@ TEST_F(SourceLocationTest, ExtensionSourceLocation) { const FileDescriptor* file_desc = GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto")); - const FieldDescriptor *int32_extension_desc = + const FieldDescriptor* int32_extension_desc = file_desc->FindExtensionByName("int32_extension"); EXPECT_TRUE(int32_extension_desc->GetSourceLocation(&loc)); EXPECT_EQ("40:3-40:55", PrintSourceLocation(loc)); - const Descriptor *c_desc = file_desc->FindMessageTypeByName("C"); + const Descriptor* c_desc = file_desc->FindMessageTypeByName("C"); EXPECT_TRUE(c_desc->GetSourceLocation(&loc)); EXPECT_EQ("42:1-46:2", PrintSourceLocation(loc)); - const FieldDescriptor *message_extension_desc = + const FieldDescriptor* message_extension_desc = c_desc->FindExtensionByName("message_extension"); EXPECT_TRUE(message_extension_desc->GetSourceLocation(&loc)); EXPECT_EQ("44:5-44:41", PrintSourceLocation(loc)); @@ -7502,7 +7541,7 @@ TEST_F(SourceLocationTest, GetSourceLocation_MissingSourceCodeInfo) { DescriptorPool bad1_pool(&pool_); const FileDescriptor* bad1_file_desc = GOOGLE_CHECK_NOTNULL(bad1_pool.BuildFile(proto)); - const Descriptor *bad1_a_desc = bad1_file_desc->FindMessageTypeByName("A"); + const Descriptor* bad1_a_desc = bad1_file_desc->FindMessageTypeByName("A"); EXPECT_FALSE(bad1_a_desc->GetSourceLocation(&loc)); } @@ -7516,7 +7555,7 @@ TEST_F(SourceLocationTest, GetSourceLocation_BogusSourceCodeInfo) { FileDescriptorProto proto; file_desc->CopyTo(&proto); // Note, this discards the SourceCodeInfo. EXPECT_FALSE(proto.has_source_code_info()); - SourceCodeInfo_Location *loc_msg = + SourceCodeInfo_Location* loc_msg = proto.mutable_source_code_info()->add_location(); loc_msg->add_path(1); loc_msg->add_path(2); @@ -7528,15 +7567,15 @@ TEST_F(SourceLocationTest, GetSourceLocation_BogusSourceCodeInfo) { DescriptorPool bad2_pool(&pool_); const FileDescriptor* bad2_file_desc = GOOGLE_CHECK_NOTNULL(bad2_pool.BuildFile(proto)); - const Descriptor *bad2_a_desc = bad2_file_desc->FindMessageTypeByName("A"); + const Descriptor* bad2_a_desc = bad2_file_desc->FindMessageTypeByName("A"); EXPECT_FALSE(bad2_a_desc->GetSourceLocation(&loc)); } // =================================================================== const char* const kCopySourceCodeInfoToTestInput = - "syntax = \"proto2\";\n" - "message Foo {}\n"; + "syntax = \"proto2\";\n" + "message Foo {}\n"; // Required since source code information is not preserved by // FileDescriptorTest. diff --git a/src/google/protobuf/duration.pb.cc b/src/google/protobuf/duration.pb.cc index 7c51badbb5..2f66112a43 100644 --- a/src/google/protobuf/duration.pb.cc +++ b/src/google/protobuf/duration.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsDuration_google_2fprotobuf_2fduration_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Duration_google_2fprotobuf_2fduration_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsDuration_google_2fprotobuf_2fduration_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fduration_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fduration_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fduration_2eproto = nullptr; @@ -76,8 +72,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto[] = "obuf.WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fduration_2eproto = { - false, InitDefaults_google_2fprotobuf_2fduration_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fduration_2eproto, "google/protobuf/duration.proto", &assign_descriptors_table_google_2fprotobuf_2fduration_2eproto, 227, }; @@ -85,6 +80,7 @@ void AddDescriptors_google_2fprotobuf_2fduration_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Duration_google_2fprotobuf_2fduration_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fduration_2eproto, deps, 0); } @@ -171,39 +167,45 @@ void Duration::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Duration::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // int64 seconds = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_seconds(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + seconds_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // int32 nanos = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_nanos(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + nanos_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Duration::MergePartialFromCodedStream( diff --git a/src/google/protobuf/duration.proto b/src/google/protobuf/duration.proto index 975fce41aa..9d8a52de29 100644 --- a/src/google/protobuf/duration.proto +++ b/src/google/protobuf/duration.proto @@ -101,7 +101,6 @@ option objc_class_prefix = "GPB"; // // message Duration { - // Signed seconds of the span of time. Must be from -315,576,000,000 // to +315,576,000,000 inclusive. Note: these bounds are computed from: // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index 44c1fd7394..89c9017f8f 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -82,7 +82,6 @@ #include <google/protobuf/repeated_field.h> #include <google/protobuf/wire_format.h> - namespace google { namespace protobuf { @@ -100,23 +99,29 @@ using internal::ArenaStringPtr; namespace { -bool IsMapFieldInApi(const FieldDescriptor* field) { - return field->is_map(); -} +bool IsMapFieldInApi(const FieldDescriptor* field) { return field->is_map(); } // Compute the byte size of the in-memory representation of the field. int FieldSpaceUsed(const FieldDescriptor* field) { typedef FieldDescriptor FD; // avoid line wrapping if (field->label() == FD::LABEL_REPEATED) { switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(RepeatedField<int32 >); - case FD::CPPTYPE_INT64 : return sizeof(RepeatedField<int64 >); - case FD::CPPTYPE_UINT32 : return sizeof(RepeatedField<uint32 >); - case FD::CPPTYPE_UINT64 : return sizeof(RepeatedField<uint64 >); - case FD::CPPTYPE_DOUBLE : return sizeof(RepeatedField<double >); - case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField<float >); - case FD::CPPTYPE_BOOL : return sizeof(RepeatedField<bool >); - case FD::CPPTYPE_ENUM : return sizeof(RepeatedField<int >); + case FD::CPPTYPE_INT32: + return sizeof(RepeatedField<int32>); + case FD::CPPTYPE_INT64: + return sizeof(RepeatedField<int64>); + case FD::CPPTYPE_UINT32: + return sizeof(RepeatedField<uint32>); + case FD::CPPTYPE_UINT64: + return sizeof(RepeatedField<uint64>); + case FD::CPPTYPE_DOUBLE: + return sizeof(RepeatedField<double>); + case FD::CPPTYPE_FLOAT: + return sizeof(RepeatedField<float>); + case FD::CPPTYPE_BOOL: + return sizeof(RepeatedField<bool>); + case FD::CPPTYPE_ENUM: + return sizeof(RepeatedField<int>); case FD::CPPTYPE_MESSAGE: if (IsMapFieldInApi(field)) { return sizeof(DynamicMapField); @@ -134,14 +139,22 @@ int FieldSpaceUsed(const FieldDescriptor* field) { } } else { switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(int32 ); - case FD::CPPTYPE_INT64 : return sizeof(int64 ); - case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); - case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); - case FD::CPPTYPE_DOUBLE : return sizeof(double ); - case FD::CPPTYPE_FLOAT : return sizeof(float ); - case FD::CPPTYPE_BOOL : return sizeof(bool ); - case FD::CPPTYPE_ENUM : return sizeof(int ); + case FD::CPPTYPE_INT32: + return sizeof(int32); + case FD::CPPTYPE_INT64: + return sizeof(int64); + case FD::CPPTYPE_UINT32: + return sizeof(uint32); + case FD::CPPTYPE_UINT64: + return sizeof(uint64); + case FD::CPPTYPE_DOUBLE: + return sizeof(double); + case FD::CPPTYPE_FLOAT: + return sizeof(float); + case FD::CPPTYPE_BOOL: + return sizeof(bool); + case FD::CPPTYPE_ENUM: + return sizeof(int); case FD::CPPTYPE_MESSAGE: return sizeof(Message*); @@ -165,14 +178,22 @@ int FieldSpaceUsed(const FieldDescriptor* field) { int OneofFieldSpaceUsed(const FieldDescriptor* field) { typedef FieldDescriptor FD; // avoid line wrapping switch (field->cpp_type()) { - case FD::CPPTYPE_INT32 : return sizeof(int32 ); - case FD::CPPTYPE_INT64 : return sizeof(int64 ); - case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); - case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); - case FD::CPPTYPE_DOUBLE : return sizeof(double ); - case FD::CPPTYPE_FLOAT : return sizeof(float ); - case FD::CPPTYPE_BOOL : return sizeof(bool ); - case FD::CPPTYPE_ENUM : return sizeof(int ); + case FD::CPPTYPE_INT32: + return sizeof(int32); + case FD::CPPTYPE_INT64: + return sizeof(int64); + case FD::CPPTYPE_UINT32: + return sizeof(uint32); + case FD::CPPTYPE_UINT64: + return sizeof(uint64); + case FD::CPPTYPE_DOUBLE: + return sizeof(double); + case FD::CPPTYPE_FLOAT: + return sizeof(float); + case FD::CPPTYPE_BOOL: + return sizeof(bool); + case FD::CPPTYPE_ENUM: + return sizeof(int); case FD::CPPTYPE_MESSAGE: return sizeof(Message*); @@ -190,9 +211,7 @@ int OneofFieldSpaceUsed(const FieldDescriptor* field) { return 0; } -inline int DivideRoundingUp(int i, int j) { - return (i + (j - 1)) / j; -} +inline int DivideRoundingUp(int i, int j) { return (i + (j - 1)) / j; } static const int kSafeAlignment = sizeof(uint64); static const int kMaxOneofUnionSize = sizeof(uint64); @@ -203,9 +222,7 @@ inline int AlignTo(int offset, int alignment) { // Rounds the given byte offset up to the next offset aligned such that any // type may be stored at it. -inline int AlignOffset(int offset) { - return AlignTo(offset, kSafeAlignment); -} +inline int AlignOffset(int offset) { return AlignTo(offset, kSafeAlignment); } #define bitsizeof(T) (sizeof(T) * 8) @@ -241,9 +258,7 @@ class DynamicMessage : public Message { TypeInfo() : prototype(NULL) {} - ~TypeInfo() { - delete prototype; - } + ~TypeInfo() { delete prototype; } }; DynamicMessage(const TypeInfo* type_info); @@ -273,9 +288,7 @@ class DynamicMessage : public Message { // care of for us. This makes DynamicMessage compatible with -fsized-delete. // It doesn't work for MSVC though. #ifndef _MSC_VER - static void operator delete(void* ptr) { - ::operator delete(ptr); - } + static void operator delete(void* ptr) { ::operator delete(ptr); } #endif // !_MSC_VER private: @@ -330,7 +343,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { const Descriptor* descriptor = type_info_->type; // Initialize oneof cases. - for (int i = 0 ; i < descriptor->oneof_decl_count(); ++i) { + for (int i = 0; i < descriptor->oneof_decl_count(); ++i) { new (OffsetToPointer(type_info_->oneof_case_offset + sizeof(uint32) * i)) uint32(0); } @@ -357,18 +370,18 @@ void DynamicMessage::SharedCtor(bool lock_factory) { } \ break; - HANDLE_TYPE(INT32 , int32 ); - HANDLE_TYPE(INT64 , int64 ); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(FLOAT , float ); - HANDLE_TYPE(BOOL , bool ); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_ENUM: if (!field->is_repeated()) { - new(field_ptr) int(field->default_value_enum()->number()); + new (field_ptr) int(field->default_value_enum()->number()); } else { new (field_ptr) RepeatedField<int>(arena_); } @@ -388,7 +401,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { type_info_->offsets[i])) ->Get()); } - ArenaStringPtr* asp = new(field_ptr) ArenaStringPtr(); + ArenaStringPtr* asp = new (field_ptr) ArenaStringPtr(); asp->UnsafeSetDefault(default_value); } else { new (field_ptr) RepeatedPtrField<std::string>(arena_); @@ -399,7 +412,7 @@ void DynamicMessage::SharedCtor(bool lock_factory) { case FieldDescriptor::CPPTYPE_MESSAGE: { if (!field->is_repeated()) { - new(field_ptr) Message*(NULL); + new (field_ptr) Message*(NULL); } else { if (IsMapFieldInApi(field)) { // We need to lock in most cases to avoid data racing. Only not lock @@ -445,7 +458,8 @@ DynamicMessage::~DynamicMessage() { if (type_info_->extensions_offset != -1) { reinterpret_cast<ExtensionSet*>( - OffsetToPointer(type_info_->extensions_offset))->~ExtensionSet(); + OffsetToPointer(type_info_->extensions_offset)) + ->~ExtensionSet(); } // We need to manually run the destructors for repeated fields and strings, @@ -459,13 +473,13 @@ DynamicMessage::~DynamicMessage() { for (int i = 0; i < descriptor->field_count(); i++) { const FieldDescriptor* field = descriptor->field(i); if (field->containing_oneof()) { - void* field_ptr = OffsetToPointer( - type_info_->oneof_case_offset - + sizeof(uint32) * field->containing_oneof()->index()); - if (*(reinterpret_cast<const uint32*>(field_ptr)) == - field->number()) { - field_ptr = OffsetToPointer(type_info_->offsets[ - descriptor->field_count() + field->containing_oneof()->index()]); + void* field_ptr = + OffsetToPointer(type_info_->oneof_case_offset + + sizeof(uint32) * field->containing_oneof()->index()); + if (*(reinterpret_cast<const uint32*>(field_ptr)) == field->number()) { + field_ptr = OffsetToPointer( + type_info_->offsets[descriptor->field_count() + + field->containing_oneof()->index()]); if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { switch (field->options().ctype()) { default: @@ -490,20 +504,20 @@ DynamicMessage::~DynamicMessage() { if (field->is_repeated()) { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - reinterpret_cast<RepeatedField<LOWERCASE>*>(field_ptr) \ - ->~RepeatedField<LOWERCASE>(); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + reinterpret_cast<RepeatedField<LOWERCASE>*>(field_ptr) \ + ->~RepeatedField<LOWERCASE>(); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -535,13 +549,13 @@ DynamicMessage::~DynamicMessage() { type_info_->prototype->OffsetToPointer( type_info_->offsets[i])) ->Get()); - reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy( - default_value, NULL); + reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy(default_value, + NULL); break; } } } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - if (!is_prototype()) { + if (!is_prototype()) { Message* message = *reinterpret_cast<Message**>(field_ptr); if (message != NULL) { delete message; @@ -569,7 +583,7 @@ void DynamicMessage::CrossLinkPrototypes() { // For singular fields, the field is just a pointer which should // point to the prototype. *reinterpret_cast<const Message**>(field_ptr) = - factory->GetPrototypeNoLock(field->message_type()); + factory->GetPrototypeNoLock(field->message_type()); } } } @@ -612,20 +626,19 @@ struct DynamicMessageFactory::PrototypeMap { }; DynamicMessageFactory::DynamicMessageFactory() - : pool_(NULL), delegate_to_generated_factory_(false), - prototypes_(new PrototypeMap) { -} + : pool_(NULL), + delegate_to_generated_factory_(false), + prototypes_(new PrototypeMap) {} DynamicMessageFactory::DynamicMessageFactory(const DescriptorPool* pool) - : pool_(pool), delegate_to_generated_factory_(false), - prototypes_(new PrototypeMap) { -} + : pool_(pool), + delegate_to_generated_factory_(false), + prototypes_(new PrototypeMap) {} DynamicMessageFactory::~DynamicMessageFactory() { for (PrototypeMap::Map::iterator iter = prototypes_->map_.begin(); iter != prototypes_->map_.end(); ++iter) { - DeleteDefaultOneofInstance(iter->second->type, - iter->second->offsets.get(), + DeleteDefaultOneofInstance(iter->second->type, iter->second->offsets.get(), iter->second->prototype); delete iter->second; } @@ -665,8 +678,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( // or not that field is set. // Compute size and offsets. - uint32* offsets = - new uint32[type->field_count() + type->oneof_decl_count()]; + uint32* offsets = new uint32[type->field_count() + type->oneof_decl_count()]; type_info->offsets.reset(offsets); // Decide all field offsets by packing in order. @@ -681,7 +693,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( } else { type_info->has_bits_offset = size; int has_bits_array_size = - DivideRoundingUp(type->field_count(), bitsizeof(uint32)); + DivideRoundingUp(type->field_count(), bitsizeof(uint32)); size += has_bits_array_size * sizeof(uint32); size = AlignOffset(size); @@ -742,7 +754,6 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( // alignment is not necessary. type_info->size = size; - // Construct the reflection object. if (type->oneof_decl_count() > 0) { @@ -777,21 +788,19 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( if (type->oneof_decl_count() > 0 || num_weak_fields > 0) { // Construct default oneof instance. - ConstructDefaultOneofInstance(type_info->type, - type_info->offsets.get(), + ConstructDefaultOneofInstance(type_info->type, type_info->offsets.get(), prototype); } - internal::ReflectionSchema schema = { - type_info->prototype, - type_info->offsets.get(), - type_info->has_bits_indices.get(), - type_info->has_bits_offset, - type_info->internal_metadata_offset, - type_info->extensions_offset, - type_info->oneof_case_offset, - type_info->size, - type_info->weak_field_map_offset}; + internal::ReflectionSchema schema = {type_info->prototype, + type_info->offsets.get(), + type_info->has_bits_indices.get(), + type_info->has_bits_offset, + type_info->internal_metadata_offset, + type_info->extensions_offset, + type_info->oneof_case_offset, + type_info->size, + type_info->weak_field_map_offset}; type_info->reflection.reset(new GeneratedMessageReflection( type_info->type, schema, type_info->pool, this)); @@ -803,31 +812,31 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock( } void DynamicMessageFactory::ConstructDefaultOneofInstance( - const Descriptor* type, - const uint32 offsets[], + const Descriptor* type, const uint32 offsets[], void* default_oneof_or_weak_instance) { for (int i = 0; i < type->oneof_decl_count(); i++) { for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = type->oneof_decl(i)->field(j); - void* field_ptr = reinterpret_cast<uint8*>( - default_oneof_or_weak_instance) + offsets[field->index()]; + void* field_ptr = + reinterpret_cast<uint8*>(default_oneof_or_weak_instance) + + offsets[field->index()]; switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - new(field_ptr) TYPE(field->default_value_##TYPE()); \ - break; +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + new (field_ptr) TYPE(field->default_value_##TYPE()); \ + break; - HANDLE_TYPE(INT32 , int32 ); - HANDLE_TYPE(INT64 , int64 ); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(FLOAT , float ); - HANDLE_TYPE(BOOL , bool ); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_ENUM: - new(field_ptr) int(field->default_value_enum()->number()); + new (field_ptr) int(field->default_value_enum()->number()); break; case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { @@ -840,7 +849,7 @@ void DynamicMessageFactory::ConstructDefaultOneofInstance( break; case FieldDescriptor::CPPTYPE_MESSAGE: { - new(field_ptr) Message*(NULL); + new (field_ptr) Message*(NULL); break; } } @@ -849,8 +858,7 @@ void DynamicMessageFactory::ConstructDefaultOneofInstance( } void DynamicMessageFactory::DeleteDefaultOneofInstance( - const Descriptor* type, - const uint32 offsets[], + const Descriptor* type, const uint32 offsets[], const void* default_oneof_instance) { for (int i = 0; i < type->oneof_decl_count(); i++) { for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { diff --git a/src/google/protobuf/dynamic_message.h b/src/google/protobuf/dynamic_message.h index 1ea6f66f18..765f9e3a63 100644 --- a/src/google/protobuf/dynamic_message.h +++ b/src/google/protobuf/dynamic_message.h @@ -42,10 +42,10 @@ #include <memory> #include <vector> +#include <google/protobuf/stubs/common.h> #include <google/protobuf/message.h> #include <google/protobuf/stubs/mutex.h> #include <google/protobuf/repeated_field.h> -#include <google/protobuf/stubs/common.h> #ifdef SWIG #error "You cannot SWIG proto headers" @@ -57,8 +57,8 @@ namespace google { namespace protobuf { // Defined in other files. -class Descriptor; // descriptor.h -class DescriptorPool; // descriptor.h +class Descriptor; // descriptor.h +class DescriptorPool; // descriptor.h // Constructs implementations of Message which can emulate types which are not // known at compile-time. @@ -155,8 +155,7 @@ class PROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory { // Helper for computing a sorted list of map entries via reflection. class PROTOBUF_EXPORT DynamicMapSorter { public: - static std::vector<const Message*> Sort(const Message& message, - int map_size, + static std::vector<const Message*> Sort(const Message& message, int map_size, const Reflection* reflection, const FieldDescriptor* field) { std::vector<const Message*> result(static_cast<size_t>(map_size)); @@ -164,7 +163,8 @@ class PROTOBUF_EXPORT DynamicMapSorter { reflection->GetRepeatedPtrField<Message>(message, field); size_t i = 0; for (RepeatedPtrField<Message>::const_pointer_iterator it = - map_field.pointer_begin(); it != map_field.pointer_end(); ) { + map_field.pointer_begin(); + it != map_field.pointer_end();) { result[i++] = *it++; } GOOGLE_DCHECK_EQ(result.size(), i); @@ -174,9 +174,9 @@ class PROTOBUF_EXPORT DynamicMapSorter { #ifndef NDEBUG for (size_t j = 1; j < static_cast<size_t>(map_size); j++) { if (!comparator(result[j - 1], result[j])) { - GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1]) ? - "internal error in map key sorting" : - "map keys are not unique"); + GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1]) + ? "internal error in map key sorting" + : "map keys are not unique"); } } #endif diff --git a/src/google/protobuf/dynamic_message_unittest.cc b/src/google/protobuf/dynamic_message_unittest.cc index e96f6589bf..42759de776 100644 --- a/src/google/protobuf/dynamic_message_unittest.cc +++ b/src/google/protobuf/dynamic_message_unittest.cc @@ -72,7 +72,7 @@ class DynamicMessageTest : public ::testing::TestWithParam<bool> { const Descriptor* proto3_descriptor_; const Message* proto3_prototype_; - DynamicMessageTest(): factory_(&pool_) {} + DynamicMessageTest() : factory_(&pool_) {} virtual void SetUp() { // We want to make sure that DynamicMessage works (particularly with @@ -86,11 +86,11 @@ class DynamicMessageTest : public ::testing::TestWithParam<bool> { unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file); unittest_import::ImportMessage::descriptor()->file()->CopyTo( - &unittest_import_file); + &unittest_import_file); unittest_import::PublicImportMessage::descriptor()->file()->CopyTo( - &unittest_import_public_file); - proto2_nofieldpresence_unittest::TestAllTypes::descriptor()-> - file()->CopyTo(&unittest_no_field_presence_file); + &unittest_import_public_file); + proto2_nofieldpresence_unittest::TestAllTypes::descriptor()->file()->CopyTo( + &unittest_no_field_presence_file); ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL); ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL); @@ -102,23 +102,22 @@ class DynamicMessageTest : public ::testing::TestWithParam<bool> { prototype_ = factory_.GetPrototype(descriptor_); extensions_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions"); + pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions"); ASSERT_TRUE(extensions_descriptor_ != NULL); extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_); packed_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes"); + pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes"); ASSERT_TRUE(packed_descriptor_ != NULL); packed_prototype_ = factory_.GetPrototype(packed_descriptor_); oneof_descriptor_ = - pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2"); + pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2"); ASSERT_TRUE(oneof_descriptor_ != NULL); oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_); - proto3_descriptor_ = - pool_.FindMessageTypeByName( - "proto2_nofieldpresence_unittest.TestAllTypes"); + proto3_descriptor_ = pool_.FindMessageTypeByName( + "proto2_nofieldpresence_unittest.TestAllTypes"); ASSERT_TRUE(proto3_descriptor_ != NULL); proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_); } @@ -146,7 +145,7 @@ TEST_P(DynamicMessageTest, IndependentOffsets) { // one to a unique value then checking that they all still have those // unique values (i.e. they don't stomp each other). Arena arena; - Message* message = prototype_->New(GetParam()? &arena : NULL); + Message* message = prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(descriptor_); reflection_tester.SetAllFieldsViaReflection(message); @@ -160,7 +159,7 @@ TEST_P(DynamicMessageTest, IndependentOffsets) { TEST_P(DynamicMessageTest, Extensions) { // Check that extensions work. Arena arena; - Message* message = extensions_prototype_->New(GetParam()? &arena : NULL); + Message* message = extensions_prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(extensions_descriptor_); reflection_tester.SetAllFieldsViaReflection(message); @@ -174,7 +173,7 @@ TEST_P(DynamicMessageTest, Extensions) { TEST_P(DynamicMessageTest, PackedFields) { // Check that packed fields work properly. Arena arena; - Message* message = packed_prototype_->New(GetParam()? &arena : NULL); + Message* message = packed_prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(packed_descriptor_); reflection_tester.SetPackedFieldsViaReflection(message); @@ -188,23 +187,25 @@ TEST_P(DynamicMessageTest, PackedFields) { TEST_P(DynamicMessageTest, Oneof) { // Check that oneof fields work properly. Arena arena; - Message* message = oneof_prototype_->New(GetParam()? &arena : NULL); + Message* message = oneof_prototype_->New(GetParam() ? &arena : NULL); // Check default values. const Descriptor* descriptor = message->GetDescriptor(); const Reflection* reflection = message->GetReflection(); - EXPECT_EQ(0, reflection->GetInt32( - *message, descriptor->FindFieldByName("foo_int"))); + EXPECT_EQ(0, reflection->GetInt32(*message, + descriptor->FindFieldByName("foo_int"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_string"))); + *message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ("", reflection->GetString(*message, + descriptor->FindFieldByName("foo_cord"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_cord"))); + *message, descriptor->FindFieldByName("foo_string_piece"))); EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_string_piece"))); - EXPECT_EQ("", reflection->GetString( - *message, descriptor->FindFieldByName("foo_bytes"))); - EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum( - *message, descriptor->FindFieldByName("foo_enum"))->number()); + *message, descriptor->FindFieldByName("foo_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::FOO, + reflection->GetEnum(*message, descriptor->FindFieldByName("foo_enum")) + ->number()); const Descriptor* nested_descriptor; const Message* nested_prototype; nested_descriptor = @@ -219,23 +220,26 @@ TEST_P(DynamicMessageTest, Oneof) { pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.FooGroup"); foogroup_prototype = factory_.GetPrototype(foogroup_descriptor); EXPECT_EQ(foogroup_prototype, - &reflection->GetMessage( - *message, descriptor->FindFieldByName("foogroup"))); + &reflection->GetMessage(*message, + descriptor->FindFieldByName("foogroup"))); EXPECT_NE(foogroup_prototype, &reflection->GetMessage( *message, descriptor->FindFieldByName("foo_lazy_message"))); - EXPECT_EQ(5, reflection->GetInt32( - *message, descriptor->FindFieldByName("bar_int"))); + EXPECT_EQ(5, reflection->GetInt32(*message, + descriptor->FindFieldByName("bar_int"))); EXPECT_EQ("STRING", reflection->GetString( - *message, descriptor->FindFieldByName("bar_string"))); + *message, descriptor->FindFieldByName("bar_string"))); EXPECT_EQ("CORD", reflection->GetString( - *message, descriptor->FindFieldByName("bar_cord"))); - EXPECT_EQ("SPIECE", reflection->GetString( - *message, descriptor->FindFieldByName("bar_string_piece"))); + *message, descriptor->FindFieldByName("bar_cord"))); + EXPECT_EQ("SPIECE", + reflection->GetString( + *message, descriptor->FindFieldByName("bar_string_piece"))); EXPECT_EQ("BYTES", reflection->GetString( - *message, descriptor->FindFieldByName("bar_bytes"))); - EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum( - *message, descriptor->FindFieldByName("bar_enum"))->number()); + *message, descriptor->FindFieldByName("bar_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::BAR, + reflection->GetEnum(*message, descriptor->FindFieldByName("bar_enum")) + ->number()); // Check set functions. TestUtil::ReflectionTester reflection_tester(oneof_descriptor_); @@ -254,7 +258,7 @@ TEST_P(DynamicMessageTest, SpaceUsed) { // to test very much here. Just make sure it appears to be working. Arena arena; - Message* message = prototype_->New(GetParam()? &arena : NULL); + Message* message = prototype_->New(GetParam() ? &arena : NULL); TestUtil::ReflectionTester reflection_tester(descriptor_); int initial_space_used = message->SpaceUsed(); diff --git a/src/google/protobuf/empty.pb.cc b/src/google/protobuf/empty.pb.cc index 57b8fe877e..c10bf94bc1 100644 --- a/src/google/protobuf/empty.pb.cc +++ b/src/google/protobuf/empty.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsEmpty_google_2fprotobuf_2fempty_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Empty_google_2fprotobuf_2fempty_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsEmpty_google_2fprotobuf_2fempty_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fempty_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fempty_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fempty_2eproto = nullptr; @@ -73,8 +69,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto[] = ".WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fempty_2eproto = { - false, InitDefaults_google_2fprotobuf_2fempty_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fempty_2eproto, "google/protobuf/empty.proto", &assign_descriptors_table_google_2fprotobuf_2fempty_2eproto, 183, }; @@ -82,6 +77,7 @@ void AddDescriptors_google_2fprotobuf_2fempty_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Empty_google_2fprotobuf_2fempty_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fempty_2eproto, deps, 0); } @@ -157,24 +153,30 @@ void Empty::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Empty::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { default: { if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Empty::MergePartialFromCodedStream( diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index d0a63b9307..67b0fcabd2 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -76,8 +76,8 @@ inline bool is_packable(WireFormatLite::WireType type) { case WireFormatLite::WIRETYPE_END_GROUP: return false; - // Do not add a default statement. Let the compiler complain when someone - // adds a new wire type. + // Do not add a default statement. Let the compiler complain when someone + // adds a new wire type. } GOOGLE_LOG(FATAL) << "can't reach here."; return false; @@ -99,20 +99,20 @@ static const ExtensionRegistry* global_registry = nullptr; // This function is only called at startup, so there is no need for thread- // safety. -void Register(const MessageLite* containing_type, - int number, ExtensionInfo info) { +void Register(const MessageLite* containing_type, int number, + ExtensionInfo info) { static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry); global_registry = local_static_registry; if (!InsertIfNotPresent(local_static_registry, std::make_pair(containing_type, number), info)) { GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \"" - << containing_type->GetTypeName() - << "\", field number " << number << "."; + << containing_type->GetTypeName() << "\", field number " + << number << "."; } } -const ExtensionInfo* FindRegisteredExtension( - const MessageLite* containing_type, int number) { +const ExtensionInfo* FindRegisteredExtension(const MessageLite* containing_type, + int number) { return global_registry == nullptr ? nullptr : FindOrNull(*global_registry, @@ -212,8 +212,8 @@ ExtensionSet::~ExtensionSet() { } } -void ExtensionSet::DeleteFlatMap( - const ExtensionSet::KeyValue* flat, uint16 flat_capacity) { +void ExtensionSet::DeleteFlatMap(const ExtensionSet::KeyValue* flat, + uint16 flat_capacity) { #ifdef __cpp_sized_deallocation // Arena::CreateArray already requires a trivially destructible type, but // ensure this constraint is not violated in the future. @@ -221,9 +221,9 @@ void ExtensionSet::DeleteFlatMap( "CreateArray requires a trivially destructible type"); // A const-cast is needed, but this is safe as we are about to deallocate the // array. - ::operator delete[]( - const_cast<ExtensionSet::KeyValue*>(flat), sizeof(*flat) * flat_capacity); -#else // !__cpp_sized_deallocation + ::operator delete[](const_cast<ExtensionSet::KeyValue*>(flat), + sizeof(*flat) * flat_capacity); +#else // !__cpp_sized_deallocation delete[] flat; #endif // !__cpp_sized_deallocation } @@ -278,10 +278,7 @@ void ExtensionSet::ClearExtension(int number) { namespace { -enum { - REPEATED_FIELD, - OPTIONAL_FIELD -}; +enum { REPEATED_FIELD, OPTIONAL_FIELD }; } // namespace @@ -292,74 +289,77 @@ enum { // ------------------------------------------------------------------- // Primitives -#define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \ - \ -LOWERCASE ExtensionSet::Get##CAMELCASE(int number, \ - LOWERCASE default_value) const { \ - const Extension* extension = FindOrNull(number); \ - if (extension == NULL || extension->is_cleared) { \ - return default_value; \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ - return extension->LOWERCASE##_value; \ - } \ -} \ - \ -void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \ - LOWERCASE value, \ - const FieldDescriptor* descriptor) { \ - Extension* extension; \ - if (MaybeNewExtension(number, descriptor, &extension)) { \ - extension->type = type; \ - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \ - extension->is_repeated = false; \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ - } \ - extension->is_cleared = false; \ - extension->LOWERCASE##_value = value; \ -} \ - \ -LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) const { \ - const Extension* extension = FindOrNull(number); \ - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - return extension->repeated_##LOWERCASE##_value->Get(index); \ -} \ - \ -void ExtensionSet::SetRepeated##CAMELCASE( \ - int number, int index, LOWERCASE value) { \ - Extension* extension = FindOrNull(number); \ - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - extension->repeated_##LOWERCASE##_value->Set(index, value); \ -} \ - \ -void ExtensionSet::Add##CAMELCASE(int number, FieldType type, \ - bool packed, LOWERCASE value, \ - const FieldDescriptor* descriptor) { \ - Extension* extension; \ - if (MaybeNewExtension(number, descriptor, &extension)) { \ - extension->type = type; \ - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \ - extension->is_repeated = true; \ - extension->is_packed = packed; \ - extension->repeated_##LOWERCASE##_value = \ - Arena::CreateMessage<RepeatedField<LOWERCASE> >(arena_); \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - GOOGLE_DCHECK_EQ(extension->is_packed, packed); \ - } \ - extension->repeated_##LOWERCASE##_value->Add(value); \ -} - -PRIMITIVE_ACCESSORS( INT32, int32, Int32) -PRIMITIVE_ACCESSORS( INT64, int64, Int64) +#define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \ + \ + LOWERCASE ExtensionSet::Get##CAMELCASE(int number, LOWERCASE default_value) \ + const { \ + const Extension* extension = FindOrNull(number); \ + if (extension == NULL || extension->is_cleared) { \ + return default_value; \ + } else { \ + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ + return extension->LOWERCASE##_value; \ + } \ + } \ + \ + void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \ + LOWERCASE value, \ + const FieldDescriptor* descriptor) { \ + Extension* extension; \ + if (MaybeNewExtension(number, descriptor, &extension)) { \ + extension->type = type; \ + GOOGLE_DCHECK_EQ(cpp_type(extension->type), \ + WireFormatLite::CPPTYPE_##UPPERCASE); \ + extension->is_repeated = false; \ + } else { \ + GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ + } \ + extension->is_cleared = false; \ + extension->LOWERCASE##_value = value; \ + } \ + \ + LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) \ + const { \ + const Extension* extension = FindOrNull(number); \ + GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ + return extension->repeated_##LOWERCASE##_value->Get(index); \ + } \ + \ + void ExtensionSet::SetRepeated##CAMELCASE(int number, int index, \ + LOWERCASE value) { \ + Extension* extension = FindOrNull(number); \ + GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ + extension->repeated_##LOWERCASE##_value->Set(index, value); \ + } \ + \ + void ExtensionSet::Add##CAMELCASE(int number, FieldType type, bool packed, \ + LOWERCASE value, \ + const FieldDescriptor* descriptor) { \ + Extension* extension; \ + if (MaybeNewExtension(number, descriptor, &extension)) { \ + extension->type = type; \ + GOOGLE_DCHECK_EQ(cpp_type(extension->type), \ + WireFormatLite::CPPTYPE_##UPPERCASE); \ + extension->is_repeated = true; \ + extension->is_packed = packed; \ + extension->repeated_##LOWERCASE##_value = \ + Arena::CreateMessage<RepeatedField<LOWERCASE>>(arena_); \ + } else { \ + GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ + GOOGLE_DCHECK_EQ(extension->is_packed, packed); \ + } \ + extension->repeated_##LOWERCASE##_value->Add(value); \ + } + +PRIMITIVE_ACCESSORS(INT32, int32, Int32) +PRIMITIVE_ACCESSORS(INT64, int64, Int64) PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32) PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64) -PRIMITIVE_ACCESSORS( FLOAT, float, Float) +PRIMITIVE_ACCESSORS(FLOAT, float, Float) PRIMITIVE_ACCESSORS(DOUBLE, double, Double) -PRIMITIVE_ACCESSORS( BOOL, bool, Bool) +PRIMITIVE_ACCESSORS(BOOL, bool, Bool) #undef PRIMITIVE_ACCESSORS @@ -390,35 +390,35 @@ void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type, static_cast<WireFormatLite::FieldType>(field_type))) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value = - Arena::CreateMessage<RepeatedField<int32> >(arena_); + Arena::CreateMessage<RepeatedField<int32>>(arena_); break; case WireFormatLite::CPPTYPE_INT64: extension->repeated_int64_value = - Arena::CreateMessage<RepeatedField<int64> >(arena_); + Arena::CreateMessage<RepeatedField<int64>>(arena_); break; case WireFormatLite::CPPTYPE_UINT32: extension->repeated_uint32_value = - Arena::CreateMessage<RepeatedField<uint32> >(arena_); + Arena::CreateMessage<RepeatedField<uint32>>(arena_); break; case WireFormatLite::CPPTYPE_UINT64: extension->repeated_uint64_value = - Arena::CreateMessage<RepeatedField<uint64> >(arena_); + Arena::CreateMessage<RepeatedField<uint64>>(arena_); break; case WireFormatLite::CPPTYPE_DOUBLE: extension->repeated_double_value = - Arena::CreateMessage<RepeatedField<double> >(arena_); + Arena::CreateMessage<RepeatedField<double>>(arena_); break; case WireFormatLite::CPPTYPE_FLOAT: extension->repeated_float_value = - Arena::CreateMessage<RepeatedField<float> >(arena_); + Arena::CreateMessage<RepeatedField<float>>(arena_); break; case WireFormatLite::CPPTYPE_BOOL: extension->repeated_bool_value = - Arena::CreateMessage<RepeatedField<bool> >(arena_); + Arena::CreateMessage<RepeatedField<bool>>(arena_); break; case WireFormatLite::CPPTYPE_ENUM: extension->repeated_enum_value = - Arena::CreateMessage<RepeatedField<int> >(arena_); + Arena::CreateMessage<RepeatedField<int>>(arena_); break; case WireFormatLite::CPPTYPE_STRING: extension->repeated_string_value = @@ -426,7 +426,7 @@ void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type, break; case WireFormatLite::CPPTYPE_MESSAGE: extension->repeated_message_value = - Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_); + Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_); break; } } @@ -446,7 +446,6 @@ void* ExtensionSet::MutableRawRepeatedField(int number) { return extension->repeated_int32_value; } - // ------------------------------------------------------------------- // Enums @@ -489,8 +488,7 @@ void ExtensionSet::SetRepeatedEnum(int number, int index, int value) { extension->repeated_enum_value->Set(index, value); } -void ExtensionSet::AddEnum(int number, FieldType type, - bool packed, int value, +void ExtensionSet::AddEnum(int number, FieldType type, bool packed, int value, const FieldDescriptor* descriptor) { Extension* extension; if (MaybeNewExtension(number, descriptor, &extension)) { @@ -499,7 +497,7 @@ void ExtensionSet::AddEnum(int number, FieldType type, extension->is_repeated = true; extension->is_packed = packed; extension->repeated_enum_value = - Arena::CreateMessage<RepeatedField<int> >(arena_); + Arena::CreateMessage<RepeatedField<int>>(arena_); } else { GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM); GOOGLE_DCHECK_EQ(extension->is_packed, packed); @@ -748,8 +746,8 @@ MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( // MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor, // MessageFactory* factory); -const MessageLite& ExtensionSet::GetRepeatedMessage( - int number, int index) const { +const MessageLite& ExtensionSet::GetRepeatedMessage(int number, + int index) const { const Extension* extension = FindOrNull(number); GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); @@ -772,7 +770,7 @@ MessageLite* ExtensionSet::AddMessage(int number, FieldType type, GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); extension->is_repeated = true; extension->repeated_message_value = - Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_); + Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_); } else { GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); } @@ -801,7 +799,7 @@ void ExtensionSet::RemoveLast(int number) { GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; GOOGLE_DCHECK(extension->is_repeated); - switch(cpp_type(extension->type)) { + switch (cpp_type(extension->type)) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value->RemoveLast(); break; @@ -848,7 +846,7 @@ void ExtensionSet::SwapElements(int number, int index1, int index2) { GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; GOOGLE_DCHECK(extension->is_repeated); - switch(cpp_type(extension->type)) { + switch (cpp_type(extension->type)) { case WireFormatLite::CPPTYPE_INT32: extension->repeated_int32_value->SwapElements(index1, index2); break; @@ -927,11 +925,11 @@ void ExtensionSet::MergeFrom(const ExtensionSet& other) { } void ExtensionSet::InternalExtensionMergeFrom( - int number, const Extension& other_extension) { + int number, const Extension& other_extension) { if (other_extension.is_repeated) { Extension* extension; - bool is_new = MaybeNewExtension(number, other_extension.descriptor, - &extension); + bool is_new = + MaybeNewExtension(number, other_extension.descriptor, &extension); if (is_new) { // Extension did not already exist in set. extension->type = other_extension.type; @@ -944,31 +942,31 @@ void ExtensionSet::InternalExtensionMergeFrom( } switch (cpp_type(other_extension.type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - if (is_new) { \ - extension->repeated_##LOWERCASE##_value = \ - Arena::CreateMessage<REPEATED_TYPE >(arena_); \ - } \ - extension->repeated_##LOWERCASE##_value->MergeFrom( \ - *other_extension.repeated_##LOWERCASE##_value); \ - break; - - HANDLE_TYPE( INT32, int32, RepeatedField < int32>); - HANDLE_TYPE( INT64, int64, RepeatedField < int64>); - HANDLE_TYPE( UINT32, uint32, RepeatedField < uint32>); - HANDLE_TYPE( UINT64, uint64, RepeatedField < uint64>); - HANDLE_TYPE( FLOAT, float, RepeatedField < float>); - HANDLE_TYPE( DOUBLE, double, RepeatedField < double>); - HANDLE_TYPE( BOOL, bool, RepeatedField < bool>); - HANDLE_TYPE( ENUM, enum, RepeatedField < int>); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \ + case WireFormatLite::CPPTYPE_##UPPERCASE: \ + if (is_new) { \ + extension->repeated_##LOWERCASE##_value = \ + Arena::CreateMessage<REPEATED_TYPE>(arena_); \ + } \ + extension->repeated_##LOWERCASE##_value->MergeFrom( \ + *other_extension.repeated_##LOWERCASE##_value); \ + break; + + HANDLE_TYPE(INT32, int32, RepeatedField<int32>); + HANDLE_TYPE(INT64, int64, RepeatedField<int64>); + HANDLE_TYPE(UINT32, uint32, RepeatedField<uint32>); + HANDLE_TYPE(UINT64, uint64, RepeatedField<uint64>); + HANDLE_TYPE(FLOAT, float, RepeatedField<float>); + HANDLE_TYPE(DOUBLE, double, RepeatedField<double>); + HANDLE_TYPE(BOOL, bool, RepeatedField<bool>); + HANDLE_TYPE(ENUM, enum, RepeatedField<int>); HANDLE_TYPE(STRING, string, RepeatedPtrField<std::string>); #undef HANDLE_TYPE case WireFormatLite::CPPTYPE_MESSAGE: if (is_new) { extension->repeated_message_value = - Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_); + Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_); } // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because // it would attempt to allocate new objects. @@ -991,32 +989,30 @@ void ExtensionSet::InternalExtensionMergeFrom( } else { if (!other_extension.is_cleared) { switch (cpp_type(other_extension.type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - Set##CAMELCASE(number, other_extension.type, \ - other_extension.LOWERCASE##_value, \ - other_extension.descriptor); \ - break; - - HANDLE_TYPE( INT32, int32, Int32); - HANDLE_TYPE( INT64, int64, Int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \ + case WireFormatLite::CPPTYPE_##UPPERCASE: \ + Set##CAMELCASE(number, other_extension.type, \ + other_extension.LOWERCASE##_value, \ + other_extension.descriptor); \ + break; + + HANDLE_TYPE(INT32, int32, Int32); + HANDLE_TYPE(INT64, int64, Int64); HANDLE_TYPE(UINT32, uint32, UInt32); HANDLE_TYPE(UINT64, uint64, UInt64); - HANDLE_TYPE( FLOAT, float, Float); + HANDLE_TYPE(FLOAT, float, Float); HANDLE_TYPE(DOUBLE, double, Double); - HANDLE_TYPE( BOOL, bool, Bool); - HANDLE_TYPE( ENUM, enum, Enum); + HANDLE_TYPE(BOOL, bool, Bool); + HANDLE_TYPE(ENUM, enum, Enum); #undef HANDLE_TYPE case WireFormatLite::CPPTYPE_STRING: - SetString(number, other_extension.type, - *other_extension.string_value, + SetString(number, other_extension.type, *other_extension.string_value, other_extension.descriptor); break; case WireFormatLite::CPPTYPE_MESSAGE: { Extension* extension; - bool is_new = MaybeNewExtension(number, - other_extension.descriptor, - &extension); + bool is_new = + MaybeNewExtension(number, other_extension.descriptor, &extension); if (is_new) { extension->type = other_extension.type; extension->is_packed = other_extension.is_packed; @@ -1036,7 +1032,7 @@ void ExtensionSet::InternalExtensionMergeFrom( } } else { GOOGLE_DCHECK_EQ(extension->type, other_extension.type); - GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed); + GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed); GOOGLE_DCHECK(!extension->is_repeated); if (other_extension.is_lazy) { if (extension->is_lazy) { @@ -1049,9 +1045,9 @@ void ExtensionSet::InternalExtensionMergeFrom( } } else { if (extension->is_lazy) { - extension->lazymessage_value->MutableMessage( - *other_extension.message_value)->CheckTypeAndMergeFrom( - *other_extension.message_value); + extension->lazymessage_value + ->MutableMessage(*other_extension.message_value) + ->CheckTypeAndMergeFrom(*other_extension.message_value); } else { extension->message_value->CheckTypeAndMergeFrom( *other_extension.message_value); @@ -1085,8 +1081,7 @@ void ExtensionSet::Swap(ExtensionSet* x) { } } -void ExtensionSet::SwapExtension(ExtensionSet* other, - int number) { +void ExtensionSet::SwapExtension(ExtensionSet* other, int number) { if (this == other) return; Extension* this_ext = FindOrNull(number); Extension* other_ext = other->FindOrNull(number); @@ -1152,9 +1147,11 @@ bool ExtensionSet::IsInitialized() const { return true; } -bool ExtensionSet::FindExtensionInfoFromTag( - uint32 tag, ExtensionFinder* extension_finder, int* field_number, - ExtensionInfo* extension, bool* was_packed_on_wire) { +bool ExtensionSet::FindExtensionInfoFromTag(uint32 tag, + ExtensionFinder* extension_finder, + int* field_number, + ExtensionInfo* extension, + bool* was_packed_on_wire) { *field_number = WireFormatLite::GetTagFieldNumber(tag); WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag); return FindExtensionInfoFromFieldNumber(wire_type, *field_number, @@ -1190,12 +1187,12 @@ bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, int number; bool was_packed_on_wire; ExtensionInfo extension; - if (!FindExtensionInfoFromTag( - tag, extension_finder, &number, &extension, &was_packed_on_wire)) { + if (!FindExtensionInfoFromTag(tag, extension_finder, &number, &extension, + &was_packed_on_wire)) { return field_skipper->SkipField(input, tag); } else { - return ParseFieldWithExtensionInfo( - number, was_packed_on_wire, extension, input, field_skipper); + return ParseFieldWithExtensionInfo(number, was_packed_on_wire, extension, + input, field_skipper); } } @@ -1225,10 +1222,11 @@ const char* ExtensionSet::ParseMessageSetItem( #endif -bool ExtensionSet::ParseFieldWithExtensionInfo( - int number, bool was_packed_on_wire, const ExtensionInfo& extension, - io::CodedInputStream* input, - FieldSkipper* field_skipper) { +bool ExtensionSet::ParseFieldWithExtensionInfo(int number, + bool was_packed_on_wire, + const ExtensionInfo& extension, + io::CodedInputStream* input, + FieldSkipper* field_skipper) { // Explicitly not read extension.is_packed, instead check whether the field // was encoded in packed form on the wire. if (was_packed_on_wire) { @@ -1237,39 +1235,40 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( io::CodedInputStream::Limit limit = input->PushLimit(size); switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - while (input->BytesUntilLimit() > 0) { \ - CPP_LOWERCASE value; \ - if (!WireFormatLite::ReadPrimitive< \ - CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \ - input, &value)) return false; \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, \ - extension.descriptor); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, Int32, int32); - HANDLE_TYPE( SINT64, Int64, int64); - HANDLE_TYPE( FIXED32, UInt32, uint32); - HANDLE_TYPE( FIXED64, UInt64, uint64); - HANDLE_TYPE(SFIXED32, Int32, int32); - HANDLE_TYPE(SFIXED64, Int64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); +#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + while (input->BytesUntilLimit() > 0) { \ + CPP_LOWERCASE value; \ + if (!WireFormatLite::ReadPrimitive<CPP_LOWERCASE, \ + WireFormatLite::TYPE_##UPPERCASE>( \ + input, &value)) \ + return false; \ + Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ + extension.is_packed, value, extension.descriptor); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, Int32, int32); + HANDLE_TYPE(SINT64, Int64, int64); + HANDLE_TYPE(FIXED32, UInt32, uint32); + HANDLE_TYPE(FIXED64, UInt64, uint64); + HANDLE_TYPE(SFIXED32, Int32, int32); + HANDLE_TYPE(SFIXED64, Int64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); #undef HANDLE_TYPE case WireFormatLite::TYPE_ENUM: while (input->BytesUntilLimit() > 0) { int value; if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( - input, &value)) return false; + input, &value)) + return false; if (extension.enum_validity_check.func( extension.enum_validity_check.arg, value)) { AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, @@ -1292,41 +1291,42 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( input->PopLimit(limit); } else { switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - CPP_LOWERCASE value; \ - if (!WireFormatLite::ReadPrimitive< \ - CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \ - input, &value)) return false; \ - if (extension.is_repeated) { \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, \ - extension.descriptor); \ - } else { \ - Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ - extension.descriptor); \ - } \ - } break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, Int32, int32); - HANDLE_TYPE( SINT64, Int64, int64); - HANDLE_TYPE( FIXED32, UInt32, uint32); - HANDLE_TYPE( FIXED64, UInt64, uint64); - HANDLE_TYPE(SFIXED32, Int32, int32); - HANDLE_TYPE(SFIXED64, Int64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); +#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: { \ + CPP_LOWERCASE value; \ + if (!WireFormatLite::ReadPrimitive<CPP_LOWERCASE, \ + WireFormatLite::TYPE_##UPPERCASE>( \ + input, &value)) \ + return false; \ + if (extension.is_repeated) { \ + Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ + extension.is_packed, value, extension.descriptor); \ + } else { \ + Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ + extension.descriptor); \ + } \ + } break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, Int32, int32); + HANDLE_TYPE(SINT64, Int64, int64); + HANDLE_TYPE(FIXED32, UInt32, uint32); + HANDLE_TYPE(FIXED64, UInt64, uint64); + HANDLE_TYPE(SFIXED32, Int32, int32); + HANDLE_TYPE(SFIXED64, Int64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); #undef HANDLE_TYPE case WireFormatLite::TYPE_ENUM: { int value; if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( - input, &value)) return false; + input, &value)) + return false; if (!extension.enum_validity_check.func( extension.enum_validity_check.arg, value)) { @@ -1342,7 +1342,7 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( break; } - case WireFormatLite::TYPE_STRING: { + case WireFormatLite::TYPE_STRING: { std::string* value = extension.is_repeated ? AddString(number, WireFormatLite::TYPE_STRING, @@ -1353,7 +1353,7 @@ bool ExtensionSet::ParseFieldWithExtensionInfo( break; } - case WireFormatLite::TYPE_BYTES: { + case WireFormatLite::TYPE_BYTES: { std::string* value = extension.is_repeated ? AddString(number, WireFormatLite::TYPE_BYTES, @@ -1510,20 +1510,20 @@ bool ExtensionSet::MaybeNewExtension(int number, void ExtensionSet::Extension::Clear() { if (is_repeated) { switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - repeated_##LOWERCASE##_value->Clear(); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); - HANDLE_TYPE( UINT32, uint32); - HANDLE_TYPE( UINT64, uint64); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( DOUBLE, double); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, enum); - HANDLE_TYPE( STRING, string); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case WireFormatLite::CPPTYPE_##UPPERCASE: \ + repeated_##LOWERCASE##_value->Clear(); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, enum); + HANDLE_TYPE(STRING, string); HANDLE_TYPE(MESSAGE, message); #undef HANDLE_TYPE } @@ -1553,39 +1553,38 @@ void ExtensionSet::Extension::Clear() { } void ExtensionSet::Extension::SerializeFieldWithCachedSizes( - int number, - io::CodedOutputStream* output) const { + int number, io::CodedOutputStream* output) const { if (is_repeated) { if (is_packed) { if (cached_size == 0) return; - WireFormatLite::WriteTag(number, - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); + WireFormatLite::WriteTag( + number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); output->WriteVarint32(cached_size); switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - WireFormatLite::Write##CAMELCASE##NoTag( \ - repeated_##LOWERCASE##_value->Get(i), output); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); - HANDLE_TYPE( ENUM, Enum, enum); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + WireFormatLite::Write##CAMELCASE##NoTag( \ + repeated_##LOWERCASE##_value->Get(i), output); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); + HANDLE_TYPE(ENUM, Enum, enum); #undef HANDLE_TYPE case WireFormatLite::TYPE_STRING: @@ -1597,59 +1596,59 @@ void ExtensionSet::Extension::SerializeFieldWithCachedSizes( } } else { switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - WireFormatLite::Write##CAMELCASE(number, \ - repeated_##LOWERCASE##_value->Get(i), output); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); - HANDLE_TYPE( STRING, String, string); - HANDLE_TYPE( BYTES, Bytes, string); - HANDLE_TYPE( ENUM, Enum, enum); - HANDLE_TYPE( GROUP, Group, message); - HANDLE_TYPE( MESSAGE, Message, message); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + WireFormatLite::Write##CAMELCASE( \ + number, repeated_##LOWERCASE##_value->Get(i), output); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); + HANDLE_TYPE(STRING, String, string); + HANDLE_TYPE(BYTES, Bytes, string); + HANDLE_TYPE(ENUM, Enum, enum); + HANDLE_TYPE(GROUP, Group, message); + HANDLE_TYPE(MESSAGE, Message, message); #undef HANDLE_TYPE } } } else if (!is_cleared) { switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - WireFormatLite::Write##CAMELCASE(number, VALUE, output); \ - break - - HANDLE_TYPE( INT32, Int32, int32_value); - HANDLE_TYPE( INT64, Int64, int64_value); - HANDLE_TYPE( UINT32, UInt32, uint32_value); - HANDLE_TYPE( UINT64, UInt64, uint64_value); - HANDLE_TYPE( SINT32, SInt32, int32_value); - HANDLE_TYPE( SINT64, SInt64, int64_value); - HANDLE_TYPE( FIXED32, Fixed32, uint32_value); - HANDLE_TYPE( FIXED64, Fixed64, uint64_value); - HANDLE_TYPE(SFIXED32, SFixed32, int32_value); - HANDLE_TYPE(SFIXED64, SFixed64, int64_value); - HANDLE_TYPE( FLOAT, Float, float_value); - HANDLE_TYPE( DOUBLE, Double, double_value); - HANDLE_TYPE( BOOL, Bool, bool_value); - HANDLE_TYPE( STRING, String, *string_value); - HANDLE_TYPE( BYTES, Bytes, *string_value); - HANDLE_TYPE( ENUM, Enum, enum_value); - HANDLE_TYPE( GROUP, Group, *message_value); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + WireFormatLite::Write##CAMELCASE(number, VALUE, output); \ + break + + HANDLE_TYPE(INT32, Int32, int32_value); + HANDLE_TYPE(INT64, Int64, int64_value); + HANDLE_TYPE(UINT32, UInt32, uint32_value); + HANDLE_TYPE(UINT64, UInt64, uint64_value); + HANDLE_TYPE(SINT32, SInt32, int32_value); + HANDLE_TYPE(SINT64, SInt64, int64_value); + HANDLE_TYPE(FIXED32, Fixed32, uint32_value); + HANDLE_TYPE(FIXED64, Fixed64, uint64_value); + HANDLE_TYPE(SFIXED32, SFixed32, int32_value); + HANDLE_TYPE(SFIXED64, SFixed64, int64_value); + HANDLE_TYPE(FLOAT, Float, float_value); + HANDLE_TYPE(DOUBLE, Double, double_value); + HANDLE_TYPE(BOOL, Bool, bool_value); + HANDLE_TYPE(STRING, String, *string_value); + HANDLE_TYPE(BYTES, Bytes, *string_value); + HANDLE_TYPE(ENUM, Enum, enum_value); + HANDLE_TYPE(GROUP, Group, *message_value); #undef HANDLE_TYPE case WireFormatLite::TYPE_MESSAGE: if (is_lazy) { @@ -1668,36 +1667,36 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { if (is_repeated) { if (is_packed) { switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - result += WireFormatLite::CAMELCASE##Size( \ - repeated_##LOWERCASE##_value->Get(i)); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( ENUM, Enum, enum); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + result += WireFormatLite::CAMELCASE##Size( \ + repeated_##LOWERCASE##_value->Get(i)); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(ENUM, Enum, enum); #undef HANDLE_TYPE // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::k##CAMELCASE##Size * \ - FromIntSize(repeated_##LOWERCASE##_value->size()); \ - break - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + result += WireFormatLite::k##CAMELCASE##Size * \ + FromIntSize(repeated_##LOWERCASE##_value->size()); \ + break + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); #undef HANDLE_TYPE case WireFormatLite::TYPE_STRING: @@ -1711,71 +1710,69 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { cached_size = ToCachedSize(result); if (result > 0) { result += io::CodedOutputStream::VarintSize32(result); - result += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(number, - WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); + result += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); } } else { size_t tag_size = WireFormatLite::TagSize(number, real_type(type)); switch (real_type(type)) { #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += tag_size * \ - FromIntSize(repeated_##LOWERCASE##_value->size()); \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - result += WireFormatLite::CAMELCASE##Size( \ - repeated_##LOWERCASE##_value->Get(i)); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( STRING, String, string); - HANDLE_TYPE( BYTES, Bytes, string); - HANDLE_TYPE( ENUM, Enum, enum); - HANDLE_TYPE( GROUP, Group, message); - HANDLE_TYPE( MESSAGE, Message, message); + case WireFormatLite::TYPE_##UPPERCASE: \ + result += tag_size * FromIntSize(repeated_##LOWERCASE##_value->size()); \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + result += WireFormatLite::CAMELCASE##Size( \ + repeated_##LOWERCASE##_value->Get(i)); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(STRING, String, string); + HANDLE_TYPE(BYTES, Bytes, string); + HANDLE_TYPE(ENUM, Enum, enum); + HANDLE_TYPE(GROUP, Group, message); + HANDLE_TYPE(MESSAGE, Message, message); #undef HANDLE_TYPE // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \ - FromIntSize(repeated_##LOWERCASE##_value->size()); \ - break - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \ + FromIntSize(repeated_##LOWERCASE##_value->size()); \ + break + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); #undef HANDLE_TYPE } } } else if (!is_cleared) { result += WireFormatLite::TagSize(number, real_type(type)); switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \ - break - - HANDLE_TYPE( INT32, Int32, int32_value); - HANDLE_TYPE( INT64, Int64, int64_value); - HANDLE_TYPE( UINT32, UInt32, uint32_value); - HANDLE_TYPE( UINT64, UInt64, uint64_value); - HANDLE_TYPE( SINT32, SInt32, int32_value); - HANDLE_TYPE( SINT64, SInt64, int64_value); - HANDLE_TYPE( STRING, String, *string_value); - HANDLE_TYPE( BYTES, Bytes, *string_value); - HANDLE_TYPE( ENUM, Enum, enum_value); - HANDLE_TYPE( GROUP, Group, *message_value); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \ + break + + HANDLE_TYPE(INT32, Int32, int32_value); + HANDLE_TYPE(INT64, Int64, int64_value); + HANDLE_TYPE(UINT32, UInt32, uint32_value); + HANDLE_TYPE(UINT64, UInt64, uint64_value); + HANDLE_TYPE(SINT32, SInt32, int32_value); + HANDLE_TYPE(SINT64, SInt64, int64_value); + HANDLE_TYPE(STRING, String, *string_value); + HANDLE_TYPE(BYTES, Bytes, *string_value); + HANDLE_TYPE(ENUM, Enum, enum_value); + HANDLE_TYPE(GROUP, Group, *message_value); #undef HANDLE_TYPE case WireFormatLite::TYPE_MESSAGE: { if (is_lazy) { @@ -1788,17 +1785,17 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { } // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::k##CAMELCASE##Size; \ - break - HANDLE_TYPE( FIXED32, Fixed32); - HANDLE_TYPE( FIXED64, Fixed64); - HANDLE_TYPE(SFIXED32, SFixed32); - HANDLE_TYPE(SFIXED64, SFixed64); - HANDLE_TYPE( FLOAT, Float); - HANDLE_TYPE( DOUBLE, Double); - HANDLE_TYPE( BOOL, Bool); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE) \ + case WireFormatLite::TYPE_##UPPERCASE: \ + result += WireFormatLite::k##CAMELCASE##Size; \ + break + HANDLE_TYPE(FIXED32, Fixed32); + HANDLE_TYPE(FIXED64, Fixed64); + HANDLE_TYPE(SFIXED32, SFixed32); + HANDLE_TYPE(SFIXED64, SFixed64); + HANDLE_TYPE(FLOAT, Float); + HANDLE_TYPE(DOUBLE, Double); + HANDLE_TYPE(BOOL, Bool); #undef HANDLE_TYPE } } @@ -1809,19 +1806,19 @@ size_t ExtensionSet::Extension::ByteSize(int number) const { int ExtensionSet::Extension::GetSize() const { GOOGLE_DCHECK(is_repeated); switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - return repeated_##LOWERCASE##_value->size() - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); - HANDLE_TYPE( UINT32, uint32); - HANDLE_TYPE( UINT64, uint64); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( DOUBLE, double); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, enum); - HANDLE_TYPE( STRING, string); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case WireFormatLite::CPPTYPE_##UPPERCASE: \ + return repeated_##LOWERCASE##_value->size() + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, enum); + HANDLE_TYPE(STRING, string); HANDLE_TYPE(MESSAGE, message); #undef HANDLE_TYPE } @@ -1835,20 +1832,20 @@ int ExtensionSet::Extension::GetSize() const { void ExtensionSet::Extension::Free() { if (is_repeated) { switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - delete repeated_##LOWERCASE##_value; \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); - HANDLE_TYPE( UINT32, uint32); - HANDLE_TYPE( UINT64, uint64); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( DOUBLE, double); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, enum); - HANDLE_TYPE( STRING, string); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case WireFormatLite::CPPTYPE_##UPPERCASE: \ + delete repeated_##LOWERCASE##_value; \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, enum); + HANDLE_TYPE(STRING, string); HANDLE_TYPE(MESSAGE, message); #undef HANDLE_TYPE } @@ -2029,8 +2026,7 @@ RepeatedStringTypeTraits::GetDefaultRepeatedField() { } void ExtensionSet::Extension::SerializeMessageSetItemWithCachedSizes( - int number, - io::CodedOutputStream* output) const { + int number, io::CodedOutputStream* output) const { if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) { // Not a valid MessageSet extension, but serialize it the normal way. SerializeFieldWithCachedSizes(number, output); @@ -2043,18 +2039,15 @@ void ExtensionSet::Extension::SerializeMessageSetItemWithCachedSizes( output->WriteTag(WireFormatLite::kMessageSetItemStartTag); // Write type ID. - WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, - number, + WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, number, output); // Write message. if (is_lazy) { - lazymessage_value->WriteMessage( - WireFormatLite::kMessageSetMessageNumber, output); + lazymessage_value->WriteMessage(WireFormatLite::kMessageSetMessageNumber, + output); } else { WireFormatLite::WriteMessageMaybeToArray( - WireFormatLite::kMessageSetMessageNumber, - *message_value, - output); + WireFormatLite::kMessageSetMessageNumber, *message_value, output); } // End group. diff --git a/src/google/protobuf/extension_set_heavy.cc b/src/google/protobuf/extension_set_heavy.cc index f48207be7c..32ca435684 100644 --- a/src/google/protobuf/extension_set_heavy.cc +++ b/src/google/protobuf/extension_set_heavy.cc @@ -58,8 +58,7 @@ namespace protobuf { namespace internal { // A FieldSkipper used to store unknown MessageSet fields into UnknownFieldSet. -class MessageSetFieldSkipper - : public UnknownFieldSetFieldSkipper { +class MessageSetFieldSkipper : public UnknownFieldSetFieldSkipper { public: explicit MessageSetFieldSkipper(UnknownFieldSet* unknown_fields) : UnknownFieldSetFieldSkipper(unknown_fields) {} @@ -68,15 +67,15 @@ class MessageSetFieldSkipper virtual bool SkipMessageSetField(io::CodedInputStream* input, int field_number); }; -bool MessageSetFieldSkipper::SkipMessageSetField( - io::CodedInputStream* input, int field_number) { +bool MessageSetFieldSkipper::SkipMessageSetField(io::CodedInputStream* input, + int field_number) { uint32 length; if (!input->ReadVarint32(&length)) return false; if (unknown_fields_ == NULL) { return input->Skip(length); } else { - return input->ReadString( - unknown_fields_->AddLengthDelimited(field_number), length); + return input->ReadString(unknown_fields_->AddLengthDelimited(field_number), + length); } } @@ -141,10 +140,10 @@ inline WireFormatLite::FieldType field_type(FieldType type) { return static_cast<WireFormatLite::FieldType>(type); } -#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ - GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? FieldDescriptor::LABEL_REPEATED \ - : FieldDescriptor::LABEL_OPTIONAL, \ - FieldDescriptor::LABEL_##LABEL); \ +#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ + GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? FieldDescriptor::LABEL_REPEATED \ + : FieldDescriptor::LABEL_OPTIONAL, \ + FieldDescriptor::LABEL_##LABEL); \ GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), FieldDescriptor::CPPTYPE_##CPPTYPE) const MessageLite& ExtensionSet::GetMessage(int number, @@ -242,7 +241,8 @@ MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( } } -ExtensionSet::Extension* ExtensionSet::MaybeNewRepeatedExtension(const FieldDescriptor* descriptor) { +ExtensionSet::Extension* ExtensionSet::MaybeNewRepeatedExtension( + const FieldDescriptor* descriptor) { Extension* extension; if (MaybeNewExtension(descriptor->number(), descriptor, &extension)) { extension->type = descriptor->type(); @@ -288,8 +288,8 @@ void ExtensionSet::AddAllocatedMessage(const FieldDescriptor* descriptor, } static bool ValidateEnumUsingDescriptor(const void* arg, int number) { - return reinterpret_cast<const EnumDescriptor*>(arg) - ->FindValueByNumber(number) != NULL; + return reinterpret_cast<const EnumDescriptor*>(arg)->FindValueByNumber( + number) != NULL; } bool DescriptorPoolExtensionFinder::Find(int number, ExtensionInfo* output) { @@ -429,15 +429,15 @@ size_t ExtensionSet::Extension::SpaceUsedExcludingSelfLong() const { repeated_##LOWERCASE##_value->SpaceUsedExcludingSelfLong(); \ break - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); - HANDLE_TYPE( UINT32, uint32); - HANDLE_TYPE( UINT64, uint64); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( DOUBLE, double); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, enum); - HANDLE_TYPE( STRING, string); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, enum); + HANDLE_TYPE(STRING, string); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: @@ -522,33 +522,33 @@ uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray( if (is_packed) { if (cached_size == 0) return target; - target = WireFormatLite::WriteTagToArray(number, - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, target); + target = WireFormatLite::WriteTagToArray( + number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, target); target = WireFormatLite::WriteInt32NoTagToArray(cached_size, target); switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case FieldDescriptor::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = WireFormatLite::Write##CAMELCASE##NoTagToArray( \ - repeated_##LOWERCASE##_value->Get(i), target); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); - HANDLE_TYPE( ENUM, Enum, enum); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case FieldDescriptor::TYPE_##UPPERCASE: \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + target = WireFormatLite::Write##CAMELCASE##NoTagToArray( \ + repeated_##LOWERCASE##_value->Get(i), target); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); + HANDLE_TYPE(ENUM, Enum, enum); #undef HANDLE_TYPE case FieldDescriptor::TYPE_STRING: @@ -560,30 +560,30 @@ uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray( } } else { switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case FieldDescriptor::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = WireFormatLite::Write##CAMELCASE##ToArray(number, \ - repeated_##LOWERCASE##_value->Get(i), target); \ - } \ - break - - HANDLE_TYPE( INT32, Int32, int32); - HANDLE_TYPE( INT64, Int64, int64); - HANDLE_TYPE( UINT32, UInt32, uint32); - HANDLE_TYPE( UINT64, UInt64, uint64); - HANDLE_TYPE( SINT32, SInt32, int32); - HANDLE_TYPE( SINT64, SInt64, int64); - HANDLE_TYPE( FIXED32, Fixed32, uint32); - HANDLE_TYPE( FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE( FLOAT, Float, float); - HANDLE_TYPE( DOUBLE, Double, double); - HANDLE_TYPE( BOOL, Bool, bool); - HANDLE_TYPE( STRING, String, string); - HANDLE_TYPE( BYTES, Bytes, string); - HANDLE_TYPE( ENUM, Enum, enum); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ + case FieldDescriptor::TYPE_##UPPERCASE: \ + for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ + target = WireFormatLite::Write##CAMELCASE##ToArray( \ + number, repeated_##LOWERCASE##_value->Get(i), target); \ + } \ + break + + HANDLE_TYPE(INT32, Int32, int32); + HANDLE_TYPE(INT64, Int64, int64); + HANDLE_TYPE(UINT32, UInt32, uint32); + HANDLE_TYPE(UINT64, UInt64, uint64); + HANDLE_TYPE(SINT32, SInt32, int32); + HANDLE_TYPE(SINT64, SInt64, int64); + HANDLE_TYPE(FIXED32, Fixed32, uint32); + HANDLE_TYPE(FIXED64, Fixed64, uint64); + HANDLE_TYPE(SFIXED32, SFixed32, int32); + HANDLE_TYPE(SFIXED64, SFixed64, int64); + HANDLE_TYPE(FLOAT, Float, float); + HANDLE_TYPE(DOUBLE, Double, double); + HANDLE_TYPE(BOOL, Bool, bool); + HANDLE_TYPE(STRING, String, string); + HANDLE_TYPE(BYTES, Bytes, string); + HANDLE_TYPE(ENUM, Enum, enum); #undef HANDLE_TYPE #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ case FieldDescriptor::TYPE_##UPPERCASE: \ @@ -593,36 +593,35 @@ uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray( } \ break - HANDLE_TYPE( GROUP, Group, message); - HANDLE_TYPE( MESSAGE, Message, message); + HANDLE_TYPE(GROUP, Group, message); + HANDLE_TYPE(MESSAGE, Message, message); #undef HANDLE_TYPE } } } else if (!is_cleared) { switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ - case FieldDescriptor::TYPE_##UPPERCASE: \ - target = WireFormatLite::Write##CAMELCASE##ToArray( \ - number, VALUE, target); \ - break - - HANDLE_TYPE( INT32, Int32, int32_value); - HANDLE_TYPE( INT64, Int64, int64_value); - HANDLE_TYPE( UINT32, UInt32, uint32_value); - HANDLE_TYPE( UINT64, UInt64, uint64_value); - HANDLE_TYPE( SINT32, SInt32, int32_value); - HANDLE_TYPE( SINT64, SInt64, int64_value); - HANDLE_TYPE( FIXED32, Fixed32, uint32_value); - HANDLE_TYPE( FIXED64, Fixed64, uint64_value); - HANDLE_TYPE(SFIXED32, SFixed32, int32_value); - HANDLE_TYPE(SFIXED64, SFixed64, int64_value); - HANDLE_TYPE( FLOAT, Float, float_value); - HANDLE_TYPE( DOUBLE, Double, double_value); - HANDLE_TYPE( BOOL, Bool, bool_value); - HANDLE_TYPE( STRING, String, *string_value); - HANDLE_TYPE( BYTES, Bytes, *string_value); - HANDLE_TYPE( ENUM, Enum, enum_value); - HANDLE_TYPE( GROUP, Group, *message_value); +#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ + case FieldDescriptor::TYPE_##UPPERCASE: \ + target = WireFormatLite::Write##CAMELCASE##ToArray(number, VALUE, target); \ + break + + HANDLE_TYPE(INT32, Int32, int32_value); + HANDLE_TYPE(INT64, Int64, int64_value); + HANDLE_TYPE(UINT32, UInt32, uint32_value); + HANDLE_TYPE(UINT64, UInt64, uint64_value); + HANDLE_TYPE(SINT32, SInt32, int32_value); + HANDLE_TYPE(SINT64, SInt64, int64_value); + HANDLE_TYPE(FIXED32, Fixed32, uint32_value); + HANDLE_TYPE(FIXED64, Fixed64, uint64_value); + HANDLE_TYPE(SFIXED32, SFixed32, int32_value); + HANDLE_TYPE(SFIXED64, SFixed64, int64_value); + HANDLE_TYPE(FLOAT, Float, float_value); + HANDLE_TYPE(DOUBLE, Double, double_value); + HANDLE_TYPE(BOOL, Bool, bool_value); + HANDLE_TYPE(STRING, String, *string_value); + HANDLE_TYPE(BYTES, Bytes, *string_value); + HANDLE_TYPE(ENUM, Enum, enum_value); + HANDLE_TYPE(GROUP, Group, *message_value); #undef HANDLE_TYPE case FieldDescriptor::TYPE_MESSAGE: if (is_lazy) { @@ -670,11 +669,11 @@ ExtensionSet::Extension::InternalSerializeMessageSetItemWithCachedSizesToArray( bool ExtensionSet::ParseFieldMaybeLazily( int wire_type, int field_number, io::CodedInputStream* input, - ExtensionFinder* extension_finder, - MessageSetFieldSkipper* field_skipper) { - return ParseField(WireFormatLite::MakeTag( - field_number, static_cast<WireFormatLite::WireType>(wire_type)), - input, extension_finder, field_skipper); + ExtensionFinder* extension_finder, MessageSetFieldSkipper* field_skipper) { + return ParseField( + WireFormatLite::MakeTag(field_number, + static_cast<WireFormatLite::WireType>(wire_type)), + input, extension_finder, field_skipper); } bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc index e33d14d8cc..6dd6f7d8f5 100644 --- a/src/google/protobuf/extension_set_unittest.cc +++ b/src/google/protobuf/extension_set_unittest.cc @@ -78,9 +78,9 @@ TEST(ExtensionSetTest, Defaults) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_EQ(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_EQ( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_EQ(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); } @@ -114,9 +114,9 @@ TEST(ExtensionSetTest, Clear) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_NE(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_NE( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_NE(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); @@ -133,7 +133,7 @@ TEST(ExtensionSetTest, ClearOneField) { TestUtil::SetAllExtensions(&message); int64 original_value = - message.GetExtension(unittest::optional_int64_extension); + message.GetExtension(unittest::optional_int64_extension); // Clear the field and make sure it shows up as cleared. message.ClearExtension(unittest::optional_int64_extension); @@ -151,20 +151,18 @@ TEST(ExtensionSetTest, ClearOneField) { TEST(ExtensionSetTest, SetAllocatedExtension) { unittest::TestAllExtensions message; - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); // Add a extension using SetAllocatedExtension unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage(); message.SetAllocatedExtension(unittest::optional_foreign_message_extension, foreign_message); - EXPECT_TRUE(message.HasExtension( - unittest::optional_foreign_message_extension)); - EXPECT_EQ(foreign_message, - message.MutableExtension( - unittest::optional_foreign_message_extension)); - EXPECT_EQ(foreign_message, - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, message.MutableExtension( + unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, &message.GetExtension( + unittest::optional_foreign_message_extension)); // SetAllocatedExtension should delete the previously existing extension. // (We reply on unittest to check memory leaks for this case) @@ -174,8 +172,8 @@ TEST(ExtensionSetTest, SetAllocatedExtension) { // SetAllocatedExtension with a NULL parameter is equivalent to ClearExtenion. message.SetAllocatedExtension(unittest::optional_foreign_message_extension, NULL); - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); } TEST(ExtensionSetTest, ReleaseExtension) { @@ -186,8 +184,7 @@ TEST(ExtensionSetTest, ReleaseExtension) { unittest::TestMessageSetExtension1* extension = new unittest::TestMessageSetExtension1(); message.SetAllocatedExtension( - unittest::TestMessageSetExtension1::message_set_extension, - extension); + unittest::TestMessageSetExtension1::message_set_extension, extension); EXPECT_TRUE(message.HasExtension( unittest::TestMessageSetExtension1::message_set_extension)); // Release the extension using ReleaseExtension @@ -200,8 +197,7 @@ TEST(ExtensionSetTest, ReleaseExtension) { // ReleaseExtension will return the underlying object even after // ClearExtension is called. message.SetAllocatedExtension( - unittest::TestMessageSetExtension1::message_set_extension, - extension); + unittest::TestMessageSetExtension1::message_set_extension, extension); message.ClearExtension( unittest::TestMessageSetExtension1::message_set_extension); released_extension = message.ReleaseExtension( @@ -216,8 +212,7 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaSetAllocatedAndRelease) { Arena::CreateMessage<unittest::TestAllExtensions>(&arena); unittest::ForeignMessage extension; message->UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - &extension); + unittest::optional_foreign_message_extension, &extension); // No copy when set. unittest::ForeignMessage* mutable_extension = message->MutableExtension(unittest::optional_foreign_message_extension); @@ -227,21 +222,19 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaSetAllocatedAndRelease) { message->UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(&extension, released_extension); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); // Set the ownership back and let the destructors run. It should not take // ownership, so this should not crash. message->UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - &extension); + unittest::optional_foreign_message_extension, &extension); } TEST(ExtensionSetTest, UnsafeArenaSetAllocatedAndRelease) { unittest::TestAllExtensions message; unittest::ForeignMessage* extension = new unittest::ForeignMessage(); message.UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + unittest::optional_foreign_message_extension, extension); // No copy when set. unittest::ForeignMessage* mutable_extension = message.MutableExtension(unittest::optional_foreign_message_extension); @@ -251,13 +244,12 @@ TEST(ExtensionSetTest, UnsafeArenaSetAllocatedAndRelease) { message.UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(extension, released_extension); - EXPECT_FALSE(message.HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension)); // Set the ownership back and let the destructors run. It should take // ownership, so this should not leak. message.UnsafeArenaSetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + unittest::optional_foreign_message_extension, extension); } TEST(ExtensionSetTest, ArenaUnsafeArenaReleaseOfHeapAlloc) { @@ -265,17 +257,16 @@ TEST(ExtensionSetTest, ArenaUnsafeArenaReleaseOfHeapAlloc) { unittest::TestAllExtensions* message = Arena::CreateMessage<unittest::TestAllExtensions>(&arena); unittest::ForeignMessage* extension = new unittest::ForeignMessage; - message->SetAllocatedExtension( - unittest::optional_foreign_message_extension, - extension); + message->SetAllocatedExtension(unittest::optional_foreign_message_extension, + extension); // The arena should maintain ownership of the heap allocated proto because we // used UnsafeArenaReleaseExtension. The leak checker will ensure this. unittest::ForeignMessage* released_extension = message->UnsafeArenaReleaseExtension( unittest::optional_foreign_message_extension); EXPECT_EQ(extension, released_extension); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); } @@ -345,10 +336,8 @@ TEST(ExtensionSetTest, SwapExtension) { TestUtil::ExpectExtensionsClear(message2); // Swap two extensions. - fields.push_back( - reflection->FindKnownExtensionByNumber(12)); - fields.push_back( - reflection->FindKnownExtensionByNumber(25)); + fields.push_back(reflection->FindKnownExtensionByNumber(12)); + fields.push_back(reflection->FindKnownExtensionByNumber(25)); reflection->SwapFields(&message1, &message2, fields); EXPECT_TRUE(message1.HasExtension(unittest::optional_int32_extension)); @@ -427,23 +416,22 @@ TEST(ExtensionSetTest, ArenaSetAllocatedMessageAndRelease) { Arena arena; unittest::TestAllExtensions* message = Arena::CreateMessage<unittest::TestAllExtensions>(&arena); - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); // Add a extension using SetAllocatedExtension unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage(); message->SetAllocatedExtension(unittest::optional_foreign_message_extension, foreign_message); // foreign_message is now owned by the arena. - EXPECT_EQ(foreign_message, - message->MutableExtension( - unittest::optional_foreign_message_extension)); + EXPECT_EQ(foreign_message, message->MutableExtension( + unittest::optional_foreign_message_extension)); // Underlying message is copied, and returned. - unittest::ForeignMessage* released_message = message->ReleaseExtension( - unittest::optional_foreign_message_extension); + unittest::ForeignMessage* released_message = + message->ReleaseExtension(unittest::optional_foreign_message_extension); delete released_message; - EXPECT_FALSE(message->HasExtension( - unittest::optional_foreign_message_extension)); + EXPECT_FALSE( + message->HasExtension(unittest::optional_foreign_message_extension)); } TEST(ExtensionSetTest, SwapExtensionBothFullWithArena) { @@ -615,14 +603,20 @@ TEST(ExtensionSetTest, NestedExtensionGroup) { source.set_optional_foreign_enum(unittest::FOREIGN_BAZ); source.SerializeToString(&data); EXPECT_TRUE(destination.ParseFromString(data)); - EXPECT_TRUE(destination.GetExtension( - unittest::TestNestedExtension::optionalgroup_extension).has_a()); - EXPECT_EQ(117, destination.GetExtension( - unittest::TestNestedExtension::optionalgroup_extension).a()); + EXPECT_TRUE( + destination + .GetExtension(unittest::TestNestedExtension::optionalgroup_extension) + .has_a()); + EXPECT_EQ(117, destination + .GetExtension( + unittest::TestNestedExtension::optionalgroup_extension) + .a()); EXPECT_TRUE(destination.HasExtension( unittest::TestNestedExtension::optional_foreign_enum_extension)); - EXPECT_EQ(unittest::FOREIGN_BAZ, destination.GetExtension( - unittest::TestNestedExtension::optional_foreign_enum_extension)); + EXPECT_EQ( + unittest::FOREIGN_BAZ, + destination.GetExtension( + unittest::TestNestedExtension::optional_foreign_enum_extension)); } TEST(ExtensionSetTest, Parsing) { @@ -743,36 +737,38 @@ TEST(ExtensionSetTest, MutableString) { TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { // Scalar primitive extensions should increase the extension set size by a // minimum of the size of the primitive type. -#define TEST_SCALAR_EXTENSIONS_SPACE_USED(type, value) \ - do { \ - unittest::TestAllExtensions message; \ - const int base_size = message.SpaceUsed(); \ - message.SetExtension(unittest::optional_##type##_extension, value); \ - int min_expected_size = base_size + \ - sizeof(message.GetExtension(unittest::optional_##type##_extension)); \ - EXPECT_LE(min_expected_size, message.SpaceUsed()); \ +#define TEST_SCALAR_EXTENSIONS_SPACE_USED(type, value) \ + do { \ + unittest::TestAllExtensions message; \ + const int base_size = message.SpaceUsed(); \ + message.SetExtension(unittest::optional_##type##_extension, value); \ + int min_expected_size = \ + base_size + \ + sizeof(message.GetExtension(unittest::optional_##type##_extension)); \ + EXPECT_LE(min_expected_size, message.SpaceUsed()); \ } while (0) - TEST_SCALAR_EXTENSIONS_SPACE_USED(int32 , 101); - TEST_SCALAR_EXTENSIONS_SPACE_USED(int64 , 102); - TEST_SCALAR_EXTENSIONS_SPACE_USED(uint32 , 103); - TEST_SCALAR_EXTENSIONS_SPACE_USED(uint64 , 104); - TEST_SCALAR_EXTENSIONS_SPACE_USED(sint32 , 105); - TEST_SCALAR_EXTENSIONS_SPACE_USED(sint64 , 106); - TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed32 , 107); - TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed64 , 108); + TEST_SCALAR_EXTENSIONS_SPACE_USED(int32, 101); + TEST_SCALAR_EXTENSIONS_SPACE_USED(int64, 102); + TEST_SCALAR_EXTENSIONS_SPACE_USED(uint32, 103); + TEST_SCALAR_EXTENSIONS_SPACE_USED(uint64, 104); + TEST_SCALAR_EXTENSIONS_SPACE_USED(sint32, 105); + TEST_SCALAR_EXTENSIONS_SPACE_USED(sint64, 106); + TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed32, 107); + TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed64, 108); TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed32, 109); TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed64, 110); - TEST_SCALAR_EXTENSIONS_SPACE_USED(float , 111); - TEST_SCALAR_EXTENSIONS_SPACE_USED(double , 112); - TEST_SCALAR_EXTENSIONS_SPACE_USED(bool , true); + TEST_SCALAR_EXTENSIONS_SPACE_USED(float, 111); + TEST_SCALAR_EXTENSIONS_SPACE_USED(double, 112); + TEST_SCALAR_EXTENSIONS_SPACE_USED(bool, true); #undef TEST_SCALAR_EXTENSIONS_SPACE_USED { unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); message.SetExtension(unittest::optional_nested_enum_extension, unittest::TestAllTypes::FOO); - int min_expected_size = base_size + + int min_expected_size = + base_size + sizeof(message.GetExtension(unittest::optional_nested_enum_extension)); EXPECT_LE(min_expected_size, message.SpaceUsed()); } @@ -794,8 +790,8 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { const int base_size = message.SpaceUsed(); unittest::ForeignMessage foreign; foreign.set_c(42); - message.MutableExtension(unittest::optional_foreign_message_extension)-> - CopyFrom(foreign); + message.MutableExtension(unittest::optional_foreign_message_extension) + ->CopyFrom(foreign); int min_expected_size = base_size + foreign.SpaceUsed(); EXPECT_LE(min_expected_size, message.SpaceUsed()); } @@ -810,45 +806,49 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { // SpaceUsed() // - Adds a large number of values (requiring allocation in the repeated // field), and ensures that that allocation is included in SpaceUsed() -#define TEST_REPEATED_EXTENSIONS_SPACE_USED(type, cpptype, value) \ - do { \ - unittest::TestAllExtensions message; \ - const int base_size = message.SpaceUsed(); \ - int min_expected_size = sizeof(RepeatedField<cpptype>) + base_size; \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - message.ClearExtension(unittest::repeated_##type##_extension); \ - const int empty_repeated_field_size = message.SpaceUsed(); \ - EXPECT_LE(min_expected_size, empty_repeated_field_size) << #type; \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - EXPECT_EQ(empty_repeated_field_size, message.SpaceUsed()) << #type; \ - message.ClearExtension(unittest::repeated_##type##_extension); \ - const int old_capacity = \ - message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ - .Capacity(); \ - EXPECT_GE(old_capacity, kMinRepeatedFieldAllocationSize); \ - for (int i = 0; i < 16; ++i) { \ - message.AddExtension(unittest::repeated_##type##_extension, value); \ - } \ - int expected_size = sizeof(cpptype) * \ - (message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ - .Capacity() - old_capacity) + empty_repeated_field_size; \ - EXPECT_LE(expected_size, message.SpaceUsed()) << #type; \ +#define TEST_REPEATED_EXTENSIONS_SPACE_USED(type, cpptype, value) \ + do { \ + unittest::TestAllExtensions message; \ + const int base_size = message.SpaceUsed(); \ + int min_expected_size = sizeof(RepeatedField<cpptype>) + base_size; \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + message.ClearExtension(unittest::repeated_##type##_extension); \ + const int empty_repeated_field_size = message.SpaceUsed(); \ + EXPECT_LE(min_expected_size, empty_repeated_field_size) << #type; \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + EXPECT_EQ(empty_repeated_field_size, message.SpaceUsed()) << #type; \ + message.ClearExtension(unittest::repeated_##type##_extension); \ + const int old_capacity = \ + message.GetRepeatedExtension(unittest::repeated_##type##_extension) \ + .Capacity(); \ + EXPECT_GE(old_capacity, kMinRepeatedFieldAllocationSize); \ + for (int i = 0; i < 16; ++i) { \ + message.AddExtension(unittest::repeated_##type##_extension, value); \ + } \ + int expected_size = \ + sizeof(cpptype) * \ + (message \ + .GetRepeatedExtension(unittest::repeated_##type##_extension) \ + .Capacity() - \ + old_capacity) + \ + empty_repeated_field_size; \ + EXPECT_LE(expected_size, message.SpaceUsed()) << #type; \ } while (0) - TEST_REPEATED_EXTENSIONS_SPACE_USED(int32 , int32 , 101); - TEST_REPEATED_EXTENSIONS_SPACE_USED(int64 , int64 , 102); - TEST_REPEATED_EXTENSIONS_SPACE_USED(uint32 , uint32, 103); - TEST_REPEATED_EXTENSIONS_SPACE_USED(uint64 , uint64, 104); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sint32 , int32 , 105); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sint64 , int64 , 106); - TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed32 , uint32, 107); - TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed64 , uint64, 108); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed32, int32 , 109); - TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed64, int64 , 110); - TEST_REPEATED_EXTENSIONS_SPACE_USED(float , float , 111); - TEST_REPEATED_EXTENSIONS_SPACE_USED(double , double, 112); - TEST_REPEATED_EXTENSIONS_SPACE_USED(bool , bool , true); + TEST_REPEATED_EXTENSIONS_SPACE_USED(int32, int32, 101); + TEST_REPEATED_EXTENSIONS_SPACE_USED(int64, int64, 102); + TEST_REPEATED_EXTENSIONS_SPACE_USED(uint32, uint32, 103); + TEST_REPEATED_EXTENSIONS_SPACE_USED(uint64, uint64, 104); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sint32, int32, 105); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sint64, int64, 106); + TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed32, uint32, 107); + TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed64, uint64, 108); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed32, int32, 109); + TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed64, int64, 110); + TEST_REPEATED_EXTENSIONS_SPACE_USED(float, float, 111); + TEST_REPEATED_EXTENSIONS_SPACE_USED(double, double, 112); + TEST_REPEATED_EXTENSIONS_SPACE_USED(bool, bool, true); TEST_REPEATED_EXTENSIONS_SPACE_USED(nested_enum, int, unittest::TestAllTypes::FOO); #undef TEST_REPEATED_EXTENSIONS_SPACE_USED @@ -864,21 +864,21 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { for (int i = 0; i < 16; ++i) { message.AddExtension(unittest::repeated_string_extension, value); } - min_expected_size += (sizeof(value) + value.size()) * - (16 - kMinRepeatedFieldAllocationSize); + min_expected_size += + (sizeof(value) + value.size()) * (16 - kMinRepeatedFieldAllocationSize); EXPECT_LE(min_expected_size, message.SpaceUsed()); } // Repeated messages { unittest::TestAllExtensions message; const int base_size = message.SpaceUsed(); - int min_expected_size = sizeof(RepeatedPtrField<unittest::ForeignMessage>) + - base_size; + int min_expected_size = + sizeof(RepeatedPtrField<unittest::ForeignMessage>) + base_size; unittest::ForeignMessage prototype; prototype.set_c(2); for (int i = 0; i < 16; ++i) { - message.AddExtension(unittest::repeated_foreign_message_extension)-> - CopyFrom(prototype); + message.AddExtension(unittest::repeated_foreign_message_extension) + ->CopyFrom(prototype); } min_expected_size += (16 - kMinRepeatedFieldAllocationSize) * prototype.SpaceUsed(); @@ -887,7 +887,7 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) { } // N.B.: We do not test range-based for here because we remain C++03 compatible. -template<typename T, typename M, typename ID> +template <typename T, typename M, typename ID> inline T SumAllExtensions(const M& message, ID extension, T zero) { T sum = zero; typename RepeatedField<T>::const_iterator iter = @@ -900,12 +900,11 @@ inline T SumAllExtensions(const M& message, ID extension, T zero) { return sum; } -template<typename T, typename M, typename ID> -inline void IncAllExtensions(M* message, ID extension, - T val) { +template <typename T, typename M, typename ID> +inline void IncAllExtensions(M* message, ID extension, T val) { typename RepeatedField<T>::iterator iter = message->MutableRepeatedExtension(extension)->begin(); - typename RepeatedField<T>::iterator end = + typename RepeatedField<T>::iterator end = message->MutableRepeatedExtension(extension)->end(); for (; iter != end; ++iter) { *iter += val; @@ -916,14 +915,19 @@ TEST(ExtensionSetTest, RepeatedFields) { unittest::TestAllExtensions message; // Test empty repeated-field case (b/12926163) - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_int32_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_nested_enum_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_string_extension).size()); - ASSERT_EQ(0, message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_int32_extension).size()); + ASSERT_EQ( + 0, message.GetRepeatedExtension(unittest::repeated_nested_enum_extension) + .size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_string_extension).size()); + ASSERT_EQ( + 0, + message.GetRepeatedExtension(unittest::repeated_nested_message_extension) + .size()); unittest::TestAllTypes::NestedMessage nested_message; nested_message.set_bb(42); @@ -949,111 +953,110 @@ TEST(ExtensionSetTest, RepeatedFields) { std::string("test")); message.AddExtension(unittest::repeated_bytes_extension, std::string("test\xFF")); - message.AddExtension( - unittest::repeated_nested_message_extension)->CopyFrom(nested_message); - message.AddExtension(unittest::repeated_nested_enum_extension, - nested_enum); + message.AddExtension(unittest::repeated_nested_message_extension) + ->CopyFrom(nested_message); + message.AddExtension(unittest::repeated_nested_enum_extension, nested_enum); } - ASSERT_EQ(10, SumAllExtensions<int32>( - message, unittest::repeated_int32_extension, 0)); - IncAllExtensions<int32>( - &message, unittest::repeated_int32_extension, 1); - ASSERT_EQ(20, SumAllExtensions<int32>( - message, unittest::repeated_int32_extension, 0)); + ASSERT_EQ(10, SumAllExtensions<int32>(message, + unittest::repeated_int32_extension, 0)); + IncAllExtensions<int32>(&message, unittest::repeated_int32_extension, 1); + ASSERT_EQ(20, SumAllExtensions<int32>(message, + unittest::repeated_int32_extension, 0)); - ASSERT_EQ(20, SumAllExtensions<int64>( - message, unittest::repeated_int64_extension, 0)); - IncAllExtensions<int64>( - &message, unittest::repeated_int64_extension, 1); - ASSERT_EQ(30, SumAllExtensions<int64>( - message, unittest::repeated_int64_extension, 0)); + ASSERT_EQ(20, SumAllExtensions<int64>(message, + unittest::repeated_int64_extension, 0)); + IncAllExtensions<int64>(&message, unittest::repeated_int64_extension, 1); + ASSERT_EQ(30, SumAllExtensions<int64>(message, + unittest::repeated_int64_extension, 0)); ASSERT_EQ(30, SumAllExtensions<uint32>( - message, unittest::repeated_uint32_extension, 0)); - IncAllExtensions<uint32>( - &message, unittest::repeated_uint32_extension, 1); + message, unittest::repeated_uint32_extension, 0)); + IncAllExtensions<uint32>(&message, unittest::repeated_uint32_extension, 1); ASSERT_EQ(40, SumAllExtensions<uint32>( - message, unittest::repeated_uint32_extension, 0)); + message, unittest::repeated_uint32_extension, 0)); ASSERT_EQ(40, SumAllExtensions<uint64>( - message, unittest::repeated_uint64_extension, 0)); - IncAllExtensions<uint64>( - &message, unittest::repeated_uint64_extension, 1); + message, unittest::repeated_uint64_extension, 0)); + IncAllExtensions<uint64>(&message, unittest::repeated_uint64_extension, 1); ASSERT_EQ(50, SumAllExtensions<uint64>( - message, unittest::repeated_uint64_extension, 0)); + message, unittest::repeated_uint64_extension, 0)); ASSERT_EQ(50, SumAllExtensions<int32>( - message, unittest::repeated_sint32_extension, 0)); - IncAllExtensions<int32>( - &message, unittest::repeated_sint32_extension, 1); + message, unittest::repeated_sint32_extension, 0)); + IncAllExtensions<int32>(&message, unittest::repeated_sint32_extension, 1); ASSERT_EQ(60, SumAllExtensions<int32>( - message, unittest::repeated_sint32_extension, 0)); + message, unittest::repeated_sint32_extension, 0)); ASSERT_EQ(60, SumAllExtensions<int64>( - message, unittest::repeated_sint64_extension, 0)); - IncAllExtensions<int64>( - &message, unittest::repeated_sint64_extension, 1); + message, unittest::repeated_sint64_extension, 0)); + IncAllExtensions<int64>(&message, unittest::repeated_sint64_extension, 1); ASSERT_EQ(70, SumAllExtensions<int64>( - message, unittest::repeated_sint64_extension, 0)); + message, unittest::repeated_sint64_extension, 0)); ASSERT_EQ(70, SumAllExtensions<uint32>( - message, unittest::repeated_fixed32_extension, 0)); - IncAllExtensions<uint32>( - &message, unittest::repeated_fixed32_extension, 1); + message, unittest::repeated_fixed32_extension, 0)); + IncAllExtensions<uint32>(&message, unittest::repeated_fixed32_extension, 1); ASSERT_EQ(80, SumAllExtensions<uint32>( - message, unittest::repeated_fixed32_extension, 0)); + message, unittest::repeated_fixed32_extension, 0)); ASSERT_EQ(80, SumAllExtensions<uint64>( - message, unittest::repeated_fixed64_extension, 0)); - IncAllExtensions<uint64>( - &message, unittest::repeated_fixed64_extension, 1); + message, unittest::repeated_fixed64_extension, 0)); + IncAllExtensions<uint64>(&message, unittest::repeated_fixed64_extension, 1); ASSERT_EQ(90, SumAllExtensions<uint64>( - message, unittest::repeated_fixed64_extension, 0)); + message, unittest::repeated_fixed64_extension, 0)); // Usually, floating-point arithmetic cannot be trusted to be exact, so it is // a Bad Idea to assert equality in a test like this. However, we're dealing // with integers with a small number of significant mantissa bits, so we // should actually have exact precision here. - ASSERT_EQ(90, SumAllExtensions<float>( - message, unittest::repeated_float_extension, 0)); - IncAllExtensions<float>( - &message, unittest::repeated_float_extension, 1); + ASSERT_EQ(90, SumAllExtensions<float>(message, + unittest::repeated_float_extension, 0)); + IncAllExtensions<float>(&message, unittest::repeated_float_extension, 1); ASSERT_EQ(100, SumAllExtensions<float>( - message, unittest::repeated_float_extension, 0)); + message, unittest::repeated_float_extension, 0)); ASSERT_EQ(100, SumAllExtensions<double>( - message, unittest::repeated_double_extension, 0)); - IncAllExtensions<double>( - &message, unittest::repeated_double_extension, 1); + message, unittest::repeated_double_extension, 0)); + IncAllExtensions<double>(&message, unittest::repeated_double_extension, 1); ASSERT_EQ(110, SumAllExtensions<double>( - message, unittest::repeated_double_extension, 0)); + message, unittest::repeated_double_extension, 0)); RepeatedPtrField<std::string>::iterator string_iter; RepeatedPtrField<std::string>::iterator string_end; - for (string_iter = message.MutableRepeatedExtension( - unittest::repeated_string_extension)->begin(), - string_end = message.MutableRepeatedExtension( - unittest::repeated_string_extension)->end(); + for (string_iter = + message + .MutableRepeatedExtension(unittest::repeated_string_extension) + ->begin(), + string_end = + message + .MutableRepeatedExtension(unittest::repeated_string_extension) + ->end(); string_iter != string_end; ++string_iter) { *string_iter += "test"; } RepeatedPtrField<std::string>::const_iterator string_const_iter; RepeatedPtrField<std::string>::const_iterator string_const_end; - for (string_const_iter = message.GetRepeatedExtension( - unittest::repeated_string_extension).begin(), - string_const_end = message.GetRepeatedExtension( - unittest::repeated_string_extension).end(); + for (string_const_iter = + message.GetRepeatedExtension(unittest::repeated_string_extension) + .begin(), + string_const_end = + message.GetRepeatedExtension(unittest::repeated_string_extension) + .end(); string_iter != string_end; ++string_iter) { ASSERT_TRUE(*string_iter == "testtest"); } RepeatedField<unittest::TestAllTypes_NestedEnum>::iterator enum_iter; RepeatedField<unittest::TestAllTypes_NestedEnum>::iterator enum_end; - for (enum_iter = message.MutableRepeatedExtension( - unittest::repeated_nested_enum_extension)->begin(), - enum_end = message.MutableRepeatedExtension( - unittest::repeated_nested_enum_extension)->end(); + for (enum_iter = message + .MutableRepeatedExtension( + unittest::repeated_nested_enum_extension) + ->begin(), + enum_end = message + .MutableRepeatedExtension( + unittest::repeated_nested_enum_extension) + ->end(); enum_iter != enum_end; ++enum_iter) { *enum_iter = unittest::TestAllTypes::NestedEnum_MAX; } @@ -1073,25 +1076,31 @@ TEST(ExtensionSetTest, RepeatedFields) { ASSERT_EQ(*enum_const_iter, unittest::TestAllTypes::NestedEnum_MAX); } - RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator - msg_iter; - RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator - msg_end; - for (msg_iter = message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)->begin(), - msg_end = message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)->end(); + RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator msg_iter; + RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator msg_end; + for (msg_iter = message + .MutableRepeatedExtension( + unittest::repeated_nested_message_extension) + ->begin(), + msg_end = message + .MutableRepeatedExtension( + unittest::repeated_nested_message_extension) + ->end(); msg_iter != msg_end; ++msg_iter) { msg_iter->set_bb(1234); } - RepeatedPtrField<unittest::TestAllTypes_NestedMessage>:: - const_iterator msg_const_iter; - RepeatedPtrField<unittest::TestAllTypes_NestedMessage>:: - const_iterator msg_const_end; - for (msg_const_iter = message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).begin(), - msg_const_end = message.GetRepeatedExtension( - unittest::repeated_nested_message_extension).end(); + RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::const_iterator + msg_const_iter; + RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::const_iterator + msg_const_end; + for (msg_const_iter = message + .GetRepeatedExtension( + unittest::repeated_nested_message_extension) + .begin(), + msg_const_end = message + .GetRepeatedExtension( + unittest::repeated_nested_message_extension) + .end(); msg_const_iter != msg_const_end; ++msg_const_iter) { ASSERT_EQ(msg_const_iter->bb(), 1234); } @@ -1099,30 +1108,30 @@ TEST(ExtensionSetTest, RepeatedFields) { // Test range-based for as well, but only if compiled as C++11. #if __cplusplus >= 201103L // Test one primitive field. - for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_int32_extension)) { + for (auto& x : + *message.MutableRepeatedExtension(unittest::repeated_int32_extension)) { x = 4321; } - for (const auto& x : message.GetRepeatedExtension( - unittest::repeated_int32_extension)) { + for (const auto& x : + message.GetRepeatedExtension(unittest::repeated_int32_extension)) { ASSERT_EQ(x, 4321); } // Test one string field. - for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_string_extension)) { + for (auto& x : + *message.MutableRepeatedExtension(unittest::repeated_string_extension)) { x = "test_range_based_for"; } - for (const auto& x : message.GetRepeatedExtension( - unittest::repeated_string_extension)) { + for (const auto& x : + message.GetRepeatedExtension(unittest::repeated_string_extension)) { ASSERT_TRUE(x == "test_range_based_for"); } // Test one message field. for (auto& x : *message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)) { + unittest::repeated_nested_message_extension)) { x.set_bb(4321); } for (const auto& x : *message.MutableRepeatedExtension( - unittest::repeated_nested_message_extension)) { + unittest::repeated_nested_message_extension)) { ASSERT_EQ(x.bb(), 4321); } #endif @@ -1132,12 +1141,13 @@ TEST(ExtensionSetTest, RepeatedFields) { TEST(ExtensionSetTest, AbsentExtension) { unittest::TestAllExtensions message; message.MutableRepeatedExtension(unittest::repeated_nested_message_extension) - ->Add()->set_bb(123); - ASSERT_EQ(1, message.ExtensionSize( - unittest::repeated_nested_message_extension)); - EXPECT_EQ( - 123, message.GetExtension( - unittest::repeated_nested_message_extension, 0).bb()); + ->Add() + ->set_bb(123); + ASSERT_EQ(1, + message.ExtensionSize(unittest::repeated_nested_message_extension)); + EXPECT_EQ(123, + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .bb()); } #ifdef PROTOBUF_HAS_DEATH_TEST @@ -1145,9 +1155,9 @@ TEST(ExtensionSetTest, AbsentExtension) { TEST(ExtensionSetTest, InvalidEnumDeath) { unittest::TestAllExtensions message; EXPECT_DEBUG_DEATH( - message.SetExtension(unittest::optional_foreign_enum_extension, - static_cast<unittest::ForeignEnum>(53)), - "IsValid"); + message.SetExtension(unittest::optional_foreign_enum_extension, + static_cast<unittest::ForeignEnum>(53)), + "IsValid"); } #endif // PROTOBUF_HAS_DEATH_TEST @@ -1223,8 +1233,8 @@ TEST(ExtensionSetTest, DynamicExtensions) { // An unknown enum value (for a known field). message.mutable_unknown_fields()->AddVarint( - unittest::TestDynamicExtensions::kDynamicEnumExtensionFieldNumber, - 12345); + unittest::TestDynamicExtensions::kDynamicEnumExtensionFieldNumber, + 12345); // A regular unknown field. message.mutable_unknown_fields()->AddLengthDelimited(54321, "unknown"); @@ -1244,24 +1254,24 @@ TEST(ExtensionSetTest, DynamicExtensions) { // Can we print it? EXPECT_EQ( - "[dynamic_extensions.scalar_extension]: 123\n" - "[dynamic_extensions.enum_extension]: FOREIGN_BAR\n" - "[dynamic_extensions.dynamic_enum_extension]: DYNAMIC_BAZ\n" - "[dynamic_extensions.message_extension] {\n" - " c: 456\n" - "}\n" - "[dynamic_extensions.dynamic_message_extension] {\n" - " dynamic_field: 789\n" - "}\n" - "[dynamic_extensions.repeated_extension]: \"foo\"\n" - "[dynamic_extensions.repeated_extension]: \"bar\"\n" - "[dynamic_extensions.packed_extension]: 12\n" - "[dynamic_extensions.packed_extension]: -34\n" - "[dynamic_extensions.packed_extension]: 56\n" - "[dynamic_extensions.packed_extension]: -78\n" - "2002: 12345\n" - "54321: \"unknown\"\n", - message.DebugString()); + "[dynamic_extensions.scalar_extension]: 123\n" + "[dynamic_extensions.enum_extension]: FOREIGN_BAR\n" + "[dynamic_extensions.dynamic_enum_extension]: DYNAMIC_BAZ\n" + "[dynamic_extensions.message_extension] {\n" + " c: 456\n" + "}\n" + "[dynamic_extensions.dynamic_message_extension] {\n" + " dynamic_field: 789\n" + "}\n" + "[dynamic_extensions.repeated_extension]: \"foo\"\n" + "[dynamic_extensions.repeated_extension]: \"bar\"\n" + "[dynamic_extensions.packed_extension]: 12\n" + "[dynamic_extensions.packed_extension]: -34\n" + "[dynamic_extensions.packed_extension]: 56\n" + "[dynamic_extensions.packed_extension]: -78\n" + "2002: 12345\n" + "54321: \"unknown\"\n", + message.DebugString()); // Can we serialize it? EXPECT_TRUE( @@ -1302,9 +1312,8 @@ TEST(ExtensionSetTest, DynamicExtensions) { file->FindExtensionByName("dynamic_message_extension"); ASSERT_TRUE(dynamic_message_extension != NULL); const Message& parent = unittest::TestAllExtensions::default_instance(); - const Message& sub_message = - parent.GetReflection()->GetMessage(parent, dynamic_message_extension, - &dynamic_factory); + const Message& sub_message = parent.GetReflection()->GetMessage( + parent, dynamic_message_extension, &dynamic_factory); const Message* prototype = dynamic_factory.GetPrototype(dynamic_message_extension->message_type()); EXPECT_EQ(prototype, &sub_message); diff --git a/src/google/protobuf/field_mask.pb.cc b/src/google/protobuf/field_mask.pb.cc index 0c54eb72e1..e360a6de73 100644 --- a/src/google/protobuf/field_mask.pb.cc +++ b/src/google/protobuf/field_mask.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsFieldMask_google_2fprotobuf_2ffield_5fmask_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsFieldMask_google_2fprotobuf_2ffield_5fmask_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2ffield_5fmask_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2ffield_5fmask_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ffield_5fmask_2eproto = nullptr; @@ -75,8 +71,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto[] "rotobuf.WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto = { - false, InitDefaults_google_2fprotobuf_2ffield_5fmask_2eproto, - descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2ffield_5fmask_2eproto, "google/protobuf/field_mask.proto", &assign_descriptors_table_google_2fprotobuf_2ffield_5fmask_2eproto, 230, }; @@ -84,6 +79,7 @@ void AddDescriptors_google_2fprotobuf_2ffield_5fmask_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FieldMask_google_2fprotobuf_2ffield_5fmask_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ffield_5fmask_2eproto, deps, 0); } @@ -165,35 +161,43 @@ void FieldMask::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FieldMask::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated string paths = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(add_paths(), ptr, ctx, "google.protobuf.FieldMask.paths"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FieldMask::MergePartialFromCodedStream( diff --git a/src/google/protobuf/generated_enum_reflection.h b/src/google/protobuf/generated_enum_reflection.h index c683368a2e..c25e51bc12 100644 --- a/src/google/protobuf/generated_enum_reflection.h +++ b/src/google/protobuf/generated_enum_reflection.h @@ -41,8 +41,8 @@ #include <string> -#include <google/protobuf/port.h> #include <google/protobuf/generated_enum_util.h> +#include <google/protobuf/port.h> #ifdef SWIG #error "You cannot SWIG proto headers" @@ -52,7 +52,7 @@ namespace google { namespace protobuf { - class EnumDescriptor; +class EnumDescriptor; } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/generated_enum_util.h b/src/google/protobuf/generated_enum_util.h index 5c4dd65973..55db124c8c 100644 --- a/src/google/protobuf/generated_enum_util.h +++ b/src/google/protobuf/generated_enum_util.h @@ -42,7 +42,8 @@ namespace protobuf { // This type trait can be used to cause templates to only match proto2 enum // types. -template <typename T> struct is_proto_enum : ::std::false_type {}; +template <typename T> +struct is_proto_enum : ::std::false_type {}; } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index 443a5db107..9b2209abeb 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -59,9 +59,7 @@ namespace protobuf { namespace internal { namespace { -bool IsMapFieldInApi(const FieldDescriptor* field) { - return field->is_map(); -} +bool IsMapFieldInApi(const FieldDescriptor* field) { return field->is_map(); } } // anonymous namespace bool ParseNamedEnum(const EnumDescriptor* descriptor, const std::string& name, @@ -99,89 +97,102 @@ const To& GetConstRefAtOffset(const Message& message, uint32 offset) { return *GetConstPointerAtOffset<To>(&message, offset); } -void ReportReflectionUsageError( - const Descriptor* descriptor, const FieldDescriptor* field, - const char* method, const char* description) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : " << description; +void ReportReflectionUsageError(const Descriptor* descriptor, + const FieldDescriptor* field, + const char* method, const char* description) { + GOOGLE_LOG(FATAL) << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : " + << description; } const char* cpptype_names_[FieldDescriptor::MAX_CPPTYPE + 1] = { - "INVALID_CPPTYPE", - "CPPTYPE_INT32", - "CPPTYPE_INT64", - "CPPTYPE_UINT32", - "CPPTYPE_UINT64", - "CPPTYPE_DOUBLE", - "CPPTYPE_FLOAT", - "CPPTYPE_BOOL", - "CPPTYPE_ENUM", - "CPPTYPE_STRING", - "CPPTYPE_MESSAGE" -}; + "INVALID_CPPTYPE", "CPPTYPE_INT32", "CPPTYPE_INT64", "CPPTYPE_UINT32", + "CPPTYPE_UINT64", "CPPTYPE_DOUBLE", "CPPTYPE_FLOAT", "CPPTYPE_BOOL", + "CPPTYPE_ENUM", "CPPTYPE_STRING", "CPPTYPE_MESSAGE"}; static void ReportReflectionUsageTypeError( const Descriptor* descriptor, const FieldDescriptor* field, - const char* method, - FieldDescriptor::CppType expected_type) { + const char* method, FieldDescriptor::CppType expected_type) { GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : Field is not the right type for this message:\n" - " Expected : " << cpptype_names_[expected_type] << "\n" - " Field type: " << cpptype_names_[field->cpp_type()]; + << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : Field is not the right type for this message:\n" + " Expected : " + << cpptype_names_[expected_type] + << "\n" + " Field type: " + << cpptype_names_[field->cpp_type()]; } static void ReportReflectionUsageEnumTypeError( const Descriptor* descriptor, const FieldDescriptor* field, const char* method, const EnumValueDescriptor* value) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::" << method << "\n" - " Message type: " << descriptor->full_name() << "\n" - " Field : " << field->full_name() << "\n" - " Problem : Enum value did not match field type:\n" - " Expected : " << field->enum_type()->full_name() << "\n" - " Actual : " << value->full_name(); -} - -#define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \ - if (!(CONDITION)) \ - ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \ + GOOGLE_LOG(FATAL) << "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::" + << method + << "\n" + " Message type: " + << descriptor->full_name() + << "\n" + " Field : " + << field->full_name() + << "\n" + " Problem : Enum value did not match field type:\n" + " Expected : " + << field->enum_type()->full_name() + << "\n" + " Actual : " + << value->full_name(); +} + +#define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \ + if (!(CONDITION)) \ + ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION) +#define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \ USAGE_CHECK((A) == (B), METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \ +#define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \ USAGE_CHECK((A) != (B), METHOD, ERROR_DESCRIPTION) -#define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \ - if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \ - ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \ - FieldDescriptor::CPPTYPE_##CPPTYPE) +#define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \ + if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \ + ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \ + FieldDescriptor::CPPTYPE_##CPPTYPE) -#define USAGE_CHECK_ENUM_VALUE(METHOD) \ - if (value->type() != field->enum_type()) \ - ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value) +#define USAGE_CHECK_ENUM_VALUE(METHOD) \ + if (value->type() != field->enum_type()) \ + ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value) -#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \ - USAGE_CHECK_EQ(field->containing_type(), descriptor_, \ - METHOD, "Field does not match message type."); -#define USAGE_CHECK_SINGULAR(METHOD) \ - USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ +#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \ + USAGE_CHECK_EQ(field->containing_type(), descriptor_, METHOD, \ + "Field does not match message type."); +#define USAGE_CHECK_SINGULAR(METHOD) \ + USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ "Field is repeated; the method requires a singular field.") -#define USAGE_CHECK_REPEATED(METHOD) \ - USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ +#define USAGE_CHECK_REPEATED(METHOD) \ + USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ "Field is singular; the method requires a repeated field.") -#define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ - USAGE_CHECK_MESSAGE_TYPE(METHOD); \ - USAGE_CHECK_##LABEL(METHOD); \ - USAGE_CHECK_TYPE(METHOD, CPPTYPE) +#define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ + USAGE_CHECK_MESSAGE_TYPE(METHOD); \ + USAGE_CHECK_##LABEL(METHOD); \ + USAGE_CHECK_TYPE(METHOD, CPPTYPE) } // namespace @@ -226,20 +237,20 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { const FieldDescriptor* field = descriptor_->field(i); if (field->is_repeated()) { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \ - .SpaceUsedExcludingSelfLong(); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \ + .SpaceUsedExcludingSelfLong(); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -272,14 +283,14 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { continue; } switch (field->cpp_type()) { - case FieldDescriptor::CPPTYPE_INT32 : - case FieldDescriptor::CPPTYPE_INT64 : + case FieldDescriptor::CPPTYPE_INT32: + case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_UINT32: case FieldDescriptor::CPPTYPE_UINT64: case FieldDescriptor::CPPTYPE_DOUBLE: - case FieldDescriptor::CPPTYPE_FLOAT : - case FieldDescriptor::CPPTYPE_BOOL : - case FieldDescriptor::CPPTYPE_ENUM : + case FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_BOOL: + case FieldDescriptor::CPPTYPE_ENUM: // Field is inline, so we've already counted it. break; @@ -331,26 +342,24 @@ size_t GeneratedMessageReflection::SpaceUsedLong(const Message& message) const { return total_size; } -void GeneratedMessageReflection::SwapField( - Message* message1, - Message* message2, - const FieldDescriptor* field) const { +void GeneratedMessageReflection::SwapField(Message* message1, Message* message2, + const FieldDescriptor* field) const { if (field->is_repeated()) { switch (field->cpp_type()) { -#define SWAP_ARRAYS(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - MutableRaw<RepeatedField<TYPE> >(message1, field)->Swap( \ - MutableRaw<RepeatedField<TYPE> >(message2, field)); \ - break; - - SWAP_ARRAYS(INT32 , int32 ); - SWAP_ARRAYS(INT64 , int64 ); +#define SWAP_ARRAYS(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + MutableRaw<RepeatedField<TYPE> >(message1, field) \ + ->Swap(MutableRaw<RepeatedField<TYPE> >(message2, field)); \ + break; + + SWAP_ARRAYS(INT32, int32); + SWAP_ARRAYS(INT64, int64); SWAP_ARRAYS(UINT32, uint32); SWAP_ARRAYS(UINT64, uint64); - SWAP_ARRAYS(FLOAT , float ); + SWAP_ARRAYS(FLOAT, float); SWAP_ARRAYS(DOUBLE, double); - SWAP_ARRAYS(BOOL , bool ); - SWAP_ARRAYS(ENUM , int ); + SWAP_ARRAYS(BOOL, bool); + SWAP_ARRAYS(ENUM, int); #undef SWAP_ARRAYS case FieldDescriptor::CPPTYPE_STRING: @@ -365,8 +374,8 @@ void GeneratedMessageReflection::SwapField( break; case FieldDescriptor::CPPTYPE_MESSAGE: if (IsMapFieldInApi(field)) { - MutableRaw<MapFieldBase>(message1, field)->Swap( - MutableRaw<MapFieldBase>(message2, field)); + MutableRaw<MapFieldBase>(message1, field) + ->Swap(MutableRaw<MapFieldBase>(message2, field)); } else { MutableRaw<RepeatedPtrFieldBase>(message1, field) ->Swap<GenericTypeHandler<Message> >( @@ -379,20 +388,20 @@ void GeneratedMessageReflection::SwapField( } } else { switch (field->cpp_type()) { -#define SWAP_VALUES(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - std::swap(*MutableRaw<TYPE>(message1, field), \ - *MutableRaw<TYPE>(message2, field)); \ - break; - - SWAP_VALUES(INT32 , int32 ); - SWAP_VALUES(INT64 , int64 ); +#define SWAP_VALUES(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + std::swap(*MutableRaw<TYPE>(message1, field), \ + *MutableRaw<TYPE>(message2, field)); \ + break; + + SWAP_VALUES(INT32, int32); + SWAP_VALUES(INT64, int64); SWAP_VALUES(UINT32, uint32); SWAP_VALUES(UINT64, uint64); - SWAP_VALUES(FLOAT , float ); + SWAP_VALUES(FLOAT, float); SWAP_VALUES(DOUBLE, double); - SWAP_VALUES(BOOL , bool ); - SWAP_VALUES(ENUM , int ); + SWAP_VALUES(BOOL, bool); + SWAP_VALUES(ENUM, int); #undef SWAP_VALUES case FieldDescriptor::CPPTYPE_MESSAGE: if (GetArena(message1) == GetArena(message2)) { @@ -421,35 +430,33 @@ void GeneratedMessageReflection::SwapField( case FieldDescriptor::CPPTYPE_STRING: switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. - case FieldOptions::STRING: - { - Arena* arena1 = GetArena(message1); - Arena* arena2 = GetArena(message2); + case FieldOptions::STRING: { + Arena* arena1 = GetArena(message1); + Arena* arena2 = GetArena(message2); - if (IsInlined(field)) { - InlinedStringField* string1 = - MutableRaw<InlinedStringField>(message1, field); - InlinedStringField* string2 = - MutableRaw<InlinedStringField>(message2, field); - string1->Swap(string2); - break; - } + if (IsInlined(field)) { + InlinedStringField* string1 = + MutableRaw<InlinedStringField>(message1, field); + InlinedStringField* string2 = + MutableRaw<InlinedStringField>(message2, field); + string1->Swap(string2); + break; + } - ArenaStringPtr* string1 = - MutableRaw<ArenaStringPtr>(message1, field); - ArenaStringPtr* string2 = - MutableRaw<ArenaStringPtr>(message2, field); - const std::string* default_ptr = - &DefaultRaw<ArenaStringPtr>(field).Get(); - if (arena1 == arena2) { - string1->Swap(string2, default_ptr, arena1); - } else { - const std::string temp = string1->Get(); - string1->Set(default_ptr, string2->Get(), arena1); - string2->Set(default_ptr, temp, arena2); - } + ArenaStringPtr* string1 = + MutableRaw<ArenaStringPtr>(message1, field); + ArenaStringPtr* string2 = + MutableRaw<ArenaStringPtr>(message2, field); + const std::string* default_ptr = + &DefaultRaw<ArenaStringPtr>(field).Get(); + if (arena1 == arena2) { + string1->Swap(string2, default_ptr, arena1); + } else { + const std::string temp = string1->Get(); + string1->Set(default_ptr, string2->Get(), arena1); + string2->Set(default_ptr, temp, arena2); } - break; + } break; } break; @@ -460,8 +467,7 @@ void GeneratedMessageReflection::SwapField( } void GeneratedMessageReflection::SwapOneofField( - Message* message1, - Message* message2, + Message* message1, Message* message2, const OneofDescriptor* oneof_descriptor) const { uint32 oneof_case1 = GetOneofCase(*message1, oneof_descriptor); uint32 oneof_case2 = GetOneofCase(*message2, oneof_descriptor); @@ -481,21 +487,21 @@ void GeneratedMessageReflection::SwapOneofField( const FieldDescriptor* field1 = NULL; if (oneof_case1 > 0) { field1 = descriptor_->FindFieldByNumber(oneof_case1); - //oneof_descriptor->field(oneof_case1); + // oneof_descriptor->field(oneof_case1); switch (field1->cpp_type()) { -#define GET_TEMP_VALUE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - temp_##TYPE = GetField<TYPE>(*message1, field1); \ - break; +#define GET_TEMP_VALUE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + temp_##TYPE = GetField<TYPE>(*message1, field1); \ + break; - GET_TEMP_VALUE(INT32 , int32 ); - GET_TEMP_VALUE(INT64 , int64 ); + GET_TEMP_VALUE(INT32, int32); + GET_TEMP_VALUE(INT64, int64); GET_TEMP_VALUE(UINT32, uint32); GET_TEMP_VALUE(UINT64, uint64); - GET_TEMP_VALUE(FLOAT , float ); + GET_TEMP_VALUE(FLOAT, float); GET_TEMP_VALUE(DOUBLE, double); - GET_TEMP_VALUE(BOOL , bool ); - GET_TEMP_VALUE(ENUM , int ); + GET_TEMP_VALUE(BOOL, bool); + GET_TEMP_VALUE(ENUM, int); #undef GET_TEMP_VALUE case FieldDescriptor::CPPTYPE_MESSAGE: temp_message = ReleaseMessage(message1, field1); @@ -512,27 +518,24 @@ void GeneratedMessageReflection::SwapOneofField( // Sets message1's oneof field from the message2's oneof field. if (oneof_case2 > 0) { - const FieldDescriptor* field2 = - descriptor_->FindFieldByNumber(oneof_case2); + const FieldDescriptor* field2 = descriptor_->FindFieldByNumber(oneof_case2); switch (field2->cpp_type()) { -#define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - SetField<TYPE>(message1, field2, GetField<TYPE>(*message2, field2)); \ - break; +#define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + SetField<TYPE>(message1, field2, GetField<TYPE>(*message2, field2)); \ + break; - SET_ONEOF_VALUE1(INT32 , int32 ); - SET_ONEOF_VALUE1(INT64 , int64 ); + SET_ONEOF_VALUE1(INT32, int32); + SET_ONEOF_VALUE1(INT64, int64); SET_ONEOF_VALUE1(UINT32, uint32); SET_ONEOF_VALUE1(UINT64, uint64); - SET_ONEOF_VALUE1(FLOAT , float ); + SET_ONEOF_VALUE1(FLOAT, float); SET_ONEOF_VALUE1(DOUBLE, double); - SET_ONEOF_VALUE1(BOOL , bool ); - SET_ONEOF_VALUE1(ENUM , int ); + SET_ONEOF_VALUE1(BOOL, bool); + SET_ONEOF_VALUE1(ENUM, int); #undef SET_ONEOF_VALUE1 case FieldDescriptor::CPPTYPE_MESSAGE: - SetAllocatedMessage(message1, - ReleaseMessage(message2, field2), - field2); + SetAllocatedMessage(message1, ReleaseMessage(message2, field2), field2); break; case FieldDescriptor::CPPTYPE_STRING: @@ -549,19 +552,19 @@ void GeneratedMessageReflection::SwapOneofField( // Sets message2's oneof field from the temp variable. if (oneof_case1 > 0) { switch (field1->cpp_type()) { -#define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - SetField<TYPE>(message2, field1, temp_##TYPE); \ - break; +#define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + SetField<TYPE>(message2, field1, temp_##TYPE); \ + break; - SET_ONEOF_VALUE2(INT32 , int32 ); - SET_ONEOF_VALUE2(INT64 , int64 ); + SET_ONEOF_VALUE2(INT32, int32); + SET_ONEOF_VALUE2(INT64, int64); SET_ONEOF_VALUE2(UINT32, uint32); SET_ONEOF_VALUE2(UINT64, uint64); - SET_ONEOF_VALUE2(FLOAT , float ); + SET_ONEOF_VALUE2(FLOAT, float); SET_ONEOF_VALUE2(DOUBLE, double); - SET_ONEOF_VALUE2(BOOL , bool ); - SET_ONEOF_VALUE2(ENUM , int ); + SET_ONEOF_VALUE2(BOOL, bool); + SET_ONEOF_VALUE2(ENUM, int); #undef SET_ONEOF_VALUE2 case FieldDescriptor::CPPTYPE_MESSAGE: SetAllocatedMessage(message2, temp_message, field1); @@ -579,26 +582,27 @@ void GeneratedMessageReflection::SwapOneofField( } } -void GeneratedMessageReflection::Swap( - Message* message1, - Message* message2) const { +void GeneratedMessageReflection::Swap(Message* message1, + Message* message2) const { if (message1 == message2) return; // TODO(kenton): Other Reflection methods should probably check this too. GOOGLE_CHECK_EQ(message1->GetReflection(), this) - << "First argument to Swap() (of type \"" - << message1->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "First argument to Swap() (of type \"" + << message1->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; GOOGLE_CHECK_EQ(message2->GetReflection(), this) - << "Second argument to Swap() (of type \"" - << message2->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "Second argument to Swap() (of type \"" + << message2->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; // Check that both messages are in the same arena (or both on the heap). We // need to copy all data if not, due to ownership semantics. @@ -653,26 +657,27 @@ void GeneratedMessageReflection::Swap( } void GeneratedMessageReflection::SwapFields( - Message* message1, - Message* message2, + Message* message1, Message* message2, const std::vector<const FieldDescriptor*>& fields) const { if (message1 == message2) return; // TODO(kenton): Other Reflection methods should probably check this too. GOOGLE_CHECK_EQ(message1->GetReflection(), this) - << "First argument to SwapFields() (of type \"" - << message1->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "First argument to SwapFields() (of type \"" + << message1->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; GOOGLE_CHECK_EQ(message2->GetReflection(), this) - << "Second argument to SwapFields() (of type \"" - << message2->GetDescriptor()->full_name() - << "\") is not compatible with this reflection object (which is for type \"" - << descriptor_->full_name() - << "\"). Note that the exact same class is required; not just the same " - "descriptor."; + << "Second argument to SwapFields() (of type \"" + << message2->GetDescriptor()->full_name() + << "\") is not compatible with this reflection object (which is for type " + "\"" + << descriptor_->full_name() + << "\"). Note that the exact same class is required; not just the same " + "descriptor."; std::set<int> swapped_oneof; @@ -681,8 +686,7 @@ void GeneratedMessageReflection::SwapFields( const FieldDescriptor* field = fields[i]; if (field->is_extension()) { MutableExtensionSet(message1)->SwapExtension( - MutableExtensionSet(message2), - field->number()); + MutableExtensionSet(message2), field->number()); } else { if (field->containing_oneof()) { int oneof_index = field->containing_oneof()->index(); @@ -732,18 +736,18 @@ int GeneratedMessageReflection::FieldSize(const Message& message, return GetExtensionSet(message).ExtensionSize(field->number()); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - return GetRaw<RepeatedField<LOWERCASE> >(message, field).size() +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + return GetRaw<RepeatedField<LOWERCASE> >(message, field).size() - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -784,24 +788,23 @@ void GeneratedMessageReflection::ClearField( // We need to set the field back to its default value. switch (field->cpp_type()) { -#define CLEAR_TYPE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - *MutableRaw<TYPE>(message, field) = \ - field->default_value_##TYPE(); \ - break; +#define CLEAR_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + *MutableRaw<TYPE>(message, field) = field->default_value_##TYPE(); \ + break; - CLEAR_TYPE(INT32 , int32 ); - CLEAR_TYPE(INT64 , int64 ); + CLEAR_TYPE(INT32, int32); + CLEAR_TYPE(INT64, int64); CLEAR_TYPE(UINT32, uint32); CLEAR_TYPE(UINT64, uint64); - CLEAR_TYPE(FLOAT , float ); + CLEAR_TYPE(FLOAT, float); CLEAR_TYPE(DOUBLE, double); - CLEAR_TYPE(BOOL , bool ); + CLEAR_TYPE(BOOL, bool); #undef CLEAR_TYPE case FieldDescriptor::CPPTYPE_ENUM: *MutableRaw<int>(message, field) = - field->default_value_enum()->number(); + field->default_value_enum()->number(); break; case FieldDescriptor::CPPTYPE_STRING: { @@ -811,15 +814,15 @@ void GeneratedMessageReflection::ClearField( if (IsInlined(field)) { const std::string* default_ptr = &DefaultRaw<InlinedStringField>(field).GetNoArena(); - MutableRaw<InlinedStringField>(message, field)->SetNoArena( - default_ptr, *default_ptr); + MutableRaw<InlinedStringField>(message, field) + ->SetNoArena(default_ptr, *default_ptr); break; } const std::string* default_ptr = &DefaultRaw<ArenaStringPtr>(field).Get(); - MutableRaw<ArenaStringPtr>(message, field)->SetAllocated( - default_ptr, NULL, GetArena(message)); + MutableRaw<ArenaStringPtr>(message, field) + ->SetAllocated(default_ptr, NULL, GetArena(message)); break; } } @@ -842,19 +845,19 @@ void GeneratedMessageReflection::ClearField( } } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw<RepeatedField<LOWERCASE> >(message, field)->Clear(); \ - break +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw<RepeatedField<LOWERCASE> >(message, field)->Clear(); \ + break - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: { @@ -883,8 +886,7 @@ void GeneratedMessageReflection::ClearField( } void GeneratedMessageReflection::RemoveLast( - Message* message, - const FieldDescriptor* field) const { + Message* message, const FieldDescriptor* field) const { USAGE_CHECK_MESSAGE_TYPE(RemoveLast); USAGE_CHECK_REPEATED(RemoveLast); @@ -892,19 +894,19 @@ void GeneratedMessageReflection::RemoveLast( MutableExtensionSet(message)->RemoveLast(field->number()); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw<RepeatedField<LOWERCASE> >(message, field)->RemoveLast(); \ - break +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw<RepeatedField<LOWERCASE> >(message, field)->RemoveLast(); \ + break - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -924,7 +926,7 @@ void GeneratedMessageReflection::RemoveLast( ->RemoveLast<GenericTypeHandler<Message> >(); } else { MutableRaw<RepeatedPtrFieldBase>(message, field) - ->RemoveLast<GenericTypeHandler<Message> >(); + ->RemoveLast<GenericTypeHandler<Message> >(); } break; } @@ -932,8 +934,7 @@ void GeneratedMessageReflection::RemoveLast( } Message* GeneratedMessageReflection::ReleaseLast( - Message* message, - const FieldDescriptor* field) const { + Message* message, const FieldDescriptor* field) const { USAGE_CHECK_ALL(ReleaseLast, REPEATED, MESSAGE); if (field->is_extension()) { @@ -946,16 +947,14 @@ Message* GeneratedMessageReflection::ReleaseLast( ->ReleaseLast<GenericTypeHandler<Message> >(); } else { return MutableRaw<RepeatedPtrFieldBase>(message, field) - ->ReleaseLast<GenericTypeHandler<Message> >(); + ->ReleaseLast<GenericTypeHandler<Message> >(); } } } -void GeneratedMessageReflection::SwapElements( - Message* message, - const FieldDescriptor* field, - int index1, - int index2) const { +void GeneratedMessageReflection::SwapElements(Message* message, + const FieldDescriptor* field, + int index1, int index2) const { USAGE_CHECK_MESSAGE_TYPE(Swap); USAGE_CHECK_REPEATED(Swap); @@ -963,20 +962,20 @@ void GeneratedMessageReflection::SwapElements( MutableExtensionSet(message)->SwapElements(field->number(), index1, index2); } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case FieldDescriptor::CPPTYPE_##UPPERCASE : \ - MutableRaw<RepeatedField<LOWERCASE> >(message, field) \ - ->SwapElements(index1, index2); \ - break - - HANDLE_TYPE( INT32, int32); - HANDLE_TYPE( INT64, int64); +#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ + case FieldDescriptor::CPPTYPE_##UPPERCASE: \ + MutableRaw<RepeatedField<LOWERCASE> >(message, field) \ + ->SwapElements(index1, index2); \ + break + + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); HANDLE_TYPE(UINT64, uint64); HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE( FLOAT, float); - HANDLE_TYPE( BOOL, bool); - HANDLE_TYPE( ENUM, int); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(ENUM, int); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_STRING: @@ -987,7 +986,7 @@ void GeneratedMessageReflection::SwapElements( ->SwapElements(index1, index2); } else { MutableRaw<RepeatedPtrFieldBase>(message, field) - ->SwapElements(index1, index2); + ->SwapElements(index1, index2); } break; } @@ -1003,8 +1002,8 @@ struct FieldNumberSorter { } }; -inline bool IsIndexInHasBitSet( - const uint32* has_bit_set, uint32 has_bit_index) { +inline bool IsIndexInHasBitSet(const uint32* has_bit_set, + uint32 has_bit_index) { GOOGLE_DCHECK_NE(has_bit_index, ~0u); return ((has_bit_set[has_bit_index / 32] >> (has_bit_index % 32)) & static_cast<uint32>(1)) != 0; @@ -1012,8 +1011,7 @@ inline bool IsIndexInHasBitSet( } // namespace void GeneratedMessageReflection::ListFields( - const Message& message, - std::vector<const FieldDescriptor*>* output) const { + const Message& message, std::vector<const FieldDescriptor*>* output) const { output->clear(); // Optimization: The default instance never has any fields set. @@ -1065,74 +1063,71 @@ void GeneratedMessageReflection::ListFields( // ------------------------------------------------------------------- #undef DEFINE_PRIMITIVE_ACCESSORS -#define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \ - PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \ - const Message& message, const FieldDescriptor* field) const { \ - USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \ - if (field->is_extension()) { \ - return GetExtensionSet(message).Get##TYPENAME( \ - field->number(), field->default_value_##PASSTYPE()); \ - } else { \ - return GetField<TYPE>(message, field); \ - } \ - } \ - \ - void GeneratedMessageReflection::Set##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - PASSTYPE value) const { \ - USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \ - if (field->is_extension()) { \ - return MutableExtensionSet(message)->Set##TYPENAME( \ - field->number(), field->type(), value, field); \ - } else { \ - SetField<TYPE>(message, field, value); \ - } \ - } \ - \ - PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \ - const Message& message, \ - const FieldDescriptor* field, int index) const { \ - USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - return GetExtensionSet(message).GetRepeated##TYPENAME( \ - field->number(), index); \ - } else { \ - return GetRepeatedField<TYPE>(message, field, index); \ - } \ - } \ - \ - void GeneratedMessageReflection::SetRepeated##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - int index, PASSTYPE value) const { \ - USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - MutableExtensionSet(message)->SetRepeated##TYPENAME( \ - field->number(), index, value); \ - } else { \ - SetRepeatedField<TYPE>(message, field, index, value); \ - } \ - } \ - \ - void GeneratedMessageReflection::Add##TYPENAME( \ - Message* message, const FieldDescriptor* field, \ - PASSTYPE value) const { \ - USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \ - if (field->is_extension()) { \ - MutableExtensionSet(message)->Add##TYPENAME( \ - field->number(), field->type(), field->options().packed(), value, \ - field); \ - } else { \ - AddField<TYPE>(message, field, value); \ - } \ - } - -DEFINE_PRIMITIVE_ACCESSORS(Int32 , int32 , int32 , INT32 ) -DEFINE_PRIMITIVE_ACCESSORS(Int64 , int64 , int64 , INT64 ) +#define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \ + PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \ + const Message& message, const FieldDescriptor* field) const { \ + USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \ + if (field->is_extension()) { \ + return GetExtensionSet(message).Get##TYPENAME( \ + field->number(), field->default_value_##PASSTYPE()); \ + } else { \ + return GetField<TYPE>(message, field); \ + } \ + } \ + \ + void GeneratedMessageReflection::Set##TYPENAME( \ + Message* message, const FieldDescriptor* field, PASSTYPE value) const { \ + USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \ + if (field->is_extension()) { \ + return MutableExtensionSet(message)->Set##TYPENAME( \ + field->number(), field->type(), value, field); \ + } else { \ + SetField<TYPE>(message, field, value); \ + } \ + } \ + \ + PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \ + const Message& message, const FieldDescriptor* field, int index) const { \ + USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + return GetExtensionSet(message).GetRepeated##TYPENAME(field->number(), \ + index); \ + } else { \ + return GetRepeatedField<TYPE>(message, field, index); \ + } \ + } \ + \ + void GeneratedMessageReflection::SetRepeated##TYPENAME( \ + Message* message, const FieldDescriptor* field, int index, \ + PASSTYPE value) const { \ + USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + MutableExtensionSet(message)->SetRepeated##TYPENAME(field->number(), \ + index, value); \ + } else { \ + SetRepeatedField<TYPE>(message, field, index, value); \ + } \ + } \ + \ + void GeneratedMessageReflection::Add##TYPENAME( \ + Message* message, const FieldDescriptor* field, PASSTYPE value) const { \ + USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \ + if (field->is_extension()) { \ + MutableExtensionSet(message)->Add##TYPENAME( \ + field->number(), field->type(), field->options().packed(), value, \ + field); \ + } else { \ + AddField<TYPE>(message, field, value); \ + } \ + } + +DEFINE_PRIMITIVE_ACCESSORS(Int32, int32, int32, INT32) +DEFINE_PRIMITIVE_ACCESSORS(Int64, int64, int64, INT64) DEFINE_PRIMITIVE_ACCESSORS(UInt32, uint32, uint32, UINT32) DEFINE_PRIMITIVE_ACCESSORS(UInt64, uint64, uint64, UINT64) -DEFINE_PRIMITIVE_ACCESSORS(Float , float , float , FLOAT ) +DEFINE_PRIMITIVE_ACCESSORS(Float, float, float, FLOAT) DEFINE_PRIMITIVE_ACCESSORS(Double, double, double, DOUBLE) -DEFINE_PRIMITIVE_ACCESSORS(Bool , bool , bool , BOOL ) +DEFINE_PRIMITIVE_ACCESSORS(Bool, bool, bool, BOOL) #undef DEFINE_PRIMITIVE_ACCESSORS // ------------------------------------------------------------------- @@ -1191,8 +1186,8 @@ void GeneratedMessageReflection::SetString(Message* message, default: // TODO(kenton): Support other string reps. case FieldOptions::STRING: { if (IsInlined(field)) { - MutableField<InlinedStringField>(message, field)->SetNoArena( - NULL, value); + MutableField<InlinedStringField>(message, field) + ->SetNoArena(NULL, value); break; } @@ -1200,8 +1195,8 @@ void GeneratedMessageReflection::SetString(Message* message, &DefaultRaw<ArenaStringPtr>(field).Get(); if (field->containing_oneof() && !HasOneofField(*message, field)) { ClearOneof(message, field->containing_oneof()); - MutableField<ArenaStringPtr>(message, field)->UnsafeSetDefault( - default_ptr); + MutableField<ArenaStringPtr>(message, field) + ->UnsafeSetDefault(default_ptr); } MutableField<ArenaStringPtr>(message, field) ->Mutable(default_ptr, GetArena(message)) @@ -1248,8 +1243,8 @@ void GeneratedMessageReflection::SetRepeatedString( const std::string& value) const { USAGE_CHECK_ALL(SetRepeatedString, REPEATED, STRING); if (field->is_extension()) { - MutableExtensionSet(message)->SetRepeatedString( - field->number(), index, value); + MutableExtensionSet(message)->SetRepeatedString(field->number(), index, + value); } else { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. @@ -1266,8 +1261,8 @@ void GeneratedMessageReflection::AddString(Message* message, const std::string& value) const { USAGE_CHECK_ALL(AddString, REPEATED, STRING); if (field->is_extension()) { - MutableExtensionSet(message)->AddString(field->number(), - field->type(), value, field); + MutableExtensionSet(message)->AddString(field->number(), field->type(), + value, field); } else { switch (field->options().ctype()) { default: // TODO(kenton): Support other string reps. @@ -1299,7 +1294,7 @@ int GeneratedMessageReflection::GetEnumValue( int32 value; if (field->is_extension()) { value = GetExtensionSet(message).GetEnum( - field->number(), field->default_value_enum()->number()); + field->number(), field->default_value_enum()->number()); } else { value = GetField<int>(message, field); } @@ -1314,9 +1309,9 @@ void GeneratedMessageReflection::SetEnum( SetEnumValueInternal(message, field, value->number()); } -void GeneratedMessageReflection::SetEnumValue( - Message* message, const FieldDescriptor* field, - int value) const { +void GeneratedMessageReflection::SetEnumValue(Message* message, + const FieldDescriptor* field, + int value) const { USAGE_CHECK_ALL(SetEnumValue, SINGULAR, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { // Check that the value is valid if we don't support direct storage of @@ -1332,11 +1327,10 @@ void GeneratedMessageReflection::SetEnumValue( } void GeneratedMessageReflection::SetEnumValueInternal( - Message* message, const FieldDescriptor* field, - int value) const { + Message* message, const FieldDescriptor* field, int value) const { if (field->is_extension()) { - MutableExtensionSet(message)->SetEnum(field->number(), field->type(), - value, field); + MutableExtensionSet(message)->SetEnum(field->number(), field->type(), value, + field); } else { SetField<int>(message, field, value); } @@ -1363,8 +1357,7 @@ int GeneratedMessageReflection::GetRepeatedEnumValue( } void GeneratedMessageReflection::SetRepeatedEnum( - Message* message, - const FieldDescriptor* field, int index, + Message* message, const FieldDescriptor* field, int index, const EnumValueDescriptor* value) const { // Usage checked by SetRepeatedEnumValue. USAGE_CHECK_ENUM_VALUE(SetRepeatedEnum); @@ -1372,8 +1365,7 @@ void GeneratedMessageReflection::SetRepeatedEnum( } void GeneratedMessageReflection::SetRepeatedEnumValue( - Message* message, - const FieldDescriptor* field, int index, + Message* message, const FieldDescriptor* field, int index, int value) const { USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { @@ -1390,12 +1382,11 @@ void GeneratedMessageReflection::SetRepeatedEnumValue( } void GeneratedMessageReflection::SetRepeatedEnumValueInternal( - Message* message, - const FieldDescriptor* field, int index, + Message* message, const FieldDescriptor* field, int index, int value) const { if (field->is_extension()) { - MutableExtensionSet(message)->SetRepeatedEnum( - field->number(), index, value); + MutableExtensionSet(message)->SetRepeatedEnum(field->number(), index, + value); } else { SetRepeatedField<int>(message, field, index, value); } @@ -1409,9 +1400,9 @@ void GeneratedMessageReflection::AddEnum( AddEnumValueInternal(message, field, value->number()); } -void GeneratedMessageReflection::AddEnumValue( - Message* message, const FieldDescriptor* field, - int value) const { +void GeneratedMessageReflection::AddEnumValue(Message* message, + const FieldDescriptor* field, + int value) const { USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM); if (!CreateUnknownEnumValues(descriptor_->file())) { // Check that the value is valid if we don't support direct storage of @@ -1427,12 +1418,11 @@ void GeneratedMessageReflection::AddEnumValue( } void GeneratedMessageReflection::AddEnumValueInternal( - Message* message, const FieldDescriptor* field, - int value) const { + Message* message, const FieldDescriptor* field, int value) const { if (field->is_extension()) { MutableExtensionSet(message)->AddEnum(field->number(), field->type(), - field->options().packed(), - value, field); + field->options().packed(), value, + field); } else { AddField<int>(message, field, value); } @@ -1448,9 +1438,8 @@ const Message& GeneratedMessageReflection::GetMessage( if (factory == NULL) factory = message_factory_; if (field->is_extension()) { - return static_cast<const Message&>( - GetExtensionSet(message).GetMessage( - field->number(), field->message_type(), factory)); + return static_cast<const Message&>(GetExtensionSet(message).GetMessage( + field->number(), field->message_type(), factory)); } else { const Message* result = GetRaw<const Message*>(message, field); if (result == NULL) { @@ -1496,8 +1485,7 @@ Message* GeneratedMessageReflection::MutableMessage( } void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage( - Message* message, - Message* sub_message, + Message* message, Message* sub_message, const FieldDescriptor* field) const { USAGE_CHECK_ALL(SetAllocatedMessage, SINGULAR, MESSAGE); @@ -1530,14 +1518,12 @@ void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage( } void GeneratedMessageReflection::SetAllocatedMessage( - Message* message, - Message* sub_message, + Message* message, Message* sub_message, const FieldDescriptor* field) const { // If message and sub-message are in different memory ownership domains // (different arenas, or one is on heap and one is not), then we may need to // do a copy. - if (sub_message != NULL && - sub_message->GetArena() != message->GetArena()) { + if (sub_message != NULL && sub_message->GetArena() != message->GetArena()) { if (sub_message->GetArena() == NULL && message->GetArena() != NULL) { // Case 1: parent is on an arena and child is heap-allocated. We can add // the child to the arena's Own() list to free on arena destruction, then @@ -1558,8 +1544,7 @@ void GeneratedMessageReflection::SetAllocatedMessage( } Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( - Message* message, - const FieldDescriptor* field, + Message* message, const FieldDescriptor* field, MessageFactory* factory) const { USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE); @@ -1588,8 +1573,7 @@ Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( } Message* GeneratedMessageReflection::ReleaseMessage( - Message* message, - const FieldDescriptor* field, + Message* message, const FieldDescriptor* field, MessageFactory* factory) const { Message* released = UnsafeArenaReleaseMessage(message, field, factory); if (GetArena(message) != NULL && released != NULL) { @@ -1625,8 +1609,8 @@ Message* GeneratedMessageReflection::MutableRepeatedMessage( if (field->is_extension()) { return static_cast<Message*>( - MutableExtensionSet(message)->MutableRepeatedMessage( - field->number(), index)); + MutableExtensionSet(message)->MutableRepeatedMessage(field->number(), + index)); } else { if (IsMapFieldInApi(field)) { return MutableRaw<MapFieldBase>(message, field) @@ -1634,14 +1618,14 @@ Message* GeneratedMessageReflection::MutableRepeatedMessage( ->Mutable<GenericTypeHandler<Message> >(index); } else { return MutableRaw<RepeatedPtrFieldBase>(message, field) - ->Mutable<GenericTypeHandler<Message> >(index); + ->Mutable<GenericTypeHandler<Message> >(index); } } } -Message* GeneratedMessageReflection::AddMessage( - Message* message, const FieldDescriptor* field, - MessageFactory* factory) const { +Message* GeneratedMessageReflection::AddMessage(Message* message, + const FieldDescriptor* field, + MessageFactory* factory) const { USAGE_CHECK_ALL(AddMessage, REPEATED, MESSAGE); if (factory == NULL) factory = message_factory_; @@ -1682,8 +1666,7 @@ Message* GeneratedMessageReflection::AddMessage( } void GeneratedMessageReflection::AddAllocatedMessage( - Message* message, const FieldDescriptor* field, - Message* new_entry) const { + Message* message, const FieldDescriptor* field, Message* new_entry) const { USAGE_CHECK_ALL(AddAllocatedMessage, REPEATED, MESSAGE); if (field->is_extension()) { @@ -1702,12 +1685,11 @@ void GeneratedMessageReflection::AddAllocatedMessage( void* GeneratedMessageReflection::MutableRawRepeatedField( Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpptype, - int ctype, const Descriptor* desc) const { + FieldDescriptor::CppType cpptype, int ctype, const Descriptor* desc) const { USAGE_CHECK_REPEATED("MutableRawRepeatedField"); if (field->cpp_type() != cpptype) - ReportReflectionUsageTypeError(descriptor_, - field, "MutableRawRepeatedField", cpptype); + ReportReflectionUsageTypeError(descriptor_, field, + "MutableRawRepeatedField", cpptype); if (desc != NULL) GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; if (field->is_extension()) { @@ -1725,12 +1707,11 @@ void* GeneratedMessageReflection::MutableRawRepeatedField( const void* GeneratedMessageReflection::GetRawRepeatedField( const Message& message, const FieldDescriptor* field, - FieldDescriptor::CppType cpptype, - int ctype, const Descriptor* desc) const { + FieldDescriptor::CppType cpptype, int ctype, const Descriptor* desc) const { USAGE_CHECK_REPEATED("GetRawRepeatedField"); if (field->cpp_type() != cpptype) - ReportReflectionUsageTypeError(descriptor_, - field, "GetRawRepeatedField", cpptype); + ReportReflectionUsageTypeError(descriptor_, field, "GetRawRepeatedField", + cpptype); if (ctype >= 0) GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; if (desc != NULL) @@ -1741,8 +1722,8 @@ const void* GeneratedMessageReflection::GetRawRepeatedField( // Map is not supported in extensions, it is acceptable to use // extension_set::MutableRawRepeatedField which does not change the message. return MutableExtensionSet(const_cast<Message*>(&message)) - ->MutableRawRepeatedField( - field->number(), field->type(), field->is_packed(), field); + ->MutableRawRepeatedField(field->number(), field->type(), + field->is_packed(), field); } else { // Trigger transform for MapField if (IsMapFieldInApi(field)) { @@ -1753,8 +1734,7 @@ const void* GeneratedMessageReflection::GetRawRepeatedField( } const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor( - const Message& message, - const OneofDescriptor* oneof_descriptor) const { + const Message& message, const OneofDescriptor* oneof_descriptor) const { uint32 field_number = GetOneofCase(message, oneof_descriptor); if (field_number == 0) { return NULL; @@ -1762,67 +1742,51 @@ const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor( return descriptor_->FindFieldByNumber(field_number); } -bool GeneratedMessageReflection::ContainsMapKey( - const Message& message, - const FieldDescriptor* field, - const MapKey& key) const { - USAGE_CHECK(IsMapFieldInApi(field), - "LookupMapValue", +bool GeneratedMessageReflection::ContainsMapKey(const Message& message, + const FieldDescriptor* field, + const MapKey& key) const { + USAGE_CHECK(IsMapFieldInApi(field), "LookupMapValue", "Field is not a map field."); return GetRaw<MapFieldBase>(message, field).ContainsMapKey(key); } bool GeneratedMessageReflection::InsertOrLookupMapValue( - Message* message, - const FieldDescriptor* field, - const MapKey& key, + Message* message, const FieldDescriptor* field, const MapKey& key, MapValueRef* val) const { - USAGE_CHECK(IsMapFieldInApi(field), - "InsertOrLookupMapValue", + USAGE_CHECK(IsMapFieldInApi(field), "InsertOrLookupMapValue", "Field is not a map field."); val->SetType(field->message_type()->FindFieldByName("value")->cpp_type()); - return MutableRaw<MapFieldBase>(message, field)->InsertOrLookupMapValue( - key, val); + return MutableRaw<MapFieldBase>(message, field) + ->InsertOrLookupMapValue(key, val); } -bool GeneratedMessageReflection::DeleteMapValue( - Message* message, - const FieldDescriptor* field, - const MapKey& key) const { - USAGE_CHECK(IsMapFieldInApi(field), - "DeleteMapValue", +bool GeneratedMessageReflection::DeleteMapValue(Message* message, + const FieldDescriptor* field, + const MapKey& key) const { + USAGE_CHECK(IsMapFieldInApi(field), "DeleteMapValue", "Field is not a map field."); return MutableRaw<MapFieldBase>(message, field)->DeleteMapValue(key); } MapIterator GeneratedMessageReflection::MapBegin( - Message* message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapBegin", - "Field is not a map field."); + Message* message, const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapBegin", "Field is not a map field."); MapIterator iter(message, field); GetRaw<MapFieldBase>(*message, field).MapBegin(&iter); return iter; } MapIterator GeneratedMessageReflection::MapEnd( - Message* message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapEnd", - "Field is not a map field."); + Message* message, const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapEnd", "Field is not a map field."); MapIterator iter(message, field); GetRaw<MapFieldBase>(*message, field).MapEnd(&iter); return iter; } -int GeneratedMessageReflection::MapSize( - const Message& message, - const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "MapSize", - "Field is not a map field."); +int GeneratedMessageReflection::MapSize(const Message& message, + const FieldDescriptor* field) const { + USAGE_CHECK(IsMapFieldInApi(field), "MapSize", "Field is not a map field."); return GetRaw<MapFieldBase>(message, field).size(); } @@ -1847,8 +1811,7 @@ const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName( const FieldDescriptor* extension = type->extension(i); if (extension->containing_type() == descriptor_ && extension->type() == FieldDescriptor::TYPE_MESSAGE && - extension->is_optional() && - extension->message_type() == type) { + extension->is_optional() && extension->message_type() == type) { // Found it. return extension; } @@ -1903,12 +1866,11 @@ bool GeneratedMessageReflection::IsInlined(const FieldDescriptor* field) const { } template <typename Type> -Type* GeneratedMessageReflection::MutableRaw(Message* message, - const FieldDescriptor* field) const { +Type* GeneratedMessageReflection::MutableRaw( + Message* message, const FieldDescriptor* field) const { return GetPointerAtOffset<Type>(message, schema_.GetFieldOffset(field)); } - inline const uint32* GeneratedMessageReflection::GetHasBits( const Message& message) const { GOOGLE_DCHECK(schema_.HasHasbits()); @@ -1981,7 +1943,7 @@ inline bool GeneratedMessageReflection::HasBit( // present only if their message-field pointer is non-NULL. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { return !schema_.IsDefaultInstance(message) && - GetRaw<const Message*>(message, field) != NULL; + GetRaw<const Message*>(message, field) != NULL; } else { // Non-message field (and non-oneof, since that was handled in HasField() // before calling us), and singular (again, checked in HasField). So, this @@ -1999,7 +1961,8 @@ inline bool GeneratedMessageReflection::HasBit( default: { if (IsInlined(field)) { return !GetField<InlinedStringField>(message, field) - .GetNoArena().empty(); + .GetNoArena() + .empty(); } return GetField<ArenaStringPtr>(message, field).Get().size() > 0; } @@ -2109,8 +2072,8 @@ inline void GeneratedMessageReflection::ClearOneof( case FieldOptions::STRING: { const std::string* default_ptr = &DefaultRaw<ArenaStringPtr>(field).Get(); - MutableField<ArenaStringPtr>(message, field)-> - Destroy(default_ptr, GetArena(message)); + MutableField<ArenaStringPtr>(message, field) + ->Destroy(default_ptr, GetArena(message)); break; } } @@ -2139,21 +2102,22 @@ inline const Type& GeneratedMessageReflection::GetField( } template <typename Type> -inline void GeneratedMessageReflection::SetField( - Message* message, const FieldDescriptor* field, const Type& value) const { +inline void GeneratedMessageReflection::SetField(Message* message, + const FieldDescriptor* field, + const Type& value) const { if (field->containing_oneof() && !HasOneofField(*message, field)) { ClearOneof(message, field->containing_oneof()); } *MutableRaw<Type>(message, field) = value; - field->containing_oneof() ? - SetOneofCase(message, field) : SetBit(message, field); + field->containing_oneof() ? SetOneofCase(message, field) + : SetBit(message, field); } template <typename Type> inline Type* GeneratedMessageReflection::MutableField( Message* message, const FieldDescriptor* field) const { - field->containing_oneof() ? - SetOneofCase(message, field) : SetBit(message, field); + field->containing_oneof() ? SetOneofCase(message, field) + : SetBit(message, field); return MutableRaw<Type>(message, field); } @@ -2171,8 +2135,8 @@ inline const Type& GeneratedMessageReflection::GetRepeatedPtrField( template <typename Type> inline void GeneratedMessageReflection::SetRepeatedField( - Message* message, const FieldDescriptor* field, - int index, Type value) const { + Message* message, const FieldDescriptor* field, int index, + Type value) const { MutableRaw<RepeatedField<Type> >(message, field)->Set(index, value); } @@ -2180,13 +2144,14 @@ template <typename Type> inline Type* GeneratedMessageReflection::MutableRepeatedField( Message* message, const FieldDescriptor* field, int index) const { RepeatedPtrField<Type>* repeated = - MutableRaw<RepeatedPtrField<Type> >(message, field); + MutableRaw<RepeatedPtrField<Type> >(message, field); return repeated->Mutable(index); } template <typename Type> -inline void GeneratedMessageReflection::AddField( - Message* message, const FieldDescriptor* field, const Type& value) const { +inline void GeneratedMessageReflection::AddField(Message* message, + const FieldDescriptor* field, + const Type& value) const { MutableRaw<RepeatedField<Type> >(message, field)->Add(value); } @@ -2194,7 +2159,7 @@ template <typename Type> inline Type* GeneratedMessageReflection::AddField( Message* message, const FieldDescriptor* field) const { RepeatedPtrField<Type>* repeated = - MutableRaw<RepeatedPtrField<Type> >(message, field); + MutableRaw<RepeatedPtrField<Type> >(message, field); return repeated->Add(); } @@ -2204,8 +2169,7 @@ MessageFactory* GeneratedMessageReflection::GetMessageFactory() const { void* GeneratedMessageReflection::RepeatedFieldData( Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const { + FieldDescriptor::CppType cpp_type, const Descriptor* message_type) const { GOOGLE_CHECK(field->is_repeated()); GOOGLE_CHECK(field->cpp_type() == cpp_type || (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && @@ -2226,16 +2190,14 @@ void* GeneratedMessageReflection::RepeatedFieldData( MapFieldBase* GeneratedMessageReflection::MutableMapData( Message* message, const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "GetMapData", + USAGE_CHECK(IsMapFieldInApi(field), "GetMapData", "Field is not a map field."); return MutableRaw<MapFieldBase>(message, field); } const MapFieldBase* GeneratedMessageReflection::GetMapData( const Message& message, const FieldDescriptor* field) const { - USAGE_CHECK(IsMapFieldInApi(field), - "GetMapData", + USAGE_CHECK(IsMapFieldInApi(field), "GetMapData", "Field is not a map field."); return &(GetRaw<MapFieldBase>(message, field)); } @@ -2261,7 +2223,7 @@ ReflectionSchema MigrationToReflectionSchema( return result; } -template<typename Schema> +template <typename Schema> class AssignDescriptorsHelper { public: AssignDescriptorsHelper(MessageFactory* factory, @@ -2309,7 +2271,7 @@ class AssignDescriptorsHelper { Metadata* file_level_metadata_; const EnumDescriptor** file_level_enum_descriptors_; const Schema* schemas_; - const Message* const * default_instance_data_; + const Message* const* default_instance_data_; const uint32* offsets_; }; @@ -2382,8 +2344,6 @@ void AssignDescriptorsImpl(const AssignDescriptorsTable* table) { void AddDescriptorsImpl(const DescriptorTable* table, const InitFunc* deps, int num_deps) { - // Ensure default instances of this proto file are initialized. - table->init_defaults(); // Ensure all dependent descriptors are registered to the generated descriptor // pool and message factory. for (int i = 0; i < num_deps; i++) { @@ -2419,7 +2379,7 @@ void RegisterAllTypesInternal(const Metadata* file_level_metadata, int size) { for (int i = 0; i < size; i++) { const GeneratedMessageReflection* reflection = static_cast<const GeneratedMessageReflection*>( - file_level_metadata[i].reflection); + file_level_metadata[i].reflection); if (reflection) { // It's not a map type MessageFactory::InternalRegisterGeneratedMessage( diff --git a/src/google/protobuf/generated_message_reflection.h b/src/google/protobuf/generated_message_reflection.h index 17a41bf256..820a148833 100644 --- a/src/google/protobuf/generated_message_reflection.h +++ b/src/google/protobuf/generated_message_reflection.h @@ -83,8 +83,8 @@ class ReflectionAccessor; class GeneratedMessageReflection; // Defined in other files. -class ExtensionSet; // extension_set.h -class WeakFieldMap; // weak_field_map.h +class ExtensionSet; // extension_set.h +class WeakFieldMap; // weak_field_map.h // This struct describes the internal layout of the message, hence this is // used to act on the message reflectively. @@ -146,7 +146,7 @@ struct ReflectionSchema { if (field->containing_oneof()) { size_t offset = static_cast<size_t>(field->containing_type()->field_count() + - field->containing_oneof()->index()); + field->containing_oneof()->index()); return OffsetValue(offsets_[offset], field->type()); } else { return GetFieldOffsetNonOneof(field); @@ -166,8 +166,8 @@ struct ReflectionSchema { uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const { return static_cast<uint32>(oneof_case_offset_) + - static_cast<uint32>( - static_cast<size_t>(oneof_descriptor->index()) * sizeof(uint32)); + static_cast<uint32>(static_cast<size_t>(oneof_descriptor->index()) * + sizeof(uint32)); } bool HasHasbits() const { return has_bits_offset_ != -1; } @@ -211,7 +211,7 @@ struct ReflectionSchema { // Returns a pointer to the default value for this field. The size and type // of the underlying data depends on the field's type. - const void *GetFieldDefault(const FieldDescriptor* field) const { + const void* GetFieldDefault(const FieldDescriptor* field) const { return reinterpret_cast<const uint8*>(default_instance_) + OffsetValue(offsets_[field->index()], field->type()); } @@ -224,7 +224,7 @@ struct ReflectionSchema { // them, ie. // // ReflectionSchema schema = {a, b, c, d, e, ...}; - // private: + // private: const Message* default_instance_; const uint32* offsets_; const uint32* has_bit_indices_; @@ -552,21 +552,18 @@ class GeneratedMessageReflection final : public Reflection { template <typename Type> const Type& GetRaw(const Message& message, - const FieldDescriptor* field) const; + const FieldDescriptor* field) const; template <typename Type> - inline Type* MutableRaw(Message* message, - const FieldDescriptor* field) const; + inline Type* MutableRaw(Message* message, const FieldDescriptor* field) const; template <typename Type> inline const Type& DefaultRaw(const FieldDescriptor* field) const; inline const uint32* GetHasBits(const Message& message) const; inline uint32* MutableHasBits(Message* message) const; - inline uint32 GetOneofCase( - const Message& message, - const OneofDescriptor* oneof_descriptor) const; + inline uint32 GetOneofCase(const Message& message, + const OneofDescriptor* oneof_descriptor) const; inline uint32* MutableOneofCase( - Message* message, - const OneofDescriptor* oneof_descriptor) const; + Message* message, const OneofDescriptor* oneof_descriptor) const; inline const ExtensionSet& GetExtensionSet(const Message& message) const; inline ExtensionSet* MutableExtensionSet(Message* message) const; inline Arena* GetArena(Message* message) const; @@ -574,29 +571,24 @@ class GeneratedMessageReflection final : public Reflection { inline const InternalMetadataWithArena& GetInternalMetadataWithArena( const Message& message) const; - inline InternalMetadataWithArena* - MutableInternalMetadataWithArena(Message* message) const; + inline InternalMetadataWithArena* MutableInternalMetadataWithArena( + Message* message) const; inline bool IsInlined(const FieldDescriptor* field) const; inline bool HasBit(const Message& message, const FieldDescriptor* field) const; - inline void SetBit(Message* message, - const FieldDescriptor* field) const; - inline void ClearBit(Message* message, - const FieldDescriptor* field) const; - inline void SwapBit(Message* message1, - Message* message2, + inline void SetBit(Message* message, const FieldDescriptor* field) const; + inline void ClearBit(Message* message, const FieldDescriptor* field) const; + inline void SwapBit(Message* message1, Message* message2, const FieldDescriptor* field) const; // This function only swaps the field. Should swap corresponding has_bit // before or after using this function. - void SwapField(Message* message1, - Message* message2, + void SwapField(Message* message1, Message* message2, const FieldDescriptor* field) const; - void SwapOneofField(Message* message1, - Message* message2, + void SwapOneofField(Message* message1, Message* message2, const OneofDescriptor* oneof_descriptor) const; inline bool HasOneofField(const Message& message, @@ -610,8 +602,8 @@ class GeneratedMessageReflection final : public Reflection { inline const Type& GetField(const Message& message, const FieldDescriptor* field) const; template <typename Type> - inline void SetField(Message* message, - const FieldDescriptor* field, const Type& value) const; + inline void SetField(Message* message, const FieldDescriptor* field, + const Type& value) const; template <typename Type> inline Type* MutableField(Message* message, const FieldDescriptor* field) const; @@ -624,42 +616,35 @@ class GeneratedMessageReflection final : public Reflection { const FieldDescriptor* field, int index) const; template <typename Type> - inline void SetRepeatedField(Message* message, - const FieldDescriptor* field, int index, - Type value) const; + inline void SetRepeatedField(Message* message, const FieldDescriptor* field, + int index, Type value) const; template <typename Type> inline Type* MutableRepeatedField(Message* message, const FieldDescriptor* field, int index) const; template <typename Type> - inline void AddField(Message* message, - const FieldDescriptor* field, const Type& value) const; + inline void AddField(Message* message, const FieldDescriptor* field, + const Type& value) const; template <typename Type> - inline Type* AddField(Message* message, - const FieldDescriptor* field) const; + inline Type* AddField(Message* message, const FieldDescriptor* field) const; int GetExtensionNumberOrDie(const Descriptor* type) const; // Internal versions of EnumValue API perform no checking. Called after checks // by public methods. - void SetEnumValueInternal(Message* message, - const FieldDescriptor* field, + void SetEnumValueInternal(Message* message, const FieldDescriptor* field, int value) const; void SetRepeatedEnumValueInternal(Message* message, - const FieldDescriptor* field, - int index, + const FieldDescriptor* field, int index, int value) const; - void AddEnumValueInternal(Message* message, - const FieldDescriptor* field, + void AddEnumValueInternal(Message* message, const FieldDescriptor* field, int value) const; - Message* UnsafeArenaReleaseMessage(Message* message, const FieldDescriptor* field, MessageFactory* factory = NULL) const; - void UnsafeArenaSetAllocatedMessage(Message* message, - Message* sub_message, + void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message, const FieldDescriptor* field) const; internal::MapFieldBase* MutableMapData( @@ -694,7 +679,6 @@ void PROTOBUF_EXPORT AssignDescriptors(AssignDescriptorsTable* table); struct PROTOBUF_EXPORT DescriptorTable { bool is_initialized; - InitFunc init_defaults; const char* descriptor; const char* filename; AssignDescriptorsTable* assign_descriptors_table; diff --git a/src/google/protobuf/generated_message_reflection_unittest.cc b/src/google/protobuf/generated_message_reflection_unittest.cc index 82f5dc120f..eea5da53d7 100644 --- a/src/google/protobuf/generated_message_reflection_unittest.cc +++ b/src/google/protobuf/generated_message_reflection_unittest.cc @@ -63,7 +63,7 @@ namespace { // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes. const FieldDescriptor* F(const std::string& name) { const FieldDescriptor* result = - unittest::TestAllTypes::descriptor()->FindFieldByName(name); + unittest::TestAllTypes::descriptor()->FindFieldByName(name); GOOGLE_CHECK(result != NULL); return result; } @@ -72,7 +72,7 @@ TEST(GeneratedMessageReflectionTest, Defaults) { // Check that all default values are set correctly in the initial message. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.ExpectClearViaReflection(message); @@ -96,7 +96,7 @@ TEST(GeneratedMessageReflectionTest, Accessors) { // values. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&message); TestUtil::ExpectAllFieldsSet(message); @@ -135,7 +135,7 @@ TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) { // embedded message does NOT return the default instance. unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); message.Clear(); @@ -152,7 +152,6 @@ TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) { &reflection->GetMessage(message, F("optional_import_message"))); } - TEST(GeneratedMessageReflectionTest, Swap) { unittest::TestAllTypes message1; unittest::TestAllTypes message2; @@ -321,7 +320,7 @@ TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) { TEST(GeneratedMessageReflectionTest, RemoveLast) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); @@ -333,7 +332,7 @@ TEST(GeneratedMessageReflectionTest, RemoveLast) { TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); TestUtil::SetAllExtensions(&message); @@ -378,21 +377,20 @@ TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) { // Now test that we actually release the right message. message.Clear(); TestUtil::SetAllExtensions(&message); - ASSERT_EQ(2, message.ExtensionSize( - unittest::repeated_foreign_message_extension)); - const protobuf_unittest::ForeignMessage* expected = message.MutableExtension( - unittest::repeated_foreign_message_extension, 1); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_message_extension)); + const protobuf_unittest::ForeignMessage* expected = + message.MutableExtension(unittest::repeated_foreign_message_extension, 1); std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast( &message, descriptor->file()->FindExtensionByName( "repeated_foreign_message_extension"))); EXPECT_EQ(expected, released.get()); - } TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); TestUtil::SetAllFields(&message); @@ -408,7 +406,7 @@ TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) { TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); TestUtil::SetAllExtensions(&message); @@ -426,7 +424,7 @@ TEST(GeneratedMessageReflectionTest, Extensions) { // values. unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&message); TestUtil::ExpectAllExtensionsSet(message); @@ -438,14 +436,14 @@ TEST(GeneratedMessageReflectionTest, Extensions) { TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) { const Reflection* reflection = - unittest::TestAllExtensions::default_instance().GetReflection(); + unittest::TestAllExtensions::default_instance().GetReflection(); const FieldDescriptor* extension1 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "optional_int32_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "optional_int32_extension"); const FieldDescriptor* extension2 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "repeated_string_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "repeated_string_extension"); EXPECT_EQ(extension1, reflection->FindKnownExtensionByNumber(extension1->number())); @@ -457,20 +455,21 @@ TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) { // Extensions of TestAllExtensions should not show up as extensions of // other types. - EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()-> - FindKnownExtensionByNumber(extension1->number()) == NULL); + EXPECT_TRUE(unittest::TestAllTypes::default_instance() + .GetReflection() + ->FindKnownExtensionByNumber(extension1->number()) == NULL); } TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) { const Reflection* reflection = - unittest::TestAllExtensions::default_instance().GetReflection(); + unittest::TestAllExtensions::default_instance().GetReflection(); const FieldDescriptor* extension1 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "optional_int32_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "optional_int32_extension"); const FieldDescriptor* extension2 = - unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( - "repeated_string_extension"); + unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName( + "repeated_string_extension"); EXPECT_EQ(extension1, reflection->FindKnownExtensionByName(extension1->full_name())); @@ -482,8 +481,9 @@ TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) { // Extensions of TestAllExtensions should not show up as extensions of // other types. - EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()-> - FindKnownExtensionByName(extension1->full_name()) == NULL); + EXPECT_TRUE(unittest::TestAllTypes::default_instance() + .GetReflection() + ->FindKnownExtensionByName(extension1->full_name()) == NULL); } TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) { @@ -491,7 +491,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) { unittest::TestAllTypes from_message2; unittest::TestAllTypes to_message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -526,7 +526,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) { unittest::TestAllTypes* to_message = Arena::CreateMessage<unittest::TestAllTypes>(&arena); TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -559,7 +559,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) { unittest::TestAllExtensions from_message2; unittest::TestAllExtensions to_message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -594,7 +594,7 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) { unittest::TestAllExtensions from_message1; unittest::TestAllExtensions from_message2; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); reflection_tester.SetAllFieldsViaReflection(&from_message1); reflection_tester.SetAllFieldsViaReflection(&from_message2); @@ -633,8 +633,8 @@ TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) { unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName( "bb"); - Message* nested = reflection->AddMessage( - &message, F("repeated_nested_message")); + Message* nested = + reflection->AddMessage(&message, F("repeated_nested_message")); nested_reflection->SetInt32(nested, nested_bb, 11); EXPECT_EQ(11, message.repeated_nested_message(0).bb()); @@ -668,7 +668,8 @@ TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) { unittest::TestAllTypes::NestedMessage* nested = new unittest::TestAllTypes::NestedMessage(); nested->set_bb(11); - reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested); + reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), + nested); EXPECT_EQ(1, message.repeated_nested_message_size()); EXPECT_EQ(11, message.repeated_nested_message(0).bb()); } @@ -689,62 +690,68 @@ TEST(GeneratedMessageReflectionTest, Oneof) { const Reflection* reflection = message.GetReflection(); // Check default values. - EXPECT_EQ(0, reflection->GetInt32( - message, descriptor->FindFieldByName("foo_int"))); - EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ( + 0, reflection->GetInt32(message, descriptor->FindFieldByName("foo_int"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_cord"))); + message, descriptor->FindFieldByName("foo_string"))); + EXPECT_EQ("", reflection->GetString(message, + descriptor->FindFieldByName("foo_cord"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_string_piece"))); + message, descriptor->FindFieldByName("foo_string_piece"))); EXPECT_EQ("", reflection->GetString( - message, descriptor->FindFieldByName("foo_bytes"))); - EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum( - message, descriptor->FindFieldByName("foo_enum"))->number()); + message, descriptor->FindFieldByName("foo_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::FOO, + reflection->GetEnum(message, descriptor->FindFieldByName("foo_enum")) + ->number()); EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(), &reflection->GetMessage( message, descriptor->FindFieldByName("foo_message"))); EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(), - &reflection->GetMessage( - message, descriptor->FindFieldByName("foogroup"))); + &reflection->GetMessage(message, + descriptor->FindFieldByName("foogroup"))); EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(), &reflection->GetMessage( message, descriptor->FindFieldByName("foo_lazy_message"))); - EXPECT_EQ(5, reflection->GetInt32( - message, descriptor->FindFieldByName("bar_int"))); + EXPECT_EQ( + 5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int"))); EXPECT_EQ("STRING", reflection->GetString( - message, descriptor->FindFieldByName("bar_string"))); + message, descriptor->FindFieldByName("bar_string"))); EXPECT_EQ("CORD", reflection->GetString( - message, descriptor->FindFieldByName("bar_cord"))); - EXPECT_EQ("SPIECE", reflection->GetString( - message, descriptor->FindFieldByName("bar_string_piece"))); + message, descriptor->FindFieldByName("bar_cord"))); + EXPECT_EQ("SPIECE", + reflection->GetString( + message, descriptor->FindFieldByName("bar_string_piece"))); EXPECT_EQ("BYTES", reflection->GetString( - message, descriptor->FindFieldByName("bar_bytes"))); - EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum( - message, descriptor->FindFieldByName("bar_enum"))->number()); + message, descriptor->FindFieldByName("bar_bytes"))); + EXPECT_EQ( + unittest::TestOneof2::BAR, + reflection->GetEnum(message, descriptor->FindFieldByName("bar_enum")) + ->number()); // Check Set functions. - reflection->SetInt32( - &message, descriptor->FindFieldByName("foo_int"), 123); - EXPECT_EQ(123, reflection->GetInt32( - message, descriptor->FindFieldByName("foo_int"))); - reflection->SetString( - &message, descriptor->FindFieldByName("foo_string"), "abc"); + reflection->SetInt32(&message, descriptor->FindFieldByName("foo_int"), 123); + EXPECT_EQ(123, reflection->GetInt32(message, + descriptor->FindFieldByName("foo_int"))); + reflection->SetString(&message, descriptor->FindFieldByName("foo_string"), + "abc"); EXPECT_EQ("abc", reflection->GetString( - message, descriptor->FindFieldByName("foo_string"))); - reflection->SetString( - &message, descriptor->FindFieldByName("foo_bytes"), "bytes"); + message, descriptor->FindFieldByName("foo_string"))); + reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"), + "bytes"); EXPECT_EQ("bytes", reflection->GetString( - message, descriptor->FindFieldByName("foo_bytes"))); - reflection->SetString( - &message, descriptor->FindFieldByName("bar_cord"), "change_cord"); - EXPECT_EQ("change_cord", reflection->GetString( - message, descriptor->FindFieldByName("bar_cord"))); - reflection->SetString( - &message, descriptor->FindFieldByName("bar_string_piece"), - "change_spiece"); - EXPECT_EQ("change_spiece", reflection->GetString( - message, descriptor->FindFieldByName("bar_string_piece"))); + message, descriptor->FindFieldByName("foo_bytes"))); + reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"), + "change_cord"); + EXPECT_EQ( + "change_cord", + reflection->GetString(message, descriptor->FindFieldByName("bar_cord"))); + reflection->SetString(&message, + descriptor->FindFieldByName("bar_string_piece"), + "change_spiece"); + EXPECT_EQ("change_spiece", + reflection->GetString( + message, descriptor->FindFieldByName("bar_string_piece"))); message.clear_foo(); message.clear_bar(); @@ -769,8 +776,8 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) { TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message1, &to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1, + &to_message); const Message& sub_message = reflection->GetMessage( to_message, descriptor->FindFieldByName("foo_lazy_message")); released = reflection->ReleaseMessage( @@ -781,12 +788,12 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) { TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2); - reflection->MutableMessage( - &from_message2, descriptor->FindFieldByName("foo_message")); + reflection->MutableMessage(&from_message2, + descriptor->FindFieldByName("foo_message")); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message2, &to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2, + &to_message); const Message& sub_message2 = reflection->GetMessage( to_message, descriptor->FindFieldByName("foo_message")); @@ -817,8 +824,8 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message1, to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1, + to_message); const Message& sub_message = reflection->GetMessage( *to_message, descriptor->FindFieldByName("foo_lazy_message")); released = reflection->ReleaseMessage( @@ -831,12 +838,12 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2); - reflection->MutableMessage( - &from_message2, descriptor->FindFieldByName("foo_message")); + reflection->MutableMessage(&from_message2, + descriptor->FindFieldByName("foo_message")); TestUtil::ReflectionTester:: - SetAllocatedOptionalMessageFieldsToMessageViaReflection( - &from_message2, to_message); + SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2, + to_message); const Message& sub_message2 = reflection->GetMessage( *to_message, descriptor->FindFieldByName("foo_message")); @@ -849,11 +856,10 @@ TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) { delete released; } - TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) { unittest::TestAllTypes message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllTypes::descriptor()); + unittest::TestAllTypes::descriptor()); // When nothing is set, we expect all released messages to be NULL. reflection_tester.ExpectMessagesReleasedViaReflection( @@ -879,7 +885,7 @@ TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) { TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) { unittest::TestAllExtensions message; TestUtil::ReflectionTester reflection_tester( - unittest::TestAllExtensions::descriptor()); + unittest::TestAllExtensions::descriptor()); // When nothing is set, we expect all released messages to be NULL. reflection_tester.ExpectMessagesReleasedViaReflection( @@ -1001,39 +1007,41 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { // Testing every single failure mode would be too much work. Let's just // check a few. EXPECT_DEATH( - reflection->GetInt32( - message, descriptor->FindFieldByName("optional_int64")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest\\.TestAllTypes\n" - " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n" - " Problem : Field is not the right type for this message:\n" - " Expected : CPPTYPE_INT32\n" - " Field type: CPPTYPE_INT64"); - EXPECT_DEATH( - reflection->GetInt32( - message, descriptor->FindFieldByName("repeated_int32")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.TestAllTypes.repeated_int32\n" - " Problem : Field is repeated; the method requires a singular field."); + reflection->GetInt32(message, + descriptor->FindFieldByName("optional_int64")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest\\.TestAllTypes\n" + " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n" + " Problem : Field is not the right type for this message:\n" + " Expected : CPPTYPE_INT32\n" + " Field type: CPPTYPE_INT64"); + EXPECT_DEATH(reflection->GetInt32( + message, descriptor->FindFieldByName("repeated_int32")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.TestAllTypes.repeated_int32\n" + " Problem : Field is repeated; the method requires a " + "singular field."); EXPECT_DEATH( - reflection->GetInt32( - message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::GetInt32\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.ForeignMessage.c\n" - " Problem : Field does not match message type."); + reflection->GetInt32( + message, + unittest::ForeignMessage::descriptor()->FindFieldByName("c")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::GetInt32\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.ForeignMessage.c\n" + " Problem : Field does not match message type."); EXPECT_DEATH( - reflection->HasField( - message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")), - "Protocol Buffer reflection usage error:\n" - " Method : google::protobuf::Reflection::HasField\n" - " Message type: protobuf_unittest.TestAllTypes\n" - " Field : protobuf_unittest.ForeignMessage.c\n" - " Problem : Field does not match message type."); + reflection->HasField( + message, + unittest::ForeignMessage::descriptor()->FindFieldByName("c")), + "Protocol Buffer reflection usage error:\n" + " Method : google::protobuf::Reflection::HasField\n" + " Message type: protobuf_unittest.TestAllTypes\n" + " Field : protobuf_unittest.ForeignMessage.c\n" + " Problem : Field does not match message type."); #undef f } diff --git a/src/google/protobuf/generated_message_table_driven.cc b/src/google/protobuf/generated_message_table_driven.cc index dc2e69315e..fb82a8ec37 100644 --- a/src/google/protobuf/generated_message_table_driven.cc +++ b/src/google/protobuf/generated_message_table_driven.cc @@ -57,32 +57,30 @@ struct UnknownFieldHandler { static constexpr bool IsLite() { return false; } static bool Skip(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, - int tag) { + io::CodedInputStream* input, int tag) { GOOGLE_DCHECK(table.unknown_field_set); return WireFormat::SkipField(input, tag, - MutableUnknownFields(msg, table.arena_offset)); + MutableUnknownFields(msg, table.arena_offset)); } - static void Varint(MessageLite* msg, const ParseTable& table, - int tag, int value) { + static void Varint(MessageLite* msg, const ParseTable& table, int tag, + int value) { GOOGLE_DCHECK(table.unknown_field_set); - MutableUnknownFields(msg, table.arena_offset)->AddVarint( - WireFormatLite::GetTagFieldNumber(tag), value); + MutableUnknownFields(msg, table.arena_offset) + ->AddVarint(WireFormatLite::GetTagFieldNumber(tag), value); } - static bool ParseExtension( - MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { + static bool ParseExtension(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input, int tag) { ExtensionSet* extensions = GetExtensionSet(msg, table.extension_offset); if (extensions == NULL) { return false; } - const Message* prototype = down_cast<const Message*>( - table.default_instance()); + const Message* prototype = + down_cast<const Message*>(table.default_instance()); GOOGLE_DCHECK(prototype != NULL); GOOGLE_DCHECK(table.unknown_field_set); @@ -95,8 +93,8 @@ struct UnknownFieldHandler { } // namespace -bool MergePartialFromCodedStream( - MessageLite* msg, const ParseTable& table, io::CodedInputStream* input) { +bool MergePartialFromCodedStream(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input) { return MergePartialFromCodedStreamImpl<UnknownFieldHandler, InternalMetadataWithArena>(msg, table, input); diff --git a/src/google/protobuf/generated_message_table_driven.h b/src/google/protobuf/generated_message_table_driven.h index 8564f5e30f..1394ab1440 100644 --- a/src/google/protobuf/generated_message_table_driven.h +++ b/src/google/protobuf/generated_message_table_driven.h @@ -114,7 +114,6 @@ struct PROTOBUF_EXPORT FieldMetadata { static int CalculateType(int fundamental_type, FieldTypeClass type_class); }; - // TODO(ckennelly): Add a static assertion to ensure that these masks do not // conflict with wiretypes. @@ -219,7 +218,7 @@ static_assert(std::is_pod<ParseTable>::value, ""); bool MergePartialFromCodedStream(MessageLite* msg, const ParseTable& table, io::CodedInputStream* input); bool MergePartialFromCodedStreamLite(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input); + io::CodedInputStream* input); template <typename Entry> bool ParseMap(io::CodedInputStream* input, void* map_field) { diff --git a/src/google/protobuf/generated_message_table_driven_lite.cc b/src/google/protobuf/generated_message_table_driven_lite.cc index 05d4f295c5..82de6bc9b0 100644 --- a/src/google/protobuf/generated_message_table_driven_lite.cc +++ b/src/google/protobuf/generated_message_table_driven_lite.cc @@ -54,8 +54,7 @@ struct UnknownFieldHandlerLite { static constexpr bool IsLite() { return true; } static bool Skip(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, - int tag) { + io::CodedInputStream* input, int tag) { GOOGLE_DCHECK(!table.unknown_field_set); io::StringOutputStream unknown_fields_string( MutableUnknownFields(msg, table.arena_offset)); @@ -65,8 +64,8 @@ struct UnknownFieldHandlerLite { &unknown_fields_stream); } - static void Varint(MessageLite* msg, const ParseTable& table, - int tag, int value) { + static void Varint(MessageLite* msg, const ParseTable& table, int tag, + int value) { GOOGLE_DCHECK(!table.unknown_field_set); io::StringOutputStream unknown_fields_string( @@ -76,9 +75,8 @@ struct UnknownFieldHandlerLite { unknown_fields_stream.WriteVarint32(value); } - static bool ParseExtension( - MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { + static bool ParseExtension(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input, int tag) { ExtensionSet* extensions = GetExtensionSet(msg, table.extension_offset); if (extensions == NULL) { return false; @@ -90,15 +88,15 @@ struct UnknownFieldHandlerLite { io::StringOutputStream unknown_fields_string( MutableUnknownFields(msg, table.arena_offset)); io::CodedOutputStream unknown_fields_stream(&unknown_fields_string, false); - return extensions->ParseField( - tag, input, prototype, &unknown_fields_stream); + return extensions->ParseField(tag, input, prototype, + &unknown_fields_stream); } }; } // namespace -bool MergePartialFromCodedStreamLite( - MessageLite* msg, const ParseTable& table, io::CodedInputStream* input) { +bool MergePartialFromCodedStreamLite(MessageLite* msg, const ParseTable& table, + io::CodedInputStream* input) { return MergePartialFromCodedStreamImpl<UnknownFieldHandlerLite, InternalMetadataWithArenaLite>( msg, table, input); diff --git a/src/google/protobuf/generated_message_table_driven_lite.h b/src/google/protobuf/generated_message_table_driven_lite.h index c8e24c4255..b36e51ba22 100644 --- a/src/google/protobuf/generated_message_table_driven_lite.h +++ b/src/google/protobuf/generated_message_table_driven_lite.h @@ -102,7 +102,7 @@ inline ExtensionSet* GetExtensionSet(MessageLite* msg, int64 extension_offset) { template <typename Type> inline Type* AddField(MessageLite* msg, int64 offset) { static_assert(std::is_pod<Type>::value || - std::is_same<Type, InlinedStringField>::value, + std::is_same<Type, InlinedStringField>::value, "Do not assign"); RepeatedField<Type>* repeated = Raw<RepeatedField<Type>>(msg, offset); @@ -157,7 +157,7 @@ inline void SetOneofField(MessageLite* msg, uint32* oneof_case, // Clears a oneof field. The field argument should correspond to the particular // field that is currently set in the oneof. inline void ClearOneofField(const ParseTableField& field, Arena* arena, - MessageLite* msg) { + MessageLite* msg) { switch (field.processing_type & kTypeMask) { case WireFormatLite::TYPE_MESSAGE: if (arena == NULL) { @@ -241,8 +241,8 @@ static inline bool HandleString(io::CodedInputStream* input, MessageLite* msg, switch (cardinality) { case Cardinality_SINGULAR: // TODO(ckennelly): Is this optimal? - s = MutableField<InlinedStringField>( - msg, has_bits, has_bit_index, offset); + s = MutableField<InlinedStringField>(msg, has_bits, has_bit_index, + offset); break; case Cardinality_REPEATED: s = AddField<InlinedStringField>(msg, offset); @@ -543,8 +543,7 @@ bool MergePartialFromCodedStreamInlined(MessageLite* msg, { Arena* const arena = GetArena<InternalMetadata>(msg, table.arena_offset); - const void* default_ptr = - table.aux[field_number].strings.default_ptr; + const void* default_ptr = table.aux[field_number].strings.default_ptr; if (PROTOBUF_PREDICT_FALSE( (!HandleString<UnknownFieldHandler, Cardinality_REPEATED, @@ -782,8 +781,8 @@ bool MergePartialFromCodedStreamInlined(MessageLite* msg, // TODO(ckennelly): Use a computed goto on GCC/LLVM. // // Mask out kRepeatedMask bit, allowing the jump table to be smaller. - switch (static_cast<WireFormatLite::FieldType>( - processing_type ^ kRepeatedMask)) { + switch (static_cast<WireFormatLite::FieldType>(processing_type ^ + kRepeatedMask)) { #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ case WireFormatLite::TYPE_##TYPE: { \ RepeatedField<CPPTYPE>* values = Raw<RepeatedField<CPPTYPE>>(msg, offset); \ diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index 26a422f4ec..363eca42bc 100644 --- a/src/google/protobuf/generated_message_util.cc +++ b/src/google/protobuf/generated_message_util.cc @@ -40,7 +40,7 @@ // We're only using this as a standard way for getting the thread id. // We're not using any thread functionality. #include <thread> // NOLINT -#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP +#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP #include <vector> @@ -497,8 +497,8 @@ struct RepeatedFieldHelper<WireFormatLite::TYPE_MESSAGE> { for (int i = 0; i < AccessorHelper::Size(array); i++) { WriteTagTo(md.tag, output); SerializeMessageTo( - static_cast<const MessageLite*>(AccessorHelper::Get(array, i)), md.ptr, - output); + static_cast<const MessageLite*>(AccessorHelper::Get(array, i)), + md.ptr, output); } } }; @@ -663,8 +663,8 @@ void SerializeInternal(const uint8* base, case FieldMetadata::kSpecial: func = reinterpret_cast<SpecialSerializer>( const_cast<void*>(field_metadata.ptr)); - func (base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, output); + func(base, field_metadata.offset, field_metadata.tag, + field_metadata.has_offset, output); break; default: // __builtin_unreachable() @@ -708,10 +708,10 @@ uint8* SerializeInternalToArray(const uint8* base, io::ArrayOutputStream array_stream(array_output.ptr, INT_MAX); io::CodedOutputStream output(&array_stream); output.SetSerializationDeterministic(is_deterministic); - func = reinterpret_cast<SpecialSerializer>( + func = reinterpret_cast<SpecialSerializer>( const_cast<void*>(field_metadata.ptr)); - func (base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, &output); + func(base, field_metadata.offset, field_metadata.tag, + field_metadata.has_offset, &output); array_output.ptr += output.ByteCount(); } break; default: @@ -768,7 +768,8 @@ namespace { void InitSCC_DFS(SCCInfoBase* scc) { if (scc->visit_status.load(std::memory_order_relaxed) != - SCCInfoBase::kUninitialized) return; + SCCInfoBase::kUninitialized) + return; scc->visit_status.store(SCCInfoBase::kRunning, std::memory_order_relaxed); // Each base is followed by an array of pointers to deps auto deps = reinterpret_cast<SCCInfoBase* const*>(scc + 1); diff --git a/src/google/protobuf/generated_message_util.h b/src/google/protobuf/generated_message_util.h index d0c0a8ab7b..bf0e5b9b0f 100644 --- a/src/google/protobuf/generated_message_util.h +++ b/src/google/protobuf/generated_message_util.h @@ -65,15 +65,17 @@ namespace protobuf { class Arena; -namespace io { class CodedInputStream; } +namespace io { +class CodedInputStream; +} namespace internal { -template<typename To, typename From> +template <typename To, typename From> inline To DownCast(From* f) { return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f); } -template<typename To, typename From> +template <typename To, typename From> inline To DownCast(From& f) { return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f); } @@ -93,8 +95,9 @@ PROTOBUF_EXPORT inline const std::string& GetEmptyString() { // helper here to keep the protobuf compiler from ever having to emit loops in // IsInitialized() methods. We want the C++ compiler to inline this or not // as it sees fit. -template <class Type> bool AllAreInitialized(const Type& t) { - for (int i = t.size(); --i >= 0; ) { +template <class Type> +bool AllAreInitialized(const Type& t) { + for (int i = t.size(); --i >= 0;) { if (!t.Get(i).IsInitialized()) return false; } return true; @@ -167,6 +170,7 @@ class PROTOBUF_EXPORT CachedSize { public: int Get() const { return size_.load(std::memory_order_relaxed); } void Set(int size) { size_.store(size, std::memory_order_relaxed); } + private: std::atomic<int> size_{0}; }; diff --git a/src/google/protobuf/has_bits.h b/src/google/protobuf/has_bits.h index ff0e176c3f..11afde7193 100644 --- a/src/google/protobuf/has_bits.h +++ b/src/google/protobuf/has_bits.h @@ -44,7 +44,7 @@ namespace google { namespace protobuf { namespace internal { -template<size_t doublewords> +template <size_t doublewords> class HasBits { public: HasBits() PROTOBUF_ALWAYS_INLINE { Clear(); } @@ -69,6 +69,10 @@ class HasBits { return !(*this == rhs); } + void Or(const HasBits<doublewords>& rhs) { + for (int i = 0; i < doublewords; i++) has_bits_[i] |= rhs[i]; + } + bool empty() const; private: diff --git a/src/google/protobuf/io/coded_stream.cc b/src/google/protobuf/io/coded_stream.cc index ff08709ac2..6aaaa0b4c6 100644 --- a/src/google/protobuf/io/coded_stream.cc +++ b/src/google/protobuf/io/coded_stream.cc @@ -38,14 +38,14 @@ // will not cross the end of the buffer, since we can avoid a lot // of branching in this case. -#include <google/protobuf/io/coded_stream_inl.h> +#include <limits.h> #include <algorithm> #include <utility> -#include <limits.h> -#include <google/protobuf/io/zero_copy_stream.h> -#include <google/protobuf/arena.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> +#include <google/protobuf/io/coded_stream_inl.h> +#include <google/protobuf/io/zero_copy_stream.h> +#include <google/protobuf/arena.h> #include <google/protobuf/stubs/stl_util.h> @@ -61,8 +61,8 @@ static const int kMaxVarintBytes = 10; static const int kMaxVarint32Bytes = 5; -inline bool NextNonEmpty(ZeroCopyInputStream* input, - const void** data, int* size) { +inline bool NextNonEmpty(ZeroCopyInputStream* input, const void** data, + int* size) { bool success; do { success = input->Next(data, size); @@ -124,8 +124,8 @@ CodedInputStream::Limit CodedInputStream::PushLimit(int byte_limit) { // and overflow. Also check that the new requested limit is before the // previous limit; otherwise we continue to enforce the previous limit. if (PROTOBUF_PREDICT_TRUE(byte_limit >= 0 && - byte_limit <= INT_MAX - current_position && - byte_limit < current_limit_ - current_position)) { + byte_limit <= INT_MAX - current_position && + byte_limit < current_limit_ - current_position)) { current_limit_ = current_position + byte_limit; RecomputeBufferLimits(); } @@ -190,7 +190,8 @@ int CodedInputStream::BytesUntilTotalBytesLimit() const { void CodedInputStream::PrintTotalBytesLimitError() { GOOGLE_LOG(ERROR) << "A protocol message was rejected because it was too " - "big (more than " << total_bytes_limit_ + "big (more than " + << total_bytes_limit_ << " bytes). To increase the limit (or to disable these " "warnings), see CodedInputStream::SetTotalBytesLimit() " "in net/proto2/io/public/coded_stream.h."; @@ -333,9 +334,9 @@ const uint8* DecodeVarint64KnownSize(const uint8* buffer, uint64* value) { // part is buffer + (number of bytes read). This function is always inlined, // so returning a pair is costless. PROTOBUF_ALWAYS_INLINE -::std::pair<bool, const uint8*> ReadVarint32FromArray( - uint32 first_byte, const uint8* buffer, - uint32* value); +::std::pair<bool, const uint8*> ReadVarint32FromArray(uint32 first_byte, + const uint8* buffer, + uint32* value); inline ::std::pair<bool, const uint8*> ReadVarint32FromArray( uint32 first_byte, const uint8* buffer, uint32* value) { // Fast path: We have enough bytes left in the buffer to guarantee that @@ -346,26 +347,35 @@ inline ::std::pair<bool, const uint8*> ReadVarint32FromArray( uint32 b; uint32 result = first_byte - 0x80; ++ptr; // We just processed the first byte. Move on to the second. - b = *(ptr++); result += b << 7; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 7; + if (!(b & 0x80)) goto done; result -= 0x80 << 7; - b = *(ptr++); result += b << 14; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 14; + if (!(b & 0x80)) goto done; result -= 0x80 << 14; - b = *(ptr++); result += b << 21; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 21; + if (!(b & 0x80)) goto done; result -= 0x80 << 21; - b = *(ptr++); result += b << 28; if (!(b & 0x80)) goto done; + b = *(ptr++); + result += b << 28; + if (!(b & 0x80)) goto done; // "result -= 0x80 << 28" is irrevelant. // If the input is larger than 32 bits, we still need to read it all // and discard the high-order bits. for (int i = 0; i < kMaxVarintBytes - kMaxVarint32Bytes; i++) { - b = *(ptr++); if (!(b & 0x80)) goto done; + b = *(ptr++); + if (!(b & 0x80)) goto done; } // We have overrun the maximum size of a varint (10 bytes). Assume // the data is corrupt. return std::make_pair(false, ptr); - done: +done: *value = result; return std::make_pair(true, ptr); } @@ -424,7 +434,7 @@ int64 CodedInputStream::ReadVarint32Fallback(uint32 first_byte_or_zero) { << "Caller should provide us with *buffer_ when buffer is non-empty"; uint32 temp; ::std::pair<bool, const uint8*> p = - ReadVarint32FromArray(first_byte_or_zero, buffer_, &temp); + ReadVarint32FromArray(first_byte_or_zero, buffer_, &temp); if (!p.first) return -1; buffer_ = p.second; return temp; @@ -635,13 +645,13 @@ CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* output) CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* output, bool do_eager_refresh) - : output_(output), - buffer_(NULL), - buffer_size_(0), - total_bytes_(0), - had_error_(false), - aliasing_enabled_(false), - is_serialization_deterministic_(IsDefaultSerializationDeterministic()) { + : output_(output), + buffer_(NULL), + buffer_size_(0), + total_bytes_(0), + had_error_(false), + aliasing_enabled_(false), + is_serialization_deterministic_(IsDefaultSerializationDeterministic()) { if (do_eager_refresh) { // Eagerly Refresh() so buffer space is immediately available. Refresh(); @@ -652,9 +662,7 @@ CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* output, } } -CodedOutputStream::~CodedOutputStream() { - Trim(); -} +CodedOutputStream::~CodedOutputStream() { Trim(); } void CodedOutputStream::Trim() { if (buffer_size_ > 0) { @@ -697,8 +705,8 @@ void CodedOutputStream::WriteRaw(const void* data, int size) { Advance(size); } -uint8* CodedOutputStream::WriteRawToArray( - const void* data, int size, uint8* target) { +uint8* CodedOutputStream::WriteRawToArray(const void* data, int size, + uint8* target) { memcpy(target, data, size); return target + size; } @@ -706,7 +714,7 @@ uint8* CodedOutputStream::WriteRawToArray( void CodedOutputStream::WriteAliasedRaw(const void* data, int size) { if (size < buffer_size_ - ) { + ) { WriteRaw(data, size); } else { Trim(); diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index 2f1f270a17..5d065fc25e 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -115,22 +115,22 @@ #include <string> #include <utility> #ifdef _MSC_VER - // Assuming windows is always little-endian. - #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) - #define PROTOBUF_LITTLE_ENDIAN 1 - #endif - #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) - // If MSVC has "/RTCc" set, it will complain about truncating casts at - // runtime. This file contains some intentional truncating casts. - #pragma runtime_checks("c", off) - #endif +// Assuming windows is always little-endian. +#if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) +#define PROTOBUF_LITTLE_ENDIAN 1 +#endif +#if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) +// If MSVC has "/RTCc" set, it will complain about truncating casts at +// runtime. This file contains some intentional truncating casts. +#pragma runtime_checks("c", off) +#endif #else - #include <sys/param.h> // __BYTE_ORDER - #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ - (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ - !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) - #define PROTOBUF_LITTLE_ENDIAN 1 - #endif +#include <sys/param.h> // __BYTE_ORDER +#if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ + (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ + !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) +#define PROTOBUF_LITTLE_ENDIAN 1 +#endif #endif #include <google/protobuf/stubs/common.h> #include <google/protobuf/port.h> @@ -158,8 +158,8 @@ class CodedInputStream; class CodedOutputStream; // Defined in other files. -class ZeroCopyInputStream; // zero_copy_stream.h -class ZeroCopyOutputStream; // zero_copy_stream.h +class ZeroCopyInputStream; // zero_copy_stream.h +class ZeroCopyOutputStream; // zero_copy_stream.h // Class which reads and decodes binary data which is composed of varint- // encoded integers and fixed-width pieces. Wraps a ZeroCopyInputStream. @@ -234,10 +234,10 @@ class PROTOBUF_EXPORT CodedInputStream { // responsible for ensuring that the buffer has sufficient space. // Read a 32-bit little-endian integer. static const uint8* ReadLittleEndian32FromArray(const uint8* buffer, - uint32* value); + uint32* value); // Read a 64-bit little-endian integer. static const uint8* ReadLittleEndian64FromArray(const uint8* buffer, - uint64* value); + uint64* value); // Read an unsigned integer with Varint encoding, truncating to 32 bits. // Reading a 32-bit value is equivalent to reading a 64-bit one and casting @@ -537,7 +537,7 @@ class PROTOBUF_EXPORT CodedInputStream { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream); const uint8* buffer_; - const uint8* buffer_end_; // pointer to the end of the buffer. + const uint8* buffer_end_; // pointer to the end of the buffer. ZeroCopyInputStream* input_; int total_bytes_read_; // total bytes read from input_, including // the current buffer @@ -547,7 +547,7 @@ class PROTOBUF_EXPORT CodedInputStream { int overflow_bytes_; // LastTagWas() stuff. - uint32 last_tag_; // result of last ReadTag() or ReadTagWithCutoff(). + uint32 last_tag_; // result of last ReadTag() or ReadTagWithCutoff(). // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly // at EOF, or by ExpectAtEnd() when it returns true. This happens when we @@ -558,7 +558,7 @@ class PROTOBUF_EXPORT CodedInputStream { bool aliasing_enabled_; // Limits - Limit current_limit_; // if position = -1, no limit is applied + Limit current_limit_; // if position = -1, no limit is applied // For simplicity, if the current buffer crosses a limit (either a normal // limit created by PushLimit() or the total bytes limit), buffer_size_ @@ -817,12 +817,8 @@ class PROTOBUF_EXPORT CodedOutputStream { (Value < (1 << 7)) ? 1 : (Value < (1 << 14)) - ? 2 - : (Value < (1 << 21)) - ? 3 - : (Value < (1 << 28)) - ? 4 - : 5; + ? 2 + : (Value < (1 << 21)) ? 3 : (Value < (1 << 28)) ? 4 : 5; }; // Returns the total number of bytes written since this object was created. @@ -878,8 +874,8 @@ class PROTOBUF_EXPORT CodedOutputStream { ZeroCopyOutputStream* output_; uint8* buffer_; int buffer_size_; - int total_bytes_; // Sum of sizes of all buffers seen so far. - bool had_error_; // Whether an error occurred during output. + int total_bytes_; // Sum of sizes of all buffers seen so far. + bool had_error_; // Whether an error occurred during output. bool aliasing_enabled_; // See EnableAliasing(). bool is_serialization_deterministic_; static std::atomic<bool> default_serialization_deterministic_; @@ -958,14 +954,13 @@ inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) { // static inline const uint8* CodedInputStream::ReadLittleEndian32FromArray( - const uint8* buffer, - uint32* value) { + const uint8* buffer, uint32* value) { #if defined(PROTOBUF_LITTLE_ENDIAN) memcpy(value, buffer, sizeof(*value)); return buffer + sizeof(*value); #else - *value = (static_cast<uint32>(buffer[0]) ) | - (static_cast<uint32>(buffer[1]) << 8) | + *value = (static_cast<uint32>(buffer[0])) | + (static_cast<uint32>(buffer[1]) << 8) | (static_cast<uint32>(buffer[2]) << 16) | (static_cast<uint32>(buffer[3]) << 24); return buffer + sizeof(*value); @@ -973,22 +968,20 @@ inline const uint8* CodedInputStream::ReadLittleEndian32FromArray( } // static inline const uint8* CodedInputStream::ReadLittleEndian64FromArray( - const uint8* buffer, - uint64* value) { + const uint8* buffer, uint64* value) { #if defined(PROTOBUF_LITTLE_ENDIAN) memcpy(value, buffer, sizeof(*value)); return buffer + sizeof(*value); #else - uint32 part0 = (static_cast<uint32>(buffer[0]) ) | - (static_cast<uint32>(buffer[1]) << 8) | + uint32 part0 = (static_cast<uint32>(buffer[0])) | + (static_cast<uint32>(buffer[1]) << 8) | (static_cast<uint32>(buffer[2]) << 16) | (static_cast<uint32>(buffer[3]) << 24); - uint32 part1 = (static_cast<uint32>(buffer[4]) ) | - (static_cast<uint32>(buffer[5]) << 8) | + uint32 part1 = (static_cast<uint32>(buffer[4])) | + (static_cast<uint32>(buffer[5]) << 8) | (static_cast<uint32>(buffer[6]) << 16) | (static_cast<uint32>(buffer[7]) << 24); - *value = static_cast<uint64>(part0) | - (static_cast<uint64>(part1) << 32); + *value = static_cast<uint64>(part0) | (static_cast<uint64>(part1) << 32); return buffer + sizeof(*value); #endif } @@ -1082,7 +1075,8 @@ inline bool CodedInputStream::ConsumedEntireMessage() { inline bool CodedInputStream::ExpectTag(uint32 expected) { if (expected < (1 << 7)) { - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) { + if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && + buffer_[0] == expected) { Advance(1); return true; } else { @@ -1103,8 +1097,8 @@ inline bool CodedInputStream::ExpectTag(uint32 expected) { } } -inline const uint8* CodedInputStream::ExpectTagFromArray( - const uint8* buffer, uint32 expected) { +inline const uint8* CodedInputStream::ExpectTagFromArray(const uint8* buffer, + uint32 expected) { if (expected < (1 << 7)) { if (buffer[0] == expected) { return buffer + 1; @@ -1128,9 +1122,8 @@ inline bool CodedInputStream::ExpectAtEnd() { // If we are at a limit we know no more bytes can be read. Otherwise, it's // hard to say without calling Refresh(), and we'd rather not do that. - if (buffer_ == buffer_end_ && - ((buffer_size_after_limit_ != 0) || - (total_bytes_read_ == current_limit_))) { + if (buffer_ == buffer_end_ && ((buffer_size_after_limit_ != 0) || + (total_bytes_read_ == current_limit_))) { last_tag_ = 0; // Pretend we called ReadTag()... legitimate_message_end_ = true; // ... and it hit EOF. return true; @@ -1190,7 +1183,7 @@ inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value, memcpy(target, &value, sizeof(value)); #else target[0] = static_cast<uint8>(value); - target[1] = static_cast<uint8>(value >> 8); + target[1] = static_cast<uint8>(value >> 8); target[2] = static_cast<uint8>(value >> 16); target[3] = static_cast<uint8>(value >> 24); #endif @@ -1206,11 +1199,11 @@ inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value, uint32 part1 = static_cast<uint32>(value >> 32); target[0] = static_cast<uint8>(part0); - target[1] = static_cast<uint8>(part0 >> 8); + target[1] = static_cast<uint8>(part0 >> 8); target[2] = static_cast<uint8>(part0 >> 16); target[3] = static_cast<uint8>(part0 >> 24); target[4] = static_cast<uint8>(part1); - target[5] = static_cast<uint8>(part1 >> 8); + target[5] = static_cast<uint8>(part1 >> 8); target[6] = static_cast<uint8>(part1 >> 16); target[7] = static_cast<uint8>(part1 >> 24); #endif @@ -1243,12 +1236,9 @@ inline void CodedOutputStream::WriteVarint64(uint64 value) { } } -inline void CodedOutputStream::WriteTag(uint32 value) { - WriteVarint32(value); -} +inline void CodedOutputStream::WriteTag(uint32 value) { WriteVarint32(value); } -inline uint8* CodedOutputStream::WriteTagToArray( - uint32 value, uint8* target) { +inline uint8* CodedOutputStream::WriteTagToArray(uint32 value, uint8* target) { return WriteVarint32ToArray(value, target); } @@ -1274,7 +1264,7 @@ inline size_t CodedOutputStream::VarintSize64(uint64 value) { inline size_t CodedOutputStream::VarintSize32SignExtended(int32 value) { if (value < 0) { - return 10; // TODO(kenton): Make this a symbolic constant. + return 10; // TODO(kenton): Make this a symbolic constant. } else { return VarintSize32(static_cast<uint32>(value)); } @@ -1284,8 +1274,8 @@ inline void CodedOutputStream::WriteString(const std::string& str) { WriteRaw(str.data(), static_cast<int>(str.size())); } -inline void CodedOutputStream::WriteRawMaybeAliased( - const void* data, int size) { +inline void CodedOutputStream::WriteRawMaybeAliased(const void* data, + int size) { if (aliasing_enabled_) { WriteAliasedRaw(data, size); } else { @@ -1302,9 +1292,7 @@ inline int CodedOutputStream::ByteCount() const { return total_bytes_ - buffer_size_; } -inline void CodedInputStream::Advance(int amount) { - buffer_ += amount; -} +inline void CodedInputStream::Advance(int amount) { buffer_ += amount; } inline void CodedOutputStream::Advance(int amount) { buffer_ += amount; @@ -1349,48 +1337,46 @@ inline int CodedInputStream::BufferSize() const { } inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input) - : buffer_(NULL), - buffer_end_(NULL), - input_(input), - total_bytes_read_(0), - overflow_bytes_(0), - last_tag_(0), - legitimate_message_end_(false), - aliasing_enabled_(false), - current_limit_(kint32max), - buffer_size_after_limit_(0), - total_bytes_limit_(kDefaultTotalBytesLimit), - recursion_budget_(default_recursion_limit_), - recursion_limit_(default_recursion_limit_), - extension_pool_(NULL), - extension_factory_(NULL) { + : buffer_(NULL), + buffer_end_(NULL), + input_(input), + total_bytes_read_(0), + overflow_bytes_(0), + last_tag_(0), + legitimate_message_end_(false), + aliasing_enabled_(false), + current_limit_(kint32max), + buffer_size_after_limit_(0), + total_bytes_limit_(kDefaultTotalBytesLimit), + recursion_budget_(default_recursion_limit_), + recursion_limit_(default_recursion_limit_), + extension_pool_(NULL), + extension_factory_(NULL) { // Eagerly Refresh() so buffer space is immediately available. Refresh(); } inline CodedInputStream::CodedInputStream(const uint8* buffer, int size) - : buffer_(buffer), - buffer_end_(buffer + size), - input_(NULL), - total_bytes_read_(size), - overflow_bytes_(0), - last_tag_(0), - legitimate_message_end_(false), - aliasing_enabled_(false), - current_limit_(size), - buffer_size_after_limit_(0), - total_bytes_limit_(kDefaultTotalBytesLimit), - recursion_budget_(default_recursion_limit_), - recursion_limit_(default_recursion_limit_), - extension_pool_(NULL), - extension_factory_(NULL) { + : buffer_(buffer), + buffer_end_(buffer + size), + input_(NULL), + total_bytes_read_(size), + overflow_bytes_(0), + last_tag_(0), + legitimate_message_end_(false), + aliasing_enabled_(false), + current_limit_(size), + buffer_size_after_limit_(0), + total_bytes_limit_(kDefaultTotalBytesLimit), + recursion_budget_(default_recursion_limit_), + recursion_limit_(default_recursion_limit_), + extension_pool_(NULL), + extension_factory_(NULL) { // Note that setting current_limit_ == size is important to prevent some // code paths from trying to access input_ and segfaulting. } -inline bool CodedInputStream::IsFlat() const { - return input_ == NULL; -} +inline bool CodedInputStream::IsFlat() const { return input_ == NULL; } inline bool CodedInputStream::Skip(int count) { if (count < 0) return false; // security: count is often user-supplied @@ -1410,9 +1396,8 @@ inline bool CodedInputStream::Skip(int count) { } // namespace protobuf } // namespace google - #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) - #pragma runtime_checks("c", restore) +#pragma runtime_checks("c", restore) #endif // _MSC_VER && !defined(__INTEL_COMPILER) #include <google/protobuf/port_undef.inc> diff --git a/src/google/protobuf/io/coded_stream_inl.h b/src/google/protobuf/io/coded_stream_inl.h index df66282a3e..0c10e65546 100644 --- a/src/google/protobuf/io/coded_stream_inl.h +++ b/src/google/protobuf/io/coded_stream_inl.h @@ -36,11 +36,11 @@ #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ #define GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ +#include <string> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/zero_copy_stream_impl_lite.h> -#include <string> #include <google/protobuf/stubs/stl_util.h> namespace google { diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index 787b25cd00..6e3c4c4e3a 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -86,45 +86,45 @@ namespace { // TODO(kenton): gTest now supports "parameterized tests" which would be // a better way to accomplish this. Rewrite when time permits. -#define TEST_1D(FIXTURE, NAME, CASES) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template <typename CaseType> \ - void DoSingleCase(const CaseType& CASES##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES " case #" << i << ": " << CASES[i]); \ - DoSingleCase(CASES[i]); \ - } \ - } \ - \ - template <typename CaseType> \ +#define TEST_1D(FIXTURE, NAME, CASES) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template <typename CaseType> \ + void DoSingleCase(const CaseType& CASES##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES " case #" << i << ": " << CASES[i]); \ + DoSingleCase(CASES[i]); \ + } \ + } \ + \ + template <typename CaseType> \ void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case) -#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template <typename CaseType1, typename CaseType2> \ - void DoSingleCase(const CaseType1& CASES1##_case, \ - const CaseType2& CASES2##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ - for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ - << #CASES2 " case #" << j << ": " << CASES2[j]); \ - DoSingleCase(CASES1[i], CASES2[j]); \ - } \ - } \ - } \ - \ - template <typename CaseType1, typename CaseType2> \ - void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ +#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template <typename CaseType1, typename CaseType2> \ + void DoSingleCase(const CaseType1& CASES1##_case, \ + const CaseType2& CASES2##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ + for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ + << #CASES2 " case #" << j << ": " << CASES2[j]); \ + DoSingleCase(CASES1[i], CASES2[j]); \ + } \ + } \ + } \ + \ + template <typename CaseType1, typename CaseType2> \ + void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ const CaseType2& CASES2##_case) // =================================================================== @@ -157,9 +157,9 @@ const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024}; // Varint tests. struct VarintCase { - uint8 bytes[10]; // Encoded bytes. - int size; // Encoded size, in bytes. - uint64 value; // Parsed value. + uint8 bytes[10]; // Encoded bytes. + int size; // Encoded size, in bytes. + uint64 value; // Parsed value. }; inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) { @@ -167,28 +167,32 @@ inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) { } VarintCase kVarintCases[] = { - // 32-bit values - {{0x00} , 1, 0}, - {{0x01} , 1, 1}, - {{0x7f} , 1, 127}, - {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882 - {{0xbe, 0xf7, 0x92, 0x84, 0x0b}, 5, // 2961488830 - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (ULL(0x0b) << 28)}, - - // 64-bit - {{0xbe, 0xf7, 0x92, 0x84, 0x1b}, 5, // 7256456126 - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (ULL(0x1b) << 28)}, - {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, 8, // 41256202580718336 - (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | - (ULL(0x43) << 28) | (ULL(0x49) << 35) | (ULL(0x24) << 42) | - (ULL(0x49) << 49)}, - // 11964378330978735131 - {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, 10, - (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | - (ULL(0x3b) << 28) | (ULL(0x56) << 35) | (ULL(0x00) << 42) | - (ULL(0x05) << 49) | (ULL(0x26) << 56) | (ULL(0x01) << 63)}, + // 32-bit values + {{0x00}, 1, 0}, + {{0x01}, 1, 1}, + {{0x7f}, 1, 127}, + {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882 + {{0xbe, 0xf7, 0x92, 0x84, 0x0b}, + 5, // 2961488830 + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | + (ULL(0x0b) << 28)}, + + // 64-bit + {{0xbe, 0xf7, 0x92, 0x84, 0x1b}, + 5, // 7256456126 + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | + (ULL(0x1b) << 28)}, + {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, + 8, // 41256202580718336 + (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | + (ULL(0x43) << 28) | (ULL(0x49) << 35) | (ULL(0x24) << 42) | + (ULL(0x49) << 49)}, + // 11964378330978735131 + {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, + 10, + (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | + (ULL(0x3b) << 28) | (ULL(0x56) << 35) | (ULL(0x00) << 42) | + (ULL(0x05) << 49) | (ULL(0x26) << 56) | (ULL(0x01) << 63)}, }; TEST_2D(CodedStreamTest, ReadVarint32, kVarintCases, kBlockSizes) { @@ -229,15 +233,14 @@ TEST_F(CodedStreamTest, EmptyInputBeforeEos) { class In : public ZeroCopyInputStream { public: In() : count_(0) {} + private: virtual bool Next(const void** data, int* size) { *data = NULL; *size = 0; return count_++ < 2; } - virtual void BackUp(int count) { - GOOGLE_LOG(FATAL) << "Tests never call this."; - } + virtual void BackUp(int count) { GOOGLE_LOG(FATAL) << "Tests never call this."; } virtual bool Skip(int count) { GOOGLE_LOG(FATAL) << "Tests never call this."; return false; @@ -291,9 +294,8 @@ TEST_1D(CodedStreamTest, ExpectTagFromArray, kVarintCases) { // If the expectation succeeds, it should return a pointer past the tag. if (kVarintCases_case.size <= 2) { - EXPECT_TRUE(NULL == - CodedInputStream::ExpectTagFromArray(buffer_, - expected_value + 1)); + EXPECT_TRUE(NULL == CodedInputStream::ExpectTagFromArray( + buffer_, expected_value + 1)); EXPECT_TRUE(buffer_ + kVarintCases_case.size == CodedInputStream::ExpectTagFromArray(buffer_, expected_value)); } else { @@ -336,7 +338,7 @@ TEST_2D(CodedStreamTest, WriteVarint32, kVarintCases, kBlockSizes) { EXPECT_EQ(kVarintCases_case.size, output.ByteCount()); EXPECT_EQ(0, - memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); + memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); } TEST_2D(CodedStreamTest, WriteVarint64, kVarintCases, kBlockSizes) { @@ -353,19 +355,17 @@ TEST_2D(CodedStreamTest, WriteVarint64, kVarintCases, kBlockSizes) { EXPECT_EQ(kVarintCases_case.size, output.ByteCount()); EXPECT_EQ(0, - memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); + memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size)); } // This test causes gcc 3.3.5 (and earlier?) to give the cryptic error: // "sorry, unimplemented: `method_call_expr' not supported by dump_expr" #if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) -int32 kSignExtendedVarintCases[] = { - 0, 1, -1, 1237894, -37895138 -}; +int32 kSignExtendedVarintCases[] = {0, 1, -1, 1237894, -37895138}; -TEST_2D(CodedStreamTest, WriteVarint32SignExtended, - kSignExtendedVarintCases, kBlockSizes) { +TEST_2D(CodedStreamTest, WriteVarint32SignExtended, kSignExtendedVarintCases, + kBlockSizes) { ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case); { @@ -419,22 +419,23 @@ inline std::ostream& operator<<(std::ostream& os, const VarintErrorCase& c) { } const VarintErrorCase kVarintErrorCases[] = { - // Control case. (Insures that there isn't something else wrong that - // makes parsing always fail.) - {{0x00}, 1, true}, + // Control case. (Insures that there isn't something else wrong that + // makes parsing always fail.) + {{0x00}, 1, true}, - // No input data. - {{}, 0, false}, + // No input data. + {{}, 0, false}, - // Input ends unexpectedly. - {{0xf0, 0xab}, 2, false}, + // Input ends unexpectedly. + {{0xf0, 0xab}, 2, false}, - // Input ends unexpectedly after 32 bits. - {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6, false}, + // Input ends unexpectedly after 32 bits. + {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6, false}, - // Longer than 10 bytes. - {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, - 11, false}, + // Longer than 10 bytes. + {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, + 11, + false}, }; TEST_2D(CodedStreamTest, ReadVarint32Error, kVarintErrorCases, kBlockSizes) { @@ -500,14 +501,14 @@ inline std::ostream& operator<<(std::ostream& os, const VarintSizeCase& c) { } VarintSizeCase kVarintSizeCases[] = { - {0u, 1}, - {1u, 1}, - {127u, 1}, - {128u, 2}, - {758923u, 3}, - {4000000000u, 5}, - {ULL(41256202580718336), 8}, - {ULL(11964378330978735131), 10}, + {0u, 1}, + {1u, 1}, + {127u, 1}, + {128u, 2}, + {758923u, 3}, + {4000000000u, 5}, + {ULL(41256202580718336), 8}, + {ULL(11964378330978735131), 10}, }; TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) { @@ -517,13 +518,13 @@ TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) { } EXPECT_EQ(kVarintSizeCases_case.size, - CodedOutputStream::VarintSize32( - static_cast<uint32>(kVarintSizeCases_case.value))); + CodedOutputStream::VarintSize32( + static_cast<uint32>(kVarintSizeCases_case.value))); } TEST_1D(CodedStreamTest, VarintSize64, kVarintSizeCases) { EXPECT_EQ(kVarintSizeCases_case.size, - CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); + CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); } TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) { @@ -552,13 +553,13 @@ TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) { // Fixed-size int tests struct Fixed32Case { - uint8 bytes[sizeof(uint32)]; // Encoded bytes. - uint32 value; // Parsed value. + uint8 bytes[sizeof(uint32)]; // Encoded bytes. + uint32 value; // Parsed value. }; struct Fixed64Case { - uint8 bytes[sizeof(uint64)]; // Encoded bytes. - uint64 value; // Parsed value. + uint8 bytes[sizeof(uint64)]; // Encoded bytes. + uint64 value; // Parsed value. }; inline std::ostream& operator<<(std::ostream& os, const Fixed32Case& c) { @@ -570,13 +571,13 @@ inline std::ostream& operator<<(std::ostream& os, const Fixed64Case& c) { } Fixed32Case kFixed32Cases[] = { - {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu}, - {{0x12, 0x34, 0x56, 0x78}, 0x78563412u}, + {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu}, + {{0x12, 0x34, 0x56, 0x78}, 0x78563412u}, }; Fixed64Case kFixed64Cases[] = { - {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78}, ULL(0x7856341290abcdef)}, - {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, ULL(0x8877665544332211)}, + {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78}, ULL(0x7856341290abcdef)}, + {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, ULL(0x8877665544332211)}, }; TEST_2D(CodedStreamTest, ReadLittleEndian32, kFixed32Cases, kBlockSizes) { @@ -647,8 +648,8 @@ TEST_1D(CodedStreamTest, ReadLittleEndian32FromArray, kFixed32Cases) { memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes)); uint32 value; - const uint8* end = CodedInputStream::ReadLittleEndian32FromArray( - buffer_, &value); + const uint8* end = + CodedInputStream::ReadLittleEndian32FromArray(buffer_, &value); EXPECT_EQ(kFixed32Cases_case.value, value); EXPECT_TRUE(end == buffer_ + sizeof(value)); } @@ -657,8 +658,8 @@ TEST_1D(CodedStreamTest, ReadLittleEndian64FromArray, kFixed64Cases) { memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes)); uint64 value; - const uint8* end = CodedInputStream::ReadLittleEndian64FromArray( - buffer_, &value); + const uint8* end = + CodedInputStream::ReadLittleEndian64FromArray(buffer_, &value); EXPECT_EQ(kFixed64Cases_case.value, value); EXPECT_TRUE(end == buffer_ + sizeof(value)); } @@ -933,7 +934,7 @@ TEST_F(CodedStreamTest, // Skip const char kSkipTestBytes[] = - "<Before skipping><To be skipped><After skipping>"; + "<Before skipping><To be skipped><After skipping>"; TEST_1D(CodedStreamTest, SkipInput, kBlockSizes) { memcpy(buffer_, kSkipTestBytes, sizeof(kSkipTestBytes)); @@ -1240,7 +1241,8 @@ TEST_F(CodedStreamTest, TotalBytesLimit) { ASSERT_EQ(1, errors.size()); EXPECT_PRED_FORMAT2(testing::IsSubstring, - "A protocol message was rejected because it was too big", errors[0]); + "A protocol message was rejected because it was too big", + errors[0]); coded_input.SetTotalBytesLimit(32, -1); EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit()); @@ -1304,35 +1306,35 @@ TEST_F(CodedStreamTest, RecursionLimit) { coded_input.SetRecursionLimit(4); // This is way too much testing for a counter. - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 - coded_input.DecrementRecursionDepth(); // 5 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 - coded_input.DecrementRecursionDepth(); // 5 - coded_input.DecrementRecursionDepth(); // 4 - coded_input.DecrementRecursionDepth(); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 - coded_input.DecrementRecursionDepth(); // 4 - coded_input.DecrementRecursionDepth(); // 3 - coded_input.DecrementRecursionDepth(); // 2 - coded_input.DecrementRecursionDepth(); // 1 - coded_input.DecrementRecursionDepth(); // 0 - coded_input.DecrementRecursionDepth(); // 0 - coded_input.DecrementRecursionDepth(); // 0 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 + coded_input.DecrementRecursionDepth(); // 5 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6 + coded_input.DecrementRecursionDepth(); // 5 + coded_input.DecrementRecursionDepth(); // 4 + coded_input.DecrementRecursionDepth(); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 + coded_input.DecrementRecursionDepth(); // 4 + coded_input.DecrementRecursionDepth(); // 3 + coded_input.DecrementRecursionDepth(); // 2 + coded_input.DecrementRecursionDepth(); // 1 + coded_input.DecrementRecursionDepth(); // 0 + coded_input.DecrementRecursionDepth(); // 0 + coded_input.DecrementRecursionDepth(); // 0 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5 coded_input.SetRecursionLimit(6); - EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6 - EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7 + EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6 + EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7 } @@ -1364,12 +1366,16 @@ class ReallyBigInputStream : public ZeroCopyInputStream { } } - void BackUp(int count) { - backup_amount_ = count; - } + void BackUp(int count) { backup_amount_ = count; } - bool Skip(int count) { GOOGLE_LOG(FATAL) << "Not implemented."; return false; } - int64 ByteCount() const { GOOGLE_LOG(FATAL) << "Not implemented."; return 0; } + bool Skip(int count) { + GOOGLE_LOG(FATAL) << "Not implemented."; + return false; + } + int64 ByteCount() const { + GOOGLE_LOG(FATAL) << "Not implemented."; + return 0; + } int backup_amount_; diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc index a569eff0a7..a3e04384eb 100644 --- a/src/google/protobuf/io/gzip_stream.cc +++ b/src/google/protobuf/io/gzip_stream.cc @@ -46,8 +46,8 @@ namespace io { static const int kDefaultBufferSize = 65536; -GzipInputStream::GzipInputStream( - ZeroCopyInputStream* sub_stream, Format format, int buffer_size) +GzipInputStream::GzipInputStream(ZeroCopyInputStream* sub_stream, Format format, + int buffer_size) : format_(format), sub_stream_(sub_stream), zerror_(Z_OK), byte_count_(0) { zcontext_.state = Z_NULL; zcontext_.zalloc = Z_NULL; @@ -74,15 +74,21 @@ GzipInputStream::~GzipInputStream() { zerror_ = inflateEnd(&zcontext_); } -static inline int internalInflateInit2( - z_stream* zcontext, GzipInputStream::Format format) { +static inline int internalInflateInit2(z_stream* zcontext, + GzipInputStream::Format format) { int windowBitsFormat = 0; switch (format) { - case GzipInputStream::GZIP: windowBitsFormat = 16; break; - case GzipInputStream::AUTO: windowBitsFormat = 32; break; - case GzipInputStream::ZLIB: windowBitsFormat = 0; break; + case GzipInputStream::GZIP: + windowBitsFormat = 16; + break; + case GzipInputStream::AUTO: + windowBitsFormat = 32; + break; + case GzipInputStream::ZLIB: + windowBitsFormat = 0; + break; } - return inflateInit2(zcontext, /* windowBits */15 | windowBitsFormat); + return inflateInit2(zcontext, /* windowBits */ 15 | windowBitsFormat); } int GzipInputStream::Inflate(int flush) { @@ -122,8 +128,8 @@ void GzipInputStream::DoNextOutput(const void** data, int* size) { // implements ZeroCopyInputStream ---------------------------------- bool GzipInputStream::Next(const void** data, int* size) { - bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) - || (zerror_ == Z_BUF_ERROR); + bool ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) || + (zerror_ == Z_BUF_ERROR); if ((!ok) || (zcontext_.next_out == NULL)) { return false; } @@ -154,8 +160,8 @@ bool GzipInputStream::Next(const void** data, int* size) { // The underlying stream's Next returned false inside Inflate. return false; } - ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) - || (zerror_ == Z_BUF_ERROR); + ok = (zerror_ == Z_OK) || (zerror_ == Z_STREAM_END) || + (zerror_ == Z_BUF_ERROR); if (!ok) { return false; } @@ -230,13 +236,10 @@ void GzipOutputStream::Init(ZeroCopyOutputStream* sub_stream, if (options.format == ZLIB) { windowBitsFormat = 0; } - zerror_ = deflateInit2( - &zcontext_, - options.compression_level, - Z_DEFLATED, - /* windowBits */15 | windowBitsFormat, - /* memLevel (default) */8, - options.compression_strategy); + zerror_ = + deflateInit2(&zcontext_, options.compression_level, Z_DEFLATED, + /* windowBits */ 15 | windowBitsFormat, + /* memLevel (default) */ 8, options.compression_strategy); } GzipOutputStream::~GzipOutputStream() { @@ -305,9 +308,9 @@ int64 GzipOutputStream::ByteCount() const { bool GzipOutputStream::Flush() { zerror_ = Deflate(Z_FULL_FLUSH); // Return true if the flush succeeded or if it was a no-op. - return (zerror_ == Z_OK) || - (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 && - zcontext_.avail_out != 0); + return (zerror_ == Z_OK) || + (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 && + zcontext_.avail_out != 0); } bool GzipOutputStream::Close() { diff --git a/src/google/protobuf/io/gzip_stream.h b/src/google/protobuf/io/gzip_stream.h index b8eadfab81..9980e5b4f6 100644 --- a/src/google/protobuf/io/gzip_stream.h +++ b/src/google/protobuf/io/gzip_stream.h @@ -70,19 +70,13 @@ class PROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream { }; // buffer_size and format may be -1 for default of 64kB and GZIP format - explicit GzipInputStream( - ZeroCopyInputStream* sub_stream, - Format format = AUTO, - int buffer_size = -1); + explicit GzipInputStream(ZeroCopyInputStream* sub_stream, + Format format = AUTO, int buffer_size = -1); virtual ~GzipInputStream(); // Return last error message or NULL if no error. - inline const char* ZlibErrorMessage() const { - return zcontext_.msg; - } - inline int ZlibErrorCode() const { - return zerror_; - } + inline const char* ZlibErrorMessage() const { return zcontext_.msg; } + inline int ZlibErrorCode() const { return zerror_; } // implements ZeroCopyInputStream ---------------------------------- bool Next(const void** data, int* size); @@ -143,19 +137,13 @@ class PROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream { explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream); // Create a GzipOutputStream with the given options. - GzipOutputStream( - ZeroCopyOutputStream* sub_stream, - const Options& options); + GzipOutputStream(ZeroCopyOutputStream* sub_stream, const Options& options); virtual ~GzipOutputStream(); // Return last error message or NULL if no error. - inline const char* ZlibErrorMessage() const { - return zcontext_.msg; - } - inline int ZlibErrorCode() const { - return zerror_; - } + inline const char* ZlibErrorMessage() const { return zcontext_.msg; } + inline int ZlibErrorCode() const { return zerror_; } // Flushes data written so far to zipped data in the underlying stream. // It is the caller's responsibility to flush the underlying stream if diff --git a/src/google/protobuf/stubs/io_win32.cc b/src/google/protobuf/io/io_win32.cc old mode 100644 new mode 100755 similarity index 98% rename from src/google/protobuf/stubs/io_win32.cc rename to src/google/protobuf/io/io_win32.cc index d0f57dfddb..b8f9bf8fae --- a/src/google/protobuf/stubs/io_win32.cc +++ b/src/google/protobuf/io/io_win32.cc @@ -29,7 +29,9 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: laszlocsomor@google.com (Laszlo Csomor) -// +// Based on original Protocol Buffers design by +// Sanjay Ghemawat, Jeff Dean, and others. + // Implementation for long-path-aware open/mkdir/access/etc. on Windows, as well // as for the supporting utility functions. // @@ -58,7 +60,7 @@ #include <wctype.h> #include <windows.h> -#include <google/protobuf/stubs/io_win32.h> +#include <google/protobuf/io/io_win32.h> #include <memory> #include <sstream> @@ -67,7 +69,7 @@ namespace google { namespace protobuf { -namespace internal { +namespace io { namespace win32 { namespace { @@ -407,7 +409,7 @@ bool wcs_to_utf8(const wchar_t* input, string* out) { } // namespace strings } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/stubs/io_win32.h b/src/google/protobuf/io/io_win32.h old mode 100644 new mode 100755 similarity index 92% rename from src/google/protobuf/stubs/io_win32.h rename to src/google/protobuf/io/io_win32.h index 69924dcf2a..cf5cb326ee --- a/src/google/protobuf/stubs/io_win32.h +++ b/src/google/protobuf/io/io_win32.h @@ -29,26 +29,28 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: laszlocsomor@google.com (Laszlo Csomor) -// +// Based on original Protocol Buffers design by +// Sanjay Ghemawat, Jeff Dean, and others. + // This file contains the declarations for Windows implementations of // commonly used POSIX functions such as open(2) and access(2), as well // as macro definitions for flags of these functions. // // By including this file you'll redefine open/access/etc. to -// ::google::protobuf::internal::win32::{open/access/etc.}. +// ::google::protobuf::io::win32::{open/access/etc.}. // Make sure you don't include a header that attempts to redeclare or // redefine these functions, that'll lead to confusing compilation // errors. It's best to #include this file as the last one to ensure that. // // This file is only used on Windows, it's empty on other platforms. -#ifndef GOOGLE_PROTOBUF_STUBS_IO_WIN32_H__ -#define GOOGLE_PROTOBUF_STUBS_IO_WIN32_H__ +#ifndef GOOGLE_PROTOBUF_IO_IO_WIN32_H__ +#define GOOGLE_PROTOBUF_IO_IO_WIN32_H__ #if defined(_WIN32) #include <string> -#include <google/protobuf/stubs/port.h> +#include <google/protobuf/port.h> #include <google/protobuf/port_def.inc> @@ -56,7 +58,7 @@ // following functions already, except for mkdir. namespace google { namespace protobuf { -namespace internal { +namespace io { namespace win32 { PROTOBUF_EXPORT FILE* fopen(const char* path, const char* mode); @@ -91,7 +93,7 @@ PROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); } // namespace strings } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google @@ -115,4 +117,4 @@ PROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); #endif // defined(_WIN32) -#endif // GOOGLE_PROTOBUF_STUBS_IO_WIN32_H__ +#endif // GOOGLE_PROTOBUF_IO_IO_WIN32_H__ diff --git a/src/google/protobuf/stubs/io_win32_unittest.cc b/src/google/protobuf/io/io_win32_unittest.cc old mode 100644 new mode 100755 similarity index 98% rename from src/google/protobuf/stubs/io_win32_unittest.cc rename to src/google/protobuf/io/io_win32_unittest.cc index 6c70d461b7..53aa3339bb --- a/src/google/protobuf/stubs/io_win32_unittest.cc +++ b/src/google/protobuf/io/io_win32_unittest.cc @@ -29,7 +29,9 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: laszlocsomor@google.com (Laszlo Csomor) -// +// Based on original Protocol Buffers design by +// Sanjay Ghemawat, Jeff Dean, and others. + // Unit tests for long-path-aware open/mkdir/access/etc. on Windows, as well as // for the supporting utility functions. // @@ -47,7 +49,7 @@ #include <wchar.h> #include <windows.h> -#include <google/protobuf/stubs/io_win32.h> +#include <google/protobuf/io/io_win32.h> #include <gtest/gtest.h> #include <memory> @@ -56,7 +58,7 @@ namespace google { namespace protobuf { -namespace internal { +namespace io { namespace win32 { namespace { @@ -444,7 +446,7 @@ TEST_F(IoWin32Test, Utf8Utf16ConversionTest) { } // namespace } // namespace win32 -} // namespace internal +} // namespace io } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/io/printer.cc b/src/google/protobuf/io/printer.cc index 736f06352b..6a86c58c1a 100644 --- a/src/google/protobuf/io/printer.cc +++ b/src/google/protobuf/io/printer.cc @@ -34,10 +34,10 @@ #include <cctype> -#include <google/protobuf/io/printer.h> -#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> +#include <google/protobuf/io/printer.h> +#include <google/protobuf/io/zero_copy_stream.h> namespace google { namespace protobuf { @@ -182,9 +182,7 @@ void Printer::Print(const std::map<std::string, std::string>& variables, WriteRaw(text + pos, size - pos); } -void Printer::Indent() { - indent_ += " "; -} +void Printer::Indent() { indent_ += " "; } void Printer::Outdent() { if (indent_.empty()) { diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h index a08ca655d4..a55946e929 100644 --- a/src/google/protobuf/io/printer.h +++ b/src/google/protobuf/io/printer.h @@ -37,8 +37,8 @@ #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ #define GOOGLE_PROTOBUF_IO_PRINTER_H__ -#include <string> #include <map> +#include <string> #include <vector> #include <google/protobuf/stubs/common.h> @@ -48,7 +48,7 @@ namespace google { namespace protobuf { namespace io { -class ZeroCopyOutputStream; // zero_copy_stream.h +class ZeroCopyOutputStream; // zero_copy_stream.h // Records annotations about a Printer's output. class PROTOBUF_EXPORT AnnotationCollector { diff --git a/src/google/protobuf/io/printer_unittest.cc b/src/google/protobuf/io/printer_unittest.cc index 50a683ec3c..b847fa70dc 100644 --- a/src/google/protobuf/io/printer_unittest.cc +++ b/src/google/protobuf/io/printer_unittest.cc @@ -77,10 +77,11 @@ TEST(Printer, BasicPrinting) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World! This is the same line.\n" - "But this is a new one.\n" - "And this is another one.", - buffer); + EXPECT_STREQ( + "Hello World! This is the same line.\n" + "But this is a new one.\n" + "And this is another one.", + buffer); } } @@ -103,12 +104,13 @@ TEST(Printer, WriteRaw) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World! This is the same line.\n" - "But this is a new one.\n" - "And this is another one." - "\n" - "From an object\n", - buffer); + EXPECT_STREQ( + "Hello World! This is the same line.\n" + "But this is a new one.\n" + "And this is another one." + "\n" + "From an object\n", + buffer); } } @@ -138,13 +140,14 @@ TEST(Printer, VariableSubstitution) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World!\n" - "bar = $foo$\n" - "RawBit\n" - "1234\n" - "A literal dollar sign: $\n" - "Now foo = blah.", - buffer); + EXPECT_STREQ( + "Hello World!\n" + "bar = $foo$\n" + "RawBit\n" + "1234\n" + "A literal dollar sign: $\n" + "Now foo = blah.", + buffer); } } @@ -163,10 +166,11 @@ TEST(Printer, InlineVariableSubstitution) { buffer[output.ByteCount()] = '\0'; - EXPECT_STREQ("Hello World!\n" - "RawBit\n" - "one two\n", - buffer); + EXPECT_STREQ( + "Hello World!\n" + "RawBit\n" + "one two\n", + buffer); } // MockDescriptorFile defines only those members that Printer uses to write out @@ -435,7 +439,6 @@ TEST(Printer, AnnotateIndentNewline) { EXPECT_EQ(4, ab->end()); } - TEST(Printer, Indenting) { char buffer[8192]; @@ -454,11 +457,13 @@ TEST(Printer, Indenting) { printer.Outdent(); printer.Print("But this is not."); printer.Indent(); - printer.Print(" And this is still the same line.\n" - "But this is indented.\n"); + printer.Print( + " And this is still the same line.\n" + "But this is indented.\n"); printer.PrintRaw("RawBit has indent at start\n"); printer.PrintRaw("but not after a raw newline\n"); - printer.Print(vars, "Note that a newline in a variable will break " + printer.Print(vars, + "Note that a newline in a variable will break " "indenting, as we see$newline$here.\n"); printer.Indent(); printer.Print("And this"); @@ -472,18 +477,18 @@ TEST(Printer, Indenting) { buffer[output.ByteCount()] = '\0'; EXPECT_STREQ( - "This is not indented.\n" - " This is indented\n" - " And so is this\n" - "But this is not. And this is still the same line.\n" - " But this is indented.\n" - " RawBit has indent at start\n" - "but not after a raw newline\n" - "Note that a newline in a variable will break indenting, as we see\n" - "here.\n" - " And this is double-indented\n" - "Back to normal.", - buffer); + "This is not indented.\n" + " This is indented\n" + " And so is this\n" + "But this is not. And this is still the same line.\n" + " But this is indented.\n" + " RawBit has indent at start\n" + "but not after a raw newline\n" + "Note that a newline in a variable will break indenting, as we see\n" + "here.\n" + " And this is double-indented\n" + "Back to normal.", + buffer); } } diff --git a/src/google/protobuf/io/strtod.cc b/src/google/protobuf/io/strtod.cc index 7b17b57ba8..42de29638d 100644 --- a/src/google/protobuf/io/strtod.cc +++ b/src/google/protobuf/io/strtod.cc @@ -49,6 +49,8 @@ namespace io { namespace { +const double MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235e+38; + // Returns a string identical to *input except that the character pointed to // by radix_pos (which should be '.') is replaced with the locale-specific // radix character. @@ -61,7 +63,7 @@ std::string LocalizeRadix(const char* input, const char* radix_pos) { char temp[16]; int size = sprintf(temp, "%.1f", 1.5); GOOGLE_CHECK_EQ(temp[0], '1'); - GOOGLE_CHECK_EQ(temp[size-1], '5'); + GOOGLE_CHECK_EQ(temp[size - 1], '5'); GOOGLE_CHECK_LE(size, 6); // Now replace the '.' in the input with it. @@ -94,8 +96,7 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { const char* localized_cstr = localized.c_str(); char* localized_endptr; result = strtod(localized_cstr, &localized_endptr); - if ((localized_endptr - localized_cstr) > - (temp_endptr - text)) { + if ((localized_endptr - localized_cstr) > (temp_endptr - text)) { // This attempt got further, so replacing the decimal must have helped. // Update original_endptr to point at the right location. if (original_endptr != NULL) { @@ -103,7 +104,7 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { int size_diff = localized.size() - strlen(text); // const_cast is necessary to match the strtod() interface. *original_endptr = const_cast<char*>( - text + (localized_endptr - localized_cstr - size_diff)); + text + (localized_endptr - localized_cstr - size_diff)); } } @@ -111,9 +112,24 @@ double NoLocaleStrtod(const char* text, char** original_endptr) { } float SafeDoubleToFloat(double value) { + // static_cast<float> on a number larger than float can result in illegal + // instruction error, so we need to manually convert it to infinity or max. if (value > std::numeric_limits<float>::max()) { + // Max float value is about 3.4028234664E38 when represented as a double. + // However, when printing float as text, it will be rounded as + // 3.4028235e+38. If we parse the value of 3.4028235e+38 from text and + // compare it to 3.4028234664E38, we may think that it is larger, but + // actually, any number between these two numbers could only be represented + // as the same max float number in float, so we should treat them the same + // as max float. + if (value <= MAX_FLOAT_AS_DOUBLE_ROUNDED) { + return std::numeric_limits<float>::max(); + } return std::numeric_limits<float>::infinity(); } else if (value < -std::numeric_limits<float>::max()) { + if (value >= -MAX_FLOAT_AS_DOUBLE_ROUNDED) { + return -std::numeric_limits<float>::max(); + } return -std::numeric_limits<float>::infinity(); } else { return static_cast<float>(value); diff --git a/src/google/protobuf/io/tokenizer.cc b/src/google/protobuf/io/tokenizer.cc index 8abcbd0547..f8cf2ebd1a 100644 --- a/src/google/protobuf/io/tokenizer.cc +++ b/src/google/protobuf/io/tokenizer.cc @@ -113,39 +113,34 @@ namespace { // Note: No class is allowed to contain '\0', since this is used to mark end- // of-input and is handled specially. -#define CHARACTER_CLASS(NAME, EXPRESSION) \ - class NAME { \ - public: \ - static inline bool InClass(char c) { \ - return EXPRESSION; \ - } \ +#define CHARACTER_CLASS(NAME, EXPRESSION) \ + class NAME { \ + public: \ + static inline bool InClass(char c) { return EXPRESSION; } \ } -CHARACTER_CLASS(Whitespace, c == ' ' || c == '\n' || c == '\t' || - c == '\r' || c == '\v' || c == '\f'); -CHARACTER_CLASS(WhitespaceNoNewline, c == ' ' || c == '\t' || - c == '\r' || c == '\v' || c == '\f'); +CHARACTER_CLASS(Whitespace, c == ' ' || c == '\n' || c == '\t' || c == '\r' || + c == '\v' || c == '\f'); +CHARACTER_CLASS(WhitespaceNoNewline, + c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f'); -CHARACTER_CLASS(Unprintable, c < ' ' && c > '\0'); +CHARACTER_CLASS(Unprintable, c<' ' && c> '\0'); CHARACTER_CLASS(Digit, '0' <= c && c <= '9'); CHARACTER_CLASS(OctalDigit, '0' <= c && c <= '7'); -CHARACTER_CLASS(HexDigit, ('0' <= c && c <= '9') || - ('a' <= c && c <= 'f') || - ('A' <= c && c <= 'F')); +CHARACTER_CLASS(HexDigit, ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || + ('A' <= c && c <= 'F')); -CHARACTER_CLASS(Letter, ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - (c == '_')); +CHARACTER_CLASS(Letter, + ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '_')); CHARACTER_CLASS(Alphanumeric, ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - ('0' <= c && c <= '9') || - (c == '_')); + ('A' <= c && c <= 'Z') || + ('0' <= c && c <= '9') || (c == '_')); CHARACTER_CLASS(Escape, c == 'a' || c == 'b' || c == 'f' || c == 'n' || - c == 'r' || c == 't' || c == 'v' || c == '\\' || - c == '?' || c == '\'' || c == '\"'); + c == 'r' || c == 't' || c == 'v' || c == '\\' || + c == '?' || c == '\'' || c == '\"'); #undef CHARACTER_CLASS @@ -161,20 +156,32 @@ inline int DigitValue(char digit) { // Inline because it's only used in one place. inline char TranslateEscape(char c) { switch (c) { - case 'a': return '\a'; - case 'b': return '\b'; - case 'f': return '\f'; - case 'n': return '\n'; - case 'r': return '\r'; - case 't': return '\t'; - case 'v': return '\v'; - case '\\': return '\\'; - case '?': return '\?'; // Trigraphs = :( - case '\'': return '\''; - case '"': return '\"'; + case 'a': + return '\a'; + case 'b': + return '\b'; + case 'f': + return '\f'; + case 'n': + return '\n'; + case 'r': + return '\r'; + case 't': + return '\t'; + case 'v': + return '\v'; + case '\\': + return '\\'; + case '?': + return '\?'; // Trigraphs = :( + case '\'': + return '\''; + case '"': + return '\"'; // We expect escape sequences to have been validated separately. - default: return '?'; + default: + return '?'; } } @@ -186,21 +193,20 @@ ErrorCollector::~ErrorCollector() {} Tokenizer::Tokenizer(ZeroCopyInputStream* input, ErrorCollector* error_collector) - : input_(input), - error_collector_(error_collector), - buffer_(NULL), - buffer_size_(0), - buffer_pos_(0), - read_error_(false), - line_(0), - column_(0), - record_target_(NULL), - record_start_(-1), - allow_f_after_float_(false), - comment_style_(CPP_COMMENT_STYLE), - require_space_after_number_(true), - allow_multiline_strings_(false) { - + : input_(input), + error_collector_(error_collector), + buffer_(NULL), + buffer_size_(0), + buffer_pos_(0), + read_error_(false), + line_(0), + column_(0), + record_target_(NULL), + record_start_(-1), + allow_f_after_float_(false), + comment_style_(CPP_COMMENT_STYLE), + require_space_after_number_(true), + allow_multiline_strings_(false) { current_.line = 0; current_.column = 0; current_.end_column = 0; @@ -249,7 +255,8 @@ void Tokenizer::Refresh() { // If we're in a token, append the rest of the buffer to it. if (record_target_ != NULL && record_start_ < buffer_size_) { - record_target_->append(buffer_ + record_start_, buffer_size_ - record_start_); + record_target_->append(buffer_ + record_start_, + buffer_size_ - record_start_); record_start_ = 0; } @@ -282,14 +289,15 @@ inline void Tokenizer::StopRecording() { // be helpful by detecting the NULL pointer, even though there's nothing // wrong with reading zero bytes from NULL. if (buffer_pos_ != record_start_) { - record_target_->append(buffer_ + record_start_, buffer_pos_ - record_start_); + record_target_->append(buffer_ + record_start_, + buffer_pos_ - record_start_); } record_target_ = NULL; record_start_ = -1; } inline void Tokenizer::StartToken() { - current_.type = TYPE_START; // Just for the sake of initializing it. + current_.type = TYPE_START; // Just for the sake of initializing it. current_.text.clear(); current_.line = line_; current_.column = column_; @@ -304,12 +312,12 @@ inline void Tokenizer::EndToken() { // ------------------------------------------------------------------- // Helper methods that consume characters. -template<typename CharacterClass> +template <typename CharacterClass> inline bool Tokenizer::LookingAt() { return CharacterClass::InClass(current_char_); } -template<typename CharacterClass> +template <typename CharacterClass> inline bool Tokenizer::TryConsumeOne() { if (CharacterClass::InClass(current_char_)) { NextChar(); @@ -328,14 +336,14 @@ inline bool Tokenizer::TryConsume(char c) { } } -template<typename CharacterClass> +template <typename CharacterClass> inline void Tokenizer::ConsumeZeroOrMore() { while (CharacterClass::InClass(current_char_)) { NextChar(); } } -template<typename CharacterClass> +template <typename CharacterClass> inline void Tokenizer::ConsumeOneOrMore(const char* error) { if (!CharacterClass::InClass(current_char_)) { AddError(error); @@ -381,25 +389,21 @@ void Tokenizer::ConsumeString(char delimiter) { } // Possibly followed by another hex digit, but again we don't care. } else if (TryConsume('u')) { - if (!TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>()) { + if (!TryConsumeOne<HexDigit>() || !TryConsumeOne<HexDigit>() || + !TryConsumeOne<HexDigit>() || !TryConsumeOne<HexDigit>()) { AddError("Expected four hex digits for \\u escape sequence."); } } else if (TryConsume('U')) { // We expect 8 hex digits; but only the range up to 0x10ffff is // legal. - if (!TryConsume('0') || - !TryConsume('0') || + if (!TryConsume('0') || !TryConsume('0') || !(TryConsume('0') || TryConsume('1')) || - !TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>() || - !TryConsumeOne<HexDigit>() || + !TryConsumeOne<HexDigit>() || !TryConsumeOne<HexDigit>() || + !TryConsumeOne<HexDigit>() || !TryConsumeOne<HexDigit>() || !TryConsumeOne<HexDigit>()) { - AddError("Expected eight hex digits up to 10ffff for \\U escape " - "sequence"); + AddError( + "Expected eight hex digits up to 10ffff for \\U escape " + "sequence"); } } else { AddError("Invalid escape sequence in string literal."); @@ -465,7 +469,7 @@ Tokenizer::TokenType Tokenizer::ConsumeNumber(bool started_with_zero, } else if (current_char_ == '.') { if (is_float) { AddError( - "Already saw decimal point or exponent; can't have another one."); + "Already saw decimal point or exponent; can't have another one."); } else { AddError("Hex and octal numbers must be integers."); } @@ -492,10 +496,8 @@ void Tokenizer::ConsumeBlockComment(std::string* content) { if (content != NULL) RecordTo(content); while (true) { - while (current_char_ != '\0' && - current_char_ != '*' && - current_char_ != '/' && - current_char_ != '\n') { + while (current_char_ != '\0' && current_char_ != '*' && + current_char_ != '/' && current_char_ != '\n') { NextChar(); } @@ -524,11 +526,11 @@ void Tokenizer::ConsumeBlockComment(std::string* content) { // Note: We didn't consume the '*' because if there is a '/' after it // we want to interpret that as the end of the comment. AddError( - "\"/*\" inside block comment. Block comments cannot be nested."); + "\"/*\" inside block comment. Block comments cannot be nested."); } else if (current_char_ == '\0') { AddError("End-of-file inside block comment."); - error_collector_->AddError( - start_line, start_column, " Comment started here."); + error_collector_->AddError(start_line, start_column, + " Comment started here."); if (content != NULL) StopRecording(); break; } @@ -613,8 +615,9 @@ bool Tokenizer::Next() { current_.line == previous_.line && current_.column == previous_.end_column) { // We don't accept syntax like "blah.123". - error_collector_->AddError(line_, column_ - 2, - "Need space between identifier and decimal point."); + error_collector_->AddError( + line_, column_ - 2, + "Need space between identifier and decimal point."); } current_.type = ConsumeNumber(false, true); } else { @@ -631,7 +634,8 @@ bool Tokenizer::Next() { } else { // Check if the high order bit is set. if (current_char_ & 0x80) { - error_collector_->AddError(line_, column_, + error_collector_->AddError( + line_, column_, StringPrintf("Interpreting non ascii codepoint %d.", static_cast<unsigned char>(current_char_))); } @@ -731,9 +735,7 @@ class CommentCollector { } } - void DetachFromPrev() { - can_attach_to_prev_ = false; - } + void DetachFromPrev() { can_attach_to_prev_ = false; } private: std::string* prev_trailing_comments_; @@ -754,7 +756,7 @@ class CommentCollector { bool can_attach_to_prev_; }; -} // namespace +} // namespace bool Tokenizer::NextWithComments(std::string* prev_trailing_comments, std::vector<std::string>* detached_comments, @@ -767,8 +769,9 @@ bool Tokenizer::NextWithComments(std::string* prev_trailing_comments, // beginning. Only UTF-8 BOM (0xEF 0xBB 0xBF) is accepted. if (TryConsume((char)0xEF)) { if (!TryConsume((char)0xBB) || !TryConsume((char)0xBF)) { - AddError("Proto file starts with 0xEF but not UTF-8 BOM. " - "Only UTF-8 is accepted for proto file."); + AddError( + "Proto file starts with 0xEF but not UTF-8 BOM. " + "Only UTF-8 is accepted for proto file."); return false; } } @@ -836,9 +839,7 @@ bool Tokenizer::NextWithComments(std::string* prev_trailing_comments, collector.DetachFromPrev(); } else { bool result = Next(); - if (!result || - current_.text == "}" || - current_.text == "]" || + if (!result || current_.text == "}" || current_.text == "]" || current_.text == ")") { // It looks like we're at the end of a scope. In this case it // makes no sense to attach a comment to the following token. @@ -863,7 +864,7 @@ bool Tokenizer::ParseInteger(const std::string& text, uint64 max_value, // Sadly, we can't just use strtoul() since it is only 32-bit and strtoull() // is non-standard. I hate the C standard library. :( -// return strtoull(text.c_str(), NULL, 0); + // return strtoull(text.c_str(), NULL, 0); const char* ptr = text.c_str(); int base = 10; @@ -917,8 +918,9 @@ double Tokenizer::ParseFloat(const std::string& text) { } GOOGLE_LOG_IF(DFATAL, end - start != text.size() || *start == '-') - << " Tokenizer::ParseFloat() passed text that could not have been" - " tokenized as a float: " << CEscape(text); + << " Tokenizer::ParseFloat() passed text that could not have been" + " tokenized as a float: " + << CEscape(text); return result; } @@ -931,22 +933,16 @@ static void AppendUTF8(uint32 code_point, std::string* output) { tmp = code_point; len = 1; } else if (code_point <= 0x07ff) { - tmp = 0x0000c080 | - ((code_point & 0x07c0) << 2) | - (code_point & 0x003f); + tmp = 0x0000c080 | ((code_point & 0x07c0) << 2) | (code_point & 0x003f); len = 2; } else if (code_point <= 0xffff) { - tmp = 0x00e08080 | - ((code_point & 0xf000) << 4) | - ((code_point & 0x0fc0) << 2) | - (code_point & 0x003f); + tmp = 0x00e08080 | ((code_point & 0xf000) << 4) | + ((code_point & 0x0fc0) << 2) | (code_point & 0x003f); len = 3; } else if (code_point <= 0x1fffff) { - tmp = 0xf0808080 | - ((code_point & 0x1c0000) << 6) | - ((code_point & 0x03f000) << 4) | - ((code_point & 0x000fc0) << 2) | - (code_point & 0x003f); + tmp = 0xf0808080 | ((code_point & 0x1c0000) << 6) | + ((code_point & 0x03f000) << 4) | ((code_point & 0x000fc0) << 2) | + (code_point & 0x003f); len = 4; } else { // UTF-16 is only defined for code points up to 0x10FFFF, and UTF-8 is @@ -986,7 +982,7 @@ static inline bool IsHeadSurrogate(uint32 code_point) { static inline bool IsTrailSurrogate(uint32 code_point) { return (code_point >= kMinTrailSurrogate) && - (code_point < kMaxTrailSurrogate); + (code_point < kMaxTrailSurrogate); } // Combine a head and trail surrogate into a single Unicode code point. @@ -994,7 +990,7 @@ static uint32 AssembleUTF16(uint32 head_surrogate, uint32 trail_surrogate) { GOOGLE_DCHECK(IsHeadSurrogate(head_surrogate)); GOOGLE_DCHECK(IsTrailSurrogate(trail_surrogate)); return 0x10000 + (((head_surrogate - kMinHeadSurrogate) << 10) | - (trail_surrogate - kMinTrailSurrogate)); + (trail_surrogate - kMinTrailSurrogate)); } // Convert the escape sequence parameter to a number of expected hex digits. @@ -1012,8 +1008,7 @@ static const char* FetchUnicodePoint(const char* ptr, uint32* code_point) { const char* p = ptr; // Fetch the code point. const int len = UnicodeLength(*p++); - if (!ReadHexDigits(p, len, code_point)) - return ptr; + if (!ReadHexDigits(p, len, code_point)) return ptr; p += len; // Check if the code point we read is a "head surrogate." If so, then we @@ -1042,9 +1037,9 @@ void Tokenizer::ParseStringAppend(const std::string& text, // empty, it's invalid, so we'll just return). const size_t text_size = text.size(); if (text_size == 0) { - GOOGLE_LOG(DFATAL) - << " Tokenizer::ParseStringAppend() passed text that could not" - " have been tokenized as a string: " << CEscape(text); + GOOGLE_LOG(DFATAL) << " Tokenizer::ParseStringAppend() passed text that could not" + " have been tokenized as a string: " + << CEscape(text); return; } @@ -1118,20 +1113,16 @@ void Tokenizer::ParseStringAppend(const std::string& text, template <typename CharacterClass> static bool AllInClass(const std::string& s) { for (int i = 0; i < s.size(); ++i) { - if (!CharacterClass::InClass(s[i])) - return false; + if (!CharacterClass::InClass(s[i])) return false; } return true; } bool Tokenizer::IsIdentifier(const std::string& text) { // Mirrors IDENTIFIER definition in Tokenizer::Next() above. - if (text.size() == 0) - return false; - if (!Letter::InClass(text.at(0))) - return false; - if (!AllInClass<Alphanumeric>(text.substr(1))) - return false; + if (text.size() == 0) return false; + if (!Letter::InClass(text.at(0))) return false; + if (!AllInClass<Alphanumeric>(text.substr(1))) return false; return true; } diff --git a/src/google/protobuf/io/tokenizer.h b/src/google/protobuf/io/tokenizer.h index 481681407a..cb33b931c9 100644 --- a/src/google/protobuf/io/tokenizer.h +++ b/src/google/protobuf/io/tokenizer.h @@ -48,7 +48,7 @@ namespace google { namespace protobuf { namespace io { -class ZeroCopyInputStream; // zero_copy_stream.h +class ZeroCopyInputStream; // zero_copy_stream.h // Defined in this file. class ErrorCollector; @@ -99,8 +99,8 @@ class PROTOBUF_EXPORT Tokenizer { ~Tokenizer(); enum TokenType { - TYPE_START, // Next() has not yet been called. - TYPE_END, // End of input reached. "text" is empty. + TYPE_START, // Next() has not yet been called. + TYPE_END, // End of input reached. "text" is empty. TYPE_IDENTIFIER, // A sequence of letters, digits, and underscores, not // starting with a digit. It is an error for a number @@ -258,17 +258,17 @@ class PROTOBUF_EXPORT Tokenizer { private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Tokenizer); - Token current_; // Returned by current(). - Token previous_; // Returned by previous(). + Token current_; // Returned by current(). + Token previous_; // Returned by previous(). ZeroCopyInputStream* input_; ErrorCollector* error_collector_; - char current_char_; // == buffer_[buffer_pos_], updated by NextChar(). - const char* buffer_; // Current buffer returned from input_. - int buffer_size_; // Size of buffer_. - int buffer_pos_; // Current position within the buffer. - bool read_error_; // Did we previously encounter a read error? + char current_char_; // == buffer_[buffer_pos_], updated by NextChar(). + const char* buffer_; // Current buffer returned from input_. + int buffer_size_; // Size of buffer_. + int buffer_pos_; // Current position within the buffer. + bool read_error_; // Did we previously encounter a read error? // Line and column number of current_char_ within the whole input stream. int line_; @@ -369,37 +369,33 @@ class PROTOBUF_EXPORT Tokenizer { // Returns true if the current character is of the given character // class, but does not consume anything. - template<typename CharacterClass> + template <typename CharacterClass> inline bool LookingAt(); // If the current character is in the given class, consume it and return // true. Otherwise return false. // e.g. TryConsumeOne<Letter>() - template<typename CharacterClass> + template <typename CharacterClass> inline bool TryConsumeOne(); // Like above, but try to consume the specific character indicated. inline bool TryConsume(char c); // Consume zero or more of the given character class. - template<typename CharacterClass> + template <typename CharacterClass> inline void ConsumeZeroOrMore(); // Consume one or more of the given character class or log the given // error message. // e.g. ConsumeOneOrMore<Digit>("Expected digits."); - template<typename CharacterClass> + template <typename CharacterClass> inline void ConsumeOneOrMore(const char* error); }; // inline methods ==================================================== -inline const Tokenizer::Token& Tokenizer::current() { - return current_; -} +inline const Tokenizer::Token& Tokenizer::current() { return current_; } -inline const Tokenizer::Token& Tokenizer::previous() { - return previous_; -} +inline const Tokenizer::Token& Tokenizer::previous() { return previous_; } inline void Tokenizer::ParseString(const std::string& text, std::string* output) { diff --git a/src/google/protobuf/io/tokenizer_unittest.cc b/src/google/protobuf/io/tokenizer_unittest.cc index 3dbee8633f..37628ab2f9 100644 --- a/src/google/protobuf/io/tokenizer_unittest.cc +++ b/src/google/protobuf/io/tokenizer_unittest.cc @@ -75,45 +75,45 @@ namespace { // which failed will be printed. The case type must be printable using // ostream::operator<<. -#define TEST_1D(FIXTURE, NAME, CASES) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template <typename CaseType> \ - void DoSingleCase(const CaseType& CASES##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES " case #" << i << ": " << CASES[i]); \ - DoSingleCase(CASES[i]); \ - } \ - } \ - \ - template <typename CaseType> \ +#define TEST_1D(FIXTURE, NAME, CASES) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template <typename CaseType> \ + void DoSingleCase(const CaseType& CASES##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES " case #" << i << ": " << CASES[i]); \ + DoSingleCase(CASES[i]); \ + } \ + } \ + \ + template <typename CaseType> \ void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case) -#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ - class FIXTURE##_##NAME##_DD : public FIXTURE { \ - protected: \ - template <typename CaseType1, typename CaseType2> \ - void DoSingleCase(const CaseType1& CASES1##_case, \ - const CaseType2& CASES2##_case); \ - }; \ - \ - TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ - for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ - for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ - SCOPED_TRACE(testing::Message() \ - << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ - << #CASES2 " case #" << j << ": " << CASES2[j]); \ - DoSingleCase(CASES1[i], CASES2[j]); \ - } \ - } \ - } \ - \ - template <typename CaseType1, typename CaseType2> \ - void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ +#define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \ + class FIXTURE##_##NAME##_DD : public FIXTURE { \ + protected: \ + template <typename CaseType1, typename CaseType2> \ + void DoSingleCase(const CaseType1& CASES1##_case, \ + const CaseType2& CASES2##_case); \ + }; \ + \ + TEST_F(FIXTURE##_##NAME##_DD, NAME) { \ + for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \ + for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \ + SCOPED_TRACE(testing::Message() \ + << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \ + << #CASES2 " case #" << j << ": " << CASES2[j]); \ + DoSingleCase(CASES1[i], CASES2[j]); \ + } \ + } \ + } \ + \ + template <typename CaseType1, typename CaseType2> \ + void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \ const CaseType2& CASES2##_case) // ------------------------------------------------------------------- @@ -123,7 +123,7 @@ namespace { class TestInputStream : public ZeroCopyInputStream { public: TestInputStream(const void* data, int size, int block_size) - : array_stream_(data, size, block_size), counter_(0) {} + : array_stream_(data, size, block_size), counter_(0) {} ~TestInputStream() {} // implements ZeroCopyInputStream ---------------------------------- @@ -141,9 +141,9 @@ class TestInputStream : public ZeroCopyInputStream { } } - void BackUp(int count) { return array_stream_.BackUp(count); } - bool Skip(int count) { return array_stream_.Skip(count); } - int64 ByteCount() const { return array_stream_.ByteCount(); } + void BackUp(int count) { return array_stream_.BackUp(count); } + bool Skip(int count) { return array_stream_.Skip(count); } + int64 ByteCount() const { return array_stream_.ByteCount(); } private: ArrayInputStream array_stream_; @@ -163,8 +163,7 @@ class TestErrorCollector : public ErrorCollector { // implements ErrorCollector --------------------------------------- void AddError(int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line, column, message); + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line, column, message); } }; @@ -205,52 +204,51 @@ inline std::ostream& operator<<(std::ostream& out, } SimpleTokenCase kSimpleTokenCases[] = { - // Test identifiers. - { "hello", Tokenizer::TYPE_IDENTIFIER }, - - // Test integers. - { "123", Tokenizer::TYPE_INTEGER }, - { "0xab6", Tokenizer::TYPE_INTEGER }, - { "0XAB6", Tokenizer::TYPE_INTEGER }, - { "0X1234567", Tokenizer::TYPE_INTEGER }, - { "0x89abcdef", Tokenizer::TYPE_INTEGER }, - { "0x89ABCDEF", Tokenizer::TYPE_INTEGER }, - { "01234567", Tokenizer::TYPE_INTEGER }, - - // Test floats. - { "123.45", Tokenizer::TYPE_FLOAT }, - { "1.", Tokenizer::TYPE_FLOAT }, - { "1e3", Tokenizer::TYPE_FLOAT }, - { "1E3", Tokenizer::TYPE_FLOAT }, - { "1e-3", Tokenizer::TYPE_FLOAT }, - { "1e+3", Tokenizer::TYPE_FLOAT }, - { "1.e3", Tokenizer::TYPE_FLOAT }, - { "1.2e3", Tokenizer::TYPE_FLOAT }, - { ".1", Tokenizer::TYPE_FLOAT }, - { ".1e3", Tokenizer::TYPE_FLOAT }, - { ".1e-3", Tokenizer::TYPE_FLOAT }, - { ".1e+3", Tokenizer::TYPE_FLOAT }, - - // Test strings. - { "'hello'", Tokenizer::TYPE_STRING }, - { "\"foo\"", Tokenizer::TYPE_STRING }, - { "'a\"b'", Tokenizer::TYPE_STRING }, - { "\"a'b\"", Tokenizer::TYPE_STRING }, - { "'a\\'b'", Tokenizer::TYPE_STRING }, - { "\"a\\\"b\"", Tokenizer::TYPE_STRING }, - { "'\\xf'", Tokenizer::TYPE_STRING }, - { "'\\0'", Tokenizer::TYPE_STRING }, - - // Test symbols. - { "+", Tokenizer::TYPE_SYMBOL }, - { ".", Tokenizer::TYPE_SYMBOL }, + // Test identifiers. + {"hello", Tokenizer::TYPE_IDENTIFIER}, + + // Test integers. + {"123", Tokenizer::TYPE_INTEGER}, + {"0xab6", Tokenizer::TYPE_INTEGER}, + {"0XAB6", Tokenizer::TYPE_INTEGER}, + {"0X1234567", Tokenizer::TYPE_INTEGER}, + {"0x89abcdef", Tokenizer::TYPE_INTEGER}, + {"0x89ABCDEF", Tokenizer::TYPE_INTEGER}, + {"01234567", Tokenizer::TYPE_INTEGER}, + + // Test floats. + {"123.45", Tokenizer::TYPE_FLOAT}, + {"1.", Tokenizer::TYPE_FLOAT}, + {"1e3", Tokenizer::TYPE_FLOAT}, + {"1E3", Tokenizer::TYPE_FLOAT}, + {"1e-3", Tokenizer::TYPE_FLOAT}, + {"1e+3", Tokenizer::TYPE_FLOAT}, + {"1.e3", Tokenizer::TYPE_FLOAT}, + {"1.2e3", Tokenizer::TYPE_FLOAT}, + {".1", Tokenizer::TYPE_FLOAT}, + {".1e3", Tokenizer::TYPE_FLOAT}, + {".1e-3", Tokenizer::TYPE_FLOAT}, + {".1e+3", Tokenizer::TYPE_FLOAT}, + + // Test strings. + {"'hello'", Tokenizer::TYPE_STRING}, + {"\"foo\"", Tokenizer::TYPE_STRING}, + {"'a\"b'", Tokenizer::TYPE_STRING}, + {"\"a'b\"", Tokenizer::TYPE_STRING}, + {"'a\\'b'", Tokenizer::TYPE_STRING}, + {"\"a\\\"b\"", Tokenizer::TYPE_STRING}, + {"'\\xf'", Tokenizer::TYPE_STRING}, + {"'\\0'", Tokenizer::TYPE_STRING}, + + // Test symbols. + {"+", Tokenizer::TYPE_SYMBOL}, + {".", Tokenizer::TYPE_SYMBOL}, }; TEST_2D(TokenizerTest, SimpleTokens, kSimpleTokenCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kSimpleTokenCases_case.input.data(), - kSimpleTokenCases_case.input.size(), - kBlockSizes_case); + kSimpleTokenCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); @@ -339,92 +337,101 @@ inline std::ostream& operator<<(std::ostream& out, } MultiTokenCase kMultiTokenCases[] = { - // Test empty input. - { "", { - { Tokenizer::TYPE_END , "" , 0, 0, 0 }, - }}, - - // Test all token types at the same time. - { "foo 1 1.2 + 'bar'", { - { Tokenizer::TYPE_IDENTIFIER, "foo" , 0, 0, 3 }, - { Tokenizer::TYPE_INTEGER , "1" , 0, 4, 5 }, - { Tokenizer::TYPE_FLOAT , "1.2" , 0, 6, 9 }, - { Tokenizer::TYPE_SYMBOL , "+" , 0, 10, 11 }, - { Tokenizer::TYPE_STRING , "'bar'", 0, 12, 17 }, - { Tokenizer::TYPE_END , "" , 0, 17, 17 }, - }}, - - // Test that consecutive symbols are parsed as separate tokens. - { "!@+%", { - { Tokenizer::TYPE_SYMBOL , "!" , 0, 0, 1 }, - { Tokenizer::TYPE_SYMBOL , "@" , 0, 1, 2 }, - { Tokenizer::TYPE_SYMBOL , "+" , 0, 2, 3 }, - { Tokenizer::TYPE_SYMBOL , "%" , 0, 3, 4 }, - { Tokenizer::TYPE_END , "" , 0, 4, 4 }, - }}, - - // Test that newlines affect line numbers correctly. - { "foo bar\nrab oof", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 4, 7 }, - { Tokenizer::TYPE_IDENTIFIER, "rab", 1, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "oof", 1, 4, 7 }, - { Tokenizer::TYPE_END , "" , 1, 7, 7 }, - }}, - - // Test that tabs affect column numbers correctly. - { "foo\tbar \tbaz", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 8, 11 }, - { Tokenizer::TYPE_IDENTIFIER, "baz", 0, 16, 19 }, - { Tokenizer::TYPE_END , "" , 0, 19, 19 }, - }}, - - // Test that tabs in string literals affect column numbers correctly. - { "\"foo\tbar\" baz", { - { Tokenizer::TYPE_STRING , "\"foo\tbar\"", 0, 0, 12 }, - { Tokenizer::TYPE_IDENTIFIER, "baz" , 0, 13, 16 }, - { Tokenizer::TYPE_END , "" , 0, 16, 16 }, - }}, - - // Test that line comments are ignored. - { "foo // This is a comment\n" - "bar // This is another comment", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 1, 0, 3 }, - { Tokenizer::TYPE_END , "" , 1, 30, 30 }, - }}, - - // Test that block comments are ignored. - { "foo /* This is a block comment */ bar", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 34, 37 }, - { Tokenizer::TYPE_END , "" , 0, 37, 37 }, - }}, - - // Test that sh-style comments are not ignored by default. - { "foo # bar\n" - "baz", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_SYMBOL , "#" , 0, 4, 5 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 0, 6, 9 }, - { Tokenizer::TYPE_IDENTIFIER, "baz", 1, 0, 3 }, - { Tokenizer::TYPE_END , "" , 1, 3, 3 }, - }}, - - // Test all whitespace chars - { "foo\n\t\r\v\fbar", { - { Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3 }, - { Tokenizer::TYPE_IDENTIFIER, "bar", 1, 11, 14 }, - { Tokenizer::TYPE_END , "" , 1, 14, 14 }, - }}, + // Test empty input. + {"", + { + {Tokenizer::TYPE_END, "", 0, 0, 0}, + }}, + + // Test all token types at the same time. + {"foo 1 1.2 + 'bar'", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_INTEGER, "1", 0, 4, 5}, + {Tokenizer::TYPE_FLOAT, "1.2", 0, 6, 9}, + {Tokenizer::TYPE_SYMBOL, "+", 0, 10, 11}, + {Tokenizer::TYPE_STRING, "'bar'", 0, 12, 17}, + {Tokenizer::TYPE_END, "", 0, 17, 17}, + }}, + + // Test that consecutive symbols are parsed as separate tokens. + {"!@+%", + { + {Tokenizer::TYPE_SYMBOL, "!", 0, 0, 1}, + {Tokenizer::TYPE_SYMBOL, "@", 0, 1, 2}, + {Tokenizer::TYPE_SYMBOL, "+", 0, 2, 3}, + {Tokenizer::TYPE_SYMBOL, "%", 0, 3, 4}, + {Tokenizer::TYPE_END, "", 0, 4, 4}, + }}, + + // Test that newlines affect line numbers correctly. + {"foo bar\nrab oof", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 4, 7}, + {Tokenizer::TYPE_IDENTIFIER, "rab", 1, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "oof", 1, 4, 7}, + {Tokenizer::TYPE_END, "", 1, 7, 7}, + }}, + + // Test that tabs affect column numbers correctly. + {"foo\tbar \tbaz", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 8, 11}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 0, 16, 19}, + {Tokenizer::TYPE_END, "", 0, 19, 19}, + }}, + + // Test that tabs in string literals affect column numbers correctly. + {"\"foo\tbar\" baz", + { + {Tokenizer::TYPE_STRING, "\"foo\tbar\"", 0, 0, 12}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 0, 13, 16}, + {Tokenizer::TYPE_END, "", 0, 16, 16}, + }}, + + // Test that line comments are ignored. + {"foo // This is a comment\n" + "bar // This is another comment", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 1, 0, 3}, + {Tokenizer::TYPE_END, "", 1, 30, 30}, + }}, + + // Test that block comments are ignored. + {"foo /* This is a block comment */ bar", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 34, 37}, + {Tokenizer::TYPE_END, "", 0, 37, 37}, + }}, + + // Test that sh-style comments are not ignored by default. + {"foo # bar\n" + "baz", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_SYMBOL, "#", 0, 4, 5}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 0, 6, 9}, + {Tokenizer::TYPE_IDENTIFIER, "baz", 1, 0, 3}, + {Tokenizer::TYPE_END, "", 1, 3, 3}, + }}, + + // Test all whitespace chars + {"foo\n\t\r\v\fbar", + { + {Tokenizer::TYPE_IDENTIFIER, "foo", 0, 0, 3}, + {Tokenizer::TYPE_IDENTIFIER, "bar", 1, 11, 14}, + {Tokenizer::TYPE_END, "", 1, 14, 14}, + }}, }; TEST_2D(TokenizerTest, MultipleTokens, kMultiTokenCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kMultiTokenCases_case.input.data(), - kMultiTokenCases_case.input.size(), - kBlockSizes_case); + kMultiTokenCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); @@ -479,14 +486,14 @@ TEST_2D(TokenizerTest, MultipleTokens, kMultiTokenCases, kBlockSizes) { TEST_1D(TokenizerTest, ShCommentStyle, kBlockSizes) { // Test the "comment_style" option. - const char* text = "foo # bar\n" - "baz // qux\n" - "corge /* grault */\n" - "garply"; + const char* text = + "foo # bar\n" + "baz // qux\n" + "corge /* grault */\n" + "garply"; const char* const kTokens[] = {"foo", // "# bar" is ignored - "baz", "/", "/", "qux", - "corge", "/", "*", "grault", "*", "/", - "garply"}; + "baz", "/", "/", "qux", "corge", "/", + "*", "grault", "*", "/", "garply"}; // Set up the tokenizer. TestInputStream input(text, strlen(text), kBlockSizes_case); @@ -526,164 +533,136 @@ inline std::ostream& operator<<(std::ostream& out, } DocCommentCase kDocCommentCases[] = { - { - "prev next", - - "", - {}, - "" - }, - - { - "prev /* ignored */ next", - - "", - {}, - "" - }, - - { - "prev // trailing comment\n" - "next", - - " trailing comment\n", - {}, - "" - }, - - { - "prev\n" - "// leading comment\n" - "// line 2\n" - "next", - - "", - {}, - " leading comment\n" - " line 2\n" - }, - - { - "prev\n" - "// trailing comment\n" - "// line 2\n" - "\n" - "next", - - " trailing comment\n" - " line 2\n", - {}, - "" - }, - - { - "prev // trailing comment\n" - "// leading comment\n" - "// line 2\n" - "next", - - " trailing comment\n", - {}, - " leading comment\n" - " line 2\n" - }, - - { - "prev /* trailing block comment */\n" - "/* leading block comment\n" - " * line 2\n" - " * line 3 */" - "next", - - " trailing block comment ", - {}, - " leading block comment\n" - " line 2\n" - " line 3 " - }, - - { - "prev\n" - "/* trailing block comment\n" - " * line 2\n" - " * line 3\n" - " */\n" - "/* leading block comment\n" - " * line 2\n" - " * line 3 */" - "next", - - " trailing block comment\n" - " line 2\n" - " line 3\n", - {}, - " leading block comment\n" - " line 2\n" - " line 3 " - }, - - { - "prev\n" - "// trailing comment\n" - "\n" - "// detached comment\n" - "// line 2\n" - "\n" - "// second detached comment\n" - "/* third detached comment\n" - " * line 2 */\n" - "// leading comment\n" - "next", - - " trailing comment\n", - { - " detached comment\n" - " line 2\n", - " second detached comment\n", - " third detached comment\n" - " line 2 " - }, - " leading comment\n" - }, - - { - "prev /**/\n" - "\n" - "// detached comment\n" - "\n" - "// leading comment\n" - "next", - - "", - { - " detached comment\n" - }, - " leading comment\n" - }, - - { - "prev /**/\n" - "// leading comment\n" - "next", - - "", - {}, - " leading comment\n" - }, - }; + {"prev next", + + "", + {}, + ""}, + + {"prev /* ignored */ next", + + "", + {}, + ""}, + + {"prev // trailing comment\n" + "next", + + " trailing comment\n", + {}, + ""}, + + {"prev\n" + "// leading comment\n" + "// line 2\n" + "next", + + "", + {}, + " leading comment\n" + " line 2\n"}, + + {"prev\n" + "// trailing comment\n" + "// line 2\n" + "\n" + "next", + + " trailing comment\n" + " line 2\n", + {}, + ""}, + + {"prev // trailing comment\n" + "// leading comment\n" + "// line 2\n" + "next", + + " trailing comment\n", + {}, + " leading comment\n" + " line 2\n"}, + + {"prev /* trailing block comment */\n" + "/* leading block comment\n" + " * line 2\n" + " * line 3 */" + "next", + + " trailing block comment ", + {}, + " leading block comment\n" + " line 2\n" + " line 3 "}, + + {"prev\n" + "/* trailing block comment\n" + " * line 2\n" + " * line 3\n" + " */\n" + "/* leading block comment\n" + " * line 2\n" + " * line 3 */" + "next", + + " trailing block comment\n" + " line 2\n" + " line 3\n", + {}, + " leading block comment\n" + " line 2\n" + " line 3 "}, + + {"prev\n" + "// trailing comment\n" + "\n" + "// detached comment\n" + "// line 2\n" + "\n" + "// second detached comment\n" + "/* third detached comment\n" + " * line 2 */\n" + "// leading comment\n" + "next", + + " trailing comment\n", + {" detached comment\n" + " line 2\n", + " second detached comment\n", + " third detached comment\n" + " line 2 "}, + " leading comment\n"}, + + {"prev /**/\n" + "\n" + "// detached comment\n" + "\n" + "// leading comment\n" + "next", + + "", + {" detached comment\n"}, + " leading comment\n"}, + + {"prev /**/\n" + "// leading comment\n" + "next", + + "", + {}, + " leading comment\n"}, +}; TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kDocCommentCases_case.input.data(), - kDocCommentCases_case.input.size(), - kBlockSizes_case); + kDocCommentCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); // Set up a second tokenizer where we'll pass all NULLs to NextWithComments(). TestInputStream input2(kDocCommentCases_case.input.data(), - kDocCommentCases_case.input.size(), - kBlockSizes_case); + kDocCommentCases_case.input.size(), kBlockSizes_case); Tokenizer tokenizer2(&input2, &error_collector); tokenizer.Next(); @@ -707,16 +686,14 @@ TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { for (int i = 0; i < detached_comments.size(); i++) { ASSERT_LT(i, GOOGLE_ARRAYSIZE(kDocCommentCases)); ASSERT_TRUE(kDocCommentCases_case.detached_comments[i] != NULL); - EXPECT_EQ(kDocCommentCases_case.detached_comments[i], - detached_comments[i]); + EXPECT_EQ(kDocCommentCases_case.detached_comments[i], detached_comments[i]); } // Verify that we matched all the detached comments. EXPECT_EQ(NULL, - kDocCommentCases_case.detached_comments[detached_comments.size()]); + kDocCommentCases_case.detached_comments[detached_comments.size()]); - EXPECT_EQ(kDocCommentCases_case.next_leading_comments, - next_leading_comments); + EXPECT_EQ(kDocCommentCases_case.next_leading_comments, next_leading_comments); } // ------------------------------------------------------------------- @@ -745,30 +722,30 @@ TEST_F(TokenizerTest, ParseInteger) { EXPECT_FALSE(Tokenizer::ParseInteger("-1", kuint64max, &i)); // Test overflows. - EXPECT_TRUE (Tokenizer::ParseInteger("0", 0, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("0", 0, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("1", 0, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("1", 1, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("12345", 12345, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("1", 1, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("12345", 12345, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("12346", 12345, &i)); - EXPECT_TRUE (Tokenizer::ParseInteger("0xFFFFFFFFFFFFFFFF" , kuint64max, &i)); + EXPECT_TRUE(Tokenizer::ParseInteger("0xFFFFFFFFFFFFFFFF", kuint64max, &i)); EXPECT_FALSE(Tokenizer::ParseInteger("0x10000000000000000", kuint64max, &i)); } TEST_F(TokenizerTest, ParseFloat) { - EXPECT_DOUBLE_EQ(1 , Tokenizer::ParseFloat("1.")); - EXPECT_DOUBLE_EQ(1e3 , Tokenizer::ParseFloat("1e3")); - EXPECT_DOUBLE_EQ(1e3 , Tokenizer::ParseFloat("1E3")); + EXPECT_DOUBLE_EQ(1, Tokenizer::ParseFloat("1.")); + EXPECT_DOUBLE_EQ(1e3, Tokenizer::ParseFloat("1e3")); + EXPECT_DOUBLE_EQ(1e3, Tokenizer::ParseFloat("1E3")); EXPECT_DOUBLE_EQ(1.5e3, Tokenizer::ParseFloat("1.5e3")); - EXPECT_DOUBLE_EQ(.1 , Tokenizer::ParseFloat(".1")); - EXPECT_DOUBLE_EQ(.25 , Tokenizer::ParseFloat(".25")); - EXPECT_DOUBLE_EQ(.1e3 , Tokenizer::ParseFloat(".1e3")); + EXPECT_DOUBLE_EQ(.1, Tokenizer::ParseFloat(".1")); + EXPECT_DOUBLE_EQ(.25, Tokenizer::ParseFloat(".25")); + EXPECT_DOUBLE_EQ(.1e3, Tokenizer::ParseFloat(".1e3")); EXPECT_DOUBLE_EQ(.25e3, Tokenizer::ParseFloat(".25e3")); EXPECT_DOUBLE_EQ(.1e+3, Tokenizer::ParseFloat(".1e+3")); EXPECT_DOUBLE_EQ(.1e-3, Tokenizer::ParseFloat(".1e-3")); - EXPECT_DOUBLE_EQ(5 , Tokenizer::ParseFloat("5")); + EXPECT_DOUBLE_EQ(5, Tokenizer::ParseFloat("5")); EXPECT_DOUBLE_EQ(6e-12, Tokenizer::ParseFloat("6e-12")); - EXPECT_DOUBLE_EQ(1.2 , Tokenizer::ParseFloat("1.2")); - EXPECT_DOUBLE_EQ(1.e2 , Tokenizer::ParseFloat("1.e2")); + EXPECT_DOUBLE_EQ(1.2, Tokenizer::ParseFloat("1.2")); + EXPECT_DOUBLE_EQ(1.e2, Tokenizer::ParseFloat("1.e2")); // Test invalid integers that may still be tokenized as integers. EXPECT_DOUBLE_EQ(1, Tokenizer::ParseFloat("1e")); @@ -782,17 +759,20 @@ TEST_F(TokenizerTest, ParseFloat) { // These should parse successfully even though they are out of range. // Overflows become infinity and underflows become zero. - EXPECT_EQ( 0.0, Tokenizer::ParseFloat("1e-9999999999999999999999999999")); + EXPECT_EQ(0.0, Tokenizer::ParseFloat("1e-9999999999999999999999999999")); EXPECT_EQ(HUGE_VAL, Tokenizer::ParseFloat("1e+9999999999999999999999999999")); #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet // Test invalid integers that will never be tokenized as integers. - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("zxy"), - "passed text that could not have been tokenized as a float"); - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("1-e0"), - "passed text that could not have been tokenized as a float"); - EXPECT_DEBUG_DEATH(Tokenizer::ParseFloat("-1.0"), - "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("zxy"), + "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("1-e0"), + "passed text that could not have been tokenized as a float"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseFloat("-1.0"), + "passed text that could not have been tokenized as a float"); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -833,8 +813,9 @@ TEST_F(TokenizerTest, ParseString) { // Test invalid strings that will never be tokenized as strings. #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet - EXPECT_DEBUG_DEATH(Tokenizer::ParseString("", &output), - "passed text that could not have been tokenized as a string"); + EXPECT_DEBUG_DEATH( + Tokenizer::ParseString("", &output), + "passed text that could not have been tokenized as a string"); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -934,8 +915,7 @@ ErrorCase kErrorCases[] = { TEST_2D(TokenizerTest, Errors, kErrorCases, kBlockSizes) { // Set up the tokenizer. TestInputStream input(kErrorCases_case.input.data(), - kErrorCases_case.input.size(), - kBlockSizes_case); + kErrorCases_case.input.size(), kBlockSizes_case); TestErrorCollector error_collector; Tokenizer tokenizer(&input, &error_collector); diff --git a/src/google/protobuf/io/zero_copy_stream_impl.cc b/src/google/protobuf/io/zero_copy_stream_impl.cc index 72328da9e6..4af76b786e 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl.cc @@ -33,19 +33,19 @@ // Sanjay Ghemawat, Jeff Dean, and others. #ifndef _MSC_VER -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> #include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #endif #include <errno.h> -#include <iostream> #include <algorithm> -#include <google/protobuf/io/zero_copy_stream_impl.h> +#include <iostream> #include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/logging.h> +#include <google/protobuf/io/io_win32.h> +#include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/stubs/stl_util.h> -#include <google/protobuf/stubs/io_win32.h> namespace google { @@ -58,11 +58,11 @@ namespace io { #define lseek(fd, offset, origin) ((off_t)-1) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::read; -using google::protobuf::internal::win32::write; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::open; +using google::protobuf::io::win32::read; +using google::protobuf::io::win32::write; #endif namespace { @@ -81,38 +81,27 @@ int close_no_eintr(int fd) { // =================================================================== FileInputStream::FileInputStream(int file_descriptor, int block_size) - : copying_input_(file_descriptor), - impl_(©ing_input_, block_size) { -} + : copying_input_(file_descriptor), impl_(©ing_input_, block_size) {} -bool FileInputStream::Close() { - return copying_input_.Close(); -} +bool FileInputStream::Close() { return copying_input_.Close(); } bool FileInputStream::Next(const void** data, int* size) { return impl_.Next(data, size); } -void FileInputStream::BackUp(int count) { - impl_.BackUp(count); -} +void FileInputStream::BackUp(int count) { impl_.BackUp(count); } -bool FileInputStream::Skip(int count) { - return impl_.Skip(count); -} +bool FileInputStream::Skip(int count) { return impl_.Skip(count); } -int64 FileInputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 FileInputStream::ByteCount() const { return impl_.ByteCount(); } FileInputStream::CopyingFileInputStream::CopyingFileInputStream( int file_descriptor) - : file_(file_descriptor), - close_on_delete_(false), - is_closed_(false), - errno_(0), - previous_seek_failed_(false) { -} + : file_(file_descriptor), + close_on_delete_(false), + is_closed_(false), + errno_(0), + previous_seek_failed_(false) {} FileInputStream::CopyingFileInputStream::~CopyingFileInputStream() { if (close_on_delete_) { @@ -156,8 +145,7 @@ int FileInputStream::CopyingFileInputStream::Read(void* buffer, int size) { int FileInputStream::CopyingFileInputStream::Skip(int count) { GOOGLE_CHECK(!is_closed_); - if (!previous_seek_failed_ && - lseek(file_, count, SEEK_CUR) != (off_t)-1) { + if (!previous_seek_failed_ && lseek(file_, count, SEEK_CUR) != (off_t)-1) { // Seek succeeded. return count; } else { @@ -175,42 +163,31 @@ int FileInputStream::CopyingFileInputStream::Skip(int count) { // =================================================================== FileOutputStream::FileOutputStream(int file_descriptor, int block_size) - : copying_output_(file_descriptor), - impl_(©ing_output_, block_size) { -} + : copying_output_(file_descriptor), impl_(©ing_output_, block_size) {} -FileOutputStream::~FileOutputStream() { - impl_.Flush(); -} +FileOutputStream::~FileOutputStream() { impl_.Flush(); } bool FileOutputStream::Close() { bool flush_succeeded = impl_.Flush(); return copying_output_.Close() && flush_succeeded; } -bool FileOutputStream::Flush() { - return impl_.Flush(); -} +bool FileOutputStream::Flush() { return impl_.Flush(); } bool FileOutputStream::Next(void** data, int* size) { return impl_.Next(data, size); } -void FileOutputStream::BackUp(int count) { - impl_.BackUp(count); -} +void FileOutputStream::BackUp(int count) { impl_.BackUp(count); } -int64 FileOutputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 FileOutputStream::ByteCount() const { return impl_.ByteCount(); } FileOutputStream::CopyingFileOutputStream::CopyingFileOutputStream( int file_descriptor) - : file_(file_descriptor), - close_on_delete_(false), - is_closed_(false), - errno_(0) { -} + : file_(file_descriptor), + close_on_delete_(false), + is_closed_(false), + errno_(0) {} FileOutputStream::CopyingFileOutputStream::~CopyingFileOutputStream() { if (close_on_delete_) { @@ -235,8 +212,8 @@ bool FileOutputStream::CopyingFileOutputStream::Close() { return true; } -bool FileOutputStream::CopyingFileOutputStream::Write( - const void* buffer, int size) { +bool FileOutputStream::CopyingFileOutputStream::Write(const void* buffer, + int size) { GOOGLE_CHECK(!is_closed_); int total_written = 0; @@ -279,17 +256,11 @@ bool IstreamInputStream::Next(const void** data, int* size) { return impl_.Next(data, size); } -void IstreamInputStream::BackUp(int count) { - impl_.BackUp(count); -} +void IstreamInputStream::BackUp(int count) { impl_.BackUp(count); } -bool IstreamInputStream::Skip(int count) { - return impl_.Skip(count); -} +bool IstreamInputStream::Skip(int count) { return impl_.Skip(count); } -int64 IstreamInputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 IstreamInputStream::ByteCount() const { return impl_.ByteCount(); } IstreamInputStream::CopyingIstreamInputStream::CopyingIstreamInputStream( std::istream* input) @@ -297,8 +268,8 @@ IstreamInputStream::CopyingIstreamInputStream::CopyingIstreamInputStream( IstreamInputStream::CopyingIstreamInputStream::~CopyingIstreamInputStream() {} -int IstreamInputStream::CopyingIstreamInputStream::Read( - void* buffer, int size) { +int IstreamInputStream::CopyingIstreamInputStream::Read(void* buffer, + int size) { input_->read(reinterpret_cast<char*>(buffer), size); int result = input_->gcount(); if (result == 0 && input_->fail() && !input_->eof()) { @@ -312,21 +283,15 @@ int IstreamInputStream::CopyingIstreamInputStream::Read( OstreamOutputStream::OstreamOutputStream(std::ostream* output, int block_size) : copying_output_(output), impl_(©ing_output_, block_size) {} -OstreamOutputStream::~OstreamOutputStream() { - impl_.Flush(); -} +OstreamOutputStream::~OstreamOutputStream() { impl_.Flush(); } bool OstreamOutputStream::Next(void** data, int* size) { return impl_.Next(data, size); } -void OstreamOutputStream::BackUp(int count) { - impl_.BackUp(count); -} +void OstreamOutputStream::BackUp(int count) { impl_.BackUp(count); } -int64 OstreamOutputStream::ByteCount() const { - return impl_.ByteCount(); -} +int64 OstreamOutputStream::ByteCount() const { return impl_.ByteCount(); } OstreamOutputStream::CopyingOstreamOutputStream::CopyingOstreamOutputStream( std::ostream* output) @@ -335,8 +300,8 @@ OstreamOutputStream::CopyingOstreamOutputStream::CopyingOstreamOutputStream( OstreamOutputStream::CopyingOstreamOutputStream::~CopyingOstreamOutputStream() { } -bool OstreamOutputStream::CopyingOstreamOutputStream::Write( - const void* buffer, int size) { +bool OstreamOutputStream::CopyingOstreamOutputStream::Write(const void* buffer, + int size) { output_->write(reinterpret_cast<const char*>(buffer), size); return output_->good(); } @@ -345,7 +310,7 @@ bool OstreamOutputStream::CopyingOstreamOutputStream::Write( ConcatenatingInputStream::ConcatenatingInputStream( ZeroCopyInputStream* const streams[], int count) - : streams_(streams), stream_count_(count), bytes_retired_(0) { + : streams_(streams), stream_count_(count), bytes_retired_(0) { } bool ConcatenatingInputStream::Next(const void** data, int* size) { diff --git a/src/google/protobuf/io/zero_copy_stream_impl.h b/src/google/protobuf/io/zero_copy_stream_impl.h index f2d26886f7..fd06c06cc2 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl.h +++ b/src/google/protobuf/io/zero_copy_stream_impl.h @@ -40,11 +40,11 @@ #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ -#include <string> #include <iosfwd> +#include <string> +#include <google/protobuf/stubs/common.h> #include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/zero_copy_stream_impl_lite.h> -#include <google/protobuf/stubs/common.h> #include <google/protobuf/port_def.inc> diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc index f5f340f6b9..69ceaab6b8 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc @@ -55,14 +55,12 @@ static const int kDefaultBlockSize = 8192; // =================================================================== -ArrayInputStream::ArrayInputStream(const void* data, int size, - int block_size) - : data_(reinterpret_cast<const uint8*>(data)), - size_(size), - block_size_(block_size > 0 ? block_size : size), - position_(0), - last_returned_size_(0) { -} +ArrayInputStream::ArrayInputStream(const void* data, int size, int block_size) + : data_(reinterpret_cast<const uint8*>(data)), + size_(size), + block_size_(block_size > 0 ? block_size : size), + position_(0), + last_returned_size_(0) {} bool ArrayInputStream::Next(const void** data, int* size) { if (position_ < size_) { @@ -73,7 +71,7 @@ bool ArrayInputStream::Next(const void** data, int* size) { return true; } else { // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. return false; } } @@ -89,7 +87,7 @@ void ArrayInputStream::BackUp(int count) { bool ArrayInputStream::Skip(int count) { GOOGLE_CHECK_GE(count, 0); - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. if (count > size_ - position_) { position_ = size_; return false; @@ -99,20 +97,17 @@ bool ArrayInputStream::Skip(int count) { } } -int64 ArrayInputStream::ByteCount() const { - return position_; -} +int64 ArrayInputStream::ByteCount() const { return position_; } // =================================================================== ArrayOutputStream::ArrayOutputStream(void* data, int size, int block_size) - : data_(reinterpret_cast<uint8*>(data)), - size_(size), - block_size_(block_size > 0 ? block_size : size), - position_(0), - last_returned_size_(0) { -} + : data_(reinterpret_cast<uint8*>(data)), + size_(size), + block_size_(block_size > 0 ? block_size : size), + position_(0), + last_returned_size_(0) {} bool ArrayOutputStream::Next(void** data, int* size) { if (position_ < size_) { @@ -123,7 +118,7 @@ bool ArrayOutputStream::Next(void** data, int* size) { return true; } else { // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. + last_returned_size_ = 0; // Don't let caller back up. return false; } } @@ -137,9 +132,7 @@ void ArrayOutputStream::BackUp(int count) { last_returned_size_ = 0; // Don't let caller back up further. } -int64 ArrayOutputStream::ByteCount() const { - return position_; -} +int64 ArrayOutputStream::ByteCount() const { return position_; } // =================================================================== @@ -209,14 +202,13 @@ int CopyingInputStream::Skip(int count) { CopyingInputStreamAdaptor::CopyingInputStreamAdaptor( CopyingInputStream* copying_stream, int block_size) - : copying_stream_(copying_stream), - owns_copying_stream_(false), - failed_(false), - position_(0), - buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), - buffer_used_(0), - backup_bytes_(0) { -} + : copying_stream_(copying_stream), + owns_copying_stream_(false), + failed_(false), + position_(0), + buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), + buffer_used_(0), + backup_bytes_(0) {} CopyingInputStreamAdaptor::~CopyingInputStreamAdaptor() { if (owns_copying_stream_) { @@ -260,12 +252,11 @@ bool CopyingInputStreamAdaptor::Next(const void** data, int* size) { void CopyingInputStreamAdaptor::BackUp(int count) { GOOGLE_CHECK(backup_bytes_ == 0 && buffer_.get() != NULL) - << " BackUp() can only be called after Next()."; + << " BackUp() can only be called after Next()."; GOOGLE_CHECK_LE(count, buffer_used_) - << " Can't back up over more bytes than were returned by the last call" - " to Next()."; - GOOGLE_CHECK_GE(count, 0) - << " Parameter to BackUp() can't be negative."; + << " Can't back up over more bytes than were returned by the last call" + " to Next()."; + GOOGLE_CHECK_GE(count, 0) << " Parameter to BackUp() can't be negative."; backup_bytes_ = count; } @@ -313,13 +304,12 @@ void CopyingInputStreamAdaptor::FreeBuffer() { CopyingOutputStreamAdaptor::CopyingOutputStreamAdaptor( CopyingOutputStream* copying_stream, int block_size) - : copying_stream_(copying_stream), - owns_copying_stream_(false), - failed_(false), - position_(0), - buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), - buffer_used_(0) { -} + : copying_stream_(copying_stream), + owns_copying_stream_(false), + failed_(false), + position_(0), + buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), + buffer_used_(0) {} CopyingOutputStreamAdaptor::~CopyingOutputStreamAdaptor() { WriteBuffer(); @@ -328,9 +318,7 @@ CopyingOutputStreamAdaptor::~CopyingOutputStreamAdaptor() { } } -bool CopyingOutputStreamAdaptor::Flush() { - return WriteBuffer(); -} +bool CopyingOutputStreamAdaptor::Flush() { return WriteBuffer(); } bool CopyingOutputStreamAdaptor::Next(void** data, int* size) { if (buffer_used_ == buffer_size_) { @@ -348,10 +336,10 @@ bool CopyingOutputStreamAdaptor::Next(void** data, int* size) { void CopyingOutputStreamAdaptor::BackUp(int count) { GOOGLE_CHECK_GE(count, 0); GOOGLE_CHECK_EQ(buffer_used_, buffer_size_) - << " BackUp() can only be called after Next()."; + << " BackUp() can only be called after Next()."; GOOGLE_CHECK_LE(count, buffer_used_) - << " Can't back up over more bytes than were returned by the last call" - " to Next()."; + << " Can't back up over more bytes than were returned by the last call" + " to Next()."; buffer_used_ -= count; } diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.h b/src/google/protobuf/io/zero_copy_stream_impl_lite.h index 6c14abcfcc..14144b558d 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.h +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.h @@ -44,12 +44,12 @@ #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ +#include <iosfwd> #include <memory> #include <string> -#include <iosfwd> -#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/stubs/callback.h> #include <google/protobuf/stubs/common.h> +#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/stubs/stl_util.h> @@ -87,8 +87,8 @@ class PROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream { const int block_size_; // How many bytes to return at a time. int position_; - int last_returned_size_; // How many bytes we returned last time Next() - // was called (used for error checking only). + int last_returned_size_; // How many bytes we returned last time Next() + // was called (used for error checking only). GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream); }; @@ -114,13 +114,13 @@ class PROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream { int64 ByteCount() const override; private: - uint8* const data_; // The byte array. - const int size_; // Total size of the array. - const int block_size_; // How many bytes to return at a time. + uint8* const data_; // The byte array. + const int size_; // Total size of the array. + const int block_size_; // How many bytes to return at a time. int position_; - int last_returned_size_; // How many bytes we returned last time Next() - // was called (used for error checking only). + int last_returned_size_; // How many bytes we returned last time Next() + // was called (used for error checking only). GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream); }; @@ -394,7 +394,7 @@ inline char* mutable_string_data(std::string* s) { // Sometimes it's faster: in some scenarios p cannot be NULL, and then the // code can avoid that check. inline std::pair<char*, bool> as_string_data(std::string* s) { - char *p = mutable_string_data(s); + char* p = mutable_string_data(s); return std::make_pair(p, true); } diff --git a/src/google/protobuf/io/zero_copy_stream_unittest.cc b/src/google/protobuf/io/zero_copy_stream_unittest.cc index 8530d73343..5f7b6104b9 100644 --- a/src/google/protobuf/io/zero_copy_stream_unittest.cc +++ b/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -50,16 +50,17 @@ #ifndef _MSC_VER #include <unistd.h> #endif +#include <errno.h> +#include <fcntl.h> #include <stdlib.h> -#include <sys/types.h> #include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> +#include <sys/types.h> #include <memory> #include <sstream> #include <google/protobuf/testing/file.h> #include <google/protobuf/test_util2.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/zero_copy_stream_impl.h> @@ -69,10 +70,9 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/logging.h> -#include <google/protobuf/testing/googletest.h> #include <google/protobuf/testing/file.h> +#include <google/protobuf/testing/googletest.h> #include <gtest/gtest.h> -#include <google/protobuf/stubs/io_win32.h> namespace google { namespace protobuf { @@ -83,17 +83,17 @@ namespace { #define pipe(fds) _pipe(fds, 4096, O_BINARY) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; -using google::protobuf::internal::win32::close; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY #ifdef _O_BINARY #define O_BINARY _O_BINARY #else -#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. +#define O_BINARY 0 // If this isn't defined, the platform doesn't need it. #endif #endif @@ -137,8 +137,8 @@ class IoTest : public testing::Test { const int IoTest::kBlockSizes[] = {-1, 1, 2, 5, 7, 10, 23, 64}; const int IoTest::kBlockSizeCount = GOOGLE_ARRAYSIZE(IoTest::kBlockSizes); -bool IoTest::WriteToOutput(ZeroCopyOutputStream* output, - const void* data, int size) { +bool IoTest::WriteToOutput(ZeroCopyOutputStream* output, const void* data, + int size) { const uint8* in = reinterpret_cast<const uint8*>(data); int in_size = size; @@ -300,7 +300,7 @@ TEST_F(IoTest, TwoSessionWrite) { static const char* strA = "0123456789"; static const char* strB = "WhirledPeas"; - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; char* temp_buffer = new char[40]; @@ -314,8 +314,8 @@ TEST_F(IoTest, TwoSessionWrite) { delete coded_output; // flush int64 pos = output->ByteCount(); delete output; - output = new ArrayOutputStream( - buffer + pos, kBufferSize - pos, kBlockSizes[i]); + output = new ArrayOutputStream(buffer + pos, kBufferSize - pos, + kBlockSizes[i]); coded_output = new CodedOutputStream(output); coded_output->WriteVarint32(strlen(strB)); coded_output->WriteRaw(strB, strlen(strB)); @@ -342,13 +342,13 @@ TEST_F(IoTest, TwoSessionWrite) { } } - delete [] temp_buffer; - delete [] buffer; + delete[] temp_buffer; + delete[] buffer; } #if HAVE_ZLIB TEST_F(IoTest, GzipIo) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { @@ -369,18 +369,17 @@ TEST_F(IoTest, GzipIo) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoWithFlush) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; // We start with i = 4 as we want a block size > 6. With block size <= 6 // Flush() fills up the entire 2K buffer with flush markers and the test @@ -405,18 +404,17 @@ TEST_F(IoTest, GzipIoWithFlush) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoContiguousFlushes) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int block_size = kBlockSizes[4]; @@ -437,15 +435,14 @@ TEST_F(IoTest, GzipIoContiguousFlushes) { size = output.ByteCount(); ArrayInputStream input(buffer, size, block_size); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, GzipIoReadAfterFlush) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int block_size = kBlockSizes[4]; @@ -464,17 +461,16 @@ TEST_F(IoTest, GzipIoReadAfterFlush) { size = output.ByteCount(); ArrayInputStream input(buffer, size, block_size); - GzipInputStream gzin( - &input, GzipInputStream::GZIP, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::GZIP, gzip_buffer_size); ReadStuff(&gzin); gzout.Close(); - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, ZlibIo) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; for (int i = 0; i < kBlockSizeCount; i++) { for (int j = 0; j < kBlockSizeCount; j++) { @@ -495,18 +491,17 @@ TEST_F(IoTest, ZlibIo) { } { ArrayInputStream input(buffer, size, kBlockSizes[j]); - GzipInputStream gzin( - &input, GzipInputStream::ZLIB, gzip_buffer_size); + GzipInputStream gzin(&input, GzipInputStream::ZLIB, gzip_buffer_size); ReadStuff(&gzin); } } } } - delete [] buffer; + delete[] buffer; } TEST_F(IoTest, ZlibIoInputAutodetect) { - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; int size; { @@ -537,7 +532,7 @@ TEST_F(IoTest, ZlibIoInputAutodetect) { GzipInputStream gzin(&input, GzipInputStream::AUTO); ReadStuff(&gzin); } - delete [] buffer; + delete[] buffer; } std::string IoTest::Compress(const std::string& data, @@ -605,7 +600,7 @@ TEST_F(IoTest, TwoSessionWriteGzip) { static const char* strA = "0123456789"; static const char* strB = "QuickBrownFox"; - const int kBufferSize = 2*1024; + const int kBufferSize = 2 * 1024; uint8* buffer = new uint8[kBufferSize]; char* temp_buffer = new char[40]; @@ -619,18 +614,18 @@ TEST_F(IoTest, TwoSessionWriteGzip) { coded_output->WriteVarint32(outlen); coded_output->WriteRaw(strA, outlen); delete coded_output; // flush - delete gzout; // flush + delete gzout; // flush int64 pos = output->ByteCount(); delete output; - output = new ArrayOutputStream( - buffer + pos, kBufferSize - pos, kBlockSizes[i]); + output = new ArrayOutputStream(buffer + pos, kBufferSize - pos, + kBlockSizes[i]); gzout = new GzipOutputStream(output); coded_output = new CodedOutputStream(gzout); outlen = strlen(strB) + 1; coded_output->WriteVarint32(outlen); coded_output->WriteRaw(strB, outlen); delete coded_output; // flush - delete gzout; // flush + delete gzout; // flush int64 size = pos + output->ByteCount(); delete output; @@ -650,10 +645,8 @@ TEST_F(IoTest, TwoSessionWriteGzip) { EXPECT_TRUE(coded_input->ReadRaw(temp_buffer, insize)); EXPECT_EQ(0, memcmp(temp_buffer, strB, insize)) << " out_block_size=" << kBlockSizes[i] - << " in_block_size=" << kBlockSizes[j] - << " pos=" << pos - << " size=" << size - << " strB=" << strB << " in=" << temp_buffer; + << " in_block_size=" << kBlockSizes[j] << " pos=" << pos + << " size=" << size << " strB=" << strB << " in=" << temp_buffer; delete coded_input; delete gzin; @@ -661,8 +654,8 @@ TEST_F(IoTest, TwoSessionWriteGzip) { } } - delete [] temp_buffer; - delete [] buffer; + delete[] temp_buffer; + delete[] buffer; } TEST_F(IoTest, GzipInputByteCountAfterClosed) { @@ -727,7 +720,7 @@ TEST_F(IoTest, FileIo) { for (int j = 0; j < kBlockSizeCount; j++) { // Make a temporary file. int file = - open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); + open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); ASSERT_GE(file, 0); { @@ -758,7 +751,7 @@ TEST_F(IoTest, GzipFileIo) { for (int j = 0; j < kBlockSizeCount; j++) { // Make a temporary file. int file = - open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); + open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0777); ASSERT_GE(file, 0); { FileOutputStream output(file, kBlockSizes[i]); @@ -800,10 +793,8 @@ class MsvcDebugDisabler { old_mode_ = _CrtSetReportMode(_CRT_ASSERT, old_mode_); } - static void MyHandler(const wchar_t *expr, - const wchar_t *func, - const wchar_t *file, - unsigned int line, + static void MyHandler(const wchar_t* expr, const wchar_t* func, + const wchar_t* file, unsigned int line, uintptr_t pReserved) { // do nothing } @@ -931,19 +922,19 @@ TEST_F(IoTest, ConcatenatingInputStream) { // Now split it up into multiple streams of varying sizes. ASSERT_EQ(68, output.ByteCount()); // Test depends on this. - ArrayInputStream input1(buffer , 12); - ArrayInputStream input2(buffer + 12, 7); - ArrayInputStream input3(buffer + 19, 6); + ArrayInputStream input1(buffer, 12); + ArrayInputStream input2(buffer + 12, 7); + ArrayInputStream input3(buffer + 19, 6); ArrayInputStream input4(buffer + 25, 15); - ArrayInputStream input5(buffer + 40, 0); + ArrayInputStream input5(buffer + 40, 0); // Note: We want to make sure we have a stream boundary somewhere between // bytes 42 and 62, which is the range that it Skip()ed by ReadStuff(). This // tests that a bug that existed in the original code for Skip() is fixed. ArrayInputStream input6(buffer + 40, 10); ArrayInputStream input7(buffer + 50, 18); // Total = 68 bytes. - ZeroCopyInputStream* streams[] = - {&input1, &input2, &input3, &input4, &input5, &input6, &input7}; + ZeroCopyInputStream* streams[] = {&input1, &input2, &input3, &input4, + &input5, &input6, &input7}; // Create the concatenating stream and read. ConcatenatingInputStream input(streams, GOOGLE_ARRAYSIZE(streams)); @@ -986,7 +977,7 @@ TEST_F(IoTest, LimitingInputStreamByteCount) { LimitingInputStream input(&array_input, kHalfBufferSize - 1); EXPECT_EQ(0, input.ByteCount()); EXPECT_TRUE(input.Next(&data, &size)); - EXPECT_EQ(kHalfBufferSize - 1 , input.ByteCount()); + EXPECT_EQ(kHalfBufferSize - 1, input.ByteCount()); } // Check that a zero-size array doesn't confuse the code. diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index 65b2f523ed..c16bdf7e63 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -56,10 +56,9 @@ void ExpectMessageMerged(const unittest::TestAllTypesLite& message) { EXPECT_EQ(message.optional_string(), "hello"); } -void AssignParsingMergeMessages( - unittest::TestAllTypesLite* msg1, - unittest::TestAllTypesLite* msg2, - unittest::TestAllTypesLite* msg3) { +void AssignParsingMergeMessages(unittest::TestAllTypesLite* msg1, + unittest::TestAllTypesLite* msg2, + unittest::TestAllTypesLite* msg3) { msg1->set_optional_int32(1); msg2->set_optional_int64(2); msg3->set_optional_int32(3); @@ -174,10 +173,10 @@ TEST(Lite, AllLite5) { unittest::TestAllTypesLite* msg2; unittest::TestAllTypesLite* msg3; -#define ASSIGN_REPEATED_FIELD(FIELD) \ - msg1 = generator.add_##FIELD(); \ - msg2 = generator.add_##FIELD(); \ - msg3 = generator.add_##FIELD(); \ +#define ASSIGN_REPEATED_FIELD(FIELD) \ + msg1 = generator.add_##FIELD(); \ + msg2 = generator.add_##FIELD(); \ + msg3 = generator.add_##FIELD(); \ AssignParsingMergeMessages(msg1, msg2, msg3) ASSIGN_REPEATED_FIELD(field1); @@ -447,8 +446,7 @@ TEST(Lite, AllLite18) { protobuf_unittest::TestMessageMapLite message; // Creates a TestAllTypes with default value - TestUtilLite::ExpectClear( - (*message.mutable_map_int32_message())[0]); + TestUtilLite::ExpectClear((*message.mutable_map_int32_message())[0]); } } @@ -836,30 +834,30 @@ TEST(Lite, AllLite42) { std::string data; { - // Check that adding more values to enum does not corrupt message - // when passed through an old client. - protobuf_unittest::V2MessageLite v2_message; - v2_message.set_int_field(800); - // Set enum field to the value not understood by the old client. - v2_message.set_enum_field(protobuf_unittest::V2_SECOND); - std::string v2_bytes = v2_message.SerializeAsString(); - - protobuf_unittest::V1MessageLite v1_message; - v1_message.ParseFromString(v2_bytes); - EXPECT_TRUE(v1_message.IsInitialized()); - EXPECT_EQ(v1_message.int_field(), v2_message.int_field()); - // V1 client does not understand V2_SECOND value, so it discards it and - // uses default value instead. - EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST); - - // However, when re-serialized, it should preserve enum value. - std::string v1_bytes = v1_message.SerializeAsString(); - - protobuf_unittest::V2MessageLite same_v2_message; - same_v2_message.ParseFromString(v1_bytes); - - EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field()); - EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field()); + // Check that adding more values to enum does not corrupt message + // when passed through an old client. + protobuf_unittest::V2MessageLite v2_message; + v2_message.set_int_field(800); + // Set enum field to the value not understood by the old client. + v2_message.set_enum_field(protobuf_unittest::V2_SECOND); + std::string v2_bytes = v2_message.SerializeAsString(); + + protobuf_unittest::V1MessageLite v1_message; + v1_message.ParseFromString(v2_bytes); + EXPECT_TRUE(v1_message.IsInitialized()); + EXPECT_EQ(v1_message.int_field(), v2_message.int_field()); + // V1 client does not understand V2_SECOND value, so it discards it and + // uses default value instead. + EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST); + + // However, when re-serialized, it should preserve enum value. + std::string v1_bytes = v1_message.SerializeAsString(); + + protobuf_unittest::V2MessageLite same_v2_message; + same_v2_message.ParseFromString(v1_bytes); + + EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field()); + EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field()); } } diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index 40f35e9255..2750c023b9 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -63,7 +63,8 @@ class Map; class MapIterator; -template <typename Enum> struct is_proto_enum; +template <typename Enum> +struct is_proto_enum; namespace internal { template <typename Derived, typename Key, typename T, @@ -96,8 +97,7 @@ class MapPair { MapPair(const Key& other_first, const T& other_second) : first(other_first), second(other_second) {} explicit MapPair(const Key& other_first) : first(other_first), second() {} - MapPair(const MapPair& other) - : first(other.first), second(other.second) {} + MapPair(const MapPair& other) : first(other.first), second(other.second) {} ~MapPair() {} @@ -183,7 +183,8 @@ class Map { private: void Init() { - elements_ = Arena::Create<InnerMap>(arena_, 0u, hasher(), Allocator(arena_)); + elements_ = + Arena::Create<InnerMap>(arena_, 0u, hasher(), Allocator(arena_)); } // re-implement std::allocator to use arena allocator for memory allocation. @@ -231,7 +232,7 @@ class Map { #if __cplusplus >= 201103L && !defined(GOOGLE_PROTOBUF_OS_APPLE) && \ !defined(GOOGLE_PROTOBUF_OS_NACL) && \ !defined(GOOGLE_PROTOBUF_OS_EMSCRIPTEN) - template<class NodeType, class... Args> + template <class NodeType, class... Args> void construct(NodeType* p, Args&&... args) { // Clang 3.6 doesn't compile static casting to void* directly. (Issue // #1266) According C++ standard 5.2.9/1: "The static_cast operator shall @@ -241,7 +242,7 @@ class Map { NodeType(std::forward<Args>(args)...); } - template<class NodeType> + template <class NodeType> void destroy(NodeType* p) { p->~NodeType(); } @@ -274,9 +275,7 @@ class Map { // To support gcc-4.4, which does not properly // support templated friend classes - Arena* arena() const { - return arena_; - } + Arena* arena() const { return arena_; } private: typedef void DestructorSkippable_; @@ -483,8 +482,7 @@ class Map { // Force bucket_index_ to be in range. bucket_index_ &= (m_->num_buckets_ - 1); // Common case: the bucket we think is relevant points to node_. - if (m_->table_[bucket_index_] == static_cast<void*>(node_)) - return true; + if (m_->table_[bucket_index_] == static_cast<void*>(node_)) return true; // Less common: the bucket is a linked list with node_ somewhere in it, // but not at the head. if (m_->TableEntryIsNonEmptyList(bucket_index_)) { @@ -720,10 +718,9 @@ class Map { GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]); // Maintain the invariant that node->next is NULL for all Nodes in Trees. node->next = NULL; - return iterator(static_cast<Tree*>(table_[b]) - ->insert(KeyPtrFromNodePtr(node)) - .first, - this, b & ~static_cast<size_t>(1)); + return iterator( + static_cast<Tree*>(table_[b])->insert(KeyPtrFromNodePtr(node)).first, + this, b & ~static_cast<size_t>(1)); } // Returns whether it did resize. Currently this is only used when @@ -834,7 +831,7 @@ class Map { } static bool TableEntryIsTree(void* const* table, size_type b) { return !TableEntryIsEmpty(table, b) && - !TableEntryIsNonEmptyList(table, b); + !TableEntryIsNonEmptyList(table, b); } static bool TableEntryIsList(void* const* table, size_type b) { return !TableEntryIsTree(table, b); @@ -942,7 +939,7 @@ class Map { size_type s = static_cast<size_type>(reinterpret_cast<uintptr_t>(this)); #if defined(__x86_64__) && defined(__GNUC__) uint32 hi, lo; - asm("rdtsc" : "=a" (lo), "=d" (hi)); + asm("rdtsc" : "=a"(lo), "=d"(hi)); s += ((static_cast<uint64>(hi) << 32) | lo); #endif return s; @@ -972,9 +969,7 @@ class Map { const_iterator() {} explicit const_iterator(const InnerIt& it) : it_(it) {} - const_reference operator*() const { - return *it_->value(); - } + const_reference operator*() const { return *it_->value(); } const_pointer operator->() const { return &(operator*()); } const_iterator& operator++() { @@ -1051,7 +1046,7 @@ class Map { // Element access T& operator[](const key_type& key) { - value_type** value = &(*elements_)[key]; + value_type** value = &(*elements_)[key]; if (*value == NULL) { *value = CreateValueTypeInternal(key); internal::MapValueInitializer<is_proto_enum<T>::value, T>::Initialize( diff --git a/src/google/protobuf/map_entry_lite.h b/src/google/protobuf/map_entry_lite.h index 756b9af0ca..c7d7558bd6 100644 --- a/src/google/protobuf/map_entry_lite.h +++ b/src/google/protobuf/map_entry_lite.h @@ -122,16 +122,16 @@ class MapEntryImpl : public Base { // Enum type cannot be used for MapTypeHandler::Read. Define a type // which will replace Enum with int. typedef typename KeyTypeHandler::MapEntryAccessorType KeyMapEntryAccessorType; - typedef typename ValueTypeHandler::MapEntryAccessorType - ValueMapEntryAccessorType; + typedef + typename ValueTypeHandler::MapEntryAccessorType ValueMapEntryAccessorType; // Constants for field number. static const int kKeyFieldNumber = 1; static const int kValueFieldNumber = 2; // Constants for field tag. - static const uint8 kKeyTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kKeyFieldNumber, KeyTypeHandler::kWireType); + static const uint8 kKeyTag = + GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kKeyFieldNumber, KeyTypeHandler::kWireType); static const uint8 kValueTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( kValueFieldNumber, ValueTypeHandler::kWireType); static const size_t kTagSize = 1; @@ -202,12 +202,14 @@ class MapEntryImpl : public Base { GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); if (tag == kKeyTag) { set_has_key(); - ptr = KeyTypeHandler::Read(ptr, ctx, mutable_key()); - if (!::down_cast<Derived*>(this)->ValidateKey()) return nullptr; + KeyMapEntryAccessorType* key = mutable_key(); + ptr = KeyTypeHandler::Read(ptr, ctx, key); + if (!Derived::ValidateKey(key)) return nullptr; } else if (tag == kValueTag) { set_has_value(); - ptr = ValueTypeHandler::Read(ptr, ctx, mutable_value()); - if (!::down_cast<Derived*>(this)->ValidateValue()) return nullptr; + ValueMapEntryAccessorType* value = mutable_value(); + ptr = ValueTypeHandler::Read(ptr, ctx, value); + if (!Derived::ValidateValue(value)) return nullptr; } else { if (tag == 0 || WireFormatLite::GetTagWireType(tag) == WireFormatLite::WIRETYPE_END_GROUP) { @@ -251,9 +253,8 @@ class MapEntryImpl : public Base { break; default: - if (tag == 0 || - WireFormatLite::GetTagWireType(tag) == - WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0 || WireFormatLite::GetTagWireType(tag) == + WireFormatLite::WIRETYPE_END_GROUP) { return true; } if (!WireFormatLite::SkipField(input, tag)) return false; @@ -265,10 +266,13 @@ class MapEntryImpl : public Base { size_t ByteSizeLong() const override { size_t size = 0; - size += has_key() ? - kTagSize + static_cast<size_t>(KeyTypeHandler::ByteSize(key())) : 0; - size += has_value() ? - kTagSize + static_cast<size_t>(ValueTypeHandler::ByteSize(value())) : 0; + size += has_key() ? kTagSize + + static_cast<size_t>(KeyTypeHandler::ByteSize(key())) + : 0; + size += has_value() + ? kTagSize + + static_cast<size_t>(ValueTypeHandler::ByteSize(value())) + : 0; return size; } @@ -288,12 +292,12 @@ class MapEntryImpl : public Base { int GetCachedSize() const override { int size = 0; - size += has_key() - ? static_cast<int>(kTagSize) + KeyTypeHandler::GetCachedSize(key()) - : 0; - size += has_value() - ? static_cast<int>(kTagSize) + ValueTypeHandler::GetCachedSize(value()) - : 0; + size += has_key() ? static_cast<int>(kTagSize) + + KeyTypeHandler::GetCachedSize(key()) + : 0; + size += has_value() ? static_cast<int>(kTagSize) + + ValueTypeHandler::GetCachedSize(value()) + : 0; return size; } @@ -332,8 +336,8 @@ class MapEntryImpl : public Base { public: void Clear() override { KeyTypeHandler::Clear(&key_, GetArenaNoVirtual()); - ValueTypeHandler::ClearMaybeByDefaultEnum( - &value_, GetArenaNoVirtual(), default_enum_value); + ValueTypeHandler::ClearMaybeByDefaultEnum(&value_, GetArenaNoVirtual(), + default_enum_value); clear_has_key(); clear_has_value(); } @@ -416,10 +420,43 @@ class MapEntryImpl : public Base { } const char* _InternalParse(const char* ptr, ParseContext* ctx) { - auto entry = NewEntry(); - ptr = entry->_InternalParse(ptr, ctx); - if (!ptr) return nullptr; - UseKeyAndValueFromEntry(); + if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kKeyTag)) { + ptr = KeyTypeHandler::Read(ptr + 1, ctx, &key_); + if (PROTOBUF_PREDICT_FALSE(!ptr || !Derived::ValidateKey(&key_))) { + return nullptr; + } + if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kValueTag)) { + typename Map::size_type map_size = map_->size(); + value_ptr_ = &(*map_)[key_]; + if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { + using T = + typename MapIf<ValueTypeHandler::kIsEnum, int*, Value*>::type; + ptr = ValueTypeHandler::Read(ptr + 1, ctx, + reinterpret_cast<T>(value_ptr_)); + if (PROTOBUF_PREDICT_FALSE(!ptr || + !Derived::ValidateValue(value_ptr_))) { + map_->erase(key_); // Failure! Undo insertion. + return nullptr; + } + if (PROTOBUF_PREDICT_TRUE(ctx->Done(&ptr))) return ptr; + if (!ptr) return nullptr; + NewEntry(); + ValueMover::Move(value_ptr_, entry_->mutable_value()); + map_->erase(key_); + goto move_key; + } + } else { + if (!ptr) return nullptr; + } + NewEntry(); + move_key: + KeyMover::Move(&key_, entry_->mutable_key()); + } else { + if (!ptr) return nullptr; + NewEntry(); + } + ptr = entry_->_InternalParse(ptr, ctx); + if (ptr) UseKeyAndValueFromEntry(); return ptr; } @@ -455,27 +492,13 @@ class MapEntryImpl : public Base { // path, so it's not a big deal. key_ = entry_->key(); value_ptr_ = &(*map_)[key_]; - MoveHelper<ValueTypeHandler::kIsEnum, - ValueTypeHandler::kIsMessage, - ValueTypeHandler::kWireType == - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - Value>::Move(entry_->mutable_value(), value_ptr_); + ValueMover::Move(entry_->mutable_value(), value_ptr_); } // After reading a key and value successfully, and inserting that data // into map_, we are not at the end of the input. This is unusual, but // allowed by the spec. bool ReadBeyondKeyValuePair(io::CodedInputStream* input) PROTOBUF_COLD { - typedef MoveHelper<KeyTypeHandler::kIsEnum, - KeyTypeHandler::kIsMessage, - KeyTypeHandler::kWireType == - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - Key> KeyMover; - typedef MoveHelper<ValueTypeHandler::kIsEnum, - ValueTypeHandler::kIsMessage, - ValueTypeHandler::kWireType == - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, - Value> ValueMover; NewEntry(); ValueMover::Move(value_ptr_, entry_->mutable_value()); map_->erase(key_); @@ -485,6 +508,17 @@ class MapEntryImpl : public Base { return result; } + typedef MoveHelper<KeyTypeHandler::kIsEnum, KeyTypeHandler::kIsMessage, + KeyTypeHandler::kWireType == + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, + Key> + KeyMover; + typedef MoveHelper<ValueTypeHandler::kIsEnum, ValueTypeHandler::kIsMessage, + ValueTypeHandler::kWireType == + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, + Value> + ValueMover; + MapField* const mf_; Map* const map_; Key key_; @@ -566,9 +600,7 @@ class MapEntryImpl : public Base { typedef void DestructorSkippable_; }; - inline Arena* GetArenaNoVirtual() const { - return arena_; - } + inline Arena* GetArenaNoVirtual() const { return arena_; } public: // Needed for constructing tables KeyOnMemory key_; @@ -629,7 +661,8 @@ struct DeconstructMapEntry<MapEntryLite<T, K, V, key, value, default_enum> > { // type is relatively small and easy to copy then copying Keys into an // array of SortItems can be beneficial. Then all the data the sorting // algorithm needs to touch is in that one array. -template <typename Key, typename PtrToKeyValuePair> struct SortItem { +template <typename Key, typename PtrToKeyValuePair> +struct SortItem { SortItem() {} explicit SortItem(PtrToKeyValuePair p) : first(p->first), second(p) {} @@ -637,16 +670,14 @@ template <typename Key, typename PtrToKeyValuePair> struct SortItem { PtrToKeyValuePair second; }; -template <typename T> struct CompareByFirstField { - bool operator()(const T& a, const T& b) const { - return a.first < b.first; - } +template <typename T> +struct CompareByFirstField { + bool operator()(const T& a, const T& b) const { return a.first < b.first; } }; -template <typename T> struct CompareByDerefFirst { - bool operator()(const T& a, const T& b) const { - return a->first < b->first; - } +template <typename T> +struct CompareByDerefFirst { + bool operator()(const T& a, const T& b) const { return a->first < b->first; } }; // Helper for table driven serialization diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc index 5ed84f3698..2a71b6b506 100644 --- a/src/google/protobuf/map_field.cc +++ b/src/google/protobuf/map_field.cc @@ -99,35 +99,35 @@ void MapFieldBase::SyncRepeatedFieldWithMap() const { // acquire here matches with release below to ensure that we can only see a // value of CLEAN after all previous changes have been synced. switch (state_.load(std::memory_order_acquire)) { - case STATE_MODIFIED_MAP: - mutex_.Lock(); - // Double check state, because another thread may have seen the same - // state and done the synchronization before the current thread. - if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_MAP) { - SyncRepeatedFieldWithMapNoLock(); - state_.store(CLEAN, std::memory_order_release); - } - mutex_.Unlock(); - break; - case CLEAN: - mutex_.Lock(); - // Double check state - if (state_.load(std::memory_order_relaxed) == CLEAN) { - if (repeated_field_ == nullptr) { - if (arena_ == nullptr) { - repeated_field_ = new RepeatedPtrField<Message>(); - } else { - repeated_field_ = - Arena::CreateMessage<RepeatedPtrField<Message> >(arena_); - } + case STATE_MODIFIED_MAP: + mutex_.Lock(); + // Double check state, because another thread may have seen the same + // state and done the synchronization before the current thread. + if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_MAP) { + SyncRepeatedFieldWithMapNoLock(); + state_.store(CLEAN, std::memory_order_release); + } + mutex_.Unlock(); + break; + case CLEAN: + mutex_.Lock(); + // Double check state + if (state_.load(std::memory_order_relaxed) == CLEAN) { + if (repeated_field_ == nullptr) { + if (arena_ == nullptr) { + repeated_field_ = new RepeatedPtrField<Message>(); + } else { + repeated_field_ = + Arena::CreateMessage<RepeatedPtrField<Message> >(arena_); } - state_.store(CLEAN, std::memory_order_release); } - mutex_.Unlock(); - break; - default: - break; - } + state_.store(CLEAN, std::memory_order_release); + } + mutex_.Unlock(); + break; + default: + break; + } } void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const { @@ -153,15 +153,12 @@ void MapFieldBase::SyncMapWithRepeatedField() const { // ------------------DynamicMapField------------------ DynamicMapField::DynamicMapField(const Message* default_entry) - : default_entry_(default_entry) { -} + : default_entry_(default_entry) {} -DynamicMapField::DynamicMapField(const Message* default_entry, - Arena* arena) +DynamicMapField::DynamicMapField(const Message* default_entry, Arena* arena) : TypeDefinedMapFieldBase<MapKey, MapValueRef>(arena), map_(arena), - default_entry_(default_entry) { -} + default_entry_(default_entry) {} DynamicMapField::~DynamicMapField() { // DynamicMapField owns map values. Need to delete them before clearing @@ -173,9 +170,7 @@ DynamicMapField::~DynamicMapField() { map_.clear(); } -int DynamicMapField::size() const { - return GetMap().size(); -} +int DynamicMapField::size() const { return GetMap().size(); } void DynamicMapField::Clear() { Map<MapKey, MapValueRef>* map = &const_cast<DynamicMapField*>(this)->map_; @@ -193,8 +188,7 @@ void DynamicMapField::Clear() { MapFieldBase::SetMapDirty(); } -bool DynamicMapField::ContainsMapKey( - const MapKey& map_key) const { +bool DynamicMapField::ContainsMapKey(const MapKey& map_key) const { const Map<MapKey, MapValueRef>& map = GetMap(); Map<MapKey, MapValueRef>::const_iterator iter = map.find(map_key); return iter != map.end(); @@ -207,12 +201,12 @@ void DynamicMapField::AllocateMapValue(MapValueRef* map_val) { // Allocate memory for the MapValueRef, and initialize to // default value. switch (val_des->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ - TYPE* value = new TYPE(); \ - map_val->SetValue(value); \ - break; \ - } +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ + TYPE* value = new TYPE(); \ + map_val->SetValue(value); \ + break; \ + } HANDLE_TYPE(INT32, int32); HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); @@ -224,8 +218,8 @@ void DynamicMapField::AllocateMapValue(MapValueRef* map_val) { HANDLE_TYPE(ENUM, int32); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: { - const Message& message = default_entry_->GetReflection()->GetMessage( - *default_entry_, val_des); + const Message& message = + default_entry_->GetReflection()->GetMessage(*default_entry_, val_des); Message* value = message.New(); map_val->SetValue(value); break; @@ -233,8 +227,8 @@ void DynamicMapField::AllocateMapValue(MapValueRef* map_val) { } } -bool DynamicMapField::InsertOrLookupMapValue( - const MapKey& map_key, MapValueRef* val) { +bool DynamicMapField::InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) { // Always use mutable map because users may change the map value by // MapValueRef. Map<MapKey, MapValueRef>* map = MutableMap(); @@ -290,8 +284,8 @@ void DynamicMapField::MergeFrom(const MapFieldBase& other) { const DynamicMapField& other_field = reinterpret_cast<const DynamicMapField&>(other); for (typename Map<MapKey, MapValueRef>::const_iterator other_it = - other_field.map_.begin(); other_it != other_field.map_.end(); - ++other_it) { + other_field.map_.begin(); + other_it != other_field.map_.end(); ++other_it) { Map<MapKey, MapValueRef>::iterator iter = map->find(other_it->first); MapValueRef* map_val; if (iter == map->end()) { diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h index c60510e506..d804dee76e 100644 --- a/src/google/protobuf/map_field.h +++ b/src/google/protobuf/map_field.h @@ -69,13 +69,9 @@ class MapFieldAccessor; class PROTOBUF_EXPORT MapFieldBase { public: MapFieldBase() - : arena_(NULL), - repeated_field_(NULL), - state_(STATE_MODIFIED_MAP) {} + : arena_(NULL), repeated_field_(NULL), state_(STATE_MODIFIED_MAP) {} explicit MapFieldBase(Arena* arena) - : arena_(arena), - repeated_field_(NULL), - state_(STATE_MODIFIED_MAP) { + : arena_(arena), repeated_field_(NULL), state_(STATE_MODIFIED_MAP) { // Mutex's destructor needs to be called explicitly to release resources // acquired in its constructor. arena->OwnDestructor(&mutex_); @@ -92,8 +88,8 @@ class PROTOBUF_EXPORT MapFieldBase { // Pure virtual map APIs for Map Reflection. virtual bool ContainsMapKey(const MapKey& map_key) const = 0; - virtual bool InsertOrLookupMapValue( - const MapKey& map_key, MapValueRef* val) = 0; + virtual bool InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) = 0; // Returns whether changes to the map are reflected in the repeated field. bool IsRepeatedFieldValid() const; // Insures operations after won't get executed before calling this. @@ -145,14 +141,15 @@ class PROTOBUF_EXPORT MapFieldBase { // synchronized to repeated field STATE_MODIFIED_REPEATED = 1, // repeated field has newly added data that // has not been synchronized to map - CLEAN = 2, // data in map and repeated field are same + CLEAN = 2, // data in map and repeated field are same }; Arena* arena_; mutable RepeatedPtrField<Message>* repeated_field_; - mutable internal::WrappedMutex mutex_; // The thread to synchronize map and repeated field - // needs to get lock first; + mutable internal::WrappedMutex + mutex_; // The thread to synchronize map and repeated field + // needs to get lock first; mutable std::atomic<State> state_; private: @@ -186,7 +183,7 @@ class PROTOBUF_EXPORT MapFieldBase { // This class provides common Map Reflection implementations for generated // message and dynamic message. -template<typename Key, typename T> +template <typename Key, typename T> class TypeDefinedMapFieldBase : public MapFieldBase { public: TypeDefinedMapFieldBase() {} @@ -321,7 +318,7 @@ template <typename T, typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType, WireFormatLite::FieldType kValueFieldType, int default_enum_value> struct MapEntryToMapField<MapEntry<T, Key, Value, kKeyFieldType, - kValueFieldType, default_enum_value> > { + kValueFieldType, default_enum_value>> { typedef MapField<T, Key, Value, kKeyFieldType, kValueFieldType, default_enum_value> MapFieldType; @@ -363,26 +360,21 @@ class PROTOBUF_EXPORT DynamicMapField } // namespace internal -#define TYPE_CHECK(EXPECTEDTYPE, METHOD) \ - if (type() != EXPECTEDTYPE) { \ - GOOGLE_LOG(FATAL) \ - << "Protocol Buffer map usage error:\n" \ - << METHOD << " type does not match\n" \ - << " Expected : " \ - << FieldDescriptor::CppTypeName(EXPECTEDTYPE) << "\n" \ - << " Actual : " \ - << FieldDescriptor::CppTypeName(type()); \ +#define TYPE_CHECK(EXPECTEDTYPE, METHOD) \ + if (type() != EXPECTEDTYPE) { \ + GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" \ + << METHOD << " type does not match\n" \ + << " Expected : " \ + << FieldDescriptor::CppTypeName(EXPECTEDTYPE) << "\n" \ + << " Actual : " << FieldDescriptor::CppTypeName(type()); \ } // MapKey is an union type for representing any possible // map key. class PROTOBUF_EXPORT MapKey { public: - MapKey() : type_(0) { - } - MapKey(const MapKey& other) : type_(0) { - CopyFrom(other); - } + MapKey() : type_(0) {} + MapKey(const MapKey& other) : type_(0) { CopyFrom(other); } MapKey& operator=(const MapKey& other) { CopyFrom(other); return *this; @@ -396,10 +388,9 @@ class PROTOBUF_EXPORT MapKey { FieldDescriptor::CppType type() const { if (type_ == 0) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer map usage error:\n" - << "MapKey::type MapKey is not initialized. " - << "Call set methods to initialize MapKey."; + GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" + << "MapKey::type MapKey is not initialized. " + << "Call set methods to initialize MapKey."; } return (FieldDescriptor::CppType)type_; } @@ -430,33 +421,27 @@ class PROTOBUF_EXPORT MapKey { } int64 GetInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, - "MapKey::GetInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapKey::GetInt64Value"); return val_.int64_value_; } uint64 GetUInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, - "MapKey::GetUInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapKey::GetUInt64Value"); return val_.uint64_value_; } int32 GetInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, - "MapKey::GetInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapKey::GetInt32Value"); return val_.int32_value_; } uint32 GetUInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, - "MapKey::GetUInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapKey::GetUInt32Value"); return val_.uint32_value_; } bool GetBoolValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, - "MapKey::GetBoolValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapKey::GetBoolValue"); return val_.bool_value_; } const std::string& GetStringValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, - "MapKey::GetStringValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapKey::GetStringValue"); return *val_.string_value_; } @@ -585,95 +570,77 @@ class PROTOBUF_EXPORT MapValueRef { MapValueRef() : data_(NULL), type_(0) {} void SetInt64Value(int64 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, - "MapValueRef::SetInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::SetInt64Value"); *reinterpret_cast<int64*>(data_) = value; } void SetUInt64Value(uint64 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, - "MapValueRef::SetUInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::SetUInt64Value"); *reinterpret_cast<uint64*>(data_) = value; } void SetInt32Value(int32 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, - "MapValueRef::SetInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::SetInt32Value"); *reinterpret_cast<int32*>(data_) = value; } void SetUInt32Value(uint32 value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, - "MapValueRef::SetUInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::SetUInt32Value"); *reinterpret_cast<uint32*>(data_) = value; } void SetBoolValue(bool value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, - "MapValueRef::SetBoolValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::SetBoolValue"); *reinterpret_cast<bool*>(data_) = value; } // TODO(jieluo) - Checks that enum is member. void SetEnumValue(int value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, - "MapValueRef::SetEnumValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue"); *reinterpret_cast<int*>(data_) = value; } void SetStringValue(const std::string& value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, - "MapValueRef::SetStringValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue"); *reinterpret_cast<std::string*>(data_) = value; } void SetFloatValue(float value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, - "MapValueRef::SetFloatValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue"); *reinterpret_cast<float*>(data_) = value; } void SetDoubleValue(double value) { - TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, - "MapValueRef::SetDoubleValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::SetDoubleValue"); *reinterpret_cast<double*>(data_) = value; } int64 GetInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, - "MapValueRef::GetInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::GetInt64Value"); return *reinterpret_cast<int64*>(data_); } uint64 GetUInt64Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, - "MapValueRef::GetUInt64Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::GetUInt64Value"); return *reinterpret_cast<uint64*>(data_); } int32 GetInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, - "MapValueRef::GetInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::GetInt32Value"); return *reinterpret_cast<int32*>(data_); } uint32 GetUInt32Value() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, - "MapValueRef::GetUInt32Value"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::GetUInt32Value"); return *reinterpret_cast<uint32*>(data_); } bool GetBoolValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, - "MapValueRef::GetBoolValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::GetBoolValue"); return *reinterpret_cast<bool*>(data_); } int GetEnumValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, - "MapValueRef::GetEnumValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::GetEnumValue"); return *reinterpret_cast<int*>(data_); } const std::string& GetStringValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, - "MapValueRef::GetStringValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::GetStringValue"); return *reinterpret_cast<std::string*>(data_); } float GetFloatValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, - "MapValueRef::GetFloatValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::GetFloatValue"); return *reinterpret_cast<float*>(data_); } double GetDoubleValue() const { - TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, - "MapValueRef::GetDoubleValue"); + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::GetDoubleValue"); return *reinterpret_cast<double*>(data_); } @@ -701,21 +668,16 @@ class PROTOBUF_EXPORT MapValueRef { friend class internal::GeneratedMessageReflection; friend class internal::DynamicMapField; - void SetType(FieldDescriptor::CppType type) { - type_ = type; - } + void SetType(FieldDescriptor::CppType type) { type_ = type; } FieldDescriptor::CppType type() const { if (type_ == 0 || data_ == NULL) { - GOOGLE_LOG(FATAL) - << "Protocol Buffer map usage error:\n" - << "MapValueRef::type MapValueRef is not initialized."; + GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" + << "MapValueRef::type MapValueRef is not initialized."; } return (FieldDescriptor::CppType)type_; } - void SetValue(const void* val) { - data_ = const_cast<void*>(val); - } + void SetValue(const void* val) { data_ = const_cast<void*>(val); } void CopyFrom(const MapValueRef& other) { type_ = other.type_; data_ = other.data_; @@ -764,9 +726,7 @@ class PROTOBUF_EXPORT MapIterator { map_->InitializeIterator(this); map_->CopyIterator(this, other); } - ~MapIterator() { - map_->DeleteIterator(this); - } + ~MapIterator() { map_->DeleteIterator(this); } MapIterator& operator=(const MapIterator& other) { map_ = other.map_; map_->CopyIterator(this, other); @@ -789,12 +749,8 @@ class PROTOBUF_EXPORT MapIterator { map_->IncreaseIterator(this); return *this; } - const MapKey& GetKey() { - return key_; - } - const MapValueRef& GetValueRef() { - return value_; - } + const MapKey& GetKey() { return key_; } + const MapValueRef& GetValueRef() { return value_; } MapValueRef* MutableValueRef() { map_->SetMapDirty(); return &value_; diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h index 85d5b03890..7baaa5ff31 100644 --- a/src/google/protobuf/map_field_inl.h +++ b/src/google/protobuf/map_field_inl.h @@ -46,25 +46,25 @@ namespace google { namespace protobuf { namespace internal { // UnwrapMapKey template -template<typename T> +template <typename T> T UnwrapMapKey(const MapKey& map_key); -template<> +template <> inline int32 UnwrapMapKey<int32>(const MapKey& map_key) { return map_key.GetInt32Value(); } -template<> +template <> inline uint32 UnwrapMapKey<uint32>(const MapKey& map_key) { return map_key.GetUInt32Value(); } -template<> +template <> inline int64 UnwrapMapKey<int64>(const MapKey& map_key) { return map_key.GetInt64Value(); } -template<> +template <> inline uint64 UnwrapMapKey<uint64>(const MapKey& map_key) { return map_key.GetUInt64Value(); } -template<> +template <> inline bool UnwrapMapKey<bool>(const MapKey& map_key) { return map_key.GetBoolValue(); } @@ -74,25 +74,25 @@ inline std::string UnwrapMapKey<std::string>(const MapKey& map_key) { } // SetMapKey template -template<typename T> +template <typename T> inline void SetMapKey(MapKey* map_key, const T& value); -template<> +template <> inline void SetMapKey<int32>(MapKey* map_key, const int32& value) { map_key->SetInt32Value(value); } -template<> +template <> inline void SetMapKey<uint32>(MapKey* map_key, const uint32& value) { map_key->SetUInt32Value(value); } -template<> +template <> inline void SetMapKey<int64>(MapKey* map_key, const int64& value) { map_key->SetInt64Value(value); } -template<> +template <> inline void SetMapKey<uint64>(MapKey* map_key, const uint64& value) { map_key->SetUInt64Value(value); } -template<> +template <> inline void SetMapKey<bool>(MapKey* map_key, const bool& value) { map_key->SetBoolValue(value); } @@ -106,7 +106,7 @@ template <typename Key, typename T> typename Map<Key, T>::const_iterator& TypeDefinedMapFieldBase<Key, T>::InternalGetIterator( const MapIterator* map_iter) const { - return *reinterpret_cast<typename Map<Key, T>::const_iterator *>( + return *reinterpret_cast<typename Map<Key, T>::const_iterator*>( map_iter->iter_); } @@ -122,15 +122,14 @@ void TypeDefinedMapFieldBase<Key, T>::MapEnd(MapIterator* map_iter) const { } template <typename Key, typename T> -bool TypeDefinedMapFieldBase<Key, T>::EqualIterator(const MapIterator& a, - const MapIterator& b) - const { +bool TypeDefinedMapFieldBase<Key, T>::EqualIterator( + const MapIterator& a, const MapIterator& b) const { return InternalGetIterator(&a) == InternalGetIterator(&b); } template <typename Key, typename T> -void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator(MapIterator* map_iter) - const { +void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator( + MapIterator* map_iter) const { ++InternalGetIterator(map_iter); SetMapIteratorValue(map_iter); } @@ -143,16 +142,15 @@ void TypeDefinedMapFieldBase<Key, T>::InitializeIterator( } template <typename Key, typename T> -void TypeDefinedMapFieldBase<Key, T>::DeleteIterator(MapIterator* map_iter) - const { - delete reinterpret_cast<typename Map<Key, T>::const_iterator *>( +void TypeDefinedMapFieldBase<Key, T>::DeleteIterator( + MapIterator* map_iter) const { + delete reinterpret_cast<typename Map<Key, T>::const_iterator*>( map_iter->iter_); } template <typename Key, typename T> void TypeDefinedMapFieldBase<Key, T>::CopyIterator( - MapIterator* this_iter, - const MapIterator& that_iter) const { + MapIterator* this_iter, const MapIterator& that_iter) const { InternalGetIterator(this_iter) = InternalGetIterator(&that_iter); this_iter->key_.SetType(that_iter.key_.type()); // MapValueRef::type() fails when containing data is null. However, if @@ -301,8 +299,8 @@ void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType, // know that this MapEntry default_type has also already been constructed. // So it's safe to just call internal_default_instance(). const Message* default_entry = Derived::internal_default_instance(); - for (typename Map<Key, T>::const_iterator it = map.begin(); - it != map.end(); ++it) { + for (typename Map<Key, T>::const_iterator it = map.begin(); it != map.end(); + ++it) { EntryType* new_entry = down_cast<EntryType*>(default_entry->New(this->MapFieldBase::arena_)); repeated_field->AddAllocated(new_entry); @@ -323,7 +321,8 @@ void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType, GOOGLE_CHECK(this->MapFieldBase::repeated_field_ != NULL); map->clear(); for (typename RepeatedPtrField<EntryType>::iterator it = - repeated_field->begin(); it != repeated_field->end(); ++it) { + repeated_field->begin(); + it != repeated_field->end(); ++it) { // Cast is needed because Map's api and internal storage is different when // value is enum. For enum, we cannot cast an int to enum. Thus, we have to // copy value. For other types, they have same exposed api type and internal diff --git a/src/google/protobuf/map_field_lite.h b/src/google/protobuf/map_field_lite.h index bef21406d8..180a788454 100644 --- a/src/google/protobuf/map_field_lite.h +++ b/src/google/protobuf/map_field_lite.h @@ -176,7 +176,7 @@ template <typename T, typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType, WireFormatLite::FieldType kValueFieldType, int default_enum_value> struct MapEntryToMapField<MapEntryLite<T, Key, Value, kKeyFieldType, - kValueFieldType, default_enum_value> > { + kValueFieldType, default_enum_value>> { typedef MapFieldLite<MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType, default_enum_value>, Key, Value, kKeyFieldType, kValueFieldType, diff --git a/src/google/protobuf/map_field_test.cc b/src/google/protobuf/map_field_test.cc index ecb25f4bcd..5c44070967 100644 --- a/src/google/protobuf/map_field_test.cc +++ b/src/google/protobuf/map_field_test.cc @@ -65,9 +65,7 @@ class MapFieldBaseStub : public MapFieldBase { MapFieldBase::SyncMapWithRepeatedField(); } // Get underlined repeated field without synchronizing map. - RepeatedPtrField<Message>* InternalRepeatedField() { - return repeated_field_; - } + RepeatedPtrField<Message>* InternalRepeatedField() { return repeated_field_; } bool IsMapClean() { return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_MAP; } @@ -80,15 +78,11 @@ class MapFieldBaseStub : public MapFieldBase { void SetRepeatedDirty() { state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed); } - bool ContainsMapKey(const MapKey& map_key) const { - return false; - } + bool ContainsMapKey(const MapKey& map_key) const { return false; } bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) { return false; } - bool DeleteMapValue(const MapKey& map_key) { - return false; - } + bool DeleteMapValue(const MapKey& map_key) { return false; } bool EqualIterator(const MapIterator& a, const MapIterator& b) const { return false; } @@ -210,8 +204,7 @@ namespace { enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY }; } // anonymous namespace -class MapFieldStateTest - : public testing::TestWithParam<State> { +class MapFieldStateTest : public testing::TestWithParam<State> { public: protected: typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType; diff --git a/src/google/protobuf/map_test.cc b/src/google/protobuf/map_test.cc index 137a4d8dd8..ec13b3c8be 100644 --- a/src/google/protobuf/map_test.cc +++ b/src/google/protobuf/map_test.cc @@ -132,9 +132,9 @@ class MapImplTest : public ::testing::Test { Map<int32, int32>::iterator it = map_.find(key); // iterator dereferenceable - EXPECT_EQ(key, (*it).first); + EXPECT_EQ(key, (*it).first); EXPECT_EQ(value, (*it).second); - EXPECT_EQ(key, it->first); + EXPECT_EQ(key, it->first); EXPECT_EQ(value, it->second); // iterator mutable @@ -244,13 +244,9 @@ TEST_F(MapImplTest, MapKeyAssignment) { EXPECT_EQ("abc", to.GetStringValue()); } -TEST_F(MapImplTest, CountNonExist) { - EXPECT_EQ(0, map_.count(0)); -} +TEST_F(MapImplTest, CountNonExist) { EXPECT_EQ(0, map_.count(0)); } -TEST_F(MapImplTest, ContainNotExist) { - EXPECT_FALSE(map_.contains(0)); -} +TEST_F(MapImplTest, ContainNotExist) { EXPECT_FALSE(map_.contains(0)); } TEST_F(MapImplTest, ImmutableContainNotExist) { EXPECT_FALSE(const_map_.contains(0)); @@ -433,8 +429,7 @@ TEST_F(MapImplTest, CopyIteratorStressTest) { } template <typename T, typename U> -static void TestValidityForAllKeysExcept(int key_to_avoid, - const T& check_map, +static void TestValidityForAllKeysExcept(int key_to_avoid, const T& check_map, const U& map) { typedef typename U::value_type value_type; // a key-value pair for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) { @@ -466,7 +461,8 @@ template <typename IteratorType> static void TestOldVersusNewIterator(int skip, Map<int, int>* m) { const int initial_size = m->size(); IteratorType it = m->begin(); - for (int i = 0; i < skip && it != m->end(); it++, i++) {} + for (int i = 0; i < skip && it != m->end(); it++, i++) { + } if (it == m->end()) return; const IteratorType old = it; GOOGLE_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first; @@ -633,8 +629,9 @@ bool IsForwardIteratorHelper(T /*t*/) { TEST_F(MapImplTest, IteratorCategory) { EXPECT_TRUE(IsForwardIteratorHelper( std::iterator_traits<Map<int, int>::iterator>::iterator_category())); - EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits< - Map<int, int>::const_iterator>::iterator_category())); + EXPECT_TRUE(IsForwardIteratorHelper( + std::iterator_traits< + Map<int, int>::const_iterator>::iterator_category())); } TEST_F(MapImplTest, InsertSingle) { @@ -948,11 +945,10 @@ TEST_F(MapImplTest, SwapBasic) { another[9398] = 41999; another[8070] = 42056; another.swap(map_); - EXPECT_THAT(another, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); - EXPECT_THAT(map_, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(9398, 41999))); + EXPECT_THAT(another, + testing::UnorderedElementsAre(testing::Pair(9398, 41999))); + EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(9398, 41999))); } TEST_F(MapImplTest, SwapArena) { @@ -965,23 +961,18 @@ TEST_F(MapImplTest, SwapArena) { m2[10244] = 10247; m2[8070] = 42056; m1.swap(map_); - EXPECT_THAT(m1, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); - EXPECT_THAT(map_, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(9398, 41999))); + EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(9398, 41999))); + EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(9398, 41999))); m2.swap(m1); - EXPECT_THAT(m1, testing::UnorderedElementsAre( - testing::Pair(8070, 42056), - testing::Pair(10244, 10247))); - EXPECT_THAT(m2, testing::UnorderedElementsAre( - testing::Pair(9398, 41999))); + EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(8070, 42056), + testing::Pair(10244, 10247))); + EXPECT_THAT(m2, testing::UnorderedElementsAre(testing::Pair(9398, 41999))); } TEST_F(MapImplTest, CopyAssignMapIterator) { TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(&message); MapIterator it1 = reflection_tester.MapBegin(&message, "map_int32_int32"); MapIterator it2 = reflection_tester.MapEnd(&message, "map_int32_int32"); @@ -991,9 +982,7 @@ TEST_F(MapImplTest, CopyAssignMapIterator) { // Map Field Reflection Test ======================================== -static int Func(int i, int j) { - return i * j; -} +static int Func(int i, int j) { return i * j; } static std::string StrFunc(int i, int j) { std::string str; @@ -1070,10 +1059,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double); const RepeatedPtrField<Message>& mf_string_string = refl->GetRepeatedPtrField<Message>(message, fd_map_string_string); - const RepeatedPtrField<Message>& - mf_int32_foreign_message = - refl->GetRepeatedPtrField<Message>( - message, fd_map_int32_foreign_message); + const RepeatedPtrField<Message>& mf_int32_foreign_message = + refl->GetRepeatedPtrField<Message>(message, fd_map_int32_foreign_message); // Get mutable RepeatedPtrField objects for all fields of interest. RepeatedPtrField<Message>* mmf_int32_int32 = @@ -1083,8 +1070,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { RepeatedPtrField<Message>* mmf_string_string = refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string); RepeatedPtrField<Message>* mmf_int32_foreign_message = - refl->MutableRepeatedPtrField<Message>( - &message, fd_map_int32_foreign_message); + refl->MutableRepeatedPtrField<Message>(&message, + fd_map_int32_foreign_message); // Make sure we can do gets through the RepeatedPtrField objects. for (int i = 0; i < 10; ++i) { @@ -1119,9 +1106,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast<const ForeignMessage&>( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } @@ -1156,9 +1142,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast<const ForeignMessage&>( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } } @@ -1193,9 +1178,8 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message->GetReflection()->GetInt32( *message_int32_message, fd_map_int32_foreign_message_key); ForeignMessage* value_int32_message = down_cast<ForeignMessage*>( - message_int32_message->GetReflection() - ->MutableMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message->GetReflection()->MutableMessage( + message_int32_message, fd_map_int32_foreign_message_value)); value_int32_message->set_c(Func(key_int32_message, -6)); } } @@ -1272,10 +1256,9 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double); const MutableRepeatedFieldRef<Message> mmf_string_string = refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string); - const MutableRepeatedFieldRef<Message> - mmf_int32_foreign_message = - refl->GetMutableRepeatedFieldRef<Message>( - &message, fd_map_int32_foreign_message); + const MutableRepeatedFieldRef<Message> mmf_int32_foreign_message = + refl->GetMutableRepeatedFieldRef<Message>(&message, + fd_map_int32_foreign_message); // Get entry default instances std::unique_ptr<Message> entry_int32_int32( @@ -1350,9 +1333,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast<const ForeignMessage&>( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } @@ -1391,9 +1373,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = down_cast<const ForeignMessage&>( - message_int32_message.GetReflection() - ->GetMessage(message_int32_message, - fd_map_int32_foreign_message_value)); + message_int32_message.GetReflection()->GetMessage( + message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); } } @@ -1518,9 +1499,9 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message = *it; int32 key = message.GetReflection()->GetInt32( message, fd_map_int32_foreign_message_key); - const ForeignMessage& sub_message = down_cast<const ForeignMessage&>( - message.GetReflection() - ->GetMessage(message, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message = + down_cast<const ForeignMessage&>(message.GetReflection()->GetMessage( + message, fd_map_int32_foreign_message_value)); result[key].MergeFrom(sub_message); ++index; } @@ -1552,8 +1533,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(1234.0, message.map_int32_double().at(4321)); entry_string_string->GetReflection()->SetString( - entry_string_string.get(), - fd_map_string_string->message_type()->field(0), "4321"); + entry_string_string.get(), fd_map_string_string->message_type()->field(0), + "4321"); entry_string_string->GetReflection()->SetString( entry_string_string.get(), fd_map_string_string->message_type()->field(1), "1234"); @@ -1575,12 +1556,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c()); // Test Reflection::AddAllocatedMessage - Message* free_entry_string_string = MessageFactory::generated_factory() - ->GetPrototype(fd_map_string_string->message_type()) - ->New(); + Message* free_entry_string_string = + MessageFactory::generated_factory() + ->GetPrototype(fd_map_string_string->message_type()) + ->New(); entry_string_string->GetReflection()->SetString( - free_entry_string_string, - fd_map_string_string->message_type()->field(0), "4321"); + free_entry_string_string, fd_map_string_string->message_type()->field(0), + "4321"); entry_string_string->GetReflection()->SetString( free_entry_string_string, fd_map_string_string->message_type()->field(1), "1234"); @@ -1672,30 +1654,30 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { { const Message& message0a = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>( - message0a.GetReflection() - ->GetMessage(message0a, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message0a = + down_cast<const ForeignMessage&>(message0a.GetReflection()->GetMessage( + message0a, fd_map_int32_foreign_message_value)); int32 int32_value0a = sub_message0a.c(); const Message& message9a = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9a = down_cast<const ForeignMessage&>( - message9a.GetReflection() - ->GetMessage(message9a, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message9a = + down_cast<const ForeignMessage&>(message9a.GetReflection()->GetMessage( + message9a, fd_map_int32_foreign_message_value)); int32 int32_value9a = sub_message9a.c(); mmf_int32_foreign_message.SwapElements(0, 9); const Message& message0b = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0b = down_cast<const ForeignMessage&>( - message0b.GetReflection() - ->GetMessage(message0b, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message0b = + down_cast<const ForeignMessage&>(message0b.GetReflection()->GetMessage( + message0b, fd_map_int32_foreign_message_value)); int32 int32_value0b = sub_message0b.c(); const Message& message9b = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9b = down_cast<const ForeignMessage&>( - message9b.GetReflection() - ->GetMessage(message9b, fd_map_int32_foreign_message_value)); + const ForeignMessage& sub_message9b = + down_cast<const ForeignMessage&>(message9b.GetReflection()->GetMessage( + message9b, fd_map_int32_foreign_message_value)); int32 int32_value9b = sub_message9b.c(); EXPECT_EQ(int32_value9a, int32_value0b); @@ -1734,34 +1716,26 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { const FieldDescriptor* fd_map_int32_foreign_message = desc->FindFieldByName("map_int32_foreign_message"); - // Get MutableRepeatedFieldRef objects for all fields of interest. + // Get MutableRepeatedFieldRef objects for all fields of interest. const MutableRepeatedFieldRef<Message> mmf_int32_int32 = - refl->GetMutableRepeatedFieldRef<Message>( - &m0, fd_map_int32_int32); + refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_int32_int32); const MutableRepeatedFieldRef<Message> mmf_int32_double = - refl->GetMutableRepeatedFieldRef<Message>( - &m0, fd_map_int32_double); + refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_int32_double); const MutableRepeatedFieldRef<Message> mmf_string_string = - refl->GetMutableRepeatedFieldRef<Message>( - &m0, fd_map_string_string); - const MutableRepeatedFieldRef<Message> - mmf_int32_foreign_message = - refl->GetMutableRepeatedFieldRef<Message>( - &m0, fd_map_int32_foreign_message); + refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_string_string); + const MutableRepeatedFieldRef<Message> mmf_int32_foreign_message = + refl->GetMutableRepeatedFieldRef<Message>(&m0, + fd_map_int32_foreign_message); // Test MutableRepeatedRef::CopyFrom mmf_int32_int32.CopyFrom( - refl->GetRepeatedFieldRef<Message>( - m1, fd_map_int32_int32)); + refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_int32)); mmf_int32_double.CopyFrom( - refl->GetRepeatedFieldRef<Message>( - m1, fd_map_int32_double)); + refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_double)); mmf_string_string.CopyFrom( - refl->GetRepeatedFieldRef<Message>( - m1, fd_map_string_string)); + refl->GetRepeatedFieldRef<Message>(m1, fd_map_string_string)); mmf_int32_foreign_message.CopyFrom( - refl->GetRepeatedFieldRef<Message>( - m1, fd_map_int32_foreign_message)); + refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10)); @@ -1772,17 +1746,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { // Test MutableRepeatedRef::MergeFrom mmf_int32_int32.MergeFrom( - refl->GetRepeatedFieldRef<Message>( - m2, fd_map_int32_int32)); + refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_int32)); mmf_int32_double.MergeFrom( - refl->GetRepeatedFieldRef<Message>( - m2, fd_map_int32_double)); + refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_double)); mmf_string_string.MergeFrom( - refl->GetRepeatedFieldRef<Message>( - m2, fd_map_string_string)); + refl->GetRepeatedFieldRef<Message>(m2, fd_map_string_string)); mmf_int32_foreign_message.MergeFrom( - refl->GetRepeatedFieldRef<Message>( - m2, fd_map_int32_foreign_message)); + refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20)); @@ -1793,17 +1763,13 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { // Test MutableRepeatedRef::Swap // Swap between m0 and m2. mmf_int32_int32.Swap( - refl->GetMutableRepeatedFieldRef<Message>( - &m2, fd_map_int32_int32)); + refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_int32_int32)); mmf_int32_double.Swap( - refl->GetMutableRepeatedFieldRef<Message>( - &m2, fd_map_int32_double)); + refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_int32_double)); mmf_string_string.Swap( - refl->GetMutableRepeatedFieldRef<Message>( - &m2, fd_map_string_string)); - mmf_int32_foreign_message.Swap( - refl->GetMutableRepeatedFieldRef<Message>( - &m2, fd_map_int32_foreign_message)); + refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_string_string)); + mmf_int32_foreign_message.Swap(refl->GetMutableRepeatedFieldRef<Message>( + &m2, fd_map_int32_foreign_message)); for (int i = 0; i < 10; ++i) { // Check the content of m0. EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); @@ -2015,10 +1981,8 @@ TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr<Message> message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(message1.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2032,10 +1996,8 @@ TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr<Message> message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(message1.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2047,23 +2009,19 @@ TEST(GeneratedMapFieldTest, DynamicMessageMergeFromDynamicMessage) { // Construct two dynamic message and sets via map reflection. DynamicMessageFactory factory; std::unique_ptr<Message> message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(message1.get()); // message2 is created by same factory. std::unique_ptr<Message> message2; - message2.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message2.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); reflection_tester.SetMapFieldsViaMapReflection(message2.get()); // message3 is created by different factory. DynamicMessageFactory factory3; std::unique_ptr<Message> message3; - message3.reset( - factory3.GetPrototype(unittest::TestMap::descriptor())->New()); + message3.reset(factory3.GetPrototype(unittest::TestMap::descriptor())->New()); reflection_tester.SetMapFieldsViaMapReflection(message3.get()); message2->MergeFrom(*message1); @@ -2087,27 +2045,23 @@ TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) { // Construct a new version of the dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr<Message> message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); message1->MergeFrom(message2); reflection_tester.ExpectMapFieldsSetViaReflection(*message1); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); } TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) { - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); unittest::TestMap message2; reflection_tester.SetMapFieldsViaMapReflection(&message2); // Construct a dynamic message via the factory. DynamicMessageFactory factory; std::unique_ptr<Message> message1; - message1.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message1.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); message1->MergeFrom(message2); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get()); @@ -2116,12 +2070,10 @@ TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) { TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) { // Construct a dynamic message via the factory. - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); DynamicMessageFactory factory; std::unique_ptr<Message> message; - message.reset( - factory.GetPrototype(unittest::TestMap::descriptor())->New()); + message.reset(factory.GetPrototype(unittest::TestMap::descriptor())->New()); reflection_tester.SetMapFieldsViaReflection(message.get()); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*message); @@ -2336,8 +2288,8 @@ TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) { for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) { bool is_key = k & 0x1; bool is_value = !is_key && (k & 0x2); - wire_format.push_back(is_key ? kKeyTag : - is_value ? kValueTag : kJunkTag); + wire_format.push_back(is_key ? kKeyTag + : is_value ? kValueTag : kJunkTag); char c = static_cast<char>(k & mask) >> 2; // One char after the tag. wire_format.push_back(c); if (is_key) expected_key = static_cast<int>(c); @@ -2499,10 +2451,10 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); // Test key then value then value then key. - s.push_back(s[2]); // Copy the key's tag. + s.push_back(s[2]); // Copy the key's tag. key = 19; - s.push_back(key); // Second key is 19 instead of 0. - s[1] += 2; // Adjust encoded size. + s.push_back(key); // Second key is 19 instead of 0. + s[1] += 2; // Adjust encoded size. ASSERT_TRUE(map_message.ParseFromString(s)); ASSERT_EQ(1, map_message.map_field().size()); ASSERT_EQ(1, map_message.map_field().count(key)); @@ -2517,8 +2469,7 @@ TEST(GeneratedMapFieldTest, MessagesMustMerge) { TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(&message); EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); @@ -2528,8 +2479,7 @@ TEST(GeneratedMapFieldReflectionTest, Accessors) { // Set every field to a unique value then go back and check all those // values. unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(&message); MapTestUtil::ExpectMapFieldsSet(message); reflection_tester.ExpectMapFieldsSetViaReflection(message); @@ -2587,8 +2537,7 @@ TEST(GeneratedMapFieldReflectionTest, ClearField) { MapTestUtil::SetMapFields(&message); MapTestUtil::ExpectMapFieldsSet(message); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.ClearMapFieldsViaReflection(&message); reflection_tester.ExpectClearViaReflection(message); reflection_tester.ExpectClearViaReflectionIterator(&message); @@ -2596,8 +2545,7 @@ TEST(GeneratedMapFieldReflectionTest, ClearField) { TEST(GeneratedMapFieldReflectionTest, RemoveLast) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); MapTestUtil::SetMapFields(&message); MapTestUtil::ExpectMapsSize(message, 2); @@ -2644,8 +2592,7 @@ TEST(GeneratedMapFieldReflectionTest, ReleaseLast) { TEST(GeneratedMapFieldReflectionTest, SwapElements) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); MapTestUtil::SetMapFields(&message); @@ -2684,8 +2631,7 @@ TEST(GeneratedMapFieldReflectionTest, SwapElements) { TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); } @@ -2740,8 +2686,7 @@ TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) { TEST(GeneratedMapFieldReflectionTest, MapEntryClear) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); } @@ -2762,8 +2707,7 @@ TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) { TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) { unittest::TestMap message; - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaMapReflection(&message); reflection_tester.ExpectMapFieldsSetViaReflection(message); reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message); @@ -2783,8 +2727,7 @@ class MapFieldInDynamicMessageTest : public testing::Test { : pool_(DescriptorPool::generated_pool()), factory_(pool_) {} virtual void SetUp() { - map_descriptor_ = - pool_->FindMessageTypeByName("protobuf_unittest.TestMap"); + map_descriptor_ = pool_->FindMessageTypeByName("protobuf_unittest.TestMap"); recursive_map_descriptor_ = pool_->FindMessageTypeByName("protobuf_unittest.TestRecursiveMapMessage"); ASSERT_TRUE(map_descriptor_ != NULL); @@ -2952,15 +2895,15 @@ TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) { MapTestUtil::SetMapFields(&message); // Set some unknown fields in message. - message.GetReflection()->MutableUnknownFields(&message)-> - AddVarint(123456, 654321); + message.GetReflection()->MutableUnknownFields(&message)->AddVarint(123456, + 654321); // Discard them. ReflectionOps::DiscardUnknownFields(&message); MapTestUtil::ExpectMapFieldsSet(message); - EXPECT_EQ(0, message.GetReflection()-> - GetUnknownFields(message).field_count()); + EXPECT_EQ(0, + message.GetReflection()->GetUnknownFields(message).field_count()); } TEST(ReflectionOpsForMapFieldTest, IsInitialized) { @@ -3042,8 +2985,7 @@ TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) { std::unique_ptr<Message> dynamic_message; dynamic_message.reset( factory.GetPrototype(unittest::TestMap::descriptor())->New()); - MapReflectionTester reflection_tester( - unittest::TestMap::descriptor()); + MapReflectionTester reflection_tester(unittest::TestMap::descriptor()); reflection_tester.SetMapFieldsViaReflection(dynamic_message.get()); reflection_tester.ExpectMapFieldsSetViaReflection(*dynamic_message); @@ -3106,7 +3048,7 @@ TEST(WireFormatForMapFieldTest, MapParseHelpers) { io::ArrayInputStream stream(data.data(), data.size()); protobuf_unittest::TestAllTypes message; EXPECT_FALSE( - message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); + message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); } } @@ -3263,10 +3205,10 @@ TEST(TextFormatMapTest, DynamicMessage) { tester.SetMapFieldsViaReflection(message.get()); std::string expected_text; - GOOGLE_CHECK_OK(File::GetContents( - TestUtil::GetTestDataPath("net/proto2/internal/" - "testdata/map_test_data.txt"), - &expected_text, true)); + GOOGLE_CHECK_OK( + File::GetContents(TestUtil::GetTestDataPath("net/proto2/internal/" + "testdata/map_test_data.txt"), + &expected_text, true)); EXPECT_EQ(message->DebugString(), expected_text); } @@ -3312,8 +3254,7 @@ TEST(TextFormatMapTest, NoDisableIterator) { (*source.mutable_map_int32_int32())[1] = 1; // Get iterator. - Map<int32, int32>::iterator iter = - source.mutable_map_int32_int32()->find(1); + Map<int32, int32>::iterator iter = source.mutable_map_int32_int32()->find(1); // Serialize message to text format, which will invalidate the previous // iterator previously. diff --git a/src/google/protobuf/map_test_util.cc b/src/google/protobuf/map_test_util.cc index 83502afd32..51c73664ad 100644 --- a/src/google/protobuf/map_test_util.cc +++ b/src/google/protobuf/map_test_util.cc @@ -78,49 +78,59 @@ void MapTestUtil::ExpectMapFieldsSetInitialized( message); } -void MapTestUtil::ExpectMapFieldsModified( - const unittest::TestMap& message) { +void MapTestUtil::ExpectMapFieldsModified(const unittest::TestMap& message) { MapTestUtilImpl::ExpectMapFieldsModified< unittest::MapEnum, unittest::MAP_ENUM_BAR, unittest::MAP_ENUM_FOO>( message); } -void MapTestUtil::ExpectMapsSize( - const unittest::TestMap& message, int size) { +void MapTestUtil::ExpectMapsSize(const unittest::TestMap& message, int size) { const Descriptor* descriptor = message.GetDescriptor(); EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_int32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int64_int64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_uint32_uint32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_uint64_uint64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sint32_sint32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sint64_sint64"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_fixed32_fixed32"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_fixed64_fixed64"))); + message, descriptor->FindFieldByName("map_int32_int32"))); EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); + message, descriptor->FindFieldByName("map_int64_int64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_uint32_uint32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_uint64_uint64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sint32_sint32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sint64_sint64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_fixed32_fixed32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_fixed64_fixed64"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); + message, descriptor->FindFieldByName("map_int32_float"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_double"))); EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_float"))); + message, descriptor->FindFieldByName("map_bool_bool"))); + EXPECT_EQ(size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_string_string"))); EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_double"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_bool_bool"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_string_string"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_bytes"))); - EXPECT_EQ(size, message.GetReflection()->FieldSize( - message, descriptor->FindFieldByName("map_int32_foreign_message"))); + message, descriptor->FindFieldByName("map_int32_bytes"))); + EXPECT_EQ( + size, + message.GetReflection()->FieldSize( + message, descriptor->FindFieldByName("map_int32_foreign_message"))); } std::vector<const Message*> MapTestUtil::GetMapEntries( @@ -129,39 +139,40 @@ std::vector<const Message*> MapTestUtil::GetMapEntries( std::vector<const Message*> result; result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_int32"), index)); + message, descriptor->FindFieldByName("map_int32_int32"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int64_int64"), index)); + message, descriptor->FindFieldByName("map_int64_int64"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_uint32_uint32"), index)); + message, descriptor->FindFieldByName("map_uint32_uint32"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_uint64_uint64"), index)); + message, descriptor->FindFieldByName("map_uint64_uint64"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sint32_sint32"), index)); + message, descriptor->FindFieldByName("map_sint32_sint32"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sint64_sint64"), index)); + message, descriptor->FindFieldByName("map_sint64_sint64"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_fixed32_fixed32"), index)); + message, descriptor->FindFieldByName("map_fixed32_fixed32"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_fixed64_fixed64"), index)); + message, descriptor->FindFieldByName("map_fixed64_fixed64"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index)); + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index)); + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_float"), index)); + message, descriptor->FindFieldByName("map_int32_float"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_double"), index)); + message, descriptor->FindFieldByName("map_int32_double"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_bool_bool"), index)); + message, descriptor->FindFieldByName("map_bool_bool"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_string_string"), index)); + message, descriptor->FindFieldByName("map_string_string"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_bytes"), index)); + message, descriptor->FindFieldByName("map_int32_bytes"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_enum"), index)); + message, descriptor->FindFieldByName("map_int32_enum"), index)); result.push_back(&message.GetReflection()->GetRepeatedMessage( - message, descriptor->FindFieldByName("map_int32_foreign_message"), index)); + message, descriptor->FindFieldByName("map_int32_foreign_message"), + index)); return result; } @@ -172,76 +183,74 @@ std::vector<const Message*> MapTestUtil::GetMapEntriesFromRelease( std::vector<const Message*> result; result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_int32"))); + message, descriptor->FindFieldByName("map_int32_int32"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int64_int64"))); + message, descriptor->FindFieldByName("map_int64_int64"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_uint32_uint32"))); + message, descriptor->FindFieldByName("map_uint32_uint32"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_uint64_uint64"))); + message, descriptor->FindFieldByName("map_uint64_uint64"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sint32_sint32"))); + message, descriptor->FindFieldByName("map_sint32_sint32"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sint64_sint64"))); + message, descriptor->FindFieldByName("map_sint64_sint64"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_fixed32_fixed32"))); + message, descriptor->FindFieldByName("map_fixed32_fixed32"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_fixed64_fixed64"))); + message, descriptor->FindFieldByName("map_fixed64_fixed64"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); + message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); + message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_float"))); + message, descriptor->FindFieldByName("map_int32_float"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_double"))); + message, descriptor->FindFieldByName("map_int32_double"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_bool_bool"))); + message, descriptor->FindFieldByName("map_bool_bool"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_string_string"))); + message, descriptor->FindFieldByName("map_string_string"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_bytes"))); + message, descriptor->FindFieldByName("map_int32_bytes"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_enum"))); + message, descriptor->FindFieldByName("map_int32_enum"))); result.push_back(message->GetReflection()->ReleaseLast( - message, descriptor->FindFieldByName("map_int32_foreign_message"))); + message, descriptor->FindFieldByName("map_int32_foreign_message"))); return result; } -MapReflectionTester::MapReflectionTester( - const Descriptor* base_descriptor) - : base_descriptor_(base_descriptor) { +MapReflectionTester::MapReflectionTester(const Descriptor* base_descriptor) + : base_descriptor_(base_descriptor) { const DescriptorPool* pool = base_descriptor->file()->pool(); map_enum_foo_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_FOO"); map_enum_bar_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAR"); map_enum_baz_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAZ"); - foreign_c_ = pool->FindFieldByName( - "protobuf_unittest.ForeignMessage.c"); - map_int32_int32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32Int32Entry.key"); - map_int32_int32_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32Int32Entry.value"); - map_int64_int64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt64Int64Entry.key"); - map_int64_int64_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt64Int64Entry.value"); - map_uint32_uint32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint32Uint32Entry.key"); + foreign_c_ = pool->FindFieldByName("protobuf_unittest.ForeignMessage.c"); + map_int32_int32_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32Int32Entry.key"); + map_int32_int32_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32Int32Entry.value"); + map_int64_int64_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt64Int64Entry.key"); + map_int64_int64_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt64Int64Entry.value"); + map_uint32_uint32_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapUint32Uint32Entry.key"); map_uint32_uint32_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapUint32Uint32Entry.value"); - map_uint64_uint64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapUint64Uint64Entry.key"); + map_uint64_uint64_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapUint64Uint64Entry.key"); map_uint64_uint64_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapUint64Uint64Entry.value"); - map_sint32_sint32_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint32Sint32Entry.key"); + map_sint32_sint32_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapSint32Sint32Entry.key"); map_sint32_sint32_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapSint32Sint32Entry.value"); - map_sint64_sint64_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapSint64Sint64Entry.key"); + map_sint64_sint64_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapSint64Sint64Entry.key"); map_sint64_sint64_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapSint64Sint64Entry.value"); map_fixed32_fixed32_key_ = pool->FindFieldByName( @@ -260,30 +269,30 @@ MapReflectionTester::MapReflectionTester( "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.key"); map_sfixed64_sfixed64_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.value"); - map_int32_float_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32FloatEntry.key"); - map_int32_float_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32FloatEntry.value"); - map_int32_double_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32DoubleEntry.key"); + map_int32_float_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32FloatEntry.key"); + map_int32_float_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32FloatEntry.value"); + map_int32_double_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32DoubleEntry.key"); map_int32_double_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapInt32DoubleEntry.value"); - map_bool_bool_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapBoolBoolEntry.key"); - map_bool_bool_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapBoolBoolEntry.value"); - map_string_string_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapStringStringEntry.key"); + map_bool_bool_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapBoolBoolEntry.key"); + map_bool_bool_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapBoolBoolEntry.value"); + map_string_string_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapStringStringEntry.key"); map_string_string_val_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapStringStringEntry.value"); - map_int32_bytes_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32BytesEntry.key"); - map_int32_bytes_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32BytesEntry.value"); - map_int32_enum_key_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32EnumEntry.key"); - map_int32_enum_val_ = pool->FindFieldByName( - "protobuf_unittest.TestMap.MapInt32EnumEntry.value"); + map_int32_bytes_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32BytesEntry.key"); + map_int32_bytes_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32BytesEntry.value"); + map_int32_enum_key_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32EnumEntry.key"); + map_int32_enum_val_ = + pool->FindFieldByName("protobuf_unittest.TestMap.MapInt32EnumEntry.value"); map_int32_foreign_message_key_ = pool->FindFieldByName( "protobuf_unittest.TestMap.MapInt32ForeignMessageEntry.key"); map_int32_foreign_message_val_ = pool->FindFieldByName( @@ -336,227 +345,203 @@ const FieldDescriptor* MapReflectionTester::F(const std::string& name) { return result; } -void MapReflectionTester::SetMapFieldsViaReflection( - Message* message) { +void MapReflectionTester::SetMapFieldsViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); Message* sub_message = NULL; Message* sub_foreign_message = NULL; // Add first element. sub_message = reflection->AddMessage(message, F("map_int32_int32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_val_, 0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_key_, 0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_val_, 0); sub_message = reflection->AddMessage(message, F("map_int64_int64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_val_, 0); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_key_, 0); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_val_, 0); sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_key_, 0); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_val_, 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_key_, + 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_val_, + 0); sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_key_, 0); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_val_, 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_key_, + 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_val_, + 0); sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_val_, 0); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_key_, + 0); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_val_, + 0); sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_val_, 0); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_key_, + 0); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_val_, + 0); sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 0); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_key_, + 0); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_val_, + 0); sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 0); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_key_, + 0); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_val_, + 0); sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 0); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 0); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_key_, 0); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_val_, 0); sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 0); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 0); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_key_, 0); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_val_, 0); sub_message = reflection->AddMessage(message, F("map_int32_float")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_float_key_, 0); - sub_message->GetReflection() - ->SetFloat(sub_message, map_int32_float_val_, 0.0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_float_key_, 0); + sub_message->GetReflection()->SetFloat(sub_message, map_int32_float_val_, + 0.0); sub_message = reflection->AddMessage(message, F("map_int32_double")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_double_key_, 0); - sub_message->GetReflection() - ->SetDouble(sub_message, map_int32_double_val_, 0.0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_double_key_, 0); + sub_message->GetReflection()->SetDouble(sub_message, map_int32_double_val_, + 0.0); sub_message = reflection->AddMessage(message, F("map_bool_bool")); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_key_, false); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_val_, false); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_key_, false); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_val_, false); sub_message = reflection->AddMessage(message, F("map_string_string")); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_key_, "0"); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_val_, "0"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_key_, + "0"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_val_, + "0"); sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_bytes_key_, 0); - sub_message->GetReflection() - ->SetString(sub_message, map_int32_bytes_val_, "0"); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_bytes_key_, 0); + sub_message->GetReflection()->SetString(sub_message, map_int32_bytes_val_, + "0"); sub_message = reflection->AddMessage(message, F("map_int32_enum")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_enum_key_, 0); - sub_message->GetReflection() - ->SetEnum(sub_message, map_int32_enum_val_, map_enum_bar_); - - sub_message = reflection - ->AddMessage(message, F("map_int32_foreign_message")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_foreign_message_key_, 0); - sub_foreign_message = sub_message->GetReflection()-> - MutableMessage(sub_message, map_int32_foreign_message_val_, NULL); - sub_foreign_message->GetReflection()-> - SetInt32(sub_foreign_message, foreign_c_, 0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_enum_key_, 0); + sub_message->GetReflection()->SetEnum(sub_message, map_int32_enum_val_, + map_enum_bar_); + + sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); + sub_message->GetReflection()->SetInt32(sub_message, + map_int32_foreign_message_key_, 0); + sub_foreign_message = sub_message->GetReflection()->MutableMessage( + sub_message, map_int32_foreign_message_val_, NULL); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 0); // Add second element sub_message = reflection->AddMessage(message, F("map_int32_int32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_int32_val_, 1); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_key_, 1); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_int32_val_, 1); sub_message = reflection->AddMessage(message, F("map_int64_int64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_int64_int64_val_, 1); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_key_, 1); + sub_message->GetReflection()->SetInt64(sub_message, map_int64_int64_val_, 1); sub_message = reflection->AddMessage(message, F("map_uint32_uint32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_key_, 1); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_uint32_uint32_val_, 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_key_, + 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_uint32_uint32_val_, + 1); sub_message = reflection->AddMessage(message, F("map_uint64_uint64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_key_, 1); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_uint64_uint64_val_, 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_key_, + 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_uint64_uint64_val_, + 1); sub_message = reflection->AddMessage(message, F("map_sint32_sint32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sint32_sint32_val_, 1); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_key_, + 1); + sub_message->GetReflection()->SetInt32(sub_message, map_sint32_sint32_val_, + 1); sub_message = reflection->AddMessage(message, F("map_sint64_sint64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sint64_sint64_val_, 1); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_key_, + 1); + sub_message->GetReflection()->SetInt64(sub_message, map_sint64_sint64_val_, + 1); sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32")); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 1); - sub_message->GetReflection() - ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_key_, + 1); + sub_message->GetReflection()->SetUInt32(sub_message, map_fixed32_fixed32_val_, + 1); sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64")); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 1); - sub_message->GetReflection() - ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_key_, + 1); + sub_message->GetReflection()->SetUInt64(sub_message, map_fixed64_fixed64_val_, + 1); sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 1); - sub_message->GetReflection() - ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 1); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_key_, 1); + sub_message->GetReflection()->SetInt32(sub_message, + map_sfixed32_sfixed32_val_, 1); sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64")); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 1); - sub_message->GetReflection() - ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 1); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_key_, 1); + sub_message->GetReflection()->SetInt64(sub_message, + map_sfixed64_sfixed64_val_, 1); sub_message = reflection->AddMessage(message, F("map_int32_float")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_float_key_, 1); - sub_message->GetReflection() - ->SetFloat(sub_message, map_int32_float_val_, 1.0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_float_key_, 1); + sub_message->GetReflection()->SetFloat(sub_message, map_int32_float_val_, + 1.0); sub_message = reflection->AddMessage(message, F("map_int32_double")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_double_key_, 1); - sub_message->GetReflection() - ->SetDouble(sub_message, map_int32_double_val_, 1.0); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_double_key_, 1); + sub_message->GetReflection()->SetDouble(sub_message, map_int32_double_val_, + 1.0); sub_message = reflection->AddMessage(message, F("map_bool_bool")); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_key_, true); - sub_message->GetReflection() - ->SetBool(sub_message, map_bool_bool_val_, true); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_key_, true); + sub_message->GetReflection()->SetBool(sub_message, map_bool_bool_val_, true); sub_message = reflection->AddMessage(message, F("map_string_string")); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_key_, "1"); - sub_message->GetReflection() - ->SetString(sub_message, map_string_string_val_, "1"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_key_, + "1"); + sub_message->GetReflection()->SetString(sub_message, map_string_string_val_, + "1"); sub_message = reflection->AddMessage(message, F("map_int32_bytes")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_bytes_key_, 1); - sub_message->GetReflection() - ->SetString(sub_message, map_int32_bytes_val_, "1"); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_bytes_key_, 1); + sub_message->GetReflection()->SetString(sub_message, map_int32_bytes_val_, + "1"); sub_message = reflection->AddMessage(message, F("map_int32_enum")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_enum_key_, 1); - sub_message->GetReflection() - ->SetEnum(sub_message, map_int32_enum_val_, map_enum_baz_); - - sub_message = reflection - ->AddMessage(message, F("map_int32_foreign_message")); - sub_message->GetReflection() - ->SetInt32(sub_message, map_int32_foreign_message_key_, 1); - sub_foreign_message = sub_message->GetReflection()-> - MutableMessage(sub_message, map_int32_foreign_message_val_, NULL); - sub_foreign_message->GetReflection()-> - SetInt32(sub_foreign_message, foreign_c_, 1); + sub_message->GetReflection()->SetInt32(sub_message, map_int32_enum_key_, 1); + sub_message->GetReflection()->SetEnum(sub_message, map_int32_enum_val_, + map_enum_baz_); + + sub_message = reflection->AddMessage(message, F("map_int32_foreign_message")); + sub_message->GetReflection()->SetInt32(sub_message, + map_int32_foreign_message_key_, 1); + sub_foreign_message = sub_message->GetReflection()->MutableMessage( + sub_message, map_int32_foreign_message_val_, NULL); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 1); } -void MapReflectionTester::SetMapFieldsViaMapReflection( - Message* message) { +void MapReflectionTester::SetMapFieldsViaMapReflection(Message* message) { const Reflection* reflection = message->GetReflection(); Message* sub_foreign_message = NULL; @@ -565,13 +550,13 @@ void MapReflectionTester::SetMapFieldsViaMapReflection( // Add first element. MapKey map_key; map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); map_val.SetInt32Value(0); map_key.SetInt64Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); map_val.SetInt64Value(0); map_key.SetUInt32Value(0); @@ -615,18 +600,18 @@ void MapReflectionTester::SetMapFieldsViaMapReflection( map_val.SetInt64Value(0); map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_float"), + map_key, &map_val)); map_val.SetFloatValue(0.0); map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_double"), + map_key, &map_val)); map_val.SetDoubleValue(0.0); map_key.SetBoolValue(false); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), + map_key, &map_val)); map_val.SetBoolValue(false); map_key.SetStringValue("0"); @@ -635,113 +620,113 @@ void MapReflectionTester::SetMapFieldsViaMapReflection( map_val.SetStringValue("0"); map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), + map_key, &map_val)); map_val.SetStringValue("0"); map_key.SetInt32Value(0); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), + map_key, &map_val)); map_val.SetEnumValue(map_enum_bar_->number()); map_key.SetInt32Value(0); EXPECT_TRUE(reflection->InsertOrLookupMapValue( message, F("map_int32_foreign_message"), map_key, &map_val)); sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 0); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 0); // Add second element map_key.SetInt32Value(1); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); map_val.SetInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); map_key.SetInt64Value(1); - EXPECT_TRUE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); + EXPECT_TRUE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); map_val.SetInt64Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint32_uint32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_uint32_uint32"), map_key, + &map_val); map_val.SetUInt32Value(1); map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint64_uint64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_uint64_uint64"), map_key, + &map_val); map_val.SetUInt64Value(1); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint32_sint32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sint32_sint32"), map_key, + &map_val); map_val.SetInt32Value(1); map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint64_sint64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sint64_sint64"), map_key, + &map_val); map_val.SetInt64Value(1); map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed32_fixed32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_fixed32_fixed32"), map_key, + &map_val); map_val.SetUInt32Value(1); map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed64_fixed64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_fixed64_fixed64"), map_key, + &map_val); map_val.SetUInt64Value(1); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed32_sfixed32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sfixed32_sfixed32"), + map_key, &map_val); map_val.SetInt32Value(1); map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed64_sfixed64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sfixed64_sfixed64"), + map_key, &map_val); map_val.SetInt64Value(1); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_float"), map_key, + &map_val); map_val.SetFloatValue(1.0); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_double"), map_key, + &map_val); map_val.SetDoubleValue(1.0); map_key.SetBoolValue(true); - reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), map_key, + &map_val); map_val.SetBoolValue(true); map_key.SetStringValue("1"); - reflection->InsertOrLookupMapValue( - message, F("map_string_string"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_string_string"), map_key, + &map_val); map_val.SetStringValue("1"); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), map_key, + &map_val); map_val.SetStringValue("1"); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), map_key, + &map_val); map_val.SetEnumValue(map_enum_baz_->number()); map_key.SetInt32Value(1); EXPECT_TRUE(reflection->InsertOrLookupMapValue( message, F("map_int32_foreign_message"), map_key, &map_val)); sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 1); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 1); } void MapReflectionTester::GetMapValueViaMapReflection( @@ -770,8 +755,7 @@ MapIterator MapReflectionTester::MapEnd(Message* message, return reflection->MapEnd(message, F(field_name)); } -void MapReflectionTester::ClearMapFieldsViaReflection( - Message* message) { +void MapReflectionTester::ClearMapFieldsViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); reflection->ClearField(message, F("map_int32_int32")); @@ -793,8 +777,7 @@ void MapReflectionTester::ClearMapFieldsViaReflection( reflection->ClearField(message, F("map_int32_foreign_message")); } -void MapReflectionTester::ModifyMapFieldsViaReflection( - Message* message) { +void MapReflectionTester::ModifyMapFieldsViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); MapValueRef map_val; Message* sub_foreign_message; @@ -802,13 +785,13 @@ void MapReflectionTester::ModifyMapFieldsViaReflection( // Modify the second element MapKey map_key; map_key.SetInt32Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int32_int32"), map_key, &map_val)); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int32_int32"), + map_key, &map_val)); map_val.SetInt32Value(2); map_key.SetInt64Value(1); - EXPECT_FALSE(reflection->InsertOrLookupMapValue( - message, F("map_int64_int64"), map_key, &map_val)); + EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F("map_int64_int64"), + map_key, &map_val)); map_val.SetInt64Value(2); map_key.SetUInt32Value(1); @@ -817,80 +800,79 @@ void MapReflectionTester::ModifyMapFieldsViaReflection( map_val.SetUInt32Value(2); map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_uint64_uint64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_uint64_uint64"), map_key, + &map_val); map_val.SetUInt64Value(2); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint32_sint32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sint32_sint32"), map_key, + &map_val); map_val.SetInt32Value(2); map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sint64_sint64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sint64_sint64"), map_key, + &map_val); map_val.SetInt64Value(2); map_key.SetUInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed32_fixed32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_fixed32_fixed32"), map_key, + &map_val); map_val.SetUInt32Value(2); map_key.SetUInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_fixed64_fixed64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_fixed64_fixed64"), map_key, + &map_val); map_val.SetUInt64Value(2); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed32_sfixed32"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sfixed32_sfixed32"), + map_key, &map_val); map_val.SetInt32Value(2); map_key.SetInt64Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_sfixed64_sfixed64"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_sfixed64_sfixed64"), + map_key, &map_val); map_val.SetInt64Value(2); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_float"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_float"), map_key, + &map_val); map_val.SetFloatValue(2.0); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_double"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_double"), map_key, + &map_val); map_val.SetDoubleValue(2.0); map_key.SetBoolValue(true); - reflection->InsertOrLookupMapValue( - message, F("map_bool_bool"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_bool_bool"), map_key, + &map_val); map_val.SetBoolValue(false); map_key.SetStringValue("1"); - reflection->InsertOrLookupMapValue( - message, F("map_string_string"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_string_string"), map_key, + &map_val); map_val.SetStringValue("2"); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_bytes"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_bytes"), map_key, + &map_val); map_val.SetStringValue("2"); map_key.SetInt32Value(1); - reflection->InsertOrLookupMapValue( - message, F("map_int32_enum"), map_key, &map_val); + reflection->InsertOrLookupMapValue(message, F("map_int32_enum"), map_key, + &map_val); map_val.SetEnumValue(map_enum_foo_->number()); map_key.SetInt32Value(1); EXPECT_FALSE(reflection->InsertOrLookupMapValue( message, F("map_int32_foreign_message"), map_key, &map_val)); sub_foreign_message = map_val.MutableMessageValue(); - sub_foreign_message->GetReflection()->SetInt32( - sub_foreign_message, foreign_c_, 2); + sub_foreign_message->GetReflection()->SetInt32(sub_foreign_message, + foreign_c_, 2); } -void MapReflectionTester::RemoveLastMapsViaReflection( - Message* message) { +void MapReflectionTester::RemoveLastMapsViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); std::vector<const FieldDescriptor*> output; @@ -902,8 +884,7 @@ void MapReflectionTester::RemoveLastMapsViaReflection( } } -void MapReflectionTester::ReleaseLastMapsViaReflection( - Message* message) { +void MapReflectionTester::ReleaseLastMapsViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); std::vector<const FieldDescriptor*> output; @@ -914,8 +895,8 @@ void MapReflectionTester::ReleaseLastMapsViaReflection( if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; Message* released = reflection->ReleaseLast(message, field); - ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: " - << field->name(); + ASSERT_TRUE(released != NULL) + << "ReleaseLast returned NULL for: " << field->name(); delete released; } } @@ -931,8 +912,8 @@ void MapReflectionTester::SwapMapsViaReflection(Message* message) { } } -void MapReflectionTester:: - MutableUnknownFieldsOfMapFieldsViaReflection(Message* message) { +void MapReflectionTester::MutableUnknownFieldsOfMapFieldsViaReflection( + Message* message) { const Reflection* reflection = message->GetReflection(); Message* sub_message = NULL; @@ -1024,15 +1005,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with RepeatedField Reflection sub_message = &reflection->GetRepeatedMessage(message, F("map_int32_int32"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_int32_key_); - int32 val = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_int32_val_); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_int32_key_); + int32 val = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_int32_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_int32_int32"), map_key)); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_int32_int32"), map_key)); } } { @@ -1043,15 +1024,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with RepeatedField Reflection sub_message = &reflection->GetRepeatedMessage(message, F("map_int64_int64"), i); - int64 key = sub_message->GetReflection()->GetInt64( - *sub_message, map_int64_int64_key_); - int64 val = sub_message->GetReflection()->GetInt64( - *sub_message, map_int64_int64_val_); + int64 key = sub_message->GetReflection()->GetInt64(*sub_message, + map_int64_int64_key_); + int64 val = sub_message->GetReflection()->GetInt64(*sub_message, + map_int64_int64_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt64Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_int64_int64"), map_key)); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_int64_int64"), map_key)); } } { @@ -1069,8 +1050,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetUInt32Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_uint32_uint32"), map_key)); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_uint32_uint32"), map_key)); } } { @@ -1087,8 +1068,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetUInt64Value(key); - EXPECT_TRUE(reflection->ContainsMapKey( - message, F("map_uint64_uint64"), map_key)); + EXPECT_TRUE( + reflection->ContainsMapKey(message, F("map_uint64_uint64"), map_key)); } } { @@ -1106,7 +1087,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetInt32Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sint32_sint32"), map_key)); + message, F("map_sint32_sint32"), map_key)); } } { @@ -1124,7 +1105,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetInt64Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sint64_sint64"), map_key)); + message, F("map_sint64_sint64"), map_key)); } } { @@ -1142,7 +1123,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetUInt32Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_fixed32_fixed32"), map_key)); + message, F("map_fixed32_fixed32"), map_key)); } } { @@ -1160,7 +1141,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetUInt64Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_fixed64_fixed64"), map_key)); + message, F("map_fixed64_fixed64"), map_key)); } } { @@ -1178,7 +1159,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetInt32Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sfixed32_sfixed32"), map_key)); + message, F("map_sfixed32_sfixed32"), map_key)); } } { @@ -1196,7 +1177,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetInt64Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_sfixed64_sfixed64"), map_key)); + message, F("map_sfixed64_sfixed64"), map_key)); } } { @@ -1206,15 +1187,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( for (int i = 0; i < 2; i++) { sub_message = &reflection->GetRepeatedMessage(message, F("map_int32_float"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_float_key_); - float val = sub_message->GetReflection()->GetFloat( - *sub_message, map_int32_float_val_); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_float_key_); + float val = sub_message->GetReflection()->GetFloat(*sub_message, + map_int32_float_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_float"), map_key)); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_float"), + map_key)); } } { @@ -1224,15 +1205,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( for (int i = 0; i < 2; i++) { sub_message = &reflection->GetRepeatedMessage(message, F("map_int32_double"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_double_key_); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_double_key_); double val = sub_message->GetReflection()->GetDouble( *sub_message, map_int32_double_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_double"), map_key)); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_double"), + map_key)); } } { @@ -1242,15 +1223,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( for (int i = 0; i < 2; i++) { sub_message = &reflection->GetRepeatedMessage(message, F("map_bool_bool"), i); - bool key = sub_message->GetReflection()->GetBool( - *sub_message, map_bool_bool_key_); - bool val = sub_message->GetReflection()->GetBool( - *sub_message, map_bool_bool_val_); + bool key = sub_message->GetReflection()->GetBool(*sub_message, + map_bool_bool_key_); + bool val = sub_message->GetReflection()->GetBool(*sub_message, + map_bool_bool_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetBoolValue(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_bool_bool"), map_key)); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_bool_bool"), + map_key)); } } { @@ -1268,7 +1249,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( // Check with Map Reflection map_key.SetStringValue(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_string_string"), map_key)); + message, F("map_string_string"), map_key)); } } { @@ -1278,15 +1259,15 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( for (int i = 0; i < 2; i++) { sub_message = &reflection->GetRepeatedMessage(message, F("map_int32_bytes"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_bytes_key_); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_bytes_key_); std::string val = sub_message->GetReflection()->GetString( *sub_message, map_int32_bytes_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_bytes"), map_key)); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_bytes"), + map_key)); } } { @@ -1294,17 +1275,17 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( map[0] = map_enum_bar_; map[1] = map_enum_baz_; for (int i = 0; i < 2; i++) { - sub_message = &reflection->GetRepeatedMessage( - message, F("map_int32_enum"), i); - int32 key = sub_message->GetReflection()->GetInt32( - *sub_message, map_int32_enum_key_); + sub_message = + &reflection->GetRepeatedMessage(message, F("map_int32_enum"), i); + int32 key = sub_message->GetReflection()->GetInt32(*sub_message, + map_int32_enum_key_); const EnumValueDescriptor* val = sub_message->GetReflection()->GetEnum( *sub_message, map_int32_enum_val_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); - EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_enum"), map_key)); + EXPECT_EQ(true, reflection->ContainsMapKey(message, F("map_int32_enum"), + map_key)); } } { @@ -1314,17 +1295,17 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflection( for (int i = 0; i < 2; i++) { sub_message = &reflection->GetRepeatedMessage( message, F("map_int32_foreign_message"), i); - int32 key = sub_message->GetReflection()->GetInt32( + int32 key = sub_message->GetReflection()->GetInt32( *sub_message, map_int32_foreign_message_key_); const Message& foreign_message = sub_message->GetReflection()->GetMessage( *sub_message, map_int32_foreign_message_val_); - int32 val = foreign_message.GetReflection()->GetInt32( - foreign_message, foreign_c_); + int32 val = foreign_message.GetReflection()->GetInt32(foreign_message, + foreign_c_); EXPECT_EQ(map[key], val); // Check with Map Reflection map_key.SetInt32Value(key); EXPECT_EQ(true, reflection->ContainsMapKey( - message, F("map_int32_foreign_message"), map_key)); + message, F("map_int32_foreign_message"), map_key)); } } } @@ -1386,10 +1367,9 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<uint32, uint32> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_uint32_uint32")); - iter != reflection->MapEnd(message, F("map_uint32_uint32")); - ++iter) { + for (MapIterator iter = + reflection->MapBegin(message, F("map_uint32_uint32")); + iter != reflection->MapEnd(message, F("map_uint32_uint32")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], iter.GetValueRef().GetUInt32Value()); } @@ -1398,10 +1378,9 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<uint64, uint64> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_uint64_uint64")); - iter != reflection->MapEnd(message, F("map_uint64_uint64")); - ++iter) { + for (MapIterator iter = + reflection->MapBegin(message, F("map_uint64_uint64")); + iter != reflection->MapEnd(message, F("map_uint64_uint64")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], iter.GetValueRef().GetUInt64Value()); } @@ -1410,10 +1389,9 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<int32, int32> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sint32_sint32")); - iter != reflection->MapEnd(message, F("map_sint32_sint32")); - ++iter) { + for (MapIterator iter = + reflection->MapBegin(message, F("map_sint32_sint32")); + iter != reflection->MapEnd(message, F("map_sint32_sint32")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetInt32Value()], iter.GetValueRef().GetInt32Value()); } @@ -1422,8 +1400,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<int64, int64> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sint64_sint64")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_sint64_sint64")); iter != reflection->MapEnd(message, F("map_sint64_sint64")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetInt64Value()], iter.GetValueRef().GetInt64Value()); @@ -1433,8 +1411,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<uint32, uint32> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_fixed32_fixed32")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_fixed32_fixed32")); iter != reflection->MapEnd(message, F("map_fixed32_fixed32")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetUInt32Value()], @@ -1445,8 +1423,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<uint64, uint64> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_fixed64_fixed64")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_fixed64_fixed64")); iter != reflection->MapEnd(message, F("map_fixed64_fixed64")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetUInt64Value()], @@ -1457,8 +1435,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<int32, int32> map; map[0] = 0; map[1] = 1; - for (MapIterator iter = reflection->MapBegin( - message, F("map_sfixed32_sfixed32")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_sfixed32_sfixed32")); iter != reflection->MapEnd(message, F("map_sfixed32_sfixed32")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetInt32Value()], @@ -1479,8 +1457,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( std::map<int32, double> map; map[0] = 0.0; map[1] = 1.0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_int32_double")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_int32_double")); iter != reflection->MapEnd(message, F("map_int32_double")); ++iter) { EXPECT_EQ(map[iter.GetKey().GetInt32Value()], iter.GetValueRef().GetDoubleValue()); @@ -1501,8 +1479,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( map["0"] = "0"; map["1"] = "1"; int size = 0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_string_string")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_string_string")); iter != reflection->MapEnd(message, F("map_string_string")); ++iter, ++size) { // Check const methods do not invalidate map. @@ -1541,8 +1519,8 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( map[0] = 0; map[1] = 1; int size = 0; - for (MapIterator iter = reflection->MapBegin( - message, F("map_int32_foreign_message")); + for (MapIterator iter = + reflection->MapBegin(message, F("map_int32_foreign_message")); iter != reflection->MapEnd(message, F("map_int32_foreign_message")); ++iter, ++size) { // Check const methods do not invalidate map. @@ -1559,8 +1537,7 @@ void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator( } } -void MapReflectionTester::ExpectClearViaReflection( - const Message& message) { +void MapReflectionTester::ExpectClearViaReflection(const Message& message) { const Reflection* reflection = message.GetReflection(); // Map fields are empty. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_int32"))); @@ -1580,12 +1557,11 @@ void MapReflectionTester::ExpectClearViaReflection( EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_bytes"))); EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_enum"))); EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_foreign_message"))); - EXPECT_TRUE(reflection->GetMapData( - message, F("map_int32_foreign_message"))->IsMapValid()); + EXPECT_TRUE(reflection->GetMapData(message, F("map_int32_foreign_message")) + ->IsMapValid()); } -void MapReflectionTester::ExpectClearViaReflectionIterator( - Message* message) { +void MapReflectionTester::ExpectClearViaReflectionIterator(Message* message) { const Reflection* reflection = message->GetReflection(); EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_int32")) == reflection->MapEnd(message, F("map_int32_int32"))); @@ -1623,8 +1599,7 @@ void MapReflectionTester::ExpectClearViaReflectionIterator( reflection->MapEnd(message, F("map_int32_foreign_message"))); } -void MapReflectionTester::ExpectMapEntryClearViaReflection( - Message* message) { +void MapReflectionTester::ExpectMapEntryClearViaReflection(Message* message) { const Reflection* reflection = message->GetReflection(); const Message* sub_message; @@ -1806,7 +1781,7 @@ void MapReflectionTester::ExpectMapEntryClearViaReflection( EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, key_descriptor)); EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message, - value_descriptor)); + value_descriptor)); } { const FieldDescriptor* descriptor = F("map_int32_enum"); diff --git a/src/google/protobuf/map_test_util.h b/src/google/protobuf/map_test_util.h index ede290b413..00d2fa4d46 100644 --- a/src/google/protobuf/map_test_util.h +++ b/src/google/protobuf/map_test_util.h @@ -63,8 +63,7 @@ class MapTestUtil { // Check that all fields have the values that they should have after // SetMapFieldsInitialized() is called. - static void ExpectMapFieldsSetInitialized( - const unittest::TestMap& message); + static void ExpectMapFieldsSetInitialized(const unittest::TestMap& message); // Expect that the message is modified as would be expected from // ModifyMapFields(). diff --git a/src/google/protobuf/map_test_util_impl.h b/src/google/protobuf/map_test_util_impl.h index 2e8b4ba4c7..70145b7e51 100644 --- a/src/google/protobuf/map_test_util_impl.h +++ b/src/google/protobuf/map_test_util_impl.h @@ -36,7 +36,7 @@ #include <gtest/gtest.h> -namespace protobuf_unittest {} // forward declaration +namespace protobuf_unittest {} // namespace protobuf_unittest namespace google { namespace protobuf { @@ -46,13 +46,13 @@ namespace unittest = ::protobuf_unittest; class MapTestUtilImpl { public: // Set every field in the TestMap message to a unique value. - template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> + template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> static void SetMapFields(MapMessage* message); // Set every field in the TestArenaMap message to a unique value. - template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> + template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> static void SetArenaMapFields(MapMessage* message); // Set every field in the message to a default value. @@ -66,14 +66,14 @@ class MapTestUtilImpl { // Check that all fields have the values that they should have after // SetMapFields() is called. - template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> + template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> static void ExpectMapFieldsSet(const MapMessage& message); // Check that all fields have the values that they should have after // SetMapFields() is called for TestArenaMap. - template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> + template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> static void ExpectArenaMapFieldsSet(const MapMessage& message); // Check that all fields have the values that they should have after @@ -83,8 +83,8 @@ class MapTestUtilImpl { // Expect that the message is modified as would be expected from // ModifyMapFields(). - template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> + template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> static void ExpectMapFieldsModified(const MapMessage& message); // Check that all fields are empty. @@ -104,8 +104,8 @@ class MapTestUtilImpl { // MapMessage* message); }; -template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> +template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> void MapTestUtilImpl::SetMapFields(MapMessage* message) { // Add first element. (*message->mutable_map_int32_int32())[0] = 0; @@ -146,8 +146,8 @@ void MapTestUtilImpl::SetMapFields(MapMessage* message) { (*message->mutable_map_int32_foreign_message())[1].set_c(1); } -template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> +template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> void MapTestUtilImpl::SetArenaMapFields(MapMessage* message) { // Add first element. (*message->mutable_map_int32_int32())[0] = 0; @@ -255,10 +255,8 @@ void MapTestUtilImpl::ExpectClear(const MapMessage& message) { EXPECT_EQ(0, message.map_int32_foreign_message().size()); } - - -template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> +template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> void MapTestUtilImpl::ExpectMapFieldsSet(const MapMessage& message) { ASSERT_EQ(2, message.map_int32_int32().size()); ASSERT_EQ(2, message.map_int64_int64().size()); @@ -315,8 +313,8 @@ void MapTestUtilImpl::ExpectMapFieldsSet(const MapMessage& message) { EXPECT_EQ(1, message.map_int32_foreign_message().at(1).c()); } -template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> +template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> void MapTestUtilImpl::ExpectArenaMapFieldsSet(const MapMessage& message) { EXPECT_EQ(2, message.map_int32_int32().size()); EXPECT_EQ(2, message.map_int64_int64().size()); @@ -377,8 +375,7 @@ void MapTestUtilImpl::ExpectArenaMapFieldsSet(const MapMessage& message) { } template <typename EnumType, EnumType enum_value, typename MapMessage> -void MapTestUtilImpl::ExpectMapFieldsSetInitialized( - const MapMessage& message) { +void MapTestUtilImpl::ExpectMapFieldsSetInitialized(const MapMessage& message) { EXPECT_EQ(1, message.map_int32_int32().size()); EXPECT_EQ(1, message.map_int64_int64().size()); EXPECT_EQ(1, message.map_uint32_uint32().size()); @@ -416,10 +413,9 @@ void MapTestUtilImpl::ExpectMapFieldsSetInitialized( EXPECT_EQ(0, message.map_int32_foreign_message().at(0).ByteSize()); } -template <typename EnumType, EnumType enum_value0, - EnumType enum_value1, typename MapMessage> -void MapTestUtilImpl::ExpectMapFieldsModified( - const MapMessage& message) { +template <typename EnumType, EnumType enum_value0, EnumType enum_value1, + typename MapMessage> +void MapTestUtilImpl::ExpectMapFieldsModified(const MapMessage& message) { // ModifyMapFields only sets the second element of each field. In addition to // verifying this, we also verify that the first element and size were *not* // modified. diff --git a/src/google/protobuf/map_type_handler.h b/src/google/protobuf/map_type_handler.h index 0caf1a7229..7964a97af1 100644 --- a/src/google/protobuf/map_type_handler.h +++ b/src/google/protobuf/map_type_handler.h @@ -46,15 +46,15 @@ namespace internal { // Used for compile time type selection. MapIf::type will be TrueType if Flag is // true and FalseType otherwise. -template<bool Flag, typename TrueType, typename FalseType> +template <bool Flag, typename TrueType, typename FalseType> struct MapIf; -template<typename TrueType, typename FalseType> +template <typename TrueType, typename FalseType> struct MapIf<true, TrueType, FalseType> { typedef TrueType type; }; -template<typename TrueType, typename FalseType> +template <typename TrueType, typename FalseType> struct MapIf<false, TrueType, FalseType> { typedef FalseType type; }; @@ -78,7 +78,8 @@ class MapValueInitializer<true, Type> { template <typename Type> class MapValueInitializer<false, Type> { public: - static inline void Initialize(Type& /* value */, int /* default_enum_value */) {} + static inline void Initialize(Type& /* value */, + int /* default_enum_value */) {} }; template <typename Type, bool is_arena_constructable> @@ -119,23 +120,23 @@ class MapWireFieldTypeTraits {}; WireFormatLite::WIRETYPE_##WireFormatType; \ }; -TYPE_TRAITS(MESSAGE , Type, LENGTH_DELIMITED, true, false) -TYPE_TRAITS(STRING , ArenaStringPtr, LENGTH_DELIMITED, false, false) -TYPE_TRAITS(BYTES , ArenaStringPtr , LENGTH_DELIMITED, false, false) -TYPE_TRAITS(INT64 , int64 , VARINT , false, false) -TYPE_TRAITS(UINT64 , uint64 , VARINT , false, false) -TYPE_TRAITS(INT32 , int32 , VARINT , false, false) -TYPE_TRAITS(UINT32 , uint32 , VARINT , false, false) -TYPE_TRAITS(SINT64 , int64 , VARINT , false, false) -TYPE_TRAITS(SINT32 , int32 , VARINT , false, false) -TYPE_TRAITS(ENUM , int , VARINT , false, true ) -TYPE_TRAITS(DOUBLE , double , FIXED64, false, false) -TYPE_TRAITS(FLOAT , float , FIXED32, false, false) -TYPE_TRAITS(FIXED64 , uint64 , FIXED64, false, false) -TYPE_TRAITS(FIXED32 , uint32 , FIXED32, false, false) -TYPE_TRAITS(SFIXED64, int64 , FIXED64, false, false) -TYPE_TRAITS(SFIXED32, int32 , FIXED32, false, false) -TYPE_TRAITS(BOOL , bool , VARINT , false, false) +TYPE_TRAITS(MESSAGE, Type, LENGTH_DELIMITED, true, false) +TYPE_TRAITS(STRING, ArenaStringPtr, LENGTH_DELIMITED, false, false) +TYPE_TRAITS(BYTES, ArenaStringPtr, LENGTH_DELIMITED, false, false) +TYPE_TRAITS(INT64, int64, VARINT, false, false) +TYPE_TRAITS(UINT64, uint64, VARINT, false, false) +TYPE_TRAITS(INT32, int32, VARINT, false, false) +TYPE_TRAITS(UINT32, uint32, VARINT, false, false) +TYPE_TRAITS(SINT64, int64, VARINT, false, false) +TYPE_TRAITS(SINT32, int32, VARINT, false, false) +TYPE_TRAITS(ENUM, int, VARINT, false, true) +TYPE_TRAITS(DOUBLE, double, FIXED64, false, false) +TYPE_TRAITS(FLOAT, float, FIXED32, false, false) +TYPE_TRAITS(FIXED64, uint64, FIXED64, false, false) +TYPE_TRAITS(FIXED32, uint32, FIXED32, false, false) +TYPE_TRAITS(SFIXED64, int64, FIXED64, false, false) +TYPE_TRAITS(SFIXED32, int32, FIXED32, false, false) +TYPE_TRAITS(BOOL, bool, VARINT, false, false) #undef TYPE_TRAITS @@ -148,7 +149,8 @@ class MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type> { // Enum type cannot be used for MapTypeHandler::Read. Define a type which will // replace Enum with int. typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_MESSAGE, - Type>::MapEntryAccessorType MapEntryAccessorType; + Type>::MapEntryAccessorType + MapEntryAccessorType; // Internal stored type in MapEntryLite for given wire field type. typedef typename MapWireFieldTypeTraits<WireFormatLite::TYPE_MESSAGE, Type>::TypeOnMemory TypeOnMemory; @@ -198,8 +200,8 @@ class MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type> { static inline void AssignDefaultValue(Type** value); // Return default instance if value is not initialized when calling const // reference accessor. - static inline const Type& DefaultIfNotInitialized( - const Type* value, const Type* default_value); + static inline const Type& DefaultIfNotInitialized(const Type* value, + const Type* default_value); // Check if all required fields have values set. static inline bool IsInitialized(Type* value); }; @@ -276,8 +278,7 @@ MAP_HANDLER(BOOL) #undef MAP_HANDLER template <typename Type> -inline size_t -MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::ByteSize( +inline size_t MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::ByteSize( const MapEntryAccessorType& value) { return WireFormatLite::MessageSizeNoVirtual(value); } @@ -290,14 +291,14 @@ MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::ByteSize( } GOOGLE_PROTOBUF_BYTE_SIZE(STRING, String) -GOOGLE_PROTOBUF_BYTE_SIZE(BYTES , Bytes) -GOOGLE_PROTOBUF_BYTE_SIZE(INT64 , Int64) +GOOGLE_PROTOBUF_BYTE_SIZE(BYTES, Bytes) +GOOGLE_PROTOBUF_BYTE_SIZE(INT64, Int64) GOOGLE_PROTOBUF_BYTE_SIZE(UINT64, UInt64) -GOOGLE_PROTOBUF_BYTE_SIZE(INT32 , Int32) +GOOGLE_PROTOBUF_BYTE_SIZE(INT32, Int32) GOOGLE_PROTOBUF_BYTE_SIZE(UINT32, UInt32) GOOGLE_PROTOBUF_BYTE_SIZE(SINT64, SInt64) GOOGLE_PROTOBUF_BYTE_SIZE(SINT32, SInt32) -GOOGLE_PROTOBUF_BYTE_SIZE(ENUM , Enum) +GOOGLE_PROTOBUF_BYTE_SIZE(ENUM, Enum) #undef GOOGLE_PROTOBUF_BYTE_SIZE @@ -308,23 +309,21 @@ GOOGLE_PROTOBUF_BYTE_SIZE(ENUM , Enum) return WireFormatLite::k##DeclaredType##Size; \ } -FIXED_BYTE_SIZE(DOUBLE , Double) -FIXED_BYTE_SIZE(FLOAT , Float) -FIXED_BYTE_SIZE(FIXED64 , Fixed64) -FIXED_BYTE_SIZE(FIXED32 , Fixed32) +FIXED_BYTE_SIZE(DOUBLE, Double) +FIXED_BYTE_SIZE(FLOAT, Float) +FIXED_BYTE_SIZE(FIXED64, Fixed64) +FIXED_BYTE_SIZE(FIXED32, Fixed32) FIXED_BYTE_SIZE(SFIXED64, SFixed64) FIXED_BYTE_SIZE(SFIXED32, SFixed32) -FIXED_BYTE_SIZE(BOOL , Bool) +FIXED_BYTE_SIZE(BOOL, Bool) #undef FIXED_BYTE_SIZE template <typename Type> -inline int -MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::GetCachedSize( +inline int MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::GetCachedSize( const MapEntryAccessorType& value) { - return static_cast<int>( - WireFormatLite::LengthDelimitedSize( - static_cast<size_t>(value.GetCachedSize()))); + return static_cast<int>(WireFormatLite::LengthDelimitedSize( + static_cast<size_t>(value.GetCachedSize()))); } #define GET_CACHED_SIZE(FieldType, DeclaredType) \ @@ -336,14 +335,14 @@ MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::GetCachedSize( } GET_CACHED_SIZE(STRING, String) -GET_CACHED_SIZE(BYTES , Bytes) -GET_CACHED_SIZE(INT64 , Int64) +GET_CACHED_SIZE(BYTES, Bytes) +GET_CACHED_SIZE(INT64, Int64) GET_CACHED_SIZE(UINT64, UInt64) -GET_CACHED_SIZE(INT32 , Int32) +GET_CACHED_SIZE(INT32, Int32) GET_CACHED_SIZE(UINT32, UInt32) GET_CACHED_SIZE(SINT64, SInt64) GET_CACHED_SIZE(SINT32, SInt32) -GET_CACHED_SIZE(ENUM , Enum) +GET_CACHED_SIZE(ENUM, Enum) #undef GET_CACHED_SIZE @@ -355,13 +354,13 @@ GET_CACHED_SIZE(ENUM , Enum) return WireFormatLite::k##DeclaredType##Size; \ } -GET_FIXED_CACHED_SIZE(DOUBLE , Double) -GET_FIXED_CACHED_SIZE(FLOAT , Float) -GET_FIXED_CACHED_SIZE(FIXED64 , Fixed64) -GET_FIXED_CACHED_SIZE(FIXED32 , Fixed32) +GET_FIXED_CACHED_SIZE(DOUBLE, Double) +GET_FIXED_CACHED_SIZE(FLOAT, Float) +GET_FIXED_CACHED_SIZE(FIXED64, Fixed64) +GET_FIXED_CACHED_SIZE(FIXED32, Fixed32) GET_FIXED_CACHED_SIZE(SFIXED64, SFixed64) GET_FIXED_CACHED_SIZE(SFIXED32, SFixed32) -GET_FIXED_CACHED_SIZE(BOOL , Bool) +GET_FIXED_CACHED_SIZE(BOOL, Bool) #undef GET_FIXED_CACHED_SIZE @@ -392,22 +391,22 @@ inline uint8* MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::WriteToArray( return WireFormatLite::Write##DeclaredType##ToArray(field, value, target); \ } -WRITE_METHOD(STRING , String) -WRITE_METHOD(BYTES , Bytes) -WRITE_METHOD(INT64 , Int64) -WRITE_METHOD(UINT64 , UInt64) -WRITE_METHOD(INT32 , Int32) -WRITE_METHOD(UINT32 , UInt32) -WRITE_METHOD(SINT64 , SInt64) -WRITE_METHOD(SINT32 , SInt32) -WRITE_METHOD(ENUM , Enum) -WRITE_METHOD(DOUBLE , Double) -WRITE_METHOD(FLOAT , Float) -WRITE_METHOD(FIXED64 , Fixed64) -WRITE_METHOD(FIXED32 , Fixed32) +WRITE_METHOD(STRING, String) +WRITE_METHOD(BYTES, Bytes) +WRITE_METHOD(INT64, Int64) +WRITE_METHOD(UINT64, UInt64) +WRITE_METHOD(INT32, Int32) +WRITE_METHOD(UINT32, UInt32) +WRITE_METHOD(SINT64, SInt64) +WRITE_METHOD(SINT32, SInt32) +WRITE_METHOD(ENUM, Enum) +WRITE_METHOD(DOUBLE, Double) +WRITE_METHOD(FLOAT, Float) +WRITE_METHOD(FIXED64, Fixed64) +WRITE_METHOD(FIXED32, Fixed32) WRITE_METHOD(SFIXED64, SFixed64) WRITE_METHOD(SFIXED32, SFixed32) -WRITE_METHOD(BOOL , Bool) +WRITE_METHOD(BOOL, Bool) #undef WRITE_METHOD @@ -554,8 +553,8 @@ READ_METHOD(BOOL) template <typename Type> inline const Type& -MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::GetExternalReference(const Type* value) { +MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::GetExternalReference( + const Type* value) { return *value; } @@ -578,10 +577,8 @@ inline void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::Clear( } template <typename Type> inline void -MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::ClearMaybeByDefaultEnum(Type** value, - Arena* /* arena */, - int /* default_enum_value */) { +MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::ClearMaybeByDefaultEnum( + Type** value, Arena* /* arena */, int /* default_enum_value */) { if (*value != NULL) (*value)->Clear(); } template <typename Type> @@ -598,14 +595,13 @@ void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::DeleteNoArena( template <typename Type> inline void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::AssignDefaultValue(Type** value) { + Type>::AssignDefaultValue(Type** value) { *value = const_cast<Type*>(Type::internal_default_instance()); } template <typename Type> -inline void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::Initialize(Type** x, - Arena* /* arena */) { +inline void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::Initialize( + Type** x, Arena* /* arena */) { *x = NULL; } @@ -617,26 +613,26 @@ inline void MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>:: } template <typename Type> -inline Type* MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::EnsureMutable(Type** value, - Arena* arena) { +inline Type* MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::EnsureMutable( + Type** value, Arena* arena) { if (*value == NULL) { - *value = - MapArenaMessageCreator<Type, Arena::is_arena_constructable<Type>:: - type::value>::CreateMessage(arena); + *value = MapArenaMessageCreator< + Type, + Arena::is_arena_constructable<Type>::type::value>::CreateMessage(arena); } return *value; } template <typename Type> -inline const Type& MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>:: - DefaultIfNotInitialized(const Type* value, const Type* default_value) { +inline const Type& +MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::DefaultIfNotInitialized( + const Type* value, const Type* default_value) { return value != NULL ? *value : *default_value; } template <typename Type> -inline bool MapTypeHandler<WireFormatLite::TYPE_MESSAGE, - Type>::IsInitialized(Type* value) { +inline bool MapTypeHandler<WireFormatLite::TYPE_MESSAGE, Type>::IsInitialized( + Type* value) { return value->IsInitialized(); } @@ -730,82 +726,81 @@ STRING_OR_BYTES_HANDLER_FUNCTIONS(STRING) STRING_OR_BYTES_HANDLER_FUNCTIONS(BYTES) #undef STRING_OR_BYTES_HANDLER_FUNCTIONS -#define PRIMITIVE_HANDLER_FUNCTIONS(FieldType) \ - template <typename Type> \ - inline const typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::MapEntryAccessorType& \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::GetExternalReference(const TypeOnMemory& value) { \ - return value; \ - } \ - template <typename Type> \ - inline size_t \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::SpaceUsedInMapEntryLong(const TypeOnMemory& /* value */) { \ - return 0; \ - } \ - template <typename Type> \ - inline size_t \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::SpaceUsedInMapLong( \ - const TypeOnMemory& /* value */) { \ - return sizeof(Type); \ - } \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Clear( \ - TypeOnMemory* value, Arena* /* arena */) { \ - *value = 0; \ - } \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ - ClearMaybeByDefaultEnum(TypeOnMemory* value, Arena* /* arena */, \ - int default_enum_value) { \ - *value = static_cast<TypeOnMemory>(default_enum_value); \ - } \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Merge( \ - const MapEntryAccessorType& from, TypeOnMemory* to, \ - Arena* /* arena */) { \ - *to = from; \ - } \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::DeleteNoArena(TypeOnMemory& /* x */) {} \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::AssignDefaultValue(TypeOnMemory* /* value */) {} \ - template <typename Type> \ - inline void \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Initialize( \ - TypeOnMemory* value, Arena* /* arena */) { \ - *value = 0; \ - } \ - template <typename Type> \ - inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ - InitializeMaybeByDefaultEnum(TypeOnMemory* value, \ - int default_enum_value, \ - Arena* /* arena */) { \ - *value = static_cast<TypeOnMemory>(default_enum_value); \ - } \ - template <typename Type> \ - inline typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::MapEntryAccessorType* \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::EnsureMutable( \ - TypeOnMemory* value, Arena* /* arena */) { \ - return value; \ - } \ - template <typename Type> \ - inline const typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::MapEntryAccessorType& \ - MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::DefaultIfNotInitialized(const TypeOnMemory& value, \ - const TypeOnMemory& \ - /* default_value */) { \ - return value; \ - } \ - template <typename Type> \ - inline bool MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ - Type>::IsInitialized(const TypeOnMemory& /* value */) { \ - return true; \ +#define PRIMITIVE_HANDLER_FUNCTIONS(FieldType) \ + template <typename Type> \ + inline const typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ + Type>::MapEntryAccessorType& \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ + Type>::GetExternalReference(const TypeOnMemory& value) { \ + return value; \ + } \ + template <typename Type> \ + inline size_t MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ + SpaceUsedInMapEntryLong(const TypeOnMemory& /* value */) { \ + return 0; \ + } \ + template <typename Type> \ + inline size_t \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::SpaceUsedInMapLong( \ + const TypeOnMemory& /* value */) { \ + return sizeof(Type); \ + } \ + template <typename Type> \ + inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Clear( \ + TypeOnMemory* value, Arena* /* arena */) { \ + *value = 0; \ + } \ + template <typename Type> \ + inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ + ClearMaybeByDefaultEnum(TypeOnMemory* value, Arena* /* arena */, \ + int default_enum_value) { \ + *value = static_cast<TypeOnMemory>(default_enum_value); \ + } \ + template <typename Type> \ + inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Merge( \ + const MapEntryAccessorType& from, TypeOnMemory* to, \ + Arena* /* arena */) { \ + *to = from; \ + } \ + template <typename Type> \ + inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ + Type>::DeleteNoArena(TypeOnMemory& /* x */) {} \ + template <typename Type> \ + inline void \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::AssignDefaultValue( \ + TypeOnMemory* /* value */) {} \ + template <typename Type> \ + inline void \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::Initialize( \ + TypeOnMemory* value, Arena* /* arena */) { \ + *value = 0; \ + } \ + template <typename Type> \ + inline void MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ + InitializeMaybeByDefaultEnum( \ + TypeOnMemory* value, int default_enum_value, Arena* /* arena */) { \ + *value = static_cast<TypeOnMemory>(default_enum_value); \ + } \ + template <typename Type> \ + inline typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ + Type>::MapEntryAccessorType* \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::EnsureMutable( \ + TypeOnMemory* value, Arena* /* arena */) { \ + return value; \ + } \ + template <typename Type> \ + inline const typename MapTypeHandler<WireFormatLite::TYPE_##FieldType, \ + Type>::MapEntryAccessorType& \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>:: \ + DefaultIfNotInitialized(const TypeOnMemory& value, \ + const TypeOnMemory& /* default_value */) { \ + return value; \ + } \ + template <typename Type> \ + inline bool \ + MapTypeHandler<WireFormatLite::TYPE_##FieldType, Type>::IsInitialized( \ + const TypeOnMemory& /* value */) { \ + return true; \ } PRIMITIVE_HANDLER_FUNCTIONS(INT64) PRIMITIVE_HANDLER_FUNCTIONS(UINT64) diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index 252841373b..e274383cd0 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -149,37 +149,37 @@ class MapReflectionTester; namespace internal { class MapFieldBase; } -class UnknownFieldSet; // unknown_field_set.h +class UnknownFieldSet; // unknown_field_set.h namespace io { -class ZeroCopyInputStream; // zero_copy_stream.h -class ZeroCopyOutputStream; // zero_copy_stream.h -class CodedInputStream; // coded_stream.h -class CodedOutputStream; // coded_stream.h -} +class ZeroCopyInputStream; // zero_copy_stream.h +class ZeroCopyOutputStream; // zero_copy_stream.h +class CodedInputStream; // coded_stream.h +class CodedOutputStream; // coded_stream.h +} // namespace io namespace python { -class MapReflectionFriend; // scalar_map_container.h +class MapReflectionFriend; // scalar_map_container.h } namespace expr { class CelMapReflectionFriend; // field_backed_map_impl.cc } namespace internal { -class MapFieldPrinterHelper; // text_format.cc +class MapFieldPrinterHelper; // text_format.cc } namespace internal { -class ReflectionAccessor; // message.cc -class ReflectionOps; // reflection_ops.h -class MapKeySorter; // wire_format.cc -class WireFormat; // wire_format.h +class ReflectionAccessor; // message.cc +class ReflectionOps; // reflection_ops.h +class MapKeySorter; // wire_format.cc +class WireFormat; // wire_format.h class MapFieldReflectionTest; // map_test.cc -} +} // namespace internal -template<typename T> -class RepeatedField; // repeated_field.h +template <typename T> +class RepeatedField; // repeated_field.h -template<typename T> +template <typename T> class RepeatedPtrField; // repeated_field.h // A container to hold message metadata. @@ -342,7 +342,6 @@ class PROTOBUF_EXPORT Message : public MessageLite { virtual void SetCachedSize(int size) const; public: - // Introspection --------------------------------------------------- @@ -366,7 +365,7 @@ class PROTOBUF_EXPORT Message : public MessageLite { // Get a struct containing the metadata for the Message. Most subclasses only // need to implement this method, rather than the GetDescriptor() and // GetReflection() wrappers. - virtual Metadata GetMetadata() const = 0; + virtual Metadata GetMetadata() const = 0; private: @@ -381,10 +380,10 @@ class RepeatedFieldAccessor; // Forward-declare RepeatedFieldRef templates. The second type parameter is // used for SFINAE tricks. Users should ignore it. -template<typename T, typename Enable = void> +template <typename T, typename Enable = void> class RepeatedFieldRef; -template<typename T, typename Enable = void> +template <typename T, typename Enable = void> class MutableRepeatedFieldRef; // This interface contains methods that can be used to dynamically access @@ -499,16 +498,13 @@ class PROTOBUF_EXPORT Reflection { virtual void Swap(Message* message1, Message* message2) const = 0; // Swap fields listed in fields vector of two messages. - virtual void SwapFields(Message* message1, - Message* message2, - const std::vector<const FieldDescriptor*>& fields) - const = 0; + virtual void SwapFields( + Message* message1, Message* message2, + const std::vector<const FieldDescriptor*>& fields) const = 0; // Swap two elements of a repeated field. - virtual void SwapElements(Message* message, - const FieldDescriptor* field, - int index1, - int index2) const = 0; + virtual void SwapElements(Message* message, const FieldDescriptor* field, + int index1, int index2) const = 0; // List all fields of the message which are currently set, except for unknown // fields, but including extension known to the parser (i.e. compiled in). @@ -526,20 +522,20 @@ class PROTOBUF_EXPORT Reflection { // These get the value of a non-repeated field. They return the default // value for fields that aren't set. - virtual int32 GetInt32 (const Message& message, - const FieldDescriptor* field) const = 0; - virtual int64 GetInt64 (const Message& message, - const FieldDescriptor* field) const = 0; + virtual int32 GetInt32(const Message& message, + const FieldDescriptor* field) const = 0; + virtual int64 GetInt64(const Message& message, + const FieldDescriptor* field) const = 0; virtual uint32 GetUInt32(const Message& message, const FieldDescriptor* field) const = 0; virtual uint64 GetUInt64(const Message& message, const FieldDescriptor* field) const = 0; - virtual float GetFloat (const Message& message, - const FieldDescriptor* field) const = 0; + virtual float GetFloat(const Message& message, + const FieldDescriptor* field) const = 0; virtual double GetDouble(const Message& message, const FieldDescriptor* field) const = 0; - virtual bool GetBool (const Message& message, - const FieldDescriptor* field) const = 0; + virtual bool GetBool(const Message& message, + const FieldDescriptor* field) const = 0; virtual std::string GetString(const Message& message, const FieldDescriptor* field) const = 0; virtual const EnumValueDescriptor* GetEnum( @@ -550,8 +546,8 @@ class PROTOBUF_EXPORT Reflection { // known value descriptor, a new value descriptor is created. (Such a value // will only be present when the new unknown-enum-value semantics are enabled // for a message.) - virtual int GetEnumValue( - const Message& message, const FieldDescriptor* field) const = 0; + virtual int GetEnumValue(const Message& message, + const FieldDescriptor* field) const = 0; // See MutableMessage() for the meaning of the "factory" parameter. virtual const Message& GetMessage(const Message& message, @@ -581,25 +577,24 @@ class PROTOBUF_EXPORT Reflection { // Singular field mutators ----------------------------------------- // These mutate the value of a non-repeated field. - virtual void SetInt32 (Message* message, - const FieldDescriptor* field, int32 value) const = 0; - virtual void SetInt64 (Message* message, - const FieldDescriptor* field, int64 value) const = 0; - virtual void SetUInt32(Message* message, - const FieldDescriptor* field, uint32 value) const = 0; - virtual void SetUInt64(Message* message, - const FieldDescriptor* field, uint64 value) const = 0; - virtual void SetFloat (Message* message, - const FieldDescriptor* field, float value) const = 0; - virtual void SetDouble(Message* message, - const FieldDescriptor* field, double value) const = 0; - virtual void SetBool (Message* message, - const FieldDescriptor* field, bool value) const = 0; + virtual void SetInt32(Message* message, const FieldDescriptor* field, + int32 value) const = 0; + virtual void SetInt64(Message* message, const FieldDescriptor* field, + int64 value) const = 0; + virtual void SetUInt32(Message* message, const FieldDescriptor* field, + uint32 value) const = 0; + virtual void SetUInt64(Message* message, const FieldDescriptor* field, + uint64 value) const = 0; + virtual void SetFloat(Message* message, const FieldDescriptor* field, + float value) const = 0; + virtual void SetDouble(Message* message, const FieldDescriptor* field, + double value) const = 0; + virtual void SetBool(Message* message, const FieldDescriptor* field, + bool value) const = 0; virtual void SetString(Message* message, const FieldDescriptor* field, const std::string& value) const = 0; - virtual void SetEnum (Message* message, - const FieldDescriptor* field, - const EnumValueDescriptor* value) const = 0; + virtual void SetEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const = 0; // Set an enum field's value with an integer rather than EnumValueDescriptor. // For proto3 this is just setting the enum field to the value specified, for // proto2 it's more complicated. If value is a known enum value the field is @@ -607,8 +602,7 @@ class PROTOBUF_EXPORT Reflection { // set. Note this matches the behavior of parsing unknown enum values. // If multiple calls with unknown values happen than they are all added to the // unknown field set in order of the calls. - virtual void SetEnumValue(Message* message, - const FieldDescriptor* field, + virtual void SetEnumValue(Message* message, const FieldDescriptor* field, int value) const = 0; // Get a mutable pointer to a field with a message type. If a MessageFactory @@ -628,8 +622,7 @@ class PROTOBUF_EXPORT Reflection { // sub_message, passing ownership to the message. If the field contained a // message, that message is deleted. If sub_message is NULL, the field is // cleared. - virtual void SetAllocatedMessage(Message* message, - Message* sub_message, + virtual void SetAllocatedMessage(Message* message, Message* sub_message, const FieldDescriptor* field) const = 0; // Releases the message specified by 'field' and returns the pointer, // ReleaseMessage() will return the message the message object if it exists. @@ -646,44 +639,44 @@ class PROTOBUF_EXPORT Reflection { // Repeated field getters ------------------------------------------ // These get the value of one element of a repeated field. - virtual int32 GetRepeatedInt32 (const Message& message, - const FieldDescriptor* field, - int index) const = 0; - virtual int64 GetRepeatedInt64 (const Message& message, - const FieldDescriptor* field, - int index) const = 0; + virtual int32 GetRepeatedInt32(const Message& message, + const FieldDescriptor* field, + int index) const = 0; + virtual int64 GetRepeatedInt64(const Message& message, + const FieldDescriptor* field, + int index) const = 0; virtual uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field, int index) const = 0; virtual uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field, int index) const = 0; - virtual float GetRepeatedFloat (const Message& message, - const FieldDescriptor* field, - int index) const = 0; + virtual float GetRepeatedFloat(const Message& message, + const FieldDescriptor* field, + int index) const = 0; virtual double GetRepeatedDouble(const Message& message, const FieldDescriptor* field, int index) const = 0; - virtual bool GetRepeatedBool (const Message& message, - const FieldDescriptor* field, - int index) const = 0; + virtual bool GetRepeatedBool(const Message& message, + const FieldDescriptor* field, + int index) const = 0; virtual std::string GetRepeatedString(const Message& message, const FieldDescriptor* field, int index) const = 0; virtual const EnumValueDescriptor* GetRepeatedEnum( - const Message& message, - const FieldDescriptor* field, int index) const = 0; + const Message& message, const FieldDescriptor* field, + int index) const = 0; // GetRepeatedEnumValue() returns an enum field's value as an integer rather // than an EnumValueDescriptor*. If the integer value does not correspond to a // known value descriptor, a new value descriptor is created. (Such a value // will only be present when the new unknown-enum-value semantics are enabled // for a message.) - virtual int GetRepeatedEnumValue( - const Message& message, - const FieldDescriptor* field, int index) const = 0; - virtual const Message& GetRepeatedMessage( - const Message& message, - const FieldDescriptor* field, int index) const = 0; + virtual int GetRepeatedEnumValue(const Message& message, + const FieldDescriptor* field, + int index) const = 0; + virtual const Message& GetRepeatedMessage(const Message& message, + const FieldDescriptor* field, + int index) const = 0; // See GetStringReference(), above. virtual const std::string& GetRepeatedStringReference( @@ -694,31 +687,24 @@ class PROTOBUF_EXPORT Reflection { // Repeated field mutators ----------------------------------------- // These mutate the value of one element of a repeated field. - virtual void SetRepeatedInt32 (Message* message, - const FieldDescriptor* field, - int index, int32 value) const = 0; - virtual void SetRepeatedInt64 (Message* message, - const FieldDescriptor* field, - int index, int64 value) const = 0; - virtual void SetRepeatedUInt32(Message* message, - const FieldDescriptor* field, + virtual void SetRepeatedInt32(Message* message, const FieldDescriptor* field, + int index, int32 value) const = 0; + virtual void SetRepeatedInt64(Message* message, const FieldDescriptor* field, + int index, int64 value) const = 0; + virtual void SetRepeatedUInt32(Message* message, const FieldDescriptor* field, int index, uint32 value) const = 0; - virtual void SetRepeatedUInt64(Message* message, - const FieldDescriptor* field, + virtual void SetRepeatedUInt64(Message* message, const FieldDescriptor* field, int index, uint64 value) const = 0; - virtual void SetRepeatedFloat (Message* message, - const FieldDescriptor* field, - int index, float value) const = 0; - virtual void SetRepeatedDouble(Message* message, - const FieldDescriptor* field, + virtual void SetRepeatedFloat(Message* message, const FieldDescriptor* field, + int index, float value) const = 0; + virtual void SetRepeatedDouble(Message* message, const FieldDescriptor* field, int index, double value) const = 0; - virtual void SetRepeatedBool (Message* message, - const FieldDescriptor* field, - int index, bool value) const = 0; + virtual void SetRepeatedBool(Message* message, const FieldDescriptor* field, + int index, bool value) const = 0; virtual void SetRepeatedString(Message* message, const FieldDescriptor* field, int index, const std::string& value) const = 0; - virtual void SetRepeatedEnum(Message* message, - const FieldDescriptor* field, int index, + virtual void SetRepeatedEnum(Message* message, const FieldDescriptor* field, + int index, const EnumValueDescriptor* value) const = 0; // Set an enum field's value with an integer rather than EnumValueDescriptor. // For proto3 this is just setting the enum field to the value specified, for @@ -732,32 +718,32 @@ class PROTOBUF_EXPORT Reflection { int value) const = 0; // Get a mutable pointer to an element of a repeated field with a message // type. - virtual Message* MutableRepeatedMessage( - Message* message, const FieldDescriptor* field, int index) const = 0; + virtual Message* MutableRepeatedMessage(Message* message, + const FieldDescriptor* field, + int index) const = 0; // Repeated field adders ------------------------------------------- // These add an element to a repeated field. - virtual void AddInt32 (Message* message, - const FieldDescriptor* field, int32 value) const = 0; - virtual void AddInt64 (Message* message, - const FieldDescriptor* field, int64 value) const = 0; - virtual void AddUInt32(Message* message, - const FieldDescriptor* field, uint32 value) const = 0; - virtual void AddUInt64(Message* message, - const FieldDescriptor* field, uint64 value) const = 0; - virtual void AddFloat (Message* message, - const FieldDescriptor* field, float value) const = 0; - virtual void AddDouble(Message* message, - const FieldDescriptor* field, double value) const = 0; - virtual void AddBool (Message* message, - const FieldDescriptor* field, bool value) const = 0; + virtual void AddInt32(Message* message, const FieldDescriptor* field, + int32 value) const = 0; + virtual void AddInt64(Message* message, const FieldDescriptor* field, + int64 value) const = 0; + virtual void AddUInt32(Message* message, const FieldDescriptor* field, + uint32 value) const = 0; + virtual void AddUInt64(Message* message, const FieldDescriptor* field, + uint64 value) const = 0; + virtual void AddFloat(Message* message, const FieldDescriptor* field, + float value) const = 0; + virtual void AddDouble(Message* message, const FieldDescriptor* field, + double value) const = 0; + virtual void AddBool(Message* message, const FieldDescriptor* field, + bool value) const = 0; virtual void AddString(Message* message, const FieldDescriptor* field, const std::string& value) const = 0; - virtual void AddEnum (Message* message, - const FieldDescriptor* field, - const EnumValueDescriptor* value) const = 0; + virtual void AddEnum(Message* message, const FieldDescriptor* field, + const EnumValueDescriptor* value) const = 0; // Add an integer value to a repeated enum field rather than // EnumValueDescriptor. For proto3 this is just setting the enum field to the // value specified, for proto2 it's more complicated. If value is a known enum @@ -765,12 +751,10 @@ class PROTOBUF_EXPORT Reflection { // to the unknown field set. Note this matches the behavior of parsing unknown // enum values. If multiple calls with unknown values happen than they are all // added to the unknown field set in order of the calls. - virtual void AddEnumValue(Message* message, - const FieldDescriptor* field, + virtual void AddEnumValue(Message* message, const FieldDescriptor* field, int value) const = 0; // See MutableMessage() for comments on the "factory" parameter. - virtual Message* AddMessage(Message* message, - const FieldDescriptor* field, + virtual Message* AddMessage(Message* message, const FieldDescriptor* field, MessageFactory* factory = NULL) const = 0; // Appends an already-allocated object 'new_entry' to the repeated field @@ -806,13 +790,13 @@ class PROTOBUF_EXPORT Reflection { // Note that to use this method users need to include the header file // "net/proto2/public/reflection.h" (which defines the RepeatedFieldRef // class templates). - template<typename T> - RepeatedFieldRef<T> GetRepeatedFieldRef( - const Message& message, const FieldDescriptor* field) const; + template <typename T> + RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message, + const FieldDescriptor* field) const; // Like GetRepeatedFieldRef() but return an object that can also be used // manipulate the underlying repeated field. - template<typename T> + template <typename T> MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef( Message* message, const FieldDescriptor* field) const; @@ -936,8 +920,8 @@ class PROTOBUF_EXPORT Reflection { const Message& message, const FieldDescriptor* field, FieldDescriptor::CppType cpptype, int ctype, const Descriptor* message_type) const { - return MutableRawRepeatedField( - const_cast<Message*>(&message), field, cpptype, ctype, message_type); + return MutableRawRepeatedField(const_cast<Message*>(&message), field, + cpptype, ctype, message_type); } // The following methods are used to implement (Mutable)RepeatedFieldRef. @@ -956,10 +940,10 @@ class PROTOBUF_EXPORT Reflection { // "cpp_type"/"message_type" is consistent with the actual type of the field. // We use 1 routine rather than 2 (const vs mutable) because it is protected // and it doesn't change the message. - virtual void* RepeatedFieldData( - Message* message, const FieldDescriptor* field, - FieldDescriptor::CppType cpp_type, - const Descriptor* message_type) const; + virtual void* RepeatedFieldData(Message* message, + const FieldDescriptor* field, + FieldDescriptor::CppType cpp_type, + const Descriptor* message_type) const; // The returned pointer should point to a singleton instance which implements // the RepeatedFieldAccessor interface. @@ -967,9 +951,9 @@ class PROTOBUF_EXPORT Reflection { const FieldDescriptor* field) const; private: - template<typename T, typename Enable> + template <typename T, typename Enable> friend class RepeatedFieldRef; - template<typename T, typename Enable> + template <typename T, typename Enable> friend class MutableRepeatedFieldRef; friend class python::MapReflectionFriend; #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND @@ -988,8 +972,8 @@ class PROTOBUF_EXPORT Reflection { // file here is not possible because it would cause a circular include cycle. // We use 1 routine rather than 2 (const vs mutable) because it is private // and mutable a repeated string field doesn't change the message. - void* MutableRawRepeatedString( - Message* message, const FieldDescriptor* field, bool is_string) const; + void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field, + bool is_string) const; friend class MapReflectionTester; // TODO(jieluo) - make the map APIs pure virtual after updating @@ -1022,16 +1006,14 @@ class PROTOBUF_EXPORT Reflection { // Returns a MapIterator referring to the first element in the map field. // If the map field is empty, this function returns the same as // reflection::MapEnd. Mutation to the field may invalidate the iterator. - virtual MapIterator MapBegin( - Message* message, - const FieldDescriptor* field) const; + virtual MapIterator MapBegin(Message* message, + const FieldDescriptor* field) const; // Returns a MapIterator referring to the theoretical element that would // follow the last element in the map field. It does not point to any // real element. Mutation to the field may invalidate the iterator. - virtual MapIterator MapEnd( - Message* message, - const FieldDescriptor* field) const; + virtual MapIterator MapEnd(Message* message, + const FieldDescriptor* field) const; // Get the number of <key, value> pair of a map field. The result may be // different from FieldSize which can have duplicate keys. @@ -1150,7 +1132,7 @@ template <typename T> const T* DynamicCastToGenerated(const Message* from) { // Compile-time assert that T is a generated type that has a // default_instance() accessor, but avoid actually calling it. - const T&(*get_default_instance)() = &T::default_instance; + const T& (*get_default_instance)() = &T::default_instance; (void)get_default_instance; // Compile-time assert that T is a subclass of google::protobuf::Message. @@ -1239,38 +1221,34 @@ Reflection::MutableRepeatedPtrField<std::string>( // ----- -template<> +template <> inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField( const Message& message, const FieldDescriptor* field) const { - return *static_cast<const RepeatedPtrField<Message>* >( - GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, - -1, NULL)); + return *static_cast<const RepeatedPtrField<Message>*>(GetRawRepeatedField( + message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, NULL)); } -template<> +template <> inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField( Message* message, const FieldDescriptor* field) const { - return static_cast<RepeatedPtrField<Message>* >( - MutableRawRepeatedField(message, field, - FieldDescriptor::CPPTYPE_MESSAGE, -1, - NULL)); + return static_cast<RepeatedPtrField<Message>*>(MutableRawRepeatedField( + message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, NULL)); } -template<typename PB> +template <typename PB> inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField( const Message& message, const FieldDescriptor* field) const { - return *static_cast<const RepeatedPtrField<PB>* >( - GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, - -1, PB::default_instance().GetDescriptor())); + return *static_cast<const RepeatedPtrField<PB>*>( + GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, + PB::default_instance().GetDescriptor())); } -template<typename PB> +template <typename PB> inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField( Message* message, const FieldDescriptor* field) const { - return static_cast<RepeatedPtrField<PB>* >( - MutableRawRepeatedField(message, field, - FieldDescriptor::CPPTYPE_MESSAGE, -1, - PB::default_instance().GetDescriptor())); + return static_cast<RepeatedPtrField<PB>*>( + MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, + -1, PB::default_instance().GetDescriptor())); } } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index f531a7c58e..e8f13440ac 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -147,13 +147,13 @@ bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) { template <bool aliasing> bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) { - // We must prevent reading more than limit from the input. Due to the nature - // of EpsCopyInputStream the stream will always read kSlopBytes ahead of - // the parser, we can't always backup so we must prevent from reading past - // limit in the first place. - io::LimitingInputStream zcis(input.zcis, input.limit); - return MergePartialFromImpl<aliasing>(&zcis, msg) && - zcis.ByteCount() == input.limit; + const char* ptr; + internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), + aliasing, &ptr, input.zcis, input.limit); + ptr = msg->_InternalParse(ptr, &ctx); + if (PROTOBUF_PREDICT_FALSE(!ptr)) return false; + ctx.BackUp(ptr); + return ctx.EndedAtLimit(); } #else // !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER @@ -276,8 +276,8 @@ bool MessageLite::ParsePartialFromZeroCopyStream( return ParseFrom<kParsePartial>(input); } -bool MessageLite::MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, - int size) { +bool MessageLite::MergePartialFromBoundedZeroCopyStream( + io::ZeroCopyInputStream* input, int size) { return ParseFrom<kMergePartial>(internal::BoundedZCIS{input, size}); } diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 8deb14343a..bc5d4c7ab9 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -122,13 +122,9 @@ inline int ToIntSize(size_t size) { template <typename T> class ExplicitlyConstructed { public: - void DefaultConstruct() { - new (&union_) T(); - } + void DefaultConstruct() { new (&union_) T(); } - void Destruct() { - get_mutable()->~T(); - } + void Destruct() { get_mutable()->~T(); } constexpr const T& get() const { return reinterpret_cast<const T&>(union_); } T* get_mutable() { return reinterpret_cast<T*>(&union_); } @@ -244,9 +240,7 @@ class PROTOBUF_EXPORT MessageLite { // The format of the returned string is subject to change, so please do not // assume it will remain stable over time. std::string DebugString() const; - std::string ShortDebugString() const { - return DebugString(); - } + std::string ShortDebugString() const { return DebugString(); } // Parsing --------------------------------------------------------- // Methods for parsing in protocol buffer format. Most of these are @@ -271,11 +265,11 @@ class PROTOBUF_EXPORT MessageLite { // Read a protocol buffer from the given zero-copy input stream, expecting // the message to be exactly "size" bytes long. If successful, exactly // this many bytes will have been consumed from the input. - bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); + bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, + int size); // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are // missing required fields. - bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, - int size); + bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); bool ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are // missing required fields. @@ -316,7 +310,7 @@ class PROTOBUF_EXPORT MessageLite { // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() // followed by IsInitialized(). #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER - virtual bool MergePartialFromCodedStream(io::CodedInputStream* input); + bool MergePartialFromCodedStream(io::CodedInputStream* input); #else virtual bool MergePartialFromCodedStream(io::CodedInputStream* input) = 0; #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER @@ -376,15 +370,12 @@ class PROTOBUF_EXPORT MessageLite { // Legacy ByteSize() API. PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead") - int ByteSize() const { - return internal::ToIntSize(ByteSizeLong()); - } + int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); } // Serializes the message without recomputing the size. The message must not // have changed since the last call to ByteSize(), and the value returned by // ByteSize must be non-negative. Otherwise the results are undefined. - virtual void SerializeWithCachedSizes( - io::CodedOutputStream* output) const; + virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const; // Functions below here are not part of the public interface. It isn't // enforced, but they should be treated as private, and will be private diff --git a/src/google/protobuf/message_unittest.inc b/src/google/protobuf/message_unittest.inc index d61ca7d33b..0cd46d4cc3 100644 --- a/src/google/protobuf/message_unittest.inc +++ b/src/google/protobuf/message_unittest.inc @@ -60,7 +60,8 @@ #include <google/protobuf/stubs/logging.h> #include <google/protobuf/testing/googletest.h> #include <gtest/gtest.h> -#include <google/protobuf/stubs/io_win32.h> + +#include <google/protobuf/io/io_win32.h> namespace google { namespace protobuf { @@ -68,8 +69,8 @@ namespace protobuf { #if defined(_WIN32) // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY diff --git a/src/google/protobuf/metadata.h b/src/google/protobuf/metadata.h index 928bbf5c0d..009a7e93a6 100644 --- a/src/google/protobuf/metadata.h +++ b/src/google/protobuf/metadata.h @@ -56,19 +56,15 @@ class InternalMetadataWithArena InternalMetadataWithArena() {} explicit InternalMetadataWithArena(Arena* arena) : InternalMetadataWithArenaBase<UnknownFieldSet, - InternalMetadataWithArena>(arena) {} + InternalMetadataWithArena>(arena) {} - void DoSwap(UnknownFieldSet* other) { - mutable_unknown_fields()->Swap(other); - } + void DoSwap(UnknownFieldSet* other) { mutable_unknown_fields()->Swap(other); } void DoMergeFrom(const UnknownFieldSet& other) { mutable_unknown_fields()->MergeFrom(other); } - void DoClear() { - mutable_unknown_fields()->Clear(); - } + void DoClear() { mutable_unknown_fields()->Clear(); } static const UnknownFieldSet& default_instance() { return *UnknownFieldSet::default_instance(); diff --git a/src/google/protobuf/metadata_lite.h b/src/google/protobuf/metadata_lite.h index e0aa52bc7a..40c7ea785d 100644 --- a/src/google/protobuf/metadata_lite.h +++ b/src/google/protobuf/metadata_lite.h @@ -143,9 +143,10 @@ class InternalMetadataWithArenaBase { return reinterpret_cast<intptr_t>(ptr_) & kPtrTagMask; } - template<typename U> U* PtrValue() const { - return reinterpret_cast<U*>( - reinterpret_cast<intptr_t>(ptr_) & kPtrValueMask); + template <typename U> + U* PtrValue() const { + return reinterpret_cast<U*>(reinterpret_cast<intptr_t>(ptr_) & + kPtrValueMask); } // If ptr_'s tag is kTagContainer, it points to an instance of this struct. @@ -160,8 +161,8 @@ class InternalMetadataWithArenaBase { // Two-step assignment works around a bug in clang's static analyzer: // https://bugs.llvm.org/show_bug.cgi?id=34198. ptr_ = container; - ptr_ = reinterpret_cast<void*>( - reinterpret_cast<intptr_t>(ptr_) | kTagContainer); + ptr_ = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(ptr_) | + kTagContainer); container->arena = my_arena; return &(container->unknown_fields); } @@ -187,9 +188,7 @@ class InternalMetadataWithArenaLite mutable_unknown_fields()->append(other); } - void DoClear() { - mutable_unknown_fields()->clear(); - } + void DoClear() { mutable_unknown_fields()->clear(); } static const std::string& default_instance() { // Can't use GetEmptyStringAlreadyInited() here because empty string diff --git a/src/google/protobuf/no_field_presence_test.cc b/src/google/protobuf/no_field_presence_test.cc index 9645d3a806..8b05277f0a 100644 --- a/src/google/protobuf/no_field_presence_test.cc +++ b/src/google/protobuf/no_field_presence_test.cc @@ -30,8 +30,8 @@ #include <string> -#include <google/protobuf/unittest_no_field_presence.pb.h> #include <google/protobuf/unittest.pb.h> +#include <google/protobuf/unittest_no_field_presence.pb.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.h> #include <gtest/gtest.h> @@ -120,8 +120,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { m->mutable_optional_proto2_message()->set_optional_int32(44); m->set_optional_nested_enum( proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto2_nofieldpresence_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_int64(101); @@ -143,8 +142,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { m->add_repeated_proto2_message()->set_optional_int32(48); m->add_repeated_nested_enum( proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto2_nofieldpresence_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -153,7 +151,7 @@ void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) { } void CheckNonDefaultValues( -const proto2_nofieldpresence_unittest::TestAllTypes& m) { + const proto2_nofieldpresence_unittest::TestAllTypes& m) { EXPECT_EQ(100, m.optional_int32()); EXPECT_EQ(101, m.optional_int64()); EXPECT_EQ(102, m.optional_uint32()); @@ -271,8 +269,9 @@ TEST(NoFieldPresenceTest, MessageFieldPresenceTest) { EXPECT_EQ(false, message.has_optional_lazy_message()); // Test field presence of a message field on the default instance. - EXPECT_EQ(false, proto2_nofieldpresence_unittest::TestAllTypes:: - default_instance().has_optional_nested_message()); + EXPECT_EQ(false, + proto2_nofieldpresence_unittest::TestAllTypes::default_instance() + .has_optional_nested_message()); } TEST(NoFieldPresenceTest, ReflectionHasFieldTest) { @@ -294,9 +293,11 @@ TEST(NoFieldPresenceTest, ReflectionHasFieldTest) { // Test field presence of a message field on the default instance. const FieldDescriptor* msg_field = desc->FindFieldByName("optional_nested_message"); - EXPECT_EQ(false, r->HasField( - proto2_nofieldpresence_unittest::TestAllTypes:: - default_instance(), msg_field)); + EXPECT_EQ( + false, + r->HasField( + proto2_nofieldpresence_unittest::TestAllTypes::default_instance(), + msg_field)); // Fill all fields, expect everything to report true (check oneofs below). FillValues(&message); @@ -441,7 +442,8 @@ TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) { message.set_optional_string(""); message.set_optional_bytes(""); message.set_optional_nested_enum( - proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // first enum entry + proto2_nofieldpresence_unittest:: + TestAllTypes_NestedEnum_FOO); // first enum entry message.set_optional_foreign_enum( proto2_nofieldpresence_unittest::FOREIGN_FOO); // first enum entry @@ -554,8 +556,9 @@ TEST(NoFieldPresenceTest, OneofPresence) { message.oneof_field_case()); message.Clear(); - message.set_oneof_enum(proto2_nofieldpresence_unittest:: - TestAllTypes_NestedEnum_FOO); // default value. + message.set_oneof_enum( + proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // default + // value. message.SerializeToString(&serialized); EXPECT_EQ(3, serialized.size()); EXPECT_TRUE(message.ParseFromString(serialized)); @@ -571,4 +574,3 @@ TEST(NoFieldPresenceTest, OneofPresence) { } // namespace } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/parse_context.cc b/src/google/protobuf/parse_context.cc index 2aec32e98e..9efa521e19 100644 --- a/src/google/protobuf/parse_context.cc +++ b/src/google/protobuf/parse_context.cc @@ -82,8 +82,8 @@ bool ParseEndsInSlopRegion(const char* begin, int overrun, int d) { d++; break; } - case 4: { // end group - if (--d < 0) return true; // We exit early + case 4: { // end group + if (--d < 0) return true; // We exit early break; } case 5: { // fixed32 @@ -114,11 +114,13 @@ const char* EpsCopyInputStream::Next(int overrun, int d) { // Note we must use memmove because the previous buffer could be part of // buffer_. std::memmove(buffer_, buffer_end_, kSlopBytes); - if (zcis_ && (d < 0 || !ParseEndsInSlopRegion(buffer_, overrun, d))) { + if (overall_limit_ > 0 && + (d < 0 || !ParseEndsInSlopRegion(buffer_, overrun, d))) { const void* data; // ZeroCopyInputStream indicates Next may return 0 size buffers. Hence // we loop. while (zcis_->Next(&data, &size_)) { + overall_limit_ -= size_; if (size_ > kSlopBytes) { // We got a large chunk std::memcpy(buffer_ + kSlopBytes, data, kSlopBytes); @@ -135,6 +137,7 @@ const char* EpsCopyInputStream::Next(int overrun, int d) { } GOOGLE_DCHECK(size_ == 0) << size_; } + overall_limit_ = 0; // Next failed, no more needs for next } // End of stream or array if (aliasing_ == kNoDelta) { @@ -260,6 +263,7 @@ const char* EpsCopyInputStream::InitFrom(io::ZeroCopyInputStream* zcis) { int size; limit_ = INT_MAX; if (zcis->Next(&data, &size)) { + overall_limit_ -= size; if (size > kSlopBytes) { auto ptr = static_cast<const char*>(data); limit_ -= size - kSlopBytes; @@ -275,6 +279,7 @@ const char* EpsCopyInputStream::InitFrom(io::ZeroCopyInputStream* zcis) { return ptr; } } + overall_limit_ = 0; next_chunk_ = nullptr; size_ = 0; limit_end_ = buffer_end_ = buffer_; @@ -322,7 +327,8 @@ std::pair<const char*, uint32> ReadTagFallback(const char* p, uint32 res) { return {nullptr, 0}; } -std::pair<const char*, uint64> ParseVarint64Fallback(const char* p, uint64 res) { +std::pair<const char*, uint64> ParseVarint64Fallback(const char* p, + uint64 res) { return ParseVarint64FallbackInline(p, res); } diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h index 1bf6100c42..81b9406a77 100644 --- a/src/google/protobuf/parse_context.h +++ b/src/google/protobuf/parse_context.h @@ -190,6 +190,9 @@ class PROTOBUF_EXPORT EpsCopyInputStream { return ptr > limit_end_ && (next_chunk_ == nullptr || ptr - buffer_end_ > limit_); } + // Returns true if more data is available, if false is returned one has to + // call Done for further checks. + bool DataAvailable(const char* ptr) { return ptr < limit_end_; } protected: // Returns true is limit (either an explicit limit or end of stream) is @@ -206,6 +209,7 @@ class PROTOBUF_EXPORT EpsCopyInputStream { } const char* InitFrom(StringPiece flat) { + overall_limit_ = 0; if (flat.size() > kSlopBytes) { limit_ = kSlopBytes; limit_end_ = buffer_end_ = flat.end() - kSlopBytes; @@ -228,6 +232,7 @@ class PROTOBUF_EXPORT EpsCopyInputStream { const char* InitFrom(io::ZeroCopyInputStream* zcis); const char* InitFrom(io::ZeroCopyInputStream* zcis, int limit) { + overall_limit_ = limit; auto res = InitFrom(zcis); limit_ = limit - static_cast<int>(buffer_end_ - res); limit_end_ = buffer_end_ + (std::min)(0, limit_); @@ -258,6 +263,7 @@ class PROTOBUF_EXPORT EpsCopyInputStream { // the ParseContext, but case 2 is most easily and optimally implemented in // DoneFallback. uint32 last_tag_minus_1_ = 0; + int overall_limit_ = INT_MAX; // Overall limit independent of pushed limits. std::pair<const char*, bool> DoneFallback(const char* ptr, int d); const char* Next(int overrun, int d); diff --git a/src/google/protobuf/preserve_unknown_enum_test.cc b/src/google/protobuf/preserve_unknown_enum_test.cc index 94addc4077..1ac4e13fb7 100644 --- a/src/google/protobuf/preserve_unknown_enum_test.cc +++ b/src/google/protobuf/preserve_unknown_enum_test.cc @@ -31,8 +31,8 @@ #include <google/protobuf/unittest.pb.h> #include <google/protobuf/unittest_preserve_unknown_enum.pb.h> #include <google/protobuf/unittest_preserve_unknown_enum2.pb.h> -#include <google/protobuf/dynamic_message.h> #include <google/protobuf/descriptor.h> +#include <google/protobuf/dynamic_message.h> #include <gtest/gtest.h> namespace google { @@ -41,22 +41,18 @@ namespace { void FillMessage( proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra* message) { - message->set_e( - proto3_preserve_unknown_enum_unittest::E_EXTRA); - message->add_repeated_e( - proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->set_e(proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->add_repeated_e(proto3_preserve_unknown_enum_unittest::E_EXTRA); message->add_repeated_packed_e( proto3_preserve_unknown_enum_unittest::E_EXTRA); message->add_repeated_packed_unexpected_e( proto3_preserve_unknown_enum_unittest::E_EXTRA); - message->set_oneof_e_1( - proto3_preserve_unknown_enum_unittest::E_EXTRA); + message->set_oneof_e_1(proto3_preserve_unknown_enum_unittest::E_EXTRA); } void CheckMessage( const proto3_preserve_unknown_enum_unittest::MyMessagePlusExtra& message) { - EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, - message.e()); + EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, message.e()); EXPECT_EQ(1, message.repeated_e_size()); EXPECT_EQ(proto3_preserve_unknown_enum_unittest::E_EXTRA, message.repeated_e(0)); @@ -72,23 +68,18 @@ void CheckMessage( void CheckMessage( const proto3_preserve_unknown_enum_unittest::MyMessage& message) { - EXPECT_EQ(static_cast<int>( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast<int>(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast<int>(message.e())); EXPECT_EQ(1, message.repeated_e_size()); - EXPECT_EQ(static_cast<int>( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast<int>(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast<int>(message.repeated_e(0))); EXPECT_EQ(1, message.repeated_packed_e_size()); - EXPECT_EQ(static_cast<int>( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast<int>(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast<int>(message.repeated_packed_e(0))); EXPECT_EQ(1, message.repeated_packed_unexpected_e_size()); - EXPECT_EQ(static_cast<int>( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast<int>(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast<int>(message.repeated_packed_unexpected_e(0))); - EXPECT_EQ(static_cast<int>( - proto3_preserve_unknown_enum_unittest::E_EXTRA), + EXPECT_EQ(static_cast<int>(proto3_preserve_unknown_enum_unittest::E_EXTRA), static_cast<int>(message.oneof_e_1())); } diff --git a/src/google/protobuf/proto3_arena_lite_unittest.cc b/src/google/protobuf/proto3_arena_lite_unittest.cc index 9c6d166f2d..e2693708a2 100644 --- a/src/google/protobuf/proto3_arena_lite_unittest.cc +++ b/src/google/protobuf/proto3_arena_lite_unittest.cc @@ -52,8 +52,7 @@ void SetAllFields(TestAllTypes* m) { m->mutable_optional_foreign_message()->set_c(43); m->set_optional_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_string("asdf"); @@ -62,8 +61,7 @@ void SetAllFields(TestAllTypes* m) { m->add_repeated_foreign_message()->set_c(47); m->add_repeated_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -81,8 +79,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(43, m.optional_foreign_message().c()); EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.optional_nested_enum()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.optional_foreign_enum()); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum()); EXPECT_EQ(true, m.has_optional_lazy_message()); EXPECT_EQ(45, m.optional_lazy_message().bb()); @@ -100,8 +97,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.repeated_nested_enum(0)); EXPECT_EQ(1, m.repeated_foreign_enum_size()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.repeated_foreign_enum(0)); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0)); EXPECT_EQ(1, m.repeated_lazy_message_size()); EXPECT_EQ(49, m.repeated_lazy_message(0).bb()); @@ -138,7 +134,7 @@ TEST(Proto3ArenaLiteTest, Swap) { TEST(Proto3ArenaLiteTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena); + TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); diff --git a/src/google/protobuf/proto3_arena_unittest.cc b/src/google/protobuf/proto3_arena_unittest.cc index e3c4777b7c..d4b0135aab 100644 --- a/src/google/protobuf/proto3_arena_unittest.cc +++ b/src/google/protobuf/proto3_arena_unittest.cc @@ -54,8 +54,7 @@ void SetAllFields(TestAllTypes* m) { m->mutable_optional_foreign_message()->set_c(43); m->set_optional_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->set_optional_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->mutable_optional_lazy_message()->set_bb(45); m->add_repeated_int32(100); m->add_repeated_string("asdf"); @@ -64,8 +63,7 @@ void SetAllFields(TestAllTypes* m) { m->add_repeated_foreign_message()->set_c(47); m->add_repeated_nested_enum( proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ); - m->add_repeated_foreign_enum( - proto3_arena_unittest::FOREIGN_BAZ); + m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ); m->add_repeated_lazy_message()->set_bb(49); m->set_oneof_uint32(1); @@ -83,8 +81,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(43, m.optional_foreign_message().c()); EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.optional_nested_enum()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.optional_foreign_enum()); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum()); EXPECT_EQ(true, m.has_optional_lazy_message()); EXPECT_EQ(45, m.optional_lazy_message().bb()); @@ -102,8 +99,7 @@ void ExpectAllFieldsSet(const TestAllTypes& m) { EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ, m.repeated_nested_enum(0)); EXPECT_EQ(1, m.repeated_foreign_enum_size()); - EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, - m.repeated_foreign_enum(0)); + EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0)); EXPECT_EQ(1, m.repeated_lazy_message_size()); EXPECT_EQ(49, m.repeated_lazy_message(0).bb()); @@ -161,7 +157,7 @@ TEST(Proto3ArenaTest, Swap) { TEST(Proto3ArenaTest, SetAllocatedMessage) { Arena arena; - TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena); + TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena); TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage; nested->set_bb(118); arena_message->set_allocated_optional_nested_message(nested); @@ -192,8 +188,8 @@ TEST(Proto3ArenaTest, MessageFieldClearViaReflection) { TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena); const Reflection* r = message->GetReflection(); const Descriptor* d = message->GetDescriptor(); - const FieldDescriptor* msg_field = d->FindFieldByName( - "optional_nested_message"); + const FieldDescriptor* msg_field = + d->FindFieldByName("optional_nested_message"); message->mutable_optional_nested_message()->set_bb(1); r->ClearField(message, msg_field); diff --git a/src/google/protobuf/reflection.h b/src/google/protobuf/reflection.h index 407ca62506..6b1e5f2eda 100644 --- a/src/google/protobuf/reflection.h +++ b/src/google/protobuf/reflection.h @@ -47,39 +47,33 @@ namespace google { namespace protobuf { namespace internal { -template<typename T, typename Enable = void> +template <typename T, typename Enable = void> struct RefTypeTraits; } // namespace internal -template<typename T> +template <typename T> RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef( const Message& message, const FieldDescriptor* field) const { return RepeatedFieldRef<T>(message, field); } -template<typename T> +template <typename T> MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef( Message* message, const FieldDescriptor* field) const { return MutableRepeatedFieldRef<T>(message, field); } // RepeatedFieldRef definition for non-message types. -template<typename T> +template <typename T> class RepeatedFieldRef< T, typename std::enable_if<!std::is_base_of<Message, T>::value>::type> { typedef typename internal::RefTypeTraits<T>::iterator IteratorType; typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } - T Get(int index) const { - return accessor_->template Get<T>(data_, index); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } + T Get(int index) const { return accessor_->template Get<T>(data_, index); } typedef IteratorType iterator; typedef IteratorType const_iterator; @@ -89,22 +83,16 @@ class RepeatedFieldRef< typedef int size_type; typedef ptrdiff_t difference_type; - iterator begin() const { - return iterator(data_, accessor_, true); - } - iterator end() const { - return iterator(data_, accessor_, false); - } + iterator begin() const { return iterator(data_, accessor_, true); } + iterator end() const { return iterator(data_, accessor_, false); } private: friend class Reflection; - RepeatedFieldRef( - const Message& message, - const FieldDescriptor* field) { + RepeatedFieldRef(const Message& message, const FieldDescriptor* field) { const Reflection* reflection = message.GetReflection(); - data_ = reflection->RepeatedFieldData( - const_cast<Message*>(&message), field, - internal::RefTypeTraits<T>::cpp_type, NULL); + data_ = reflection->RepeatedFieldData(const_cast<Message*>(&message), field, + internal::RefTypeTraits<T>::cpp_type, + NULL); accessor_ = reflection->RepeatedFieldAccessor(field); } @@ -113,50 +101,38 @@ class RepeatedFieldRef< }; // MutableRepeatedFieldRef definition for non-message types. -template<typename T> +template <typename T> class MutableRepeatedFieldRef< T, typename std::enable_if<!std::is_base_of<Message, T>::value>::type> { typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } - T Get(int index) const { - return accessor_->template Get<T>(data_, index); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } + T Get(int index) const { return accessor_->template Get<T>(data_, index); } void Set(int index, const T& value) const { accessor_->template Set<T>(data_, index, value); } - void Add(const T& value) const { - accessor_->template Add<T>(data_, value); - } - void RemoveLast() const { - accessor_->RemoveLast(data_); - } + void Add(const T& value) const { accessor_->template Add<T>(data_, value); } + void RemoveLast() const { accessor_->RemoveLast(data_); } void SwapElements(int index1, int index2) const { accessor_->SwapElements(data_, index1, index2); } - void Clear() const { - accessor_->Clear(data_); - } + void Clear() const { accessor_->Clear(data_); } void Swap(const MutableRepeatedFieldRef& other) const { accessor_->Swap(data_, other.accessor_, other.data_); } - template<typename Container> + template <typename Container> void MergeFrom(const Container& container) const { typedef typename Container::const_iterator Iterator; for (Iterator it = container.begin(); it != container.end(); ++it) { Add(*it); } } - template<typename Container> + template <typename Container> void CopyFrom(const Container& container) const { Clear(); MergeFrom(container); @@ -164,9 +140,7 @@ class MutableRepeatedFieldRef< private: friend class Reflection; - MutableRepeatedFieldRef( - Message* message, - const FieldDescriptor* field) { + MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) { const Reflection* reflection = message->GetReflection(); data_ = reflection->RepeatedFieldData( message, field, internal::RefTypeTraits<T>::cpp_type, NULL); @@ -178,19 +152,15 @@ class MutableRepeatedFieldRef< }; // RepeatedFieldRef definition for message types. -template<typename T> +template <typename T> class RepeatedFieldRef< T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> { typedef typename internal::RefTypeTraits<T>::iterator IteratorType; typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } // This method returns a reference to the underlying message object if it // exists. If a message object doesn't exist (e.g., data stored in serialized // form), scratch_space will be filled with the data and a reference to it @@ -205,9 +175,7 @@ class RepeatedFieldRef< } // Create a new message of the same type as the messages stored in this // repeated field. Caller takes ownership of the returned object. - T* NewMessage() const { - return static_cast<T*>(default_instance_->New()); - } + T* NewMessage() const { return static_cast<T*>(default_instance_->New()); } typedef IteratorType iterator; typedef IteratorType const_iterator; @@ -227,9 +195,7 @@ class RepeatedFieldRef< private: friend class Reflection; - RepeatedFieldRef( - const Message& message, - const FieldDescriptor* field) { + RepeatedFieldRef(const Message& message, const FieldDescriptor* field) { const Reflection* reflection = message.GetReflection(); data_ = reflection->RepeatedFieldData( const_cast<Message*>(&message), field, @@ -246,56 +212,44 @@ class RepeatedFieldRef< }; // MutableRepeatedFieldRef definition for message types. -template<typename T> +template <typename T> class MutableRepeatedFieldRef< T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> { typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; public: - bool empty() const { - return accessor_->IsEmpty(data_); - } - int size() const { - return accessor_->Size(data_); - } + bool empty() const { return accessor_->IsEmpty(data_); } + int size() const { return accessor_->Size(data_); } // See comments for RepeatedFieldRef<Message>::Get() const T& Get(int index, T* scratch_space) const { return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space)); } // Create a new message of the same type as the messages stored in this // repeated field. Caller takes ownership of the returned object. - T* NewMessage() const { - return static_cast<T*>(default_instance_->New()); - } + T* NewMessage() const { return static_cast<T*>(default_instance_->New()); } void Set(int index, const T& value) const { accessor_->Set(data_, index, &value); } - void Add(const T& value) const { - accessor_->Add(data_, &value); - } - void RemoveLast() const { - accessor_->RemoveLast(data_); - } + void Add(const T& value) const { accessor_->Add(data_, &value); } + void RemoveLast() const { accessor_->RemoveLast(data_); } void SwapElements(int index1, int index2) const { accessor_->SwapElements(data_, index1, index2); } - void Clear() const { - accessor_->Clear(data_); - } + void Clear() const { accessor_->Clear(data_); } void Swap(const MutableRepeatedFieldRef& other) const { accessor_->Swap(data_, other.accessor_, other.data_); } - template<typename Container> + template <typename Container> void MergeFrom(const Container& container) const { typedef typename Container::const_iterator Iterator; for (Iterator it = container.begin(); it != container.end(); ++it) { Add(*it); } } - template<typename Container> + template <typename Container> void CopyFrom(const Container& container) const { Clear(); MergeFrom(container); @@ -303,9 +257,7 @@ class MutableRepeatedFieldRef< private: friend class Reflection; - MutableRepeatedFieldRef( - Message* message, - const FieldDescriptor* field) { + MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) { const Reflection* reflection = message->GetReflection(); data_ = reflection->RepeatedFieldData( message, field, internal::RefTypeTraits<T>::cpp_type, @@ -360,8 +312,8 @@ class PROTOBUF_EXPORT RepeatedFieldAccessor { // exists, or fill the data into scratch_space and return scratch_space. // Callers of this method must ensure scratch_space is a valid pointer // to a mutable object of the correct type. - virtual const Value* Get( - const Field* data, int index, Value* scratch_space) const = 0; + virtual const Value* Get(const Field* data, int index, + Value* scratch_space) const = 0; virtual void Clear(Field* data) const = 0; virtual void Set(Field* data, int index, const Value* value) const = 0; @@ -394,16 +346,15 @@ class PROTOBUF_EXPORT RepeatedFieldAccessor { // Templated methods that make using this interface easier for non-message // types. - template<typename T> + template <typename T> T Get(const Field* data, int index) const { typedef typename RefTypeTraits<T>::AccessorValueType ActualType; ActualType scratch_space; - return static_cast<T>( - *reinterpret_cast<const ActualType*>( - Get(data, index, static_cast<Value*>(&scratch_space)))); + return static_cast<T>(*reinterpret_cast<const ActualType*>( + Get(data, index, static_cast<Value*>(&scratch_space)))); } - template<typename T, typename ValueType> + template <typename T, typename ValueType> void Set(Field* data, int index, const ValueType& value) const { typedef typename RefTypeTraits<T>::AccessorValueType ActualType; // In this RepeatedFieldAccessor interface we pass/return data using @@ -417,7 +368,7 @@ class PROTOBUF_EXPORT RepeatedFieldAccessor { Set(data, index, static_cast<const Value*>(&tmp)); } - template<typename T, typename ValueType> + template <typename T, typename ValueType> void Add(Field* data, const ValueType& value) const { typedef typename RefTypeTraits<T>::AccessorValueType ActualType; // In this RepeatedFieldAccessor interface we pass/return data using @@ -440,7 +391,7 @@ class PROTOBUF_EXPORT RepeatedFieldAccessor { }; // Implement (Mutable)RepeatedFieldRef::iterator -template<typename T> +template <typename T> class RepeatedFieldRefIterator : public std::iterator<std::forward_iterator_tag, T> { typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType; @@ -459,17 +410,14 @@ class RepeatedFieldRefIterator scratch_space_(begin ? new AccessorValueType : nullptr) {} // Constructor for message fields. RepeatedFieldRefIterator(const void* data, - const RepeatedFieldAccessor* accessor, - bool begin, + const RepeatedFieldAccessor* accessor, bool begin, AccessorValueType* scratch_space) - : data_(data), accessor_(accessor), - iterator_(begin ? accessor->BeginIterator(data) : - accessor->EndIterator(data)), - scratch_space_(scratch_space) { - } - ~RepeatedFieldRefIterator() { - accessor_->DeleteIterator(data_, iterator_); - } + : data_(data), + accessor_(accessor), + iterator_(begin ? accessor->BeginIterator(data) + : accessor->EndIterator(data)), + scratch_space_(scratch_space) {} + ~RepeatedFieldRefIterator() { accessor_->DeleteIterator(data_, iterator_); } RepeatedFieldRefIterator operator++(int) { RepeatedFieldRefIterator tmp(*this); iterator_ = accessor_->AdvanceIterator(data_, iterator_); @@ -481,14 +429,12 @@ class RepeatedFieldRefIterator } IteratorValueType operator*() const { return static_cast<IteratorValueType>( - *static_cast<const AccessorValueType*>( - accessor_->GetIteratorValue( - data_, iterator_, scratch_space_.get()))); + *static_cast<const AccessorValueType*>(accessor_->GetIteratorValue( + data_, iterator_, scratch_space_.get()))); } IteratorPointerType operator->() const { return static_cast<IteratorPointerType>( - accessor_->GetIteratorValue( - data_, iterator_, scratch_space_.get())); + accessor_->GetIteratorValue(data_, iterator_, scratch_space_.get())); } bool operator!=(const RepeatedFieldRefIterator& other) const { assert(data_ == other.data_); @@ -500,9 +446,9 @@ class RepeatedFieldRefIterator } RepeatedFieldRefIterator(const RepeatedFieldRefIterator& other) - : data_(other.data_), accessor_(other.accessor_), - iterator_(accessor_->CopyIterator(data_, other.iterator_)) { - } + : data_(other.data_), + accessor_(other.accessor_), + iterator_(accessor_->CopyIterator(data_, other.iterator_)) {} RepeatedFieldRefIterator& operator=(const RepeatedFieldRefIterator& other) { if (this != &other) { accessor_->DeleteIterator(data_, iterator_); @@ -523,16 +469,17 @@ class RepeatedFieldRefIterator // TypeTraits that maps the type parameter T of RepeatedFieldRef or // MutableRepeatedFieldRef to corresponding iterator type, // RepeatedFieldAccessor type, etc. -template<typename T> +template <typename T> struct PrimitiveTraits { static const bool is_primitive = false; }; -#define DEFINE_PRIMITIVE(TYPE, type) \ - template<> struct PrimitiveTraits<type> { \ - static const bool is_primitive = true; \ - static const FieldDescriptor::CppType cpp_type = \ - FieldDescriptor::CPPTYPE_ ## TYPE; \ - }; +#define DEFINE_PRIMITIVE(TYPE, type) \ + template <> \ + struct PrimitiveTraits<type> { \ + static const bool is_primitive = true; \ + static const FieldDescriptor::CppType cpp_type = \ + FieldDescriptor::CPPTYPE_##TYPE; \ + }; DEFINE_PRIMITIVE(INT32, int32) DEFINE_PRIMITIVE(UINT32, uint32) DEFINE_PRIMITIVE(INT64, int64) @@ -542,7 +489,7 @@ DEFINE_PRIMITIVE(DOUBLE, double) DEFINE_PRIMITIVE(BOOL, bool) #undef DEFINE_PRIMITIVE -template<typename T> +template <typename T> struct RefTypeTraits< T, typename std::enable_if<PrimitiveTraits<T>::is_primitive>::type> { typedef RepeatedFieldRefIterator<T> iterator; @@ -550,14 +497,11 @@ struct RefTypeTraits< typedef T AccessorValueType; typedef T IteratorValueType; typedef T* IteratorPointerType; - static const FieldDescriptor::CppType cpp_type = - PrimitiveTraits<T>::cpp_type; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const FieldDescriptor::CppType cpp_type = PrimitiveTraits<T>::cpp_type; + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; -template<typename T> +template <typename T> struct RefTypeTraits< T, typename std::enable_if<is_proto_enum<T>::value>::type> { typedef RepeatedFieldRefIterator<T> iterator; @@ -568,9 +512,7 @@ struct RefTypeTraits< typedef int32* IteratorPointerType; static const FieldDescriptor::CppType cpp_type = FieldDescriptor::CPPTYPE_ENUM; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; template <typename T> @@ -583,25 +525,21 @@ struct RefTypeTraits< typedef const std::string* IteratorPointerType; static const FieldDescriptor::CppType cpp_type = FieldDescriptor::CPPTYPE_STRING; - static const Descriptor* GetMessageFieldDescriptor() { - return NULL; - } + static const Descriptor* GetMessageFieldDescriptor() { return NULL; } }; -template<typename T> +template <typename T> struct MessageDescriptorGetter { static const Descriptor* get() { return T::default_instance().GetDescriptor(); } }; -template<> +template <> struct MessageDescriptorGetter<Message> { - static const Descriptor* get() { - return NULL; - } + static const Descriptor* get() { return NULL; } }; -template<typename T> +template <typename T> struct RefTypeTraits< T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> { typedef RepeatedFieldRefIterator<T> iterator; diff --git a/src/google/protobuf/reflection_internal.h b/src/google/protobuf/reflection_internal.h index b4b1d41029..40c070fc79 100644 --- a/src/google/protobuf/reflection_internal.h +++ b/src/google/protobuf/reflection_internal.h @@ -82,7 +82,7 @@ class RandomAccessRepeatedFieldAccessor : public RepeatedFieldAccessor { // Base class for RepeatedFieldAccessor implementations that manipulates // RepeatedField<T>. -template<typename T> +template <typename T> class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor { public: RepeatedFieldWrapper() {} @@ -137,7 +137,7 @@ class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor { // Base class for RepeatedFieldAccessor implementations that manipulates // RepeatedPtrField<T>. -template<typename T> +template <typename T> class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor { public: bool IsEmpty(const Field* data) const override { diff --git a/src/google/protobuf/reflection_ops.cc b/src/google/protobuf/reflection_ops.cc index f8f9dc3f24..3ced1419f4 100644 --- a/src/google/protobuf/reflection_ops.cc +++ b/src/google/protobuf/reflection_ops.cc @@ -74,9 +74,9 @@ void ReflectionOps::Merge(const Message& from, Message* to) { const Descriptor* descriptor = from.GetDescriptor(); GOOGLE_CHECK_EQ(to->GetDescriptor(), descriptor) - << "Tried to merge messages of different types " - << "(merge " << descriptor->full_name() - << " to " << to->GetDescriptor()->full_name() << ")"; + << "Tried to merge messages of different types " + << "(merge " << descriptor->full_name() << " to " + << to->GetDescriptor()->full_name() << ")"; const Reflection* from_reflection = GetReflectionOrDie(from); const Reflection* to_reflection = GetReflectionOrDie(*to); @@ -99,8 +99,7 @@ void ReflectionOps::Merge(const Message& from, Message* to) { if (is_from_generated == is_to_generated && field->is_map()) { const MapFieldBase* from_field = from_reflection->GetMapData(from, field); - MapFieldBase* to_field = - to_reflection->MutableMapData(to, field); + MapFieldBase* to_field = to_reflection->MutableMapData(to, field); if (to_field->IsMapValid() && from_field->IsMapValid()) { to_field->MergeFrom(*from_field); continue; @@ -109,58 +108,58 @@ void ReflectionOps::Merge(const Message& from, Message* to) { int count = from_reflection->FieldSize(from, field); for (int j = 0; j < count; j++) { switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, METHOD) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - to_reflection->Add##METHOD(to, field, \ - from_reflection->GetRepeated##METHOD(from, field, j)); \ - break; - - HANDLE_TYPE(INT32 , Int32 ); - HANDLE_TYPE(INT64 , Int64 ); +#define HANDLE_TYPE(CPPTYPE, METHOD) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + to_reflection->Add##METHOD( \ + to, field, from_reflection->GetRepeated##METHOD(from, field, j)); \ + break; + + HANDLE_TYPE(INT32, Int32); + HANDLE_TYPE(INT64, Int64); HANDLE_TYPE(UINT32, UInt32); HANDLE_TYPE(UINT64, UInt64); - HANDLE_TYPE(FLOAT , Float ); + HANDLE_TYPE(FLOAT, Float); HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL , Bool ); + HANDLE_TYPE(BOOL, Bool); HANDLE_TYPE(STRING, String); - HANDLE_TYPE(ENUM , Enum ); + HANDLE_TYPE(ENUM, Enum); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: to_reflection->AddMessage(to, field)->MergeFrom( - from_reflection->GetRepeatedMessage(from, field, j)); + from_reflection->GetRepeatedMessage(from, field, j)); break; } } } else { switch (field->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, METHOD) \ - case FieldDescriptor::CPPTYPE_##CPPTYPE: \ - to_reflection->Set##METHOD(to, field, \ - from_reflection->Get##METHOD(from, field)); \ - break; - - HANDLE_TYPE(INT32 , Int32 ); - HANDLE_TYPE(INT64 , Int64 ); +#define HANDLE_TYPE(CPPTYPE, METHOD) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: \ + to_reflection->Set##METHOD(to, field, \ + from_reflection->Get##METHOD(from, field)); \ + break; + + HANDLE_TYPE(INT32, Int32); + HANDLE_TYPE(INT64, Int64); HANDLE_TYPE(UINT32, UInt32); HANDLE_TYPE(UINT64, UInt64); - HANDLE_TYPE(FLOAT , Float ); + HANDLE_TYPE(FLOAT, Float); HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL , Bool ); + HANDLE_TYPE(BOOL, Bool); HANDLE_TYPE(STRING, String); - HANDLE_TYPE(ENUM , Enum ); + HANDLE_TYPE(ENUM, Enum); #undef HANDLE_TYPE case FieldDescriptor::CPPTYPE_MESSAGE: to_reflection->MutableMessage(to, field)->MergeFrom( - from_reflection->GetMessage(from, field)); + from_reflection->GetMessage(from, field)); break; } } } to_reflection->MutableUnknownFields(to)->MergeFrom( - from_reflection->GetUnknownFields(from)); + from_reflection->GetUnknownFields(from)); } void ReflectionOps::Clear(Message* message) { @@ -221,7 +220,7 @@ bool ReflectionOps::IsInitialized(const Message& message) { for (int j = 0; j < size; j++) { if (!reflection->GetRepeatedMessage(message, field, j) - .IsInitialized()) { + .IsInitialized()) { return false; } } @@ -257,7 +256,8 @@ void ReflectionOps::DiscardUnknownFields(Message* message) { MapIterator end(message, field); for (map_field->MapBegin(&iter), map_field->MapEnd(&end); iter != end; ++iter) { - iter.MutableValueRef()->MutableMessageValue() + iter.MutableValueRef() + ->MutableMessageValue() ->DiscardUnknownFields(); } continue; @@ -269,7 +269,7 @@ void ReflectionOps::DiscardUnknownFields(Message* message) { int size = reflection->FieldSize(*message, field); for (int j = 0; j < size; j++) { reflection->MutableRepeatedMessage(message, field, j) - ->DiscardUnknownFields(); + ->DiscardUnknownFields(); } } else { reflection->MutableMessage(message, field)->DiscardUnknownFields(); @@ -324,16 +324,14 @@ void ReflectionOps::FindInitializationErrors(const Message& message, for (int j = 0; j < size; j++) { const Message& sub_message = - reflection->GetRepeatedMessage(message, field, j); + reflection->GetRepeatedMessage(message, field, j); FindInitializationErrors(sub_message, - SubMessagePrefix(prefix, field, j), - errors); + SubMessagePrefix(prefix, field, j), errors); } } else { const Message& sub_message = reflection->GetMessage(message, field); FindInitializationErrors(sub_message, - SubMessagePrefix(prefix, field, -1), - errors); + SubMessagePrefix(prefix, field, -1), errors); } } } diff --git a/src/google/protobuf/reflection_ops_unittest.cc b/src/google/protobuf/reflection_ops_unittest.cc index 0f46ff133e..bd141933c6 100644 --- a/src/google/protobuf/reflection_ops_unittest.cc +++ b/src/google/protobuf/reflection_ops_unittest.cc @@ -33,9 +33,9 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include <google/protobuf/reflection_ops.h> -#include <google/protobuf/descriptor.h> -#include <google/protobuf/unittest.pb.h> #include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> +#include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> @@ -128,18 +128,21 @@ TEST(ReflectionOpsTest, MergeExtensions) { TestUtil::SetAllExtensions(&message); // This field will test merging into an empty spot. - message2.SetExtension(unittest::optional_int32_extension, - message.GetExtension(unittest::optional_int32_extension)); + message2.SetExtension( + unittest::optional_int32_extension, + message.GetExtension(unittest::optional_int32_extension)); message.ClearExtension(unittest::optional_int32_extension); // This tests overwriting. - message2.SetExtension(unittest::optional_string_extension, - message.GetExtension(unittest::optional_string_extension)); + message2.SetExtension( + unittest::optional_string_extension, + message.GetExtension(unittest::optional_string_extension)); message.SetExtension(unittest::optional_string_extension, "something else"); // This tests concatenating. - message2.AddExtension(unittest::repeated_int32_extension, - message.GetExtension(unittest::repeated_int32_extension, 1)); + message2.AddExtension( + unittest::repeated_int32_extension, + message.GetExtension(unittest::repeated_int32_extension, 1)); int32 i = message.GetExtension(unittest::repeated_int32_extension, 0); message.ClearExtension(unittest::repeated_int32_extension); message.AddExtension(unittest::repeated_int32_extension, i); @@ -192,9 +195,7 @@ TEST(ReflectionOpsTest, MergeFromSelf) { unittest::TestAllTypes message; - EXPECT_DEATH( - ReflectionOps::Merge(message, &message), - "&from"); + EXPECT_DEATH(ReflectionOps::Merge(message, &message), "&from"); } #endif // PROTOBUF_HAS_DEATH_TEST @@ -235,9 +236,9 @@ TEST(ReflectionOpsTest, ClearExtensions) { &message.GetExtension(unittest::optionalgroup_extension)); EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(), &message.GetExtension(unittest::optional_nested_message_extension)); - EXPECT_NE(&unittest::ForeignMessage::default_instance(), - &message.GetExtension( - unittest::optional_foreign_message_extension)); + EXPECT_NE( + &unittest::ForeignMessage::default_instance(), + &message.GetExtension(unittest::optional_foreign_message_extension)); EXPECT_NE(&unittest_import::ImportMessage::default_instance(), &message.GetExtension(unittest::optional_import_message_extension)); } @@ -274,30 +275,29 @@ TEST(ReflectionOpsTest, DiscardUnknownFields) { TestUtil::SetAllFields(&message); // Set some unknown fields in message. - message.mutable_unknown_fields() - ->AddVarint(123456, 654321); + message.mutable_unknown_fields()->AddVarint(123456, 654321); message.mutable_optional_nested_message() - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); message.mutable_repeated_nested_message(0) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); EXPECT_EQ(1, message.unknown_fields().field_count()); - EXPECT_EQ(1, message.optional_nested_message() - .unknown_fields().field_count()); - EXPECT_EQ(1, message.repeated_nested_message(0) - .unknown_fields().field_count()); + EXPECT_EQ(1, + message.optional_nested_message().unknown_fields().field_count()); + EXPECT_EQ(1, + message.repeated_nested_message(0).unknown_fields().field_count()); // Discard them. ReflectionOps::DiscardUnknownFields(&message); TestUtil::ExpectAllFieldsSet(message); EXPECT_EQ(0, message.unknown_fields().field_count()); - EXPECT_EQ(0, message.optional_nested_message() - .unknown_fields().field_count()); - EXPECT_EQ(0, message.repeated_nested_message(0) - .unknown_fields().field_count()); + EXPECT_EQ(0, + message.optional_nested_message().unknown_fields().field_count()); + EXPECT_EQ(0, + message.repeated_nested_message(0).unknown_fields().field_count()); } TEST(ReflectionOpsTest, DiscardUnknownExtensions) { @@ -305,34 +305,35 @@ TEST(ReflectionOpsTest, DiscardUnknownExtensions) { TestUtil::SetAllExtensions(&message); // Set some unknown fields. - message.mutable_unknown_fields() - ->AddVarint(123456, 654321); + message.mutable_unknown_fields()->AddVarint(123456, 654321); message.MutableExtension(unittest::optional_nested_message_extension) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); message.MutableExtension(unittest::repeated_nested_message_extension, 0) - ->mutable_unknown_fields() - ->AddVarint(123456, 654321); + ->mutable_unknown_fields() + ->AddVarint(123456, 654321); EXPECT_EQ(1, message.unknown_fields().field_count()); + EXPECT_EQ(1, message.GetExtension(unittest::optional_nested_message_extension) + .unknown_fields() + .field_count()); EXPECT_EQ(1, - message.GetExtension(unittest::optional_nested_message_extension) - .unknown_fields().field_count()); - EXPECT_EQ(1, - message.GetExtension(unittest::repeated_nested_message_extension, 0) - .unknown_fields().field_count()); + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .unknown_fields() + .field_count()); // Discard them. ReflectionOps::DiscardUnknownFields(&message); TestUtil::ExpectAllExtensionsSet(message); EXPECT_EQ(0, message.unknown_fields().field_count()); + EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension) + .unknown_fields() + .field_count()); EXPECT_EQ(0, - message.GetExtension(unittest::optional_nested_message_extension) - .unknown_fields().field_count()); - EXPECT_EQ(0, - message.GetExtension(unittest::repeated_nested_message_extension, 0) - .unknown_fields().field_count()); + message.GetExtension(unittest::repeated_nested_message_extension, 0) + .unknown_fields() + .field_count()); } TEST(ReflectionOpsTest, IsInitialized) { @@ -435,16 +436,17 @@ TEST(ReflectionOpsTest, FindForeignInitializationErrors) { message.mutable_optional_message(); message.add_repeated_message(); message.add_repeated_message(); - EXPECT_EQ("optional_message.a," - "optional_message.b," - "optional_message.c," - "repeated_message[0].a," - "repeated_message[0].b," - "repeated_message[0].c," - "repeated_message[1].a," - "repeated_message[1].b," - "repeated_message[1].c", - FindInitializationErrors(message)); + EXPECT_EQ( + "optional_message.a," + "optional_message.b," + "optional_message.c," + "repeated_message[0].a," + "repeated_message[0].b," + "repeated_message[0].c," + "repeated_message[1].a," + "repeated_message[1].b," + "repeated_message[1].c", + FindInitializationErrors(message)); } TEST(ReflectionOpsTest, FindExtensionInitializationErrors) { @@ -452,23 +454,23 @@ TEST(ReflectionOpsTest, FindExtensionInitializationErrors) { message.MutableExtension(unittest::TestRequired::single); message.AddExtension(unittest::TestRequired::multi); message.AddExtension(unittest::TestRequired::multi); - EXPECT_EQ("(protobuf_unittest.TestRequired.single).a," - "(protobuf_unittest.TestRequired.single).b," - "(protobuf_unittest.TestRequired.single).c," - "(protobuf_unittest.TestRequired.multi)[0].a," - "(protobuf_unittest.TestRequired.multi)[0].b," - "(protobuf_unittest.TestRequired.multi)[0].c," - "(protobuf_unittest.TestRequired.multi)[1].a," - "(protobuf_unittest.TestRequired.multi)[1].b," - "(protobuf_unittest.TestRequired.multi)[1].c", - FindInitializationErrors(message)); + EXPECT_EQ( + "(protobuf_unittest.TestRequired.single).a," + "(protobuf_unittest.TestRequired.single).b," + "(protobuf_unittest.TestRequired.single).c," + "(protobuf_unittest.TestRequired.multi)[0].a," + "(protobuf_unittest.TestRequired.multi)[0].b," + "(protobuf_unittest.TestRequired.multi)[0].c," + "(protobuf_unittest.TestRequired.multi)[1].a," + "(protobuf_unittest.TestRequired.multi)[1].b," + "(protobuf_unittest.TestRequired.multi)[1].c", + FindInitializationErrors(message)); } TEST(ReflectionOpsTest, FindOneofInitializationErrors) { unittest::TestRequiredOneof message; message.mutable_foo_message(); - EXPECT_EQ("foo_message.required_double", - FindInitializationErrors(message)); + EXPECT_EQ("foo_message.required_double", FindInitializationErrors(message)); } } // namespace diff --git a/src/google/protobuf/repeated_field.cc b/src/google/protobuf/repeated_field.cc index 44b045738a..e5dbe025be 100644 --- a/src/google/protobuf/repeated_field.cc +++ b/src/google/protobuf/repeated_field.cc @@ -34,9 +34,9 @@ #include <algorithm> -#include <google/protobuf/repeated_field.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> +#include <google/protobuf/repeated_field.h> #include <google/protobuf/port_def.inc> @@ -56,9 +56,8 @@ void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) { Arena* arena = GetArenaNoVirtual(); new_size = std::max(kMinRepeatedFieldAllocationSize, std::max(total_size_ * 2, new_size)); - GOOGLE_CHECK_LE(new_size, - (std::numeric_limits<size_t>::max() - kRepHeaderSize) / - sizeof(old_rep->elements[0])) + GOOGLE_CHECK_LE(new_size, (std::numeric_limits<size_t>::max() - kRepHeaderSize) / + sizeof(old_rep->elements[0])) << "Requested size is too large to fit into size_t."; size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size; if (arena == NULL) { diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 2552e232c2..4c631f05da 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -111,7 +111,6 @@ inline int CalculateReserve(Iter begin, Iter end) { } } // namespace internal - // RepeatedField is used to represent repeated fields of a primitive type (in // other words, everything except strings and nested Messages). Most users will // not ever use a RepeatedField directly; they will use the get-by-index, @@ -220,15 +219,11 @@ class RepeatedField final { // Reverse iterator support typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; - reverse_iterator rbegin() { - return reverse_iterator(end()); - } + reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - reverse_iterator rend() { - return reverse_iterator(begin()); - } + reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } @@ -350,13 +345,15 @@ class RepeatedField final { const Element* unsafe_data() const; }; -template<typename Element> +template <typename Element> const size_t RepeatedField<Element>::kRepHeaderSize = reinterpret_cast<size_t>(&reinterpret_cast<Rep*>(16)->elements[0]) - 16; namespace internal { -template <typename It> class RepeatedPtrIterator; -template <typename It, typename VoidPtr> class RepeatedPtrOverPtrsIterator; +template <typename It> +class RepeatedPtrIterator; +template <typename It, typename VoidPtr> +class RepeatedPtrOverPtrsIterator; } // namespace internal namespace internal { @@ -393,22 +390,24 @@ struct TypeImplementsMergeBehaviorProbeForMergeFrom { // We mangle these names a bit to avoid compatibility issues in 'unclean' // include environments that may have, e.g., "#define test ..." (yes, this // exists). - template<typename U, typename RetType, RetType (U::*)(const U& arg)> - struct CheckType; - template<typename U> static HasMerge Check( - CheckType<U, void, &U::MergeFrom>*); - template<typename U> static HasMerge Check( - CheckType<U, bool, &U::MergeFrom>*); - template<typename U> static HasNoMerge Check(...); + template <typename U, typename RetType, RetType (U::*)(const U& arg)> + struct CheckType; + template <typename U> + static HasMerge Check(CheckType<U, void, &U::MergeFrom>*); + template <typename U> + static HasMerge Check(CheckType<U, bool, &U::MergeFrom>*); + template <typename U> + static HasNoMerge Check(...); // Resolves to either std::true_type or std::false_type. typedef std::integral_constant<bool, - (sizeof(Check<T>(0)) == sizeof(HasMerge))> type; + (sizeof(Check<T>(0)) == sizeof(HasMerge))> + type; }; template <typename T, typename = void> -struct TypeImplementsMergeBehavior : - TypeImplementsMergeBehaviorProbeForMergeFrom<T> {}; +struct TypeImplementsMergeBehavior + : TypeImplementsMergeBehaviorProbeForMergeFrom<T> {}; template <> @@ -529,7 +528,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { template <typename TypeHandler> typename TypeHandler::Type* AddFromCleared(); - template<typename TypeHandler> + template <typename TypeHandler> void AddAllocated(typename TypeHandler::Type* value) { typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t; AddAllocatedInternal<TypeHandler>(value, t); @@ -573,12 +572,9 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { typename TypeHandler::Type* ReleaseLastInternal(std::false_type); template <typename TypeHandler> - PROTOBUF_NOINLINE void SwapFallback( - RepeatedPtrFieldBase* other); + PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other); - inline Arena* GetArenaNoVirtual() const { - return arena_; - } + inline Arena* GetArenaNoVirtual() const { return arena_; } private: static const int kInitialSize = 0; @@ -594,11 +590,11 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { // Placing all fields directly in the RepeatedPtrFieldBase instance costs // significant performance for memory-sensitive workloads. Arena* arena_; - int current_size_; - int total_size_; + int current_size_; + int total_size_; struct Rep { - int allocated_size; - void* elements[1]; + int allocated_size; + void* elements[1]; }; static const size_t kRepHeaderSize = sizeof(Rep) - sizeof(void*); // Contains arena ptr and the elements array. We also keep the invariant that @@ -616,13 +612,14 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { // Non-templated inner function to avoid code duplication. Takes a function // pointer to the type-specific (templated) inner allocate/merge loop. - void MergeFromInternal( - const RepeatedPtrFieldBase& other, - void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int)); + void MergeFromInternal(const RepeatedPtrFieldBase& other, + void (RepeatedPtrFieldBase::*inner_loop)(void**, + void**, int, + int)); - template<typename TypeHandler> - void MergeFromInnerLoop( - void** our_elems, void** other_elems, int length, int already_allocated); + template <typename TypeHandler> + void MergeFromInnerLoop(void** our_elems, void** other_elems, int length, + int already_allocated); // Internal helper: extend array space if necessary to contain |extend_amount| // more elements, and return a pointer to the element immediately following @@ -716,7 +713,7 @@ template <> inline Arena* GenericTypeHandler<MessageLite>::GetArena(MessageLite* value) { return value->GetArena(); } -template<> +template <> inline void* GenericTypeHandler<MessageLite>::GetMaybeArenaPointer( MessageLite* value) { return value->GetMaybeArenaPointer(); @@ -880,15 +877,11 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { // Reverse iterator support typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; - reverse_iterator rbegin() { - return reverse_iterator(end()); - } + reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - reverse_iterator rend() { - return reverse_iterator(begin()); - } + reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } @@ -1058,23 +1051,15 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { template <typename Element> inline RepeatedField<Element>::RepeatedField() - : current_size_(0), - total_size_(0), - ptr_(NULL) { -} + : current_size_(0), total_size_(0), ptr_(NULL) {} template <typename Element> inline RepeatedField<Element>::RepeatedField(Arena* arena) - : current_size_(0), - total_size_(0), - ptr_(arena) { -} + : current_size_(0), total_size_(0), ptr_(arena) {} template <typename Element> inline RepeatedField<Element>::RepeatedField(const RepeatedField& other) - : current_size_(0), - total_size_(0), - ptr_(NULL) { + : current_size_(0), total_size_(0), ptr_(NULL) { if (other.current_size_ != 0) { Reserve(other.size()); AddNAlreadyReserved(other.size()); @@ -1085,9 +1070,7 @@ inline RepeatedField<Element>::RepeatedField(const RepeatedField& other) template <typename Element> template <typename Iter> RepeatedField<Element>::RepeatedField(Iter begin, const Iter& end) - : current_size_(0), - total_size_(0), - ptr_(NULL) { + : current_size_(0), total_size_(0), ptr_(NULL) { int reserve = internal::CalculateReserve(begin, end); if (reserve != -1) { if (reserve == 0) { @@ -1118,10 +1101,9 @@ RepeatedField<Element>::~RepeatedField() { } template <typename Element> -inline RepeatedField<Element>& -RepeatedField<Element>::operator=(const RepeatedField& other) { - if (this != &other) - CopyFrom(other); +inline RepeatedField<Element>& RepeatedField<Element>::operator=( + const RepeatedField& other) { + if (this != &other) CopyFrom(other); return *this; } @@ -1129,7 +1111,8 @@ template <typename Element> inline RepeatedField<Element>::RepeatedField(RepeatedField&& other) noexcept : RepeatedField() { // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. + // other is on an arena. This field can't be on an arena because arena + // construction always uses the Arena* accepting constructor. if (other.GetArenaNoVirtual()) { CopyFrom(other); } else { @@ -1167,19 +1150,19 @@ inline int RepeatedField<Element>::Capacity() const { return total_size_; } -template<typename Element> +template <typename Element> inline void RepeatedField<Element>::AddAlreadyReserved(const Element& value) { GOOGLE_DCHECK_LT(current_size_, total_size_); elements()[current_size_++] = value; } -template<typename Element> +template <typename Element> inline Element* RepeatedField<Element>::AddAlreadyReserved() { GOOGLE_DCHECK_LT(current_size_, total_size_); return &elements()[current_size_++]; } -template<typename Element> +template <typename Element> inline Element* RepeatedField<Element>::AddNAlreadyReserved(int n) { GOOGLE_DCHECK_GE(total_size_ - current_size_, n) << total_size_ << ", " << current_size_; @@ -1194,7 +1177,7 @@ inline Element* RepeatedField<Element>::AddNAlreadyReserved(int n) { return ret; } -template<typename Element> +template <typename Element> inline void RepeatedField<Element>::Resize(int new_size, const Element& value) { GOOGLE_DCHECK_GE(new_size, 0); if (new_size > current_size_) { @@ -1258,16 +1241,15 @@ inline void RepeatedField<Element>::RemoveLast() { } template <typename Element> -void RepeatedField<Element>::ExtractSubrange( - int start, int num, Element* elements) { +void RepeatedField<Element>::ExtractSubrange(int start, int num, + Element* elements) { GOOGLE_DCHECK_GE(start, 0); GOOGLE_DCHECK_GE(num, 0); GOOGLE_DCHECK_LE(start + num, this->current_size_); // Save the values of the removed elements if requested. if (elements != NULL) { - for (int i = 0; i < num; ++i) - elements[i] = this->Get(i + start); + for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start); } // Slide remaining elements down to fill the gap. @@ -1383,8 +1365,7 @@ RepeatedField<Element>::cbegin() const { return total_size_ > 0 ? elements() : NULL; } template <typename Element> -inline typename RepeatedField<Element>::iterator -RepeatedField<Element>::end() { +inline typename RepeatedField<Element>::iterator RepeatedField<Element>::end() { return total_size_ > 0 ? elements() + current_size_ : NULL; } template <typename Element> @@ -1417,7 +1398,8 @@ void RepeatedField<Element>::Reserve(int new_size) { static_cast<size_t>(new_size), (std::numeric_limits<size_t>::max() - kRepHeaderSize) / sizeof(Element)) << "Requested size is too large to fit into size_t."; - size_t bytes = kRepHeaderSize + sizeof(Element) * static_cast<size_t>(new_size); + size_t bytes = + kRepHeaderSize + sizeof(Element) * static_cast<size_t>(new_size); if (arena == NULL) { new_rep = static_cast<Rep*>(::operator new(bytes)); } else { @@ -1459,22 +1441,23 @@ inline void RepeatedField<Element>::Truncate(int new_size) { } template <typename Element> -inline void RepeatedField<Element>::MoveArray( - Element* to, Element* from, int array_size) { +inline void RepeatedField<Element>::MoveArray(Element* to, Element* from, + int array_size) { CopyArray(to, from, array_size); } template <typename Element> -inline void RepeatedField<Element>::CopyArray( - Element* to, const Element* from, int array_size) { +inline void RepeatedField<Element>::CopyArray(Element* to, const Element* from, + int array_size) { internal::ElementCopier<Element>()(to, from, array_size); } namespace internal { template <typename Element, bool HasTrivialCopy> -void ElementCopier<Element, HasTrivialCopy>::operator()( - Element* to, const Element* from, int array_size) { +void ElementCopier<Element, HasTrivialCopy>::operator()(Element* to, + const Element* from, + int array_size) { std::copy(from, from + array_size, to); } @@ -1493,11 +1476,7 @@ struct ElementCopier<Element, true> { namespace internal { inline RepeatedPtrFieldBase::RepeatedPtrFieldBase() - : arena_(NULL), - current_size_(0), - total_size_(0), - rep_(NULL) { -} + : arena_(NULL), current_size_(0), total_size_(0), rep_(NULL) {} inline RepeatedPtrFieldBase::RepeatedPtrFieldBase(Arena* arena) : arena_(arena), current_size_(0), total_size_(0), rep_(NULL) {} @@ -1545,17 +1524,13 @@ void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) { temp.Destroy<TypeHandler>(); // Frees rep_ if `other` had no arena. } -inline bool RepeatedPtrFieldBase::empty() const { - return current_size_ == 0; -} +inline bool RepeatedPtrFieldBase::empty() const { return current_size_ == 0; } -inline int RepeatedPtrFieldBase::size() const { - return current_size_; -} +inline int RepeatedPtrFieldBase::size() const { return current_size_; } template <typename TypeHandler> -inline const typename TypeHandler::WeakType& -RepeatedPtrFieldBase::Get(int index) const { +inline const typename TypeHandler::WeakType& RepeatedPtrFieldBase::Get( + int index) const { GOOGLE_DCHECK_GE(index, 0); GOOGLE_DCHECK_LT(index, current_size_); return *cast<TypeHandler>(rep_->elements[index]); @@ -1577,8 +1552,7 @@ inline typename TypeHandler::Type& RepeatedPtrFieldBase::at(int index) { } template <typename TypeHandler> -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::Mutable(int index) { +inline typename TypeHandler::Type* RepeatedPtrFieldBase::Mutable(int index) { GOOGLE_DCHECK_GE(index, 0); GOOGLE_DCHECK_LT(index, current_size_); return cast<TypeHandler>(rep_->elements[index]); @@ -1653,8 +1627,8 @@ template <typename TypeHandler> inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) { GOOGLE_DCHECK_NE(&other, this); if (other.current_size_ == 0) return; - MergeFromInternal( - other, &RepeatedPtrFieldBase::MergeFromInnerLoop<TypeHandler>); + MergeFromInternal(other, + &RepeatedPtrFieldBase::MergeFromInnerLoop<TypeHandler>); } inline void RepeatedPtrFieldBase::MergeFromInternal( @@ -1665,8 +1639,8 @@ inline void RepeatedPtrFieldBase::MergeFromInternal( void** other_elements = other.rep_->elements; void** new_elements = InternalExtend(other_size); int allocated_elems = rep_->allocated_size - current_size_; - (this->*inner_loop)(new_elements, other_elements, - other_size, allocated_elems); + (this->*inner_loop)(new_elements, other_elements, other_size, + allocated_elems); current_size_ += other_size; if (rep_->allocated_size < current_size_) { rep_->allocated_size = current_size_; @@ -1674,9 +1648,10 @@ inline void RepeatedPtrFieldBase::MergeFromInternal( } // Merges other_elems to our_elems. -template<typename TypeHandler> -void RepeatedPtrFieldBase::MergeFromInnerLoop( - void** our_elems, void** other_elems, int length, int already_allocated) { +template <typename TypeHandler> +void RepeatedPtrFieldBase::MergeFromInnerLoop(void** our_elems, + void** other_elems, int length, + int already_allocated) { // Split into two loops, over ranges [0, allocated) and [allocated, length), // to avoid a branch within the loop. for (int i = 0; i < already_allocated && i < length; i++) { @@ -1706,9 +1681,7 @@ inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) { RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other); } -inline int RepeatedPtrFieldBase::Capacity() const { - return total_size_; -} +inline int RepeatedPtrFieldBase::Capacity() const { return total_size_; } inline void* const* RepeatedPtrFieldBase::raw_data() const { return rep_ ? rep_->elements : NULL; @@ -1726,8 +1699,8 @@ inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() { } template <typename TypeHandler> -inline const typename TypeHandler::Type* const* -RepeatedPtrFieldBase::data() const { +inline const typename TypeHandler::Type* const* RepeatedPtrFieldBase::data() + const { // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this // method entirely. return reinterpret_cast<const typename TypeHandler::Type* const*>(raw_data()); @@ -1743,8 +1716,8 @@ inline size_t RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong() const { size_t allocated_bytes = static_cast<size_t>(total_size_) * sizeof(void*); if (rep_ != NULL) { for (int i = 0; i < rep_->allocated_size; ++i) { - allocated_bytes += TypeHandler::SpaceUsedLong( - *cast<TypeHandler>(rep_->elements[i])); + allocated_bytes += + TypeHandler::SpaceUsedLong(*cast<TypeHandler>(rep_->elements[i])); } allocated_bytes += kRepHeaderSize; } @@ -1763,13 +1736,11 @@ inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() { // AddAllocated version that implements arena-safe copying behavior. template <typename TypeHandler> void RepeatedPtrFieldBase::AddAllocatedInternal( - typename TypeHandler::Type* value, - std::true_type) { - Arena* element_arena = reinterpret_cast<Arena*>( - TypeHandler::GetMaybeArenaPointer(value)); + typename TypeHandler::Type* value, std::true_type) { + Arena* element_arena = + reinterpret_cast<Arena*>(TypeHandler::GetMaybeArenaPointer(value)); Arena* arena = GetArenaNoVirtual(); - if (arena == element_arena && rep_ && - rep_->allocated_size < total_size_) { + if (arena == element_arena && rep_ && rep_->allocated_size < total_size_) { // Fast path: underlying arena representation (tagged pointer) is equal to // our arena pointer, and we can add to array without resizing it (at least // one slot that is not allocated). @@ -1783,13 +1754,13 @@ void RepeatedPtrFieldBase::AddAllocatedInternal( current_size_ = current_size_ + 1; rep_->allocated_size = rep_->allocated_size + 1; } else { - AddAllocatedSlowWithCopy<TypeHandler>( - value, TypeHandler::GetArena(value), arena); + AddAllocatedSlowWithCopy<TypeHandler>(value, TypeHandler::GetArena(value), + arena); } } // Slowpath handles all cases, copying if necessary. -template<typename TypeHandler> +template <typename TypeHandler> void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy( // Pass value_arena and my_arena to avoid duplicate virtual call (value) or // load (mine). @@ -1813,9 +1784,8 @@ void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy( // AddAllocated version that does not implement arena-safe copying behavior. template <typename TypeHandler> void RepeatedPtrFieldBase::AddAllocatedInternal( - typename TypeHandler::Type* value, - std::false_type) { - if (rep_ && rep_->allocated_size < total_size_) { + typename TypeHandler::Type* value, std::false_type) { + if (rep_ && rep_->allocated_size < total_size_) { // Fast path: underlying arena representation (tagged pointer) is equal to // our arena pointer, and we can add to array without resizing it (at least // one slot that is not allocated). @@ -1846,8 +1816,8 @@ void RepeatedPtrFieldBase::UnsafeArenaAddAllocated( // cleared objects awaiting reuse. We don't want to grow the array in this // case because otherwise a loop calling AddAllocated() followed by Clear() // would leak memory. - TypeHandler::Delete( - cast<TypeHandler>(rep_->elements[current_size_]), arena_); + TypeHandler::Delete(cast<TypeHandler>(rep_->elements[current_size_]), + arena_); } else if (current_size_ < rep_->allocated_size) { // We have some cleared objects. We don't care about their order, so we // can just move the first one to the end to make space. @@ -1863,8 +1833,8 @@ void RepeatedPtrFieldBase::UnsafeArenaAddAllocated( // ReleaseLast() for types that implement merge/copy behavior. template <typename TypeHandler> -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::ReleaseLastInternal(std::true_type) { +inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal( + std::true_type) { // First, release an element. typename TypeHandler::Type* result = UnsafeArenaReleaseLast<TypeHandler>(); // Now perform a copy if we're on an arena. @@ -1884,8 +1854,8 @@ RepeatedPtrFieldBase::ReleaseLastInternal(std::true_type) { // an arena, since the user really should implement the copy operation in this // case. template <typename TypeHandler> -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::ReleaseLastInternal(std::false_type) { +inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal( + std::false_type) { GOOGLE_DCHECK(GetArenaNoVirtual() == NULL) << "ReleaseLast() called on a RepeatedPtrField that is on an arena, " << "with a type that does not implement MergeFrom. This is unsafe; " @@ -1895,7 +1865,7 @@ RepeatedPtrFieldBase::ReleaseLastInternal(std::false_type) { template <typename TypeHandler> inline typename TypeHandler::Type* - RepeatedPtrFieldBase::UnsafeArenaReleaseLast() { +RepeatedPtrFieldBase::UnsafeArenaReleaseLast() { GOOGLE_DCHECK_GT(current_size_, 0); typename TypeHandler::Type* result = cast<TypeHandler>(rep_->elements[--current_size_]); @@ -1942,16 +1912,14 @@ inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() { template <typename Element> class RepeatedPtrField<Element>::TypeHandler - : public internal::GenericTypeHandler<Element> { -}; + : public internal::GenericTypeHandler<Element> {}; template <> class RepeatedPtrField<std::string>::TypeHandler : public internal::StringTypeHandler {}; template <typename Element> -inline RepeatedPtrField<Element>::RepeatedPtrField() - : RepeatedPtrFieldBase() {} +inline RepeatedPtrField<Element>::RepeatedPtrField() : RepeatedPtrFieldBase() {} template <typename Element> inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena) @@ -1960,14 +1928,14 @@ inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena) template <typename Element> inline RepeatedPtrField<Element>::RepeatedPtrField( const RepeatedPtrField& other) - : RepeatedPtrFieldBase() { + : RepeatedPtrFieldBase() { MergeFrom(other); } template <typename Element> template <typename Iter> -inline RepeatedPtrField<Element>::RepeatedPtrField( - Iter begin, const Iter& end) { +inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, + const Iter& end) { int reserve = internal::CalculateReserve(begin, end); if (reserve != -1) { Reserve(reserve); @@ -1985,8 +1953,7 @@ RepeatedPtrField<Element>::~RepeatedPtrField() { template <typename Element> inline RepeatedPtrField<Element>& RepeatedPtrField<Element>::operator=( const RepeatedPtrField& other) { - if (this != &other) - CopyFrom(other); + if (this != &other) CopyFrom(other); return *this; } @@ -1995,7 +1962,8 @@ inline RepeatedPtrField<Element>::RepeatedPtrField( RepeatedPtrField&& other) noexcept : RepeatedPtrField() { // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. + // other is on an arena. This field can't be on an arena because arena + // construction always uses the Arena* accepting constructor. if (other.GetArenaNoVirtual()) { CopyFrom(other); } else { @@ -2076,8 +2044,8 @@ inline void RepeatedPtrField<Element>::DeleteSubrange(int start, int num) { } template <typename Element> -inline void RepeatedPtrField<Element>::ExtractSubrange( - int start, int num, Element** elements) { +inline void RepeatedPtrField<Element>::ExtractSubrange(int start, int num, + Element** elements) { typename internal::TypeImplementsMergeBehavior< typename TypeHandler::Type>::type t; ExtractSubrangeInternal(start, num, elements, t); @@ -2099,8 +2067,8 @@ inline void RepeatedPtrField<Element>::ExtractSubrangeInternal( // If we're on an arena, we perform a copy for each element so that the // returned elements are heap-allocated. for (int i = 0; i < num; ++i) { - Element* element = RepeatedPtrFieldBase:: - Mutable<TypeHandler>(i + start); + Element* element = + RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start); typename TypeHandler::Type* new_value = TypeHandler::NewFromPrototype(element, NULL); TypeHandler::Merge(*element, new_value); @@ -2118,7 +2086,7 @@ inline void RepeatedPtrField<Element>::ExtractSubrangeInternal( // ExtractSubrange() implementation for types that do not implement merge/copy // behavior. -template<typename Element> +template <typename Element> inline void RepeatedPtrField<Element>::ExtractSubrangeInternal( int start, int num, Element** elements, std::false_type) { // This case is identical to UnsafeArenaExtractSubrange(). However, since @@ -2161,8 +2129,7 @@ inline void RepeatedPtrField<Element>::MergeFrom( } template <typename Element> -inline void RepeatedPtrField<Element>::CopyFrom( - const RepeatedPtrField& other) { +inline void RepeatedPtrField<Element>::CopyFrom(const RepeatedPtrField& other) { RepeatedPtrFieldBase::CopyFrom<TypeHandler>(other); } @@ -2193,16 +2160,14 @@ inline const Element* const* RepeatedPtrField<Element>::data() const { template <typename Element> inline void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) { - if (this == other) - return; + if (this == other) return; RepeatedPtrFieldBase::Swap<TypeHandler>(other); } template <typename Element> inline void RepeatedPtrField<Element>::UnsafeArenaSwap( RepeatedPtrField* other) { - if (this == other) - return; + if (this == other) return; RepeatedPtrFieldBase::InternalSwap(other); } @@ -2281,31 +2246,22 @@ namespace internal { // // This code based on net/proto/proto-array-internal.h by Jeffrey Yasskin // (jyasskin@google.com). -template<typename Element> -class RepeatedPtrIterator - : public std::iterator< - std::random_access_iterator_tag, Element> { +template <typename Element> +class RepeatedPtrIterator { public: - typedef RepeatedPtrIterator<Element> iterator; - typedef std::iterator< - std::random_access_iterator_tag, Element> superclass; - - // Shadow the value_type in std::iterator<> because const_iterator::value_type - // needs to be T, not const T. - typedef typename std::remove_const<Element>::type value_type; - - // Let the compiler know that these are type names, so we don't have to - // write "typename" in front of them everywhere. - typedef typename superclass::reference reference; - typedef typename superclass::pointer pointer; - typedef typename superclass::difference_type difference_type; + using iterator = RepeatedPtrIterator<Element>; + using iterator_category = std::random_access_iterator_tag; + using value_type = typename std::remove_const<Element>::type; + using difference_type = std::ptrdiff_t; + using pointer = Element*; + using reference = Element&; RepeatedPtrIterator() : it_(NULL) {} explicit RepeatedPtrIterator(void* const* it) : it_(it) {} // Allow "upcasting" from RepeatedPtrIterator<T**> to // RepeatedPtrIterator<const T*const*>. - template<typename OtherElement> + template <typename OtherElement> RepeatedPtrIterator(const RepeatedPtrIterator<OtherElement>& other) : it_(other.it_) { // Force a compiler error if the other type is not convertible to ours. @@ -2316,13 +2272,19 @@ class RepeatedPtrIterator // dereferenceable reference operator*() const { return *reinterpret_cast<Element*>(*it_); } - pointer operator->() const { return &(operator*()); } + pointer operator->() const { return &(operator*()); } // {inc,dec}rementable - iterator& operator++() { ++it_; return *this; } - iterator operator++(int) { return iterator(it_++); } - iterator& operator--() { --it_; return *this; } - iterator operator--(int) { return iterator(it_--); } + iterator& operator++() { + ++it_; + return *this; + } + iterator operator++(int) { return iterator(it_++); } + iterator& operator--() { + --it_; + return *this; + } + iterator operator--(int) { return iterator(it_--); } // equality_comparable bool operator==(const iterator& x) const { return it_ == x.it_; } @@ -2363,7 +2325,7 @@ class RepeatedPtrIterator difference_type operator-(const iterator& x) const { return it_ - x.it_; } private: - template<typename OtherElement> + template <typename OtherElement> friend class RepeatedPtrIterator; // The internal iterator. @@ -2378,34 +2340,33 @@ class RepeatedPtrIterator // referenced by the iterator. It should either be "void *" for a mutable // iterator, or "const void* const" for a constant iterator. template <typename Element, typename VoidPtr> -class RepeatedPtrOverPtrsIterator - : public std::iterator<std::random_access_iterator_tag, Element> { +class RepeatedPtrOverPtrsIterator { public: - typedef RepeatedPtrOverPtrsIterator<Element, VoidPtr> iterator; - typedef std::iterator<std::random_access_iterator_tag, Element> superclass; - - // Shadow the value_type in std::iterator<> because const_iterator::value_type - // needs to be T, not const T. - typedef typename std::remove_const<Element>::type value_type; - - // Let the compiler know that these are type names, so we don't have to - // write "typename" in front of them everywhere. - typedef typename superclass::reference reference; - typedef typename superclass::pointer pointer; - typedef typename superclass::difference_type difference_type; + using iterator = RepeatedPtrOverPtrsIterator<Element, VoidPtr>; + using iterator_category = std::random_access_iterator_tag; + using value_type = typename std::remove_const<Element>::type; + using difference_type = std::ptrdiff_t; + using pointer = Element*; + using reference = Element&; RepeatedPtrOverPtrsIterator() : it_(NULL) {} explicit RepeatedPtrOverPtrsIterator(VoidPtr* it) : it_(it) {} // dereferenceable reference operator*() const { return *reinterpret_cast<Element*>(it_); } - pointer operator->() const { return &(operator*()); } + pointer operator->() const { return &(operator*()); } // {inc,dec}rementable - iterator& operator++() { ++it_; return *this; } - iterator operator++(int) { return iterator(it_++); } - iterator& operator--() { --it_; return *this; } - iterator operator--(int) { return iterator(it_--); } + iterator& operator++() { + ++it_; + return *this; + } + iterator operator++(int) { return iterator(it_++); } + iterator& operator--() { + --it_; + return *this; + } + iterator operator--(int) { return iterator(it_--); } // equality_comparable bool operator==(const iterator& x) const { return it_ == x.it_; } @@ -2446,7 +2407,7 @@ class RepeatedPtrOverPtrsIterator difference_type operator-(const iterator& x) const { return it_ - x.it_; } private: - template<typename OtherElement> + template <typename OtherElement> friend class RepeatedPtrIterator; // The internal iterator. @@ -2528,23 +2489,19 @@ RepeatedPtrField<Element>::pointer_end() const { namespace internal { // A back inserter for RepeatedField objects. -template<typename T> class RepeatedFieldBackInsertIterator +template <typename T> +class RepeatedFieldBackInsertIterator : public std::iterator<std::output_iterator_tag, T> { public: explicit RepeatedFieldBackInsertIterator( RepeatedField<T>* const mutable_field) - : field_(mutable_field) { - } + : field_(mutable_field) {} RepeatedFieldBackInsertIterator<T>& operator=(const T& value) { field_->Add(value); return *this; } - RepeatedFieldBackInsertIterator<T>& operator*() { - return *this; - } - RepeatedFieldBackInsertIterator<T>& operator++() { - return *this; - } + RepeatedFieldBackInsertIterator<T>& operator*() { return *this; } + RepeatedFieldBackInsertIterator<T>& operator++() { return *this; } RepeatedFieldBackInsertIterator<T>& operator++(int /* unused */) { return *this; } @@ -2554,13 +2511,12 @@ template<typename T> class RepeatedFieldBackInsertIterator }; // A back inserter for RepeatedPtrField objects. -template<typename T> class RepeatedPtrFieldBackInsertIterator +template <typename T> +class RepeatedPtrFieldBackInsertIterator : public std::iterator<std::output_iterator_tag, T> { public: - RepeatedPtrFieldBackInsertIterator( - RepeatedPtrField<T>* const mutable_field) - : field_(mutable_field) { - } + RepeatedPtrFieldBackInsertIterator(RepeatedPtrField<T>* const mutable_field) + : field_(mutable_field) {} RepeatedPtrFieldBackInsertIterator<T>& operator=(const T& value) { *field_->Add() = value; return *this; @@ -2574,12 +2530,8 @@ template<typename T> class RepeatedPtrFieldBackInsertIterator *field_->Add() = std::move(value); return *this; } - RepeatedPtrFieldBackInsertIterator<T>& operator*() { - return *this; - } - RepeatedPtrFieldBackInsertIterator<T>& operator++() { - return *this; - } + RepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; } + RepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; } RepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) { return *this; } @@ -2590,26 +2542,21 @@ template<typename T> class RepeatedPtrFieldBackInsertIterator // A back inserter for RepeatedPtrFields that inserts by transferring ownership // of a pointer. -template<typename T> class AllocatedRepeatedPtrFieldBackInsertIterator +template <typename T> +class AllocatedRepeatedPtrFieldBackInsertIterator : public std::iterator<std::output_iterator_tag, T> { public: explicit AllocatedRepeatedPtrFieldBackInsertIterator( RepeatedPtrField<T>* const mutable_field) - : field_(mutable_field) { - } + : field_(mutable_field) {} AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=( T* const ptr_to_value) { field_->AddAllocated(ptr_to_value); return *this; } - AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() { - return *this; - } - AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() { - return *this; - } - AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++( - int /* unused */) { + AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; } + AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; } + AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) { return *this; } @@ -2619,7 +2566,7 @@ template<typename T> class AllocatedRepeatedPtrFieldBackInsertIterator // Almost identical to AllocatedRepeatedPtrFieldBackInsertIterator. This one // uses the UnsafeArenaAddAllocated instead. -template<typename T> +template <typename T> class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator : public std::iterator<std::output_iterator_tag, T> { public: @@ -2627,7 +2574,7 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator RepeatedPtrField<T>* const mutable_field) : field_(mutable_field) {} UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=( - T const* const ptr_to_value) { + T const* const ptr_to_value) { field_->UnsafeArenaAddAllocated(const_cast<T*>(ptr_to_value)); return *this; } @@ -2650,30 +2597,34 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator // Provides a back insert iterator for RepeatedField instances, // similar to std::back_inserter(). -template<typename T> internal::RepeatedFieldBackInsertIterator<T> -RepeatedFieldBackInserter(RepeatedField<T>* const mutable_field) { +template <typename T> +internal::RepeatedFieldBackInsertIterator<T> RepeatedFieldBackInserter( + RepeatedField<T>* const mutable_field) { return internal::RepeatedFieldBackInsertIterator<T>(mutable_field); } // Provides a back insert iterator for RepeatedPtrField instances, // similar to std::back_inserter(). -template<typename T> internal::RepeatedPtrFieldBackInsertIterator<T> -RepeatedPtrFieldBackInserter(RepeatedPtrField<T>* const mutable_field) { +template <typename T> +internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedPtrFieldBackInserter( + RepeatedPtrField<T>* const mutable_field) { return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field); } // Special back insert iterator for RepeatedPtrField instances, just in // case someone wants to write generic template code that can access both // RepeatedFields and RepeatedPtrFields using a common name. -template<typename T> internal::RepeatedPtrFieldBackInsertIterator<T> -RepeatedFieldBackInserter(RepeatedPtrField<T>* const mutable_field) { +template <typename T> +internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedFieldBackInserter( + RepeatedPtrField<T>* const mutable_field) { return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field); } // Provides a back insert iterator for RepeatedPtrField instances // similar to std::back_inserter() which transfers the ownership while // copying elements. -template<typename T> internal::AllocatedRepeatedPtrFieldBackInsertIterator<T> +template <typename T> +internal::AllocatedRepeatedPtrFieldBackInsertIterator<T> AllocatedRepeatedPtrFieldBackInserter( RepeatedPtrField<T>* const mutable_field) { return internal::AllocatedRepeatedPtrFieldBackInsertIterator<T>( diff --git a/src/google/protobuf/repeated_field_reflection_unittest.cc b/src/google/protobuf/repeated_field_reflection_unittest.cc index 6ec428e048..04991027ee 100644 --- a/src/google/protobuf/repeated_field_reflection_unittest.cc +++ b/src/google/protobuf/repeated_field_reflection_unittest.cc @@ -35,9 +35,9 @@ #include <google/protobuf/stubs/casts.h> #include <google/protobuf/stubs/stringprintf.h> -#include <google/protobuf/dynamic_message.h> -#include <google/protobuf/unittest.pb.h> #include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> +#include <google/protobuf/dynamic_message.h> #include <google/protobuf/reflection.h> #include <gtest/gtest.h> @@ -45,14 +45,12 @@ namespace google { namespace protobuf { using unittest::ForeignMessage; -using unittest::TestAllTypes; using unittest::TestAllExtensions; +using unittest::TestAllTypes; namespace { -static int Func(int i, int j) { - return i * j; -} +static int Func(int i, int j) { return i * j; } static std::string StrFunc(int i, int j) { std::string str; @@ -98,11 +96,10 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { const RepeatedPtrField<std::string>& rpf_string = refl->GetRepeatedPtrField<std::string>(message, fd_repeated_string); const RepeatedPtrField<ForeignMessage>& rpf_foreign_message = - refl->GetRepeatedPtrField<ForeignMessage>( - message, fd_repeated_foreign_message); + refl->GetRepeatedPtrField<ForeignMessage>(message, + fd_repeated_foreign_message); const RepeatedPtrField<Message>& rpf_message = - refl->GetRepeatedPtrField<Message>( - message, fd_repeated_foreign_message); + refl->GetRepeatedPtrField<Message>(message, fd_repeated_foreign_message); // Get mutable RepeatedPtrField objects for all fields of interest. RepeatedPtrField<std::string>* mrpf_string = @@ -111,8 +108,8 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { refl->MutableRepeatedPtrField<ForeignMessage>( &message, fd_repeated_foreign_message); RepeatedPtrField<Message>* mrpf_message = - refl->MutableRepeatedPtrField<Message>( - &message, fd_repeated_foreign_message); + refl->MutableRepeatedPtrField<Message>(&message, + fd_repeated_foreign_message); // Make sure we can do gets and sets through the Repeated[Ptr]Field objects. for (int i = 0; i < 10; ++i) { @@ -149,12 +146,13 @@ TEST(RepeatedFieldReflectionTest, RegularFields) { // Make sure types are checked correctly at runtime. const FieldDescriptor* fd_optional_int32 = desc->FindFieldByName("optional_int32"); - EXPECT_DEATH(refl->GetRepeatedField<int32>( - message, fd_optional_int32), "requires a repeated field"); - EXPECT_DEATH(refl->GetRepeatedField<double>( - message, fd_repeated_int32), "not the right type"); + EXPECT_DEATH(refl->GetRepeatedField<int32>(message, fd_optional_int32), + "requires a repeated field"); + EXPECT_DEATH(refl->GetRepeatedField<double>(message, fd_repeated_int32), + "not the right type"); EXPECT_DEATH(refl->GetRepeatedPtrField<TestAllTypes>( - message, fd_repeated_foreign_message), "wrong submessage type"); + message, fd_repeated_foreign_message), + "wrong submessage type"); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -165,8 +163,8 @@ TEST(RepeatedFieldReflectionTest, ExtensionFields) { const Descriptor* desc = extended_message.GetDescriptor(); for (int i = 0; i < 10; ++i) { - extended_message.AddExtension( - unittest::repeated_int64_extension, Func(i, 1)); + extended_message.AddExtension(unittest::repeated_int64_extension, + Func(i, 1)); } const FieldDescriptor* fd_repeated_int64_extension = @@ -177,15 +175,14 @@ TEST(RepeatedFieldReflectionTest, ExtensionFields) { refl->GetRepeatedField<int64>(extended_message, fd_repeated_int64_extension); - RepeatedField<int64>* mrf_int64_extension = - refl->MutableRepeatedField<int64>(&extended_message, - fd_repeated_int64_extension); + RepeatedField<int64>* mrf_int64_extension = refl->MutableRepeatedField<int64>( + &extended_message, fd_repeated_int64_extension); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i)); mrf_int64_extension->Set(i, Func(i, -1)); - EXPECT_EQ(Func(i, -1), - extended_message.GetExtension(unittest::repeated_int64_extension, i)); + EXPECT_EQ(Func(i, -1), extended_message.GetExtension( + unittest::repeated_int64_extension, i)); } } @@ -194,8 +191,8 @@ void TestRepeatedFieldRefIteratorForPrimitive( const Ref& handle, const MessageType& message, ValueType (MessageType::*GetFunc)(int) const) { int index = 0; - for (typename Ref::const_iterator it = handle.begin(); - it != handle.end(); ++it) { + for (typename Ref::const_iterator it = handle.begin(); it != handle.end(); + ++it) { EXPECT_EQ((message.*GetFunc)(index), *it); ++index; } @@ -248,11 +245,10 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { const RepeatedFieldRef<std::string> rf_string = refl->GetRepeatedFieldRef<std::string>(message, fd_repeated_string); const RepeatedFieldRef<ForeignMessage> rf_foreign_message = - refl->GetRepeatedFieldRef<ForeignMessage>( - message, fd_repeated_foreign_message); + refl->GetRepeatedFieldRef<ForeignMessage>(message, + fd_repeated_foreign_message); const RepeatedFieldRef<Message> rf_message = - refl->GetRepeatedFieldRef<Message>( - message, fd_repeated_foreign_message); + refl->GetRepeatedFieldRef<Message>(message, fd_repeated_foreign_message); // Get MutableRepeatedFieldRef objects for all fields of interest. const MutableRepeatedFieldRef<int32> mrf_int32 = @@ -266,8 +262,8 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { refl->GetMutableRepeatedFieldRef<ForeignMessage>( &message, fd_repeated_foreign_message); const MutableRepeatedFieldRef<Message> mrf_message = - refl->GetMutableRepeatedFieldRef<Message>( - &message, fd_repeated_foreign_message); + refl->GetMutableRepeatedFieldRef<Message>(&message, + fd_repeated_foreign_message); EXPECT_EQ(message.repeated_int32_size(), rf_int32.size()); EXPECT_EQ(message.repeated_int32_size(), mrf_int32.size()); @@ -275,8 +271,7 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(message.repeated_double_size(), mrf_double.size()); EXPECT_EQ(message.repeated_string_size(), rf_string.size()); EXPECT_EQ(message.repeated_string_size(), mrf_string.size()); - EXPECT_EQ(message.repeated_foreign_message_size(), - rf_foreign_message.size()); + EXPECT_EQ(message.repeated_foreign_message_size(), rf_foreign_message.size()); EXPECT_EQ(message.repeated_foreign_message_size(), mrf_foreign_message.size()); EXPECT_EQ(message.repeated_foreign_message_size(), rf_message.size()); @@ -301,16 +296,19 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { EXPECT_EQ(rf_string.Get(i), StrFunc(i, 5)); ForeignMessage scratch_space; EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); - EXPECT_EQ(down_cast<const ForeignMessage&>( - rf_message.Get(i, &scratch_space)).c(), Func(i, 6)); + EXPECT_EQ( + down_cast<const ForeignMessage&>(rf_message.Get(i, &scratch_space)).c(), + Func(i, 6)); // Check gets through mutable objects. EXPECT_EQ(mrf_int32.Get(i), Func(i, 1)); EXPECT_EQ(mrf_double.Get(i), Func(i, 2)); EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); - EXPECT_EQ(down_cast<const ForeignMessage&>( - mrf_message.Get(i, &scratch_space)).c(), Func(i, 6)); + EXPECT_EQ( + down_cast<const ForeignMessage&>(mrf_message.Get(i, &scratch_space)) + .c(), + Func(i, 6)); // Check sets through mutable objects. mrf_int32.Set(i, Func(i, -1)); @@ -431,12 +429,13 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) { // Make sure types are checked correctly at runtime. const FieldDescriptor* fd_optional_int32 = desc->FindFieldByName("optional_int32"); - EXPECT_DEATH(refl->GetRepeatedFieldRef<int32>( - message, fd_optional_int32), ""); - EXPECT_DEATH(refl->GetRepeatedFieldRef<double>( - message, fd_repeated_int32), ""); + EXPECT_DEATH(refl->GetRepeatedFieldRef<int32>(message, fd_optional_int32), + ""); + EXPECT_DEATH(refl->GetRepeatedFieldRef<double>(message, fd_repeated_int32), + ""); EXPECT_DEATH(refl->GetRepeatedFieldRef<TestAllTypes>( - message, fd_repeated_foreign_message), ""); + message, fd_repeated_foreign_message), + ""); #endif // PROTOBUF_HAS_DEATH_TEST } @@ -455,16 +454,14 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForEnums) { const RepeatedFieldRef<TestAllTypes::NestedEnum> enum_ref = refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>( message, fd_repeated_nested_enum); - const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> - mutable_enum_ref = + const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> mutable_enum_ref = refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>( &message, fd_repeated_nested_enum); const RepeatedFieldRef<int32> int32_ref = - refl->GetRepeatedFieldRef<int32>( - message, fd_repeated_nested_enum); + refl->GetRepeatedFieldRef<int32>(message, fd_repeated_nested_enum); const MutableRepeatedFieldRef<int32> mutable_int32_ref = - refl->GetMutableRepeatedFieldRef<int32>( - &message, fd_repeated_nested_enum); + refl->GetMutableRepeatedFieldRef<int32>(&message, + fd_repeated_nested_enum); EXPECT_EQ(message.repeated_nested_enum_size(), enum_ref.size()); EXPECT_EQ(message.repeated_nested_enum_size(), mutable_enum_ref.size()); @@ -539,8 +536,8 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) { const Descriptor* desc = extended_message.GetDescriptor(); for (int i = 0; i < 10; ++i) { - extended_message.AddExtension( - unittest::repeated_int64_extension, Func(i, 1)); + extended_message.AddExtension(unittest::repeated_int64_extension, + Func(i, 1)); } const FieldDescriptor* fd_repeated_int64_extension = @@ -549,17 +546,17 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) { const RepeatedFieldRef<int64> rf_int64_extension = refl->GetRepeatedFieldRef<int64>(extended_message, - fd_repeated_int64_extension); + fd_repeated_int64_extension); const MutableRepeatedFieldRef<int64> mrf_int64_extension = refl->GetMutableRepeatedFieldRef<int64>(&extended_message, - fd_repeated_int64_extension); + fd_repeated_int64_extension); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i)); mrf_int64_extension.Set(i, Func(i, -1)); - EXPECT_EQ(Func(i, -1), - extended_message.GetExtension(unittest::repeated_int64_extension, i)); + EXPECT_EQ(Func(i, -1), extended_message.GetExtension( + unittest::repeated_int64_extension, i)); } } @@ -610,24 +607,20 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message = refl->GetMutableRepeatedFieldRef<ForeignMessage>( &m0, fd_repeated_foreign_message); - const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> - mrf_nested_enum = + const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> mrf_nested_enum = refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>( &m0, fd_repeated_nested_enum); // Test MutableRepeatedRef::CopyFrom - mrf_int32.CopyFrom( - refl->GetRepeatedFieldRef<int32>(m1, fd_repeated_int32)); + mrf_int32.CopyFrom(refl->GetRepeatedFieldRef<int32>(m1, fd_repeated_int32)); mrf_double.CopyFrom( refl->GetRepeatedFieldRef<double>(m1, fd_repeated_double)); mrf_string.CopyFrom( refl->GetRepeatedFieldRef<std::string>(m1, fd_repeated_string)); - mrf_foreign_message.CopyFrom( - refl->GetRepeatedFieldRef<ForeignMessage>( - m1, fd_repeated_foreign_message)); - mrf_nested_enum.CopyFrom( - refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>( - m1, fd_repeated_nested_enum)); + mrf_foreign_message.CopyFrom(refl->GetRepeatedFieldRef<ForeignMessage>( + m1, fd_repeated_foreign_message)); + mrf_nested_enum.CopyFrom(refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>( + m1, fd_repeated_nested_enum)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 11), m0.repeated_int32(i)); EXPECT_EQ(Func(i, 12), m0.repeated_double(i)); @@ -637,18 +630,15 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { } // Test MutableRepeatedRef::MergeFrom - mrf_int32.MergeFrom( - refl->GetRepeatedFieldRef<int32>(m2, fd_repeated_int32)); + mrf_int32.MergeFrom(refl->GetRepeatedFieldRef<int32>(m2, fd_repeated_int32)); mrf_double.MergeFrom( refl->GetRepeatedFieldRef<double>(m2, fd_repeated_double)); mrf_string.MergeFrom( refl->GetRepeatedFieldRef<std::string>(m2, fd_repeated_string)); - mrf_foreign_message.MergeFrom( - refl->GetRepeatedFieldRef<ForeignMessage>( - m2, fd_repeated_foreign_message)); - mrf_nested_enum.MergeFrom( - refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>( - m2, fd_repeated_nested_enum)); + mrf_foreign_message.MergeFrom(refl->GetRepeatedFieldRef<ForeignMessage>( + m2, fd_repeated_foreign_message)); + mrf_nested_enum.MergeFrom(refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>( + m2, fd_repeated_nested_enum)); for (int i = 0; i < 10; ++i) { EXPECT_EQ(Func(i, 21), m0.repeated_int32(i + 10)); EXPECT_EQ(Func(i, 22), m0.repeated_double(i + 10)); @@ -665,9 +655,8 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { refl->GetMutableRepeatedFieldRef<double>(&m2, fd_repeated_double)); mrf_string.Swap( refl->GetMutableRepeatedFieldRef<std::string>(&m2, fd_repeated_string)); - mrf_foreign_message.Swap( - refl->GetMutableRepeatedFieldRef<ForeignMessage>( - &m2, fd_repeated_foreign_message)); + mrf_foreign_message.Swap(refl->GetMutableRepeatedFieldRef<ForeignMessage>( + &m2, fd_repeated_foreign_message)); mrf_nested_enum.Swap( refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>( &m2, fd_repeated_nested_enum)); @@ -710,12 +699,12 @@ TEST(RepeatedFieldReflectionTest, RepeatedFieldRefDynamicMessage) { const Reflection* refl = dynamic_message->GetReflection(); MutableRepeatedFieldRef<int32> rf_int32 = - refl->GetMutableRepeatedFieldRef<int32>( - dynamic_message.get(), fd_repeated_int32); + refl->GetMutableRepeatedFieldRef<int32>(dynamic_message.get(), + fd_repeated_int32); rf_int32.Add(1234); EXPECT_EQ(1, refl->FieldSize(*dynamic_message, fd_repeated_int32)); - EXPECT_EQ(1234, refl->GetRepeatedInt32(*dynamic_message, - fd_repeated_int32, 0)); + EXPECT_EQ(1234, + refl->GetRepeatedInt32(*dynamic_message, fd_repeated_int32, 0)); } } // namespace diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 25ee24c425..610cb91df4 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -629,27 +629,23 @@ TEST(RepeatedField, ExtractSubrange) { for (int start = 0; start < sz - num; ++start) { // Create RepeatedField with sz elements having values 0 through sz-1. RepeatedField<int32> field; - for (int i = 0; i < sz; ++i) - field.Add(i); + for (int i = 0; i < sz; ++i) field.Add(i); EXPECT_EQ(field.size(), sz); // Create a catcher array and call ExtractSubrange. int32 catcher[10]; - for (int i = 0; i < 10; ++i) - catcher[i] = -1; + for (int i = 0; i < 10; ++i) catcher[i] = -1; field.ExtractSubrange(start, num, catcher); // Does the resulting array have the right size? EXPECT_EQ(field.size(), sz - num); // Were the removed elements extracted into the catcher array? - for (int i = 0; i < num; ++i) - EXPECT_EQ(catcher[i], start + i); + for (int i = 0; i < num; ++i) EXPECT_EQ(catcher[i], start + i); EXPECT_EQ(catcher[num], -1); // Does the resulting array contain the right values? - for (int i = 0; i < start; ++i) - EXPECT_EQ(field.Get(i), i); + for (int i = 0; i < start; ++i) EXPECT_EQ(field.Get(i), i); for (int i = start; i < field.size(); ++i) EXPECT_EQ(field.Get(i), i + num); } @@ -1279,15 +1275,13 @@ TEST(RepeatedPtrField, ExtractSubrange) { field.AddAllocated(subject[i]); } EXPECT_EQ(field.size(), sz + extra); - for (int i = 0; i < extra; ++i) - field.RemoveLast(); + for (int i = 0; i < extra; ++i) field.RemoveLast(); EXPECT_EQ(field.size(), sz); EXPECT_EQ(field.ClearedCount(), extra); // Create a catcher array and call ExtractSubrange. std::string* catcher[10]; - for (int i = 0; i < 10; ++i) - catcher[i] = NULL; + for (int i = 0; i < 10; ++i) catcher[i] = NULL; field.ExtractSubrange(start, num, catcher); // Does the resulting array have the right size? @@ -1306,8 +1300,7 @@ TEST(RepeatedPtrField, ExtractSubrange) { // Reinstate the cleared elements. EXPECT_EQ(field.ClearedCount(), extra); - for (int i = 0; i < extra; ++i) - field.Add(); + for (int i = 0; i < extra; ++i) field.Add(); EXPECT_EQ(field.ClearedCount(), 0); EXPECT_EQ(field.size(), sz - num + extra); @@ -1315,15 +1308,13 @@ TEST(RepeatedPtrField, ExtractSubrange) { for (int i = sz; i < sz + extra; ++i) { int count = 0; for (int j = sz; j < sz + extra; ++j) { - if (field.Mutable(j - num) == subject[i]) - count += 1; + if (field.Mutable(j - num) == subject[i]) count += 1; } EXPECT_EQ(count, 1); } // Release the caught elements. - for (int i = 0; i < num; ++i) - delete catcher[i]; + for (int i = 0; i < num; ++i) delete catcher[i]; } } } @@ -1719,7 +1710,6 @@ TEST_F(RepeatedPtrFieldPtrsIteratorTest, Sort) { EXPECT_EQ("y", proto_array_.Get(9)); } - // ----------------------------------------------------------------------------- // Unit-tests for the insert iterators // google::protobuf::RepeatedFieldBackInserter, @@ -1785,8 +1775,7 @@ class RepeatedFieldInsertionIteratorsTest : public testing::Test { }; TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) { - EXPECT_TRUE(std::equal(fibonacci.begin(), - fibonacci.end(), + EXPECT_TRUE(std::equal(fibonacci.begin(), fibonacci.end(), protobuffer.repeated_int32().begin())); EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(), protobuffer.repeated_int32().end(), @@ -1794,12 +1783,10 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) { } TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) { - EXPECT_TRUE(std::equal(halves.begin(), - halves.end(), + EXPECT_TRUE(std::equal(halves.begin(), halves.end(), protobuffer.repeated_double().begin())); EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(), - protobuffer.repeated_double().end(), - halves.begin())); + protobuffer.repeated_double().end(), halves.begin())); } TEST_F(RepeatedFieldInsertionIteratorsTest, Words) { @@ -1817,8 +1804,9 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) { words.push_back("six"); words.push_back("pence"); protobuffer.mutable_repeated_string()->Clear(); - std::copy(words.begin(), words.end(), RepeatedPtrFieldBackInserter( - protobuffer.mutable_repeated_string())); + std::copy( + words.begin(), words.end(), + RepeatedPtrFieldBackInserter(protobuffer.mutable_repeated_string())); ASSERT_EQ(words.size(), protobuffer.repeated_string_size()); for (int i = 0; i < words.size(); ++i) EXPECT_EQ(words.at(i), protobuffer.repeated_string(i)); @@ -1864,8 +1852,9 @@ TEST_F(RepeatedFieldInsertionIteratorsTest, *new_data = "name-" + StrCat(i); } TestAllTypes testproto; - std::copy(data.begin(), data.end(), AllocatedRepeatedPtrFieldBackInserter( - testproto.mutable_repeated_string())); + std::copy(data.begin(), data.end(), + AllocatedRepeatedPtrFieldBackInserter( + testproto.mutable_repeated_string())); EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString()); } diff --git a/src/google/protobuf/service.cc b/src/google/protobuf/service.cc index a6616b40cf..53945684cb 100644 --- a/src/google/protobuf/service.cc +++ b/src/google/protobuf/service.cc @@ -43,4 +43,3 @@ RpcController::~RpcController() {} } // namespace protobuf } // namespace google - diff --git a/src/google/protobuf/service.h b/src/google/protobuf/service.h index a9339eb885..d6b4ab82e7 100644 --- a/src/google/protobuf/service.h +++ b/src/google/protobuf/service.h @@ -119,10 +119,10 @@ class RpcController; class RpcChannel; // Defined in other files. -class Descriptor; // descriptor.h -class ServiceDescriptor; // descriptor.h -class MethodDescriptor; // descriptor.h -class Message; // message.h +class Descriptor; // descriptor.h +class ServiceDescriptor; // descriptor.h +class MethodDescriptor; // descriptor.h +class Message; // message.h // Abstract base interface for protocol-buffer-based RPC services. Services // themselves are abstract interfaces (implemented either by servers or as @@ -137,10 +137,7 @@ class PROTOBUF_EXPORT Service { // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second // parameter to the constructor to tell it to delete its RpcChannel when // destroyed. - enum ChannelOwnership { - STUB_OWNS_CHANNEL, - STUB_DOESNT_OWN_CHANNEL - }; + enum ChannelOwnership { STUB_OWNS_CHANNEL, STUB_DOESNT_OWN_CHANNEL }; // Get the ServiceDescriptor describing this service and its methods. virtual const ServiceDescriptor* GetDescriptor() = 0; @@ -171,10 +168,8 @@ class PROTOBUF_EXPORT Service { // RpcController can be queried to determine if an error occurred and // possibly to get more information about the error. virtual void CallMethod(const MethodDescriptor* method, - RpcController* controller, - const Message* request, - Message* response, - Closure* done) = 0; + RpcController* controller, const Message* request, + Message* response, Closure* done) = 0; // CallMethod() requires that the request and response passed in are of a // particular subclass of Message. GetRequestPrototype() and @@ -190,9 +185,9 @@ class PROTOBUF_EXPORT Service { // request->ParseFromString(input); // service->CallMethod(method, *request, response, callback); virtual const Message& GetRequestPrototype( - const MethodDescriptor* method) const = 0; + const MethodDescriptor* method) const = 0; virtual const Message& GetResponsePrototype( - const MethodDescriptor* method) const = 0; + const MethodDescriptor* method) const = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Service); @@ -283,10 +278,8 @@ class PROTOBUF_EXPORT RpcChannel { // need not be of any specific class as long as their descriptors are // method->input_type() and method->output_type(). virtual void CallMethod(const MethodDescriptor* method, - RpcController* controller, - const Message* request, - Message* response, - Closure* done) = 0; + RpcController* controller, const Message* request, + Message* response, Closure* done) = 0; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel); diff --git a/src/google/protobuf/source_context.pb.cc b/src/google/protobuf/source_context.pb.cc index 477091b93a..1f66be5a58 100644 --- a/src/google/protobuf/source_context.pb.cc +++ b/src/google/protobuf/source_context.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsSourceContext_google_2fprotobuf_2fsource_5fcontext_2epro PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsSourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fsource_5fcontext_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fsource_5fcontext_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fsource_5fcontext_2eproto = nullptr; @@ -76,8 +72,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eprot "pesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto = { - false, InitDefaults_google_2fprotobuf_2fsource_5fcontext_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fsource_5fcontext_2eproto, "google/protobuf/source_context.proto", &assign_descriptors_table_google_2fprotobuf_2fsource_5fcontext_2eproto, 251, }; @@ -85,6 +80,7 @@ void AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SourceContext_google_2fprotobuf_2fsource_5fcontext_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fsource_5fcontext_2eproto, deps, 0); } @@ -156,31 +152,37 @@ void SourceContext::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* SourceContext::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string file_name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_file_name(), ptr, ctx, "google.protobuf.SourceContext.file_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool SourceContext::MergePartialFromCodedStream( diff --git a/src/google/protobuf/struct.pb.cc b/src/google/protobuf/struct.pb.cc index aed2db90a8..a34cac3980 100644 --- a/src/google/protobuf/struct.pb.cc +++ b/src/google/protobuf/struct.pb.cc @@ -72,10 +72,6 @@ static void InitDefaultsListValue_google_2fprotobuf_2fstruct_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsListValue_google_2fprotobuf_2fstruct_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fstruct_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fstruct_2eproto[4]; static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2fstruct_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fstruct_2eproto = nullptr; @@ -155,8 +151,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto[] = "3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fstruct_2eproto = { - false, InitDefaults_google_2fprotobuf_2fstruct_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fstruct_2eproto, "google/protobuf/struct.proto", &assign_descriptors_table_google_2fprotobuf_2fstruct_2eproto, 641, }; @@ -164,6 +159,7 @@ void AddDescriptors_google_2fprotobuf_2fstruct_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ListValue_google_2fprotobuf_2fstruct_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fstruct_2eproto, deps, 0); } @@ -276,35 +272,43 @@ void Struct::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Struct::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // map<string, .google.protobuf.Value> fields = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(&fields_, ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Struct::MergePartialFromCodedStream( @@ -776,17 +780,18 @@ void Value::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // .google.protobuf.NullValue null_value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_null_value(static_cast<PROTOBUF_NAMESPACE_ID::NullValue>(val)); break; } @@ -801,43 +806,48 @@ const char* Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_string_value(), ptr, ctx, "google.protobuf.Value.string_value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // bool bool_value = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 32) goto handle_unusual; set_bool_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.Struct struct_value = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; ptr = ctx->ParseMessage(mutable_struct_value(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.ListValue list_value = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; ptr = ctx->ParseMessage(mutable_list_value(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Value::MergePartialFromCodedStream( @@ -1293,35 +1303,43 @@ void ListValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* ListValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // repeated .google.protobuf.Value values = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_values(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 10 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 10) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool ListValue::MergePartialFromCodedStream( diff --git a/src/google/protobuf/struct.pb.h b/src/google/protobuf/struct.pb.h index c4979a7a5f..6e81a09b1a 100644 --- a/src/google/protobuf/struct.pb.h +++ b/src/google/protobuf/struct.pb.h @@ -90,9 +90,13 @@ constexpr NullValue NullValue_MAX = NULL_VALUE; constexpr int NullValue_ARRAYSIZE = NullValue_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* NullValue_descriptor(); -inline const std::string& NullValue_Name(NullValue value) { +template<typename T> +inline const std::string& NullValue_Name(T enum_t_value) { + static_assert(::std::is_same<T, NullValue>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function NullValue_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - NullValue_descriptor(), value); + NullValue_descriptor(), enum_t_value); } inline bool NullValue_Parse( const std::string& name, NullValue* value) { @@ -116,10 +120,10 @@ public: Struct_FieldsEntry_DoNotUse(::PROTOBUF_NAMESPACE_ID::Arena* arena); void MergeFrom(const Struct_FieldsEntry_DoNotUse& other); static const Struct_FieldsEntry_DoNotUse* internal_default_instance() { return reinterpret_cast<const Struct_FieldsEntry_DoNotUse*>(&_Struct_FieldsEntry_DoNotUse_default_instance_); } - bool ValidateKey() const { - return ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(key().data(), key().size(), ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Struct.FieldsEntry.key"); + static bool ValidateKey(std::string* s) { + return ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(s->data(), s->size(), ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::PARSE, "google.protobuf.Struct.FieldsEntry.key"); } - bool ValidateValue() const { return true; } + static bool ValidateValue(void*) { return true; } void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& other) final; ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const; }; diff --git a/src/google/protobuf/struct.proto b/src/google/protobuf/struct.proto index 7d7808e7fb..ed990e31d9 100644 --- a/src/google/protobuf/struct.proto +++ b/src/google/protobuf/struct.proto @@ -40,7 +40,6 @@ option java_outer_classname = "StructProto"; option java_multiple_files = true; option objc_class_prefix = "GPB"; - // `Struct` represents a structured data value, consisting of fields // which map to dynamically typed values. In some languages, `Struct` // might be supported by a native representation. For example, in diff --git a/src/google/protobuf/test_util_lite.cc b/src/google/protobuf/test_util_lite.cc index 81728dfc3f..a83d109298 100644 --- a/src/google/protobuf/test_util_lite.cc +++ b/src/google/protobuf/test_util_lite.cc @@ -42,112 +42,112 @@ namespace google { namespace protobuf { void TestUtilLite::SetAllFields(unittest::TestAllTypesLite* message) { - message->set_optional_int32 (101); - message->set_optional_int64 (102); - message->set_optional_uint32 (103); - message->set_optional_uint64 (104); - message->set_optional_sint32 (105); - message->set_optional_sint64 (106); - message->set_optional_fixed32 (107); - message->set_optional_fixed64 (108); + message->set_optional_int32(101); + message->set_optional_int64(102); + message->set_optional_uint32(103); + message->set_optional_uint64(104); + message->set_optional_sint32(105); + message->set_optional_sint64(106); + message->set_optional_fixed32(107); + message->set_optional_fixed64(108); message->set_optional_sfixed32(109); message->set_optional_sfixed64(110); - message->set_optional_float (111); - message->set_optional_double (112); - message->set_optional_bool (true); - message->set_optional_string ("115"); - message->set_optional_bytes ("116"); - - message->mutable_optionalgroup ()->set_a(117); - message->mutable_optional_nested_message ()->set_bb(118); - message->mutable_optional_foreign_message ()->set_c(119); - message->mutable_optional_import_message ()->set_d(120); + message->set_optional_float(111); + message->set_optional_double(112); + message->set_optional_bool(true); + message->set_optional_string("115"); + message->set_optional_bytes("116"); + + message->mutable_optionalgroup()->set_a(117); + message->mutable_optional_nested_message()->set_bb(118); + message->mutable_optional_foreign_message()->set_c(119); + message->mutable_optional_import_message()->set_d(120); message->mutable_optional_public_import_message()->set_e(126); - message->mutable_optional_lazy_message ()->set_bb(127); + message->mutable_optional_lazy_message()->set_bb(127); - message->set_optional_nested_enum (unittest::TestAllTypesLite::BAZ ); - message->set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ ); - message->set_optional_import_enum (unittest_import::IMPORT_LITE_BAZ); + message->set_optional_nested_enum(unittest::TestAllTypesLite::BAZ); + message->set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ); + message->set_optional_import_enum(unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->add_repeated_int32 (201); - message->add_repeated_int64 (202); - message->add_repeated_uint32 (203); - message->add_repeated_uint64 (204); - message->add_repeated_sint32 (205); - message->add_repeated_sint64 (206); - message->add_repeated_fixed32 (207); - message->add_repeated_fixed64 (208); + message->add_repeated_int32(201); + message->add_repeated_int64(202); + message->add_repeated_uint32(203); + message->add_repeated_uint64(204); + message->add_repeated_sint32(205); + message->add_repeated_sint64(206); + message->add_repeated_fixed32(207); + message->add_repeated_fixed64(208); message->add_repeated_sfixed32(209); message->add_repeated_sfixed64(210); - message->add_repeated_float (211); - message->add_repeated_double (212); - message->add_repeated_bool (true); - message->add_repeated_string ("215"); - message->add_repeated_bytes ("216"); - - message->add_repeatedgroup ()->set_a(217); - message->add_repeated_nested_message ()->set_bb(218); + message->add_repeated_float(211); + message->add_repeated_double(212); + message->add_repeated_bool(true); + message->add_repeated_string("215"); + message->add_repeated_bytes("216"); + + message->add_repeatedgroup()->set_a(217); + message->add_repeated_nested_message()->set_bb(218); message->add_repeated_foreign_message()->set_c(219); - message->add_repeated_import_message ()->set_d(220); - message->add_repeated_lazy_message ()->set_bb(227); + message->add_repeated_import_message()->set_d(220); + message->add_repeated_lazy_message()->set_bb(227); - message->add_repeated_nested_enum (unittest::TestAllTypesLite::BAR ); - message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAR ); - message->add_repeated_import_enum (unittest_import::IMPORT_LITE_BAR); + message->add_repeated_nested_enum(unittest::TestAllTypesLite::BAR); + message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAR); + message->add_repeated_import_enum(unittest_import::IMPORT_LITE_BAR); // Add a second one of each field. - message->add_repeated_int32 (301); - message->add_repeated_int64 (302); - message->add_repeated_uint32 (303); - message->add_repeated_uint64 (304); - message->add_repeated_sint32 (305); - message->add_repeated_sint64 (306); - message->add_repeated_fixed32 (307); - message->add_repeated_fixed64 (308); + message->add_repeated_int32(301); + message->add_repeated_int64(302); + message->add_repeated_uint32(303); + message->add_repeated_uint64(304); + message->add_repeated_sint32(305); + message->add_repeated_sint64(306); + message->add_repeated_fixed32(307); + message->add_repeated_fixed64(308); message->add_repeated_sfixed32(309); message->add_repeated_sfixed64(310); - message->add_repeated_float (311); - message->add_repeated_double (312); - message->add_repeated_bool (false); - message->add_repeated_string ("315"); - message->add_repeated_bytes ("316"); - - message->add_repeatedgroup ()->set_a(317); - message->add_repeated_nested_message ()->set_bb(318); + message->add_repeated_float(311); + message->add_repeated_double(312); + message->add_repeated_bool(false); + message->add_repeated_string("315"); + message->add_repeated_bytes("316"); + + message->add_repeatedgroup()->set_a(317); + message->add_repeated_nested_message()->set_bb(318); message->add_repeated_foreign_message()->set_c(319); - message->add_repeated_import_message ()->set_d(320); - message->add_repeated_lazy_message ()->set_bb(327); + message->add_repeated_import_message()->set_d(320); + message->add_repeated_lazy_message()->set_bb(327); - message->add_repeated_nested_enum (unittest::TestAllTypesLite::BAZ ); - message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAZ ); - message->add_repeated_import_enum (unittest_import::IMPORT_LITE_BAZ); + message->add_repeated_nested_enum(unittest::TestAllTypesLite::BAZ); + message->add_repeated_foreign_enum(unittest::FOREIGN_LITE_BAZ); + message->add_repeated_import_enum(unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->set_default_int32 (401); - message->set_default_int64 (402); - message->set_default_uint32 (403); - message->set_default_uint64 (404); - message->set_default_sint32 (405); - message->set_default_sint64 (406); - message->set_default_fixed32 (407); - message->set_default_fixed64 (408); + message->set_default_int32(401); + message->set_default_int64(402); + message->set_default_uint32(403); + message->set_default_uint64(404); + message->set_default_sint32(405); + message->set_default_sint64(406); + message->set_default_fixed32(407); + message->set_default_fixed64(408); message->set_default_sfixed32(409); message->set_default_sfixed64(410); - message->set_default_float (411); - message->set_default_double (412); - message->set_default_bool (false); - message->set_default_string ("415"); - message->set_default_bytes ("416"); + message->set_default_float(411); + message->set_default_double(412); + message->set_default_bool(false); + message->set_default_string("415"); + message->set_default_bytes("416"); - message->set_default_nested_enum (unittest::TestAllTypesLite::FOO ); - message->set_default_foreign_enum(unittest::FOREIGN_LITE_FOO ); - message->set_default_import_enum (unittest_import::IMPORT_LITE_FOO); + message->set_default_nested_enum(unittest::TestAllTypesLite::FOO); + message->set_default_foreign_enum(unittest::FOREIGN_LITE_FOO); + message->set_default_import_enum(unittest_import::IMPORT_LITE_FOO); message->set_oneof_uint32(601); @@ -159,31 +159,31 @@ void TestUtilLite::SetAllFields(unittest::TestAllTypesLite* message) { // ------------------------------------------------------------------- void TestUtilLite::ModifyRepeatedFields(unittest::TestAllTypesLite* message) { - message->set_repeated_int32 (1, 501); - message->set_repeated_int64 (1, 502); - message->set_repeated_uint32 (1, 503); - message->set_repeated_uint64 (1, 504); - message->set_repeated_sint32 (1, 505); - message->set_repeated_sint64 (1, 506); - message->set_repeated_fixed32 (1, 507); - message->set_repeated_fixed64 (1, 508); + message->set_repeated_int32(1, 501); + message->set_repeated_int64(1, 502); + message->set_repeated_uint32(1, 503); + message->set_repeated_uint64(1, 504); + message->set_repeated_sint32(1, 505); + message->set_repeated_sint64(1, 506); + message->set_repeated_fixed32(1, 507); + message->set_repeated_fixed64(1, 508); message->set_repeated_sfixed32(1, 509); message->set_repeated_sfixed64(1, 510); - message->set_repeated_float (1, 511); - message->set_repeated_double (1, 512); - message->set_repeated_bool (1, true); - message->set_repeated_string (1, "515"); - message->set_repeated_bytes (1, "516"); - - message->mutable_repeatedgroup (1)->set_a(517); - message->mutable_repeated_nested_message (1)->set_bb(518); + message->set_repeated_float(1, 511); + message->set_repeated_double(1, 512); + message->set_repeated_bool(1, true); + message->set_repeated_string(1, "515"); + message->set_repeated_bytes(1, "516"); + + message->mutable_repeatedgroup(1)->set_a(517); + message->mutable_repeated_nested_message(1)->set_bb(518); message->mutable_repeated_foreign_message(1)->set_c(519); - message->mutable_repeated_import_message (1)->set_d(520); - message->mutable_repeated_lazy_message (1)->set_bb(527); + message->mutable_repeated_import_message(1)->set_d(520); + message->mutable_repeated_lazy_message(1)->set_bb(527); - message->set_repeated_nested_enum (1, unittest::TestAllTypesLite::FOO ); - message->set_repeated_foreign_enum(1, unittest::FOREIGN_LITE_FOO ); - message->set_repeated_import_enum (1, unittest_import::IMPORT_LITE_FOO); + message->set_repeated_nested_enum(1, unittest::TestAllTypesLite::FOO); + message->set_repeated_foreign_enum(1, unittest::FOREIGN_LITE_FOO); + message->set_repeated_import_enum(1, unittest_import::IMPORT_LITE_FOO); } @@ -191,199 +191,199 @@ void TestUtilLite::ModifyRepeatedFields(unittest::TestAllTypesLite* message) { void TestUtilLite::ExpectAllFieldsSet( const unittest::TestAllTypesLite& message) { - EXPECT_TRUE(message.has_optional_int32 ()); - EXPECT_TRUE(message.has_optional_int64 ()); - EXPECT_TRUE(message.has_optional_uint32 ()); - EXPECT_TRUE(message.has_optional_uint64 ()); - EXPECT_TRUE(message.has_optional_sint32 ()); - EXPECT_TRUE(message.has_optional_sint64 ()); - EXPECT_TRUE(message.has_optional_fixed32 ()); - EXPECT_TRUE(message.has_optional_fixed64 ()); + EXPECT_TRUE(message.has_optional_int32()); + EXPECT_TRUE(message.has_optional_int64()); + EXPECT_TRUE(message.has_optional_uint32()); + EXPECT_TRUE(message.has_optional_uint64()); + EXPECT_TRUE(message.has_optional_sint32()); + EXPECT_TRUE(message.has_optional_sint64()); + EXPECT_TRUE(message.has_optional_fixed32()); + EXPECT_TRUE(message.has_optional_fixed64()); EXPECT_TRUE(message.has_optional_sfixed32()); EXPECT_TRUE(message.has_optional_sfixed64()); - EXPECT_TRUE(message.has_optional_float ()); - EXPECT_TRUE(message.has_optional_double ()); - EXPECT_TRUE(message.has_optional_bool ()); - EXPECT_TRUE(message.has_optional_string ()); - EXPECT_TRUE(message.has_optional_bytes ()); - - EXPECT_TRUE(message.has_optionalgroup ()); - EXPECT_TRUE(message.has_optional_nested_message ()); - EXPECT_TRUE(message.has_optional_foreign_message ()); - EXPECT_TRUE(message.has_optional_import_message ()); + EXPECT_TRUE(message.has_optional_float()); + EXPECT_TRUE(message.has_optional_double()); + EXPECT_TRUE(message.has_optional_bool()); + EXPECT_TRUE(message.has_optional_string()); + EXPECT_TRUE(message.has_optional_bytes()); + + EXPECT_TRUE(message.has_optionalgroup()); + EXPECT_TRUE(message.has_optional_nested_message()); + EXPECT_TRUE(message.has_optional_foreign_message()); + EXPECT_TRUE(message.has_optional_import_message()); EXPECT_TRUE(message.has_optional_public_import_message()); - EXPECT_TRUE(message.has_optional_lazy_message ()); + EXPECT_TRUE(message.has_optional_lazy_message()); - EXPECT_TRUE(message.optionalgroup ().has_a()); - EXPECT_TRUE(message.optional_nested_message ().has_bb()); - EXPECT_TRUE(message.optional_foreign_message ().has_c()); - EXPECT_TRUE(message.optional_import_message ().has_d()); + EXPECT_TRUE(message.optionalgroup().has_a()); + EXPECT_TRUE(message.optional_nested_message().has_bb()); + EXPECT_TRUE(message.optional_foreign_message().has_c()); + EXPECT_TRUE(message.optional_import_message().has_d()); EXPECT_TRUE(message.optional_public_import_message().has_e()); - EXPECT_TRUE(message.optional_lazy_message ().has_bb()); + EXPECT_TRUE(message.optional_lazy_message().has_bb()); - EXPECT_TRUE(message.has_optional_nested_enum ()); + EXPECT_TRUE(message.has_optional_nested_enum()); EXPECT_TRUE(message.has_optional_foreign_enum()); - EXPECT_TRUE(message.has_optional_import_enum ()); - - - EXPECT_EQ(101 , message.optional_int32 ()); - EXPECT_EQ(102 , message.optional_int64 ()); - EXPECT_EQ(103 , message.optional_uint32 ()); - EXPECT_EQ(104 , message.optional_uint64 ()); - EXPECT_EQ(105 , message.optional_sint32 ()); - EXPECT_EQ(106 , message.optional_sint64 ()); - EXPECT_EQ(107 , message.optional_fixed32 ()); - EXPECT_EQ(108 , message.optional_fixed64 ()); - EXPECT_EQ(109 , message.optional_sfixed32()); - EXPECT_EQ(110 , message.optional_sfixed64()); - EXPECT_EQ(111 , message.optional_float ()); - EXPECT_EQ(112 , message.optional_double ()); - EXPECT_EQ(true , message.optional_bool ()); - EXPECT_EQ("115", message.optional_string ()); - EXPECT_EQ("116", message.optional_bytes ()); - - EXPECT_EQ(117, message.optionalgroup ().a()); - EXPECT_EQ(118, message.optional_nested_message ().bb()); - EXPECT_EQ(119, message.optional_foreign_message ().c()); - EXPECT_EQ(120, message.optional_import_message ().d()); + EXPECT_TRUE(message.has_optional_import_enum()); + + + EXPECT_EQ(101, message.optional_int32()); + EXPECT_EQ(102, message.optional_int64()); + EXPECT_EQ(103, message.optional_uint32()); + EXPECT_EQ(104, message.optional_uint64()); + EXPECT_EQ(105, message.optional_sint32()); + EXPECT_EQ(106, message.optional_sint64()); + EXPECT_EQ(107, message.optional_fixed32()); + EXPECT_EQ(108, message.optional_fixed64()); + EXPECT_EQ(109, message.optional_sfixed32()); + EXPECT_EQ(110, message.optional_sfixed64()); + EXPECT_EQ(111, message.optional_float()); + EXPECT_EQ(112, message.optional_double()); + EXPECT_EQ(true, message.optional_bool()); + EXPECT_EQ("115", message.optional_string()); + EXPECT_EQ("116", message.optional_bytes()); + + EXPECT_EQ(117, message.optionalgroup().a()); + EXPECT_EQ(118, message.optional_nested_message().bb()); + EXPECT_EQ(119, message.optional_foreign_message().c()); + EXPECT_EQ(120, message.optional_import_message().d()); EXPECT_EQ(126, message.optional_public_import_message().e()); - EXPECT_EQ(127, message.optional_lazy_message ().bb()); + EXPECT_EQ(127, message.optional_lazy_message().bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.optional_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.optional_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.optional_import_enum ()); + EXPECT_EQ(unittest::TestAllTypesLite::BAZ, message.optional_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.optional_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.optional_import_enum()); // ----------------------------------------------------------------- - ASSERT_EQ(2, message.repeated_int32_size ()); - ASSERT_EQ(2, message.repeated_int64_size ()); - ASSERT_EQ(2, message.repeated_uint32_size ()); - ASSERT_EQ(2, message.repeated_uint64_size ()); - ASSERT_EQ(2, message.repeated_sint32_size ()); - ASSERT_EQ(2, message.repeated_sint64_size ()); - ASSERT_EQ(2, message.repeated_fixed32_size ()); - ASSERT_EQ(2, message.repeated_fixed64_size ()); + ASSERT_EQ(2, message.repeated_int32_size()); + ASSERT_EQ(2, message.repeated_int64_size()); + ASSERT_EQ(2, message.repeated_uint32_size()); + ASSERT_EQ(2, message.repeated_uint64_size()); + ASSERT_EQ(2, message.repeated_sint32_size()); + ASSERT_EQ(2, message.repeated_sint64_size()); + ASSERT_EQ(2, message.repeated_fixed32_size()); + ASSERT_EQ(2, message.repeated_fixed64_size()); ASSERT_EQ(2, message.repeated_sfixed32_size()); ASSERT_EQ(2, message.repeated_sfixed64_size()); - ASSERT_EQ(2, message.repeated_float_size ()); - ASSERT_EQ(2, message.repeated_double_size ()); - ASSERT_EQ(2, message.repeated_bool_size ()); - ASSERT_EQ(2, message.repeated_string_size ()); - ASSERT_EQ(2, message.repeated_bytes_size ()); - - ASSERT_EQ(2, message.repeatedgroup_size ()); - ASSERT_EQ(2, message.repeated_nested_message_size ()); + ASSERT_EQ(2, message.repeated_float_size()); + ASSERT_EQ(2, message.repeated_double_size()); + ASSERT_EQ(2, message.repeated_bool_size()); + ASSERT_EQ(2, message.repeated_string_size()); + ASSERT_EQ(2, message.repeated_bytes_size()); + + ASSERT_EQ(2, message.repeatedgroup_size()); + ASSERT_EQ(2, message.repeated_nested_message_size()); ASSERT_EQ(2, message.repeated_foreign_message_size()); - ASSERT_EQ(2, message.repeated_import_message_size ()); - ASSERT_EQ(2, message.repeated_lazy_message_size ()); - ASSERT_EQ(2, message.repeated_nested_enum_size ()); - ASSERT_EQ(2, message.repeated_foreign_enum_size ()); - ASSERT_EQ(2, message.repeated_import_enum_size ()); - - - EXPECT_EQ(201 , message.repeated_int32 (0)); - EXPECT_EQ(202 , message.repeated_int64 (0)); - EXPECT_EQ(203 , message.repeated_uint32 (0)); - EXPECT_EQ(204 , message.repeated_uint64 (0)); - EXPECT_EQ(205 , message.repeated_sint32 (0)); - EXPECT_EQ(206 , message.repeated_sint64 (0)); - EXPECT_EQ(207 , message.repeated_fixed32 (0)); - EXPECT_EQ(208 , message.repeated_fixed64 (0)); - EXPECT_EQ(209 , message.repeated_sfixed32(0)); - EXPECT_EQ(210 , message.repeated_sfixed64(0)); - EXPECT_EQ(211 , message.repeated_float (0)); - EXPECT_EQ(212 , message.repeated_double (0)); - EXPECT_EQ(true , message.repeated_bool (0)); - EXPECT_EQ("215", message.repeated_string (0)); - EXPECT_EQ("216", message.repeated_bytes (0)); - - EXPECT_EQ(217, message.repeatedgroup (0).a()); - EXPECT_EQ(218, message.repeated_nested_message (0).bb()); + ASSERT_EQ(2, message.repeated_import_message_size()); + ASSERT_EQ(2, message.repeated_lazy_message_size()); + ASSERT_EQ(2, message.repeated_nested_enum_size()); + ASSERT_EQ(2, message.repeated_foreign_enum_size()); + ASSERT_EQ(2, message.repeated_import_enum_size()); + + + EXPECT_EQ(201, message.repeated_int32(0)); + EXPECT_EQ(202, message.repeated_int64(0)); + EXPECT_EQ(203, message.repeated_uint32(0)); + EXPECT_EQ(204, message.repeated_uint64(0)); + EXPECT_EQ(205, message.repeated_sint32(0)); + EXPECT_EQ(206, message.repeated_sint64(0)); + EXPECT_EQ(207, message.repeated_fixed32(0)); + EXPECT_EQ(208, message.repeated_fixed64(0)); + EXPECT_EQ(209, message.repeated_sfixed32(0)); + EXPECT_EQ(210, message.repeated_sfixed64(0)); + EXPECT_EQ(211, message.repeated_float(0)); + EXPECT_EQ(212, message.repeated_double(0)); + EXPECT_EQ(true, message.repeated_bool(0)); + EXPECT_EQ("215", message.repeated_string(0)); + EXPECT_EQ("216", message.repeated_bytes(0)); + + EXPECT_EQ(217, message.repeatedgroup(0).a()); + EXPECT_EQ(218, message.repeated_nested_message(0).bb()); EXPECT_EQ(219, message.repeated_foreign_message(0).c()); - EXPECT_EQ(220, message.repeated_import_message (0).d()); - EXPECT_EQ(227, message.repeated_lazy_message (0).bb()); - - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.repeated_nested_enum (0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.repeated_foreign_enum(0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum (0)); - - EXPECT_EQ(301 , message.repeated_int32 (1)); - EXPECT_EQ(302 , message.repeated_int64 (1)); - EXPECT_EQ(303 , message.repeated_uint32 (1)); - EXPECT_EQ(304 , message.repeated_uint64 (1)); - EXPECT_EQ(305 , message.repeated_sint32 (1)); - EXPECT_EQ(306 , message.repeated_sint64 (1)); - EXPECT_EQ(307 , message.repeated_fixed32 (1)); - EXPECT_EQ(308 , message.repeated_fixed64 (1)); - EXPECT_EQ(309 , message.repeated_sfixed32(1)); - EXPECT_EQ(310 , message.repeated_sfixed64(1)); - EXPECT_EQ(311 , message.repeated_float (1)); - EXPECT_EQ(312 , message.repeated_double (1)); - EXPECT_EQ(false, message.repeated_bool (1)); - EXPECT_EQ("315", message.repeated_string (1)); - EXPECT_EQ("316", message.repeated_bytes (1)); - - EXPECT_EQ(317, message.repeatedgroup (1).a()); - EXPECT_EQ(318, message.repeated_nested_message (1).bb()); + EXPECT_EQ(220, message.repeated_import_message(0).d()); + EXPECT_EQ(227, message.repeated_lazy_message(0).bb()); + + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.repeated_nested_enum(0)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.repeated_foreign_enum(0)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum(0)); + + EXPECT_EQ(301, message.repeated_int32(1)); + EXPECT_EQ(302, message.repeated_int64(1)); + EXPECT_EQ(303, message.repeated_uint32(1)); + EXPECT_EQ(304, message.repeated_uint64(1)); + EXPECT_EQ(305, message.repeated_sint32(1)); + EXPECT_EQ(306, message.repeated_sint64(1)); + EXPECT_EQ(307, message.repeated_fixed32(1)); + EXPECT_EQ(308, message.repeated_fixed64(1)); + EXPECT_EQ(309, message.repeated_sfixed32(1)); + EXPECT_EQ(310, message.repeated_sfixed64(1)); + EXPECT_EQ(311, message.repeated_float(1)); + EXPECT_EQ(312, message.repeated_double(1)); + EXPECT_EQ(false, message.repeated_bool(1)); + EXPECT_EQ("315", message.repeated_string(1)); + EXPECT_EQ("316", message.repeated_bytes(1)); + + EXPECT_EQ(317, message.repeatedgroup(1).a()); + EXPECT_EQ(318, message.repeated_nested_message(1).bb()); EXPECT_EQ(319, message.repeated_foreign_message(1).c()); - EXPECT_EQ(320, message.repeated_import_message (1).d()); - EXPECT_EQ(327, message.repeated_lazy_message (1).bb()); + EXPECT_EQ(320, message.repeated_import_message(1).d()); + EXPECT_EQ(327, message.repeated_lazy_message(1).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.repeated_nested_enum (1)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.repeated_foreign_enum(1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.repeated_import_enum (1)); + EXPECT_EQ(unittest::TestAllTypesLite::BAZ, message.repeated_nested_enum(1)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.repeated_foreign_enum(1)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.repeated_import_enum(1)); // ----------------------------------------------------------------- - EXPECT_TRUE(message.has_default_int32 ()); - EXPECT_TRUE(message.has_default_int64 ()); - EXPECT_TRUE(message.has_default_uint32 ()); - EXPECT_TRUE(message.has_default_uint64 ()); - EXPECT_TRUE(message.has_default_sint32 ()); - EXPECT_TRUE(message.has_default_sint64 ()); - EXPECT_TRUE(message.has_default_fixed32 ()); - EXPECT_TRUE(message.has_default_fixed64 ()); + EXPECT_TRUE(message.has_default_int32()); + EXPECT_TRUE(message.has_default_int64()); + EXPECT_TRUE(message.has_default_uint32()); + EXPECT_TRUE(message.has_default_uint64()); + EXPECT_TRUE(message.has_default_sint32()); + EXPECT_TRUE(message.has_default_sint64()); + EXPECT_TRUE(message.has_default_fixed32()); + EXPECT_TRUE(message.has_default_fixed64()); EXPECT_TRUE(message.has_default_sfixed32()); EXPECT_TRUE(message.has_default_sfixed64()); - EXPECT_TRUE(message.has_default_float ()); - EXPECT_TRUE(message.has_default_double ()); - EXPECT_TRUE(message.has_default_bool ()); - EXPECT_TRUE(message.has_default_string ()); - EXPECT_TRUE(message.has_default_bytes ()); + EXPECT_TRUE(message.has_default_float()); + EXPECT_TRUE(message.has_default_double()); + EXPECT_TRUE(message.has_default_bool()); + EXPECT_TRUE(message.has_default_string()); + EXPECT_TRUE(message.has_default_bytes()); - EXPECT_TRUE(message.has_default_nested_enum ()); + EXPECT_TRUE(message.has_default_nested_enum()); EXPECT_TRUE(message.has_default_foreign_enum()); - EXPECT_TRUE(message.has_default_import_enum ()); - - - EXPECT_EQ(401 , message.default_int32 ()); - EXPECT_EQ(402 , message.default_int64 ()); - EXPECT_EQ(403 , message.default_uint32 ()); - EXPECT_EQ(404 , message.default_uint64 ()); - EXPECT_EQ(405 , message.default_sint32 ()); - EXPECT_EQ(406 , message.default_sint64 ()); - EXPECT_EQ(407 , message.default_fixed32 ()); - EXPECT_EQ(408 , message.default_fixed64 ()); - EXPECT_EQ(409 , message.default_sfixed32()); - EXPECT_EQ(410 , message.default_sfixed64()); - EXPECT_EQ(411 , message.default_float ()); - EXPECT_EQ(412 , message.default_double ()); - EXPECT_EQ(false, message.default_bool ()); - EXPECT_EQ("415", message.default_string ()); - EXPECT_EQ("416", message.default_bytes ()); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.default_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.default_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.default_import_enum ()); - - - EXPECT_FALSE(message.has_oneof_uint32 ()); + EXPECT_TRUE(message.has_default_import_enum()); + + + EXPECT_EQ(401, message.default_int32()); + EXPECT_EQ(402, message.default_int64()); + EXPECT_EQ(403, message.default_uint32()); + EXPECT_EQ(404, message.default_uint64()); + EXPECT_EQ(405, message.default_sint32()); + EXPECT_EQ(406, message.default_sint64()); + EXPECT_EQ(407, message.default_fixed32()); + EXPECT_EQ(408, message.default_fixed64()); + EXPECT_EQ(409, message.default_sfixed32()); + EXPECT_EQ(410, message.default_sfixed64()); + EXPECT_EQ(411, message.default_float()); + EXPECT_EQ(412, message.default_double()); + EXPECT_EQ(false, message.default_bool()); + EXPECT_EQ("415", message.default_string()); + EXPECT_EQ("416", message.default_bytes()); + + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.default_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.default_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.default_import_enum()); + + + EXPECT_FALSE(message.has_oneof_uint32()); EXPECT_FALSE(message.has_oneof_nested_message()); - EXPECT_FALSE(message.has_oneof_string ()); - EXPECT_TRUE(message.has_oneof_bytes ()); + EXPECT_FALSE(message.has_oneof_string()); + EXPECT_TRUE(message.has_oneof_bytes()); EXPECT_EQ("604", message.oneof_bytes()); } @@ -392,145 +392,145 @@ void TestUtilLite::ExpectAllFieldsSet( void TestUtilLite::ExpectClear(const unittest::TestAllTypesLite& message) { // has_blah() should initially be false for all optional fields. - EXPECT_FALSE(message.has_optional_int32 ()); - EXPECT_FALSE(message.has_optional_int64 ()); - EXPECT_FALSE(message.has_optional_uint32 ()); - EXPECT_FALSE(message.has_optional_uint64 ()); - EXPECT_FALSE(message.has_optional_sint32 ()); - EXPECT_FALSE(message.has_optional_sint64 ()); - EXPECT_FALSE(message.has_optional_fixed32 ()); - EXPECT_FALSE(message.has_optional_fixed64 ()); + EXPECT_FALSE(message.has_optional_int32()); + EXPECT_FALSE(message.has_optional_int64()); + EXPECT_FALSE(message.has_optional_uint32()); + EXPECT_FALSE(message.has_optional_uint64()); + EXPECT_FALSE(message.has_optional_sint32()); + EXPECT_FALSE(message.has_optional_sint64()); + EXPECT_FALSE(message.has_optional_fixed32()); + EXPECT_FALSE(message.has_optional_fixed64()); EXPECT_FALSE(message.has_optional_sfixed32()); EXPECT_FALSE(message.has_optional_sfixed64()); - EXPECT_FALSE(message.has_optional_float ()); - EXPECT_FALSE(message.has_optional_double ()); - EXPECT_FALSE(message.has_optional_bool ()); - EXPECT_FALSE(message.has_optional_string ()); - EXPECT_FALSE(message.has_optional_bytes ()); - - EXPECT_FALSE(message.has_optionalgroup ()); - EXPECT_FALSE(message.has_optional_nested_message ()); - EXPECT_FALSE(message.has_optional_foreign_message ()); - EXPECT_FALSE(message.has_optional_import_message ()); + EXPECT_FALSE(message.has_optional_float()); + EXPECT_FALSE(message.has_optional_double()); + EXPECT_FALSE(message.has_optional_bool()); + EXPECT_FALSE(message.has_optional_string()); + EXPECT_FALSE(message.has_optional_bytes()); + + EXPECT_FALSE(message.has_optionalgroup()); + EXPECT_FALSE(message.has_optional_nested_message()); + EXPECT_FALSE(message.has_optional_foreign_message()); + EXPECT_FALSE(message.has_optional_import_message()); EXPECT_FALSE(message.has_optional_public_import_message()); - EXPECT_FALSE(message.has_optional_lazy_message ()); + EXPECT_FALSE(message.has_optional_lazy_message()); - EXPECT_FALSE(message.has_optional_nested_enum ()); + EXPECT_FALSE(message.has_optional_nested_enum()); EXPECT_FALSE(message.has_optional_foreign_enum()); - EXPECT_FALSE(message.has_optional_import_enum ()); + EXPECT_FALSE(message.has_optional_import_enum()); // Optional fields without defaults are set to zero or something like it. - EXPECT_EQ(0 , message.optional_int32 ()); - EXPECT_EQ(0 , message.optional_int64 ()); - EXPECT_EQ(0 , message.optional_uint32 ()); - EXPECT_EQ(0 , message.optional_uint64 ()); - EXPECT_EQ(0 , message.optional_sint32 ()); - EXPECT_EQ(0 , message.optional_sint64 ()); - EXPECT_EQ(0 , message.optional_fixed32 ()); - EXPECT_EQ(0 , message.optional_fixed64 ()); - EXPECT_EQ(0 , message.optional_sfixed32()); - EXPECT_EQ(0 , message.optional_sfixed64()); - EXPECT_EQ(0 , message.optional_float ()); - EXPECT_EQ(0 , message.optional_double ()); - EXPECT_EQ(false, message.optional_bool ()); - EXPECT_EQ("" , message.optional_string ()); - EXPECT_EQ("" , message.optional_bytes ()); + EXPECT_EQ(0, message.optional_int32()); + EXPECT_EQ(0, message.optional_int64()); + EXPECT_EQ(0, message.optional_uint32()); + EXPECT_EQ(0, message.optional_uint64()); + EXPECT_EQ(0, message.optional_sint32()); + EXPECT_EQ(0, message.optional_sint64()); + EXPECT_EQ(0, message.optional_fixed32()); + EXPECT_EQ(0, message.optional_fixed64()); + EXPECT_EQ(0, message.optional_sfixed32()); + EXPECT_EQ(0, message.optional_sfixed64()); + EXPECT_EQ(0, message.optional_float()); + EXPECT_EQ(0, message.optional_double()); + EXPECT_EQ(false, message.optional_bool()); + EXPECT_EQ("", message.optional_string()); + EXPECT_EQ("", message.optional_bytes()); // Embedded messages should also be clear. - EXPECT_FALSE(message.optionalgroup ().has_a()); - EXPECT_FALSE(message.optional_nested_message ().has_bb()); - EXPECT_FALSE(message.optional_foreign_message ().has_c()); - EXPECT_FALSE(message.optional_import_message ().has_d()); + EXPECT_FALSE(message.optionalgroup().has_a()); + EXPECT_FALSE(message.optional_nested_message().has_bb()); + EXPECT_FALSE(message.optional_foreign_message().has_c()); + EXPECT_FALSE(message.optional_import_message().has_d()); EXPECT_FALSE(message.optional_public_import_message().has_e()); - EXPECT_FALSE(message.optional_lazy_message ().has_bb()); + EXPECT_FALSE(message.optional_lazy_message().has_bb()); - EXPECT_EQ(0, message.optionalgroup ().a()); - EXPECT_EQ(0, message.optional_nested_message ().bb()); + EXPECT_EQ(0, message.optionalgroup().a()); + EXPECT_EQ(0, message.optional_nested_message().bb()); EXPECT_EQ(0, message.optional_foreign_message().c()); - EXPECT_EQ(0, message.optional_import_message ().d()); + EXPECT_EQ(0, message.optional_import_message().d()); // Enums without defaults are set to the first value in the enum. - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.optional_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.optional_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.optional_import_enum ()); + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.optional_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.optional_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.optional_import_enum()); // Repeated fields are empty. - EXPECT_EQ(0, message.repeated_int32_size ()); - EXPECT_EQ(0, message.repeated_int64_size ()); - EXPECT_EQ(0, message.repeated_uint32_size ()); - EXPECT_EQ(0, message.repeated_uint64_size ()); - EXPECT_EQ(0, message.repeated_sint32_size ()); - EXPECT_EQ(0, message.repeated_sint64_size ()); - EXPECT_EQ(0, message.repeated_fixed32_size ()); - EXPECT_EQ(0, message.repeated_fixed64_size ()); + EXPECT_EQ(0, message.repeated_int32_size()); + EXPECT_EQ(0, message.repeated_int64_size()); + EXPECT_EQ(0, message.repeated_uint32_size()); + EXPECT_EQ(0, message.repeated_uint64_size()); + EXPECT_EQ(0, message.repeated_sint32_size()); + EXPECT_EQ(0, message.repeated_sint64_size()); + EXPECT_EQ(0, message.repeated_fixed32_size()); + EXPECT_EQ(0, message.repeated_fixed64_size()); EXPECT_EQ(0, message.repeated_sfixed32_size()); EXPECT_EQ(0, message.repeated_sfixed64_size()); - EXPECT_EQ(0, message.repeated_float_size ()); - EXPECT_EQ(0, message.repeated_double_size ()); - EXPECT_EQ(0, message.repeated_bool_size ()); - EXPECT_EQ(0, message.repeated_string_size ()); - EXPECT_EQ(0, message.repeated_bytes_size ()); - - EXPECT_EQ(0, message.repeatedgroup_size ()); - EXPECT_EQ(0, message.repeated_nested_message_size ()); + EXPECT_EQ(0, message.repeated_float_size()); + EXPECT_EQ(0, message.repeated_double_size()); + EXPECT_EQ(0, message.repeated_bool_size()); + EXPECT_EQ(0, message.repeated_string_size()); + EXPECT_EQ(0, message.repeated_bytes_size()); + + EXPECT_EQ(0, message.repeatedgroup_size()); + EXPECT_EQ(0, message.repeated_nested_message_size()); EXPECT_EQ(0, message.repeated_foreign_message_size()); - EXPECT_EQ(0, message.repeated_import_message_size ()); - EXPECT_EQ(0, message.repeated_lazy_message_size ()); - EXPECT_EQ(0, message.repeated_nested_enum_size ()); - EXPECT_EQ(0, message.repeated_foreign_enum_size ()); - EXPECT_EQ(0, message.repeated_import_enum_size ()); + EXPECT_EQ(0, message.repeated_import_message_size()); + EXPECT_EQ(0, message.repeated_lazy_message_size()); + EXPECT_EQ(0, message.repeated_nested_enum_size()); + EXPECT_EQ(0, message.repeated_foreign_enum_size()); + EXPECT_EQ(0, message.repeated_import_enum_size()); // has_blah() should also be false for all default fields. - EXPECT_FALSE(message.has_default_int32 ()); - EXPECT_FALSE(message.has_default_int64 ()); - EXPECT_FALSE(message.has_default_uint32 ()); - EXPECT_FALSE(message.has_default_uint64 ()); - EXPECT_FALSE(message.has_default_sint32 ()); - EXPECT_FALSE(message.has_default_sint64 ()); - EXPECT_FALSE(message.has_default_fixed32 ()); - EXPECT_FALSE(message.has_default_fixed64 ()); + EXPECT_FALSE(message.has_default_int32()); + EXPECT_FALSE(message.has_default_int64()); + EXPECT_FALSE(message.has_default_uint32()); + EXPECT_FALSE(message.has_default_uint64()); + EXPECT_FALSE(message.has_default_sint32()); + EXPECT_FALSE(message.has_default_sint64()); + EXPECT_FALSE(message.has_default_fixed32()); + EXPECT_FALSE(message.has_default_fixed64()); EXPECT_FALSE(message.has_default_sfixed32()); EXPECT_FALSE(message.has_default_sfixed64()); - EXPECT_FALSE(message.has_default_float ()); - EXPECT_FALSE(message.has_default_double ()); - EXPECT_FALSE(message.has_default_bool ()); - EXPECT_FALSE(message.has_default_string ()); - EXPECT_FALSE(message.has_default_bytes ()); + EXPECT_FALSE(message.has_default_float()); + EXPECT_FALSE(message.has_default_double()); + EXPECT_FALSE(message.has_default_bool()); + EXPECT_FALSE(message.has_default_string()); + EXPECT_FALSE(message.has_default_bytes()); - EXPECT_FALSE(message.has_default_nested_enum ()); + EXPECT_FALSE(message.has_default_nested_enum()); EXPECT_FALSE(message.has_default_foreign_enum()); - EXPECT_FALSE(message.has_default_import_enum ()); + EXPECT_FALSE(message.has_default_import_enum()); // Fields with defaults have their default values (duh). - EXPECT_EQ( 41 , message.default_int32 ()); - EXPECT_EQ( 42 , message.default_int64 ()); - EXPECT_EQ( 43 , message.default_uint32 ()); - EXPECT_EQ( 44 , message.default_uint64 ()); - EXPECT_EQ(-45 , message.default_sint32 ()); - EXPECT_EQ( 46 , message.default_sint64 ()); - EXPECT_EQ( 47 , message.default_fixed32 ()); - EXPECT_EQ( 48 , message.default_fixed64 ()); - EXPECT_EQ( 49 , message.default_sfixed32()); - EXPECT_EQ(-50 , message.default_sfixed64()); - EXPECT_EQ( 51.5 , message.default_float ()); - EXPECT_EQ( 52e3 , message.default_double ()); - EXPECT_EQ(true , message.default_bool ()); - EXPECT_EQ("hello", message.default_string ()); - EXPECT_EQ("world", message.default_bytes ()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.default_nested_enum ()); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.default_foreign_enum()); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.default_import_enum ()); - - - EXPECT_FALSE(message.has_oneof_uint32 ()); + EXPECT_EQ(41, message.default_int32()); + EXPECT_EQ(42, message.default_int64()); + EXPECT_EQ(43, message.default_uint32()); + EXPECT_EQ(44, message.default_uint64()); + EXPECT_EQ(-45, message.default_sint32()); + EXPECT_EQ(46, message.default_sint64()); + EXPECT_EQ(47, message.default_fixed32()); + EXPECT_EQ(48, message.default_fixed64()); + EXPECT_EQ(49, message.default_sfixed32()); + EXPECT_EQ(-50, message.default_sfixed64()); + EXPECT_EQ(51.5, message.default_float()); + EXPECT_EQ(52e3, message.default_double()); + EXPECT_EQ(true, message.default_bool()); + EXPECT_EQ("hello", message.default_string()); + EXPECT_EQ("world", message.default_bytes()); + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.default_nested_enum()); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.default_foreign_enum()); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.default_import_enum()); + + + EXPECT_FALSE(message.has_oneof_uint32()); EXPECT_FALSE(message.has_oneof_nested_message()); - EXPECT_FALSE(message.has_oneof_string ()); - EXPECT_FALSE(message.has_oneof_bytes ()); + EXPECT_FALSE(message.has_oneof_string()); + EXPECT_FALSE(message.has_oneof_bytes()); } // ------------------------------------------------------------------- @@ -540,188 +540,188 @@ void TestUtilLite::ExpectRepeatedFieldsModified( // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - ASSERT_EQ(2, message.repeated_int32_size ()); - ASSERT_EQ(2, message.repeated_int64_size ()); - ASSERT_EQ(2, message.repeated_uint32_size ()); - ASSERT_EQ(2, message.repeated_uint64_size ()); - ASSERT_EQ(2, message.repeated_sint32_size ()); - ASSERT_EQ(2, message.repeated_sint64_size ()); - ASSERT_EQ(2, message.repeated_fixed32_size ()); - ASSERT_EQ(2, message.repeated_fixed64_size ()); + ASSERT_EQ(2, message.repeated_int32_size()); + ASSERT_EQ(2, message.repeated_int64_size()); + ASSERT_EQ(2, message.repeated_uint32_size()); + ASSERT_EQ(2, message.repeated_uint64_size()); + ASSERT_EQ(2, message.repeated_sint32_size()); + ASSERT_EQ(2, message.repeated_sint64_size()); + ASSERT_EQ(2, message.repeated_fixed32_size()); + ASSERT_EQ(2, message.repeated_fixed64_size()); ASSERT_EQ(2, message.repeated_sfixed32_size()); ASSERT_EQ(2, message.repeated_sfixed64_size()); - ASSERT_EQ(2, message.repeated_float_size ()); - ASSERT_EQ(2, message.repeated_double_size ()); - ASSERT_EQ(2, message.repeated_bool_size ()); - ASSERT_EQ(2, message.repeated_string_size ()); - ASSERT_EQ(2, message.repeated_bytes_size ()); - - ASSERT_EQ(2, message.repeatedgroup_size ()); - ASSERT_EQ(2, message.repeated_nested_message_size ()); + ASSERT_EQ(2, message.repeated_float_size()); + ASSERT_EQ(2, message.repeated_double_size()); + ASSERT_EQ(2, message.repeated_bool_size()); + ASSERT_EQ(2, message.repeated_string_size()); + ASSERT_EQ(2, message.repeated_bytes_size()); + + ASSERT_EQ(2, message.repeatedgroup_size()); + ASSERT_EQ(2, message.repeated_nested_message_size()); ASSERT_EQ(2, message.repeated_foreign_message_size()); - ASSERT_EQ(2, message.repeated_import_message_size ()); - ASSERT_EQ(2, message.repeated_lazy_message_size ()); - ASSERT_EQ(2, message.repeated_nested_enum_size ()); - ASSERT_EQ(2, message.repeated_foreign_enum_size ()); - ASSERT_EQ(2, message.repeated_import_enum_size ()); - - - EXPECT_EQ(201 , message.repeated_int32 (0)); - EXPECT_EQ(202 , message.repeated_int64 (0)); - EXPECT_EQ(203 , message.repeated_uint32 (0)); - EXPECT_EQ(204 , message.repeated_uint64 (0)); - EXPECT_EQ(205 , message.repeated_sint32 (0)); - EXPECT_EQ(206 , message.repeated_sint64 (0)); - EXPECT_EQ(207 , message.repeated_fixed32 (0)); - EXPECT_EQ(208 , message.repeated_fixed64 (0)); - EXPECT_EQ(209 , message.repeated_sfixed32(0)); - EXPECT_EQ(210 , message.repeated_sfixed64(0)); - EXPECT_EQ(211 , message.repeated_float (0)); - EXPECT_EQ(212 , message.repeated_double (0)); - EXPECT_EQ(true , message.repeated_bool (0)); - EXPECT_EQ("215", message.repeated_string (0)); - EXPECT_EQ("216", message.repeated_bytes (0)); - - EXPECT_EQ(217, message.repeatedgroup (0).a()); - EXPECT_EQ(218, message.repeated_nested_message (0).bb()); + ASSERT_EQ(2, message.repeated_import_message_size()); + ASSERT_EQ(2, message.repeated_lazy_message_size()); + ASSERT_EQ(2, message.repeated_nested_enum_size()); + ASSERT_EQ(2, message.repeated_foreign_enum_size()); + ASSERT_EQ(2, message.repeated_import_enum_size()); + + + EXPECT_EQ(201, message.repeated_int32(0)); + EXPECT_EQ(202, message.repeated_int64(0)); + EXPECT_EQ(203, message.repeated_uint32(0)); + EXPECT_EQ(204, message.repeated_uint64(0)); + EXPECT_EQ(205, message.repeated_sint32(0)); + EXPECT_EQ(206, message.repeated_sint64(0)); + EXPECT_EQ(207, message.repeated_fixed32(0)); + EXPECT_EQ(208, message.repeated_fixed64(0)); + EXPECT_EQ(209, message.repeated_sfixed32(0)); + EXPECT_EQ(210, message.repeated_sfixed64(0)); + EXPECT_EQ(211, message.repeated_float(0)); + EXPECT_EQ(212, message.repeated_double(0)); + EXPECT_EQ(true, message.repeated_bool(0)); + EXPECT_EQ("215", message.repeated_string(0)); + EXPECT_EQ("216", message.repeated_bytes(0)); + + EXPECT_EQ(217, message.repeatedgroup(0).a()); + EXPECT_EQ(218, message.repeated_nested_message(0).bb()); EXPECT_EQ(219, message.repeated_foreign_message(0).c()); - EXPECT_EQ(220, message.repeated_import_message (0).d()); - EXPECT_EQ(227, message.repeated_lazy_message (0).bb()); + EXPECT_EQ(220, message.repeated_import_message(0).d()); + EXPECT_EQ(227, message.repeated_lazy_message(0).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.repeated_nested_enum (0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.repeated_foreign_enum(0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum (0)); + EXPECT_EQ(unittest::TestAllTypesLite::BAR, message.repeated_nested_enum(0)); + EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.repeated_foreign_enum(0)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.repeated_import_enum(0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(501 , message.repeated_int32 (1)); - EXPECT_EQ(502 , message.repeated_int64 (1)); - EXPECT_EQ(503 , message.repeated_uint32 (1)); - EXPECT_EQ(504 , message.repeated_uint64 (1)); - EXPECT_EQ(505 , message.repeated_sint32 (1)); - EXPECT_EQ(506 , message.repeated_sint64 (1)); - EXPECT_EQ(507 , message.repeated_fixed32 (1)); - EXPECT_EQ(508 , message.repeated_fixed64 (1)); - EXPECT_EQ(509 , message.repeated_sfixed32(1)); - EXPECT_EQ(510 , message.repeated_sfixed64(1)); - EXPECT_EQ(511 , message.repeated_float (1)); - EXPECT_EQ(512 , message.repeated_double (1)); - EXPECT_EQ(true , message.repeated_bool (1)); - EXPECT_EQ("515", message.repeated_string (1)); - EXPECT_EQ("516", message.repeated_bytes (1)); - - EXPECT_EQ(517, message.repeatedgroup (1).a()); - EXPECT_EQ(518, message.repeated_nested_message (1).bb()); + EXPECT_EQ(501, message.repeated_int32(1)); + EXPECT_EQ(502, message.repeated_int64(1)); + EXPECT_EQ(503, message.repeated_uint32(1)); + EXPECT_EQ(504, message.repeated_uint64(1)); + EXPECT_EQ(505, message.repeated_sint32(1)); + EXPECT_EQ(506, message.repeated_sint64(1)); + EXPECT_EQ(507, message.repeated_fixed32(1)); + EXPECT_EQ(508, message.repeated_fixed64(1)); + EXPECT_EQ(509, message.repeated_sfixed32(1)); + EXPECT_EQ(510, message.repeated_sfixed64(1)); + EXPECT_EQ(511, message.repeated_float(1)); + EXPECT_EQ(512, message.repeated_double(1)); + EXPECT_EQ(true, message.repeated_bool(1)); + EXPECT_EQ("515", message.repeated_string(1)); + EXPECT_EQ("516", message.repeated_bytes(1)); + + EXPECT_EQ(517, message.repeatedgroup(1).a()); + EXPECT_EQ(518, message.repeated_nested_message(1).bb()); EXPECT_EQ(519, message.repeated_foreign_message(1).c()); - EXPECT_EQ(520, message.repeated_import_message (1).d()); - EXPECT_EQ(527, message.repeated_lazy_message (1).bb()); + EXPECT_EQ(520, message.repeated_import_message(1).d()); + EXPECT_EQ(527, message.repeated_lazy_message(1).bb()); - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.repeated_nested_enum (1)); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.repeated_foreign_enum(1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.repeated_import_enum (1)); + EXPECT_EQ(unittest::TestAllTypesLite::FOO, message.repeated_nested_enum(1)); + EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.repeated_foreign_enum(1)); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.repeated_import_enum(1)); } // ------------------------------------------------------------------- void TestUtilLite::SetPackedFields(unittest::TestPackedTypesLite* message) { - message->add_packed_int32 (601); - message->add_packed_int64 (602); - message->add_packed_uint32 (603); - message->add_packed_uint64 (604); - message->add_packed_sint32 (605); - message->add_packed_sint64 (606); - message->add_packed_fixed32 (607); - message->add_packed_fixed64 (608); + message->add_packed_int32(601); + message->add_packed_int64(602); + message->add_packed_uint32(603); + message->add_packed_uint64(604); + message->add_packed_sint32(605); + message->add_packed_sint64(606); + message->add_packed_fixed32(607); + message->add_packed_fixed64(608); message->add_packed_sfixed32(609); message->add_packed_sfixed64(610); - message->add_packed_float (611); - message->add_packed_double (612); - message->add_packed_bool (true); - message->add_packed_enum (unittest::FOREIGN_LITE_BAR); + message->add_packed_float(611); + message->add_packed_double(612); + message->add_packed_bool(true); + message->add_packed_enum(unittest::FOREIGN_LITE_BAR); // add a second one of each field - message->add_packed_int32 (701); - message->add_packed_int64 (702); - message->add_packed_uint32 (703); - message->add_packed_uint64 (704); - message->add_packed_sint32 (705); - message->add_packed_sint64 (706); - message->add_packed_fixed32 (707); - message->add_packed_fixed64 (708); + message->add_packed_int32(701); + message->add_packed_int64(702); + message->add_packed_uint32(703); + message->add_packed_uint64(704); + message->add_packed_sint32(705); + message->add_packed_sint64(706); + message->add_packed_fixed32(707); + message->add_packed_fixed64(708); message->add_packed_sfixed32(709); message->add_packed_sfixed64(710); - message->add_packed_float (711); - message->add_packed_double (712); - message->add_packed_bool (false); - message->add_packed_enum (unittest::FOREIGN_LITE_BAZ); + message->add_packed_float(711); + message->add_packed_double(712); + message->add_packed_bool(false); + message->add_packed_enum(unittest::FOREIGN_LITE_BAZ); } // ------------------------------------------------------------------- void TestUtilLite::ModifyPackedFields(unittest::TestPackedTypesLite* message) { - message->set_packed_int32 (1, 801); - message->set_packed_int64 (1, 802); - message->set_packed_uint32 (1, 803); - message->set_packed_uint64 (1, 804); - message->set_packed_sint32 (1, 805); - message->set_packed_sint64 (1, 806); - message->set_packed_fixed32 (1, 807); - message->set_packed_fixed64 (1, 808); + message->set_packed_int32(1, 801); + message->set_packed_int64(1, 802); + message->set_packed_uint32(1, 803); + message->set_packed_uint64(1, 804); + message->set_packed_sint32(1, 805); + message->set_packed_sint64(1, 806); + message->set_packed_fixed32(1, 807); + message->set_packed_fixed64(1, 808); message->set_packed_sfixed32(1, 809); message->set_packed_sfixed64(1, 810); - message->set_packed_float (1, 811); - message->set_packed_double (1, 812); - message->set_packed_bool (1, true); - message->set_packed_enum (1, unittest::FOREIGN_LITE_FOO); + message->set_packed_float(1, 811); + message->set_packed_double(1, 812); + message->set_packed_bool(1, true); + message->set_packed_enum(1, unittest::FOREIGN_LITE_FOO); } // ------------------------------------------------------------------- void TestUtilLite::ExpectPackedFieldsSet( const unittest::TestPackedTypesLite& message) { - ASSERT_EQ(2, message.packed_int32_size ()); - ASSERT_EQ(2, message.packed_int64_size ()); - ASSERT_EQ(2, message.packed_uint32_size ()); - ASSERT_EQ(2, message.packed_uint64_size ()); - ASSERT_EQ(2, message.packed_sint32_size ()); - ASSERT_EQ(2, message.packed_sint64_size ()); - ASSERT_EQ(2, message.packed_fixed32_size ()); - ASSERT_EQ(2, message.packed_fixed64_size ()); + ASSERT_EQ(2, message.packed_int32_size()); + ASSERT_EQ(2, message.packed_int64_size()); + ASSERT_EQ(2, message.packed_uint32_size()); + ASSERT_EQ(2, message.packed_uint64_size()); + ASSERT_EQ(2, message.packed_sint32_size()); + ASSERT_EQ(2, message.packed_sint64_size()); + ASSERT_EQ(2, message.packed_fixed32_size()); + ASSERT_EQ(2, message.packed_fixed64_size()); ASSERT_EQ(2, message.packed_sfixed32_size()); ASSERT_EQ(2, message.packed_sfixed64_size()); - ASSERT_EQ(2, message.packed_float_size ()); - ASSERT_EQ(2, message.packed_double_size ()); - ASSERT_EQ(2, message.packed_bool_size ()); - ASSERT_EQ(2, message.packed_enum_size ()); - - EXPECT_EQ(601 , message.packed_int32 (0)); - EXPECT_EQ(602 , message.packed_int64 (0)); - EXPECT_EQ(603 , message.packed_uint32 (0)); - EXPECT_EQ(604 , message.packed_uint64 (0)); - EXPECT_EQ(605 , message.packed_sint32 (0)); - EXPECT_EQ(606 , message.packed_sint64 (0)); - EXPECT_EQ(607 , message.packed_fixed32 (0)); - EXPECT_EQ(608 , message.packed_fixed64 (0)); - EXPECT_EQ(609 , message.packed_sfixed32(0)); - EXPECT_EQ(610 , message.packed_sfixed64(0)); - EXPECT_EQ(611 , message.packed_float (0)); - EXPECT_EQ(612 , message.packed_double (0)); - EXPECT_EQ(true , message.packed_bool (0)); + ASSERT_EQ(2, message.packed_float_size()); + ASSERT_EQ(2, message.packed_double_size()); + ASSERT_EQ(2, message.packed_bool_size()); + ASSERT_EQ(2, message.packed_enum_size()); + + EXPECT_EQ(601, message.packed_int32(0)); + EXPECT_EQ(602, message.packed_int64(0)); + EXPECT_EQ(603, message.packed_uint32(0)); + EXPECT_EQ(604, message.packed_uint64(0)); + EXPECT_EQ(605, message.packed_sint32(0)); + EXPECT_EQ(606, message.packed_sint64(0)); + EXPECT_EQ(607, message.packed_fixed32(0)); + EXPECT_EQ(608, message.packed_fixed64(0)); + EXPECT_EQ(609, message.packed_sfixed32(0)); + EXPECT_EQ(610, message.packed_sfixed64(0)); + EXPECT_EQ(611, message.packed_float(0)); + EXPECT_EQ(612, message.packed_double(0)); + EXPECT_EQ(true, message.packed_bool(0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.packed_enum(0)); - EXPECT_EQ(701 , message.packed_int32 (1)); - EXPECT_EQ(702 , message.packed_int64 (1)); - EXPECT_EQ(703 , message.packed_uint32 (1)); - EXPECT_EQ(704 , message.packed_uint64 (1)); - EXPECT_EQ(705 , message.packed_sint32 (1)); - EXPECT_EQ(706 , message.packed_sint64 (1)); - EXPECT_EQ(707 , message.packed_fixed32 (1)); - EXPECT_EQ(708 , message.packed_fixed64 (1)); - EXPECT_EQ(709 , message.packed_sfixed32(1)); - EXPECT_EQ(710 , message.packed_sfixed64(1)); - EXPECT_EQ(711 , message.packed_float (1)); - EXPECT_EQ(712 , message.packed_double (1)); - EXPECT_EQ(false, message.packed_bool (1)); + EXPECT_EQ(701, message.packed_int32(1)); + EXPECT_EQ(702, message.packed_int64(1)); + EXPECT_EQ(703, message.packed_uint32(1)); + EXPECT_EQ(704, message.packed_uint64(1)); + EXPECT_EQ(705, message.packed_sint32(1)); + EXPECT_EQ(706, message.packed_sint64(1)); + EXPECT_EQ(707, message.packed_fixed32(1)); + EXPECT_EQ(708, message.packed_fixed64(1)); + EXPECT_EQ(709, message.packed_sfixed32(1)); + EXPECT_EQ(710, message.packed_sfixed64(1)); + EXPECT_EQ(711, message.packed_float(1)); + EXPECT_EQ(712, message.packed_double(1)); + EXPECT_EQ(false, message.packed_bool(1)); EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.packed_enum(1)); } @@ -730,20 +730,20 @@ void TestUtilLite::ExpectPackedFieldsSet( void TestUtilLite::ExpectPackedClear( const unittest::TestPackedTypesLite& message) { // Packed repeated fields are empty. - EXPECT_EQ(0, message.packed_int32_size ()); - EXPECT_EQ(0, message.packed_int64_size ()); - EXPECT_EQ(0, message.packed_uint32_size ()); - EXPECT_EQ(0, message.packed_uint64_size ()); - EXPECT_EQ(0, message.packed_sint32_size ()); - EXPECT_EQ(0, message.packed_sint64_size ()); - EXPECT_EQ(0, message.packed_fixed32_size ()); - EXPECT_EQ(0, message.packed_fixed64_size ()); + EXPECT_EQ(0, message.packed_int32_size()); + EXPECT_EQ(0, message.packed_int64_size()); + EXPECT_EQ(0, message.packed_uint32_size()); + EXPECT_EQ(0, message.packed_uint64_size()); + EXPECT_EQ(0, message.packed_sint32_size()); + EXPECT_EQ(0, message.packed_sint64_size()); + EXPECT_EQ(0, message.packed_fixed32_size()); + EXPECT_EQ(0, message.packed_fixed64_size()); EXPECT_EQ(0, message.packed_sfixed32_size()); EXPECT_EQ(0, message.packed_sfixed64_size()); - EXPECT_EQ(0, message.packed_float_size ()); - EXPECT_EQ(0, message.packed_double_size ()); - EXPECT_EQ(0, message.packed_bool_size ()); - EXPECT_EQ(0, message.packed_enum_size ()); + EXPECT_EQ(0, message.packed_float_size()); + EXPECT_EQ(0, message.packed_double_size()); + EXPECT_EQ(0, message.packed_bool_size()); + EXPECT_EQ(0, message.packed_enum_size()); } // ------------------------------------------------------------------- @@ -751,49 +751,49 @@ void TestUtilLite::ExpectPackedClear( void TestUtilLite::ExpectPackedFieldsModified( const unittest::TestPackedTypesLite& message) { // Do the same for packed repeated fields. - ASSERT_EQ(2, message.packed_int32_size ()); - ASSERT_EQ(2, message.packed_int64_size ()); - ASSERT_EQ(2, message.packed_uint32_size ()); - ASSERT_EQ(2, message.packed_uint64_size ()); - ASSERT_EQ(2, message.packed_sint32_size ()); - ASSERT_EQ(2, message.packed_sint64_size ()); - ASSERT_EQ(2, message.packed_fixed32_size ()); - ASSERT_EQ(2, message.packed_fixed64_size ()); + ASSERT_EQ(2, message.packed_int32_size()); + ASSERT_EQ(2, message.packed_int64_size()); + ASSERT_EQ(2, message.packed_uint32_size()); + ASSERT_EQ(2, message.packed_uint64_size()); + ASSERT_EQ(2, message.packed_sint32_size()); + ASSERT_EQ(2, message.packed_sint64_size()); + ASSERT_EQ(2, message.packed_fixed32_size()); + ASSERT_EQ(2, message.packed_fixed64_size()); ASSERT_EQ(2, message.packed_sfixed32_size()); ASSERT_EQ(2, message.packed_sfixed64_size()); - ASSERT_EQ(2, message.packed_float_size ()); - ASSERT_EQ(2, message.packed_double_size ()); - ASSERT_EQ(2, message.packed_bool_size ()); - ASSERT_EQ(2, message.packed_enum_size ()); - - EXPECT_EQ(601 , message.packed_int32 (0)); - EXPECT_EQ(602 , message.packed_int64 (0)); - EXPECT_EQ(603 , message.packed_uint32 (0)); - EXPECT_EQ(604 , message.packed_uint64 (0)); - EXPECT_EQ(605 , message.packed_sint32 (0)); - EXPECT_EQ(606 , message.packed_sint64 (0)); - EXPECT_EQ(607 , message.packed_fixed32 (0)); - EXPECT_EQ(608 , message.packed_fixed64 (0)); - EXPECT_EQ(609 , message.packed_sfixed32(0)); - EXPECT_EQ(610 , message.packed_sfixed64(0)); - EXPECT_EQ(611 , message.packed_float (0)); - EXPECT_EQ(612 , message.packed_double (0)); - EXPECT_EQ(true , message.packed_bool (0)); + ASSERT_EQ(2, message.packed_float_size()); + ASSERT_EQ(2, message.packed_double_size()); + ASSERT_EQ(2, message.packed_bool_size()); + ASSERT_EQ(2, message.packed_enum_size()); + + EXPECT_EQ(601, message.packed_int32(0)); + EXPECT_EQ(602, message.packed_int64(0)); + EXPECT_EQ(603, message.packed_uint32(0)); + EXPECT_EQ(604, message.packed_uint64(0)); + EXPECT_EQ(605, message.packed_sint32(0)); + EXPECT_EQ(606, message.packed_sint64(0)); + EXPECT_EQ(607, message.packed_fixed32(0)); + EXPECT_EQ(608, message.packed_fixed64(0)); + EXPECT_EQ(609, message.packed_sfixed32(0)); + EXPECT_EQ(610, message.packed_sfixed64(0)); + EXPECT_EQ(611, message.packed_float(0)); + EXPECT_EQ(612, message.packed_double(0)); + EXPECT_EQ(true, message.packed_bool(0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.packed_enum(0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(801 , message.packed_int32 (1)); - EXPECT_EQ(802 , message.packed_int64 (1)); - EXPECT_EQ(803 , message.packed_uint32 (1)); - EXPECT_EQ(804 , message.packed_uint64 (1)); - EXPECT_EQ(805 , message.packed_sint32 (1)); - EXPECT_EQ(806 , message.packed_sint64 (1)); - EXPECT_EQ(807 , message.packed_fixed32 (1)); - EXPECT_EQ(808 , message.packed_fixed64 (1)); - EXPECT_EQ(809 , message.packed_sfixed32(1)); - EXPECT_EQ(810 , message.packed_sfixed64(1)); - EXPECT_EQ(811 , message.packed_float (1)); - EXPECT_EQ(812 , message.packed_double (1)); - EXPECT_EQ(true , message.packed_bool (1)); + EXPECT_EQ(801, message.packed_int32(1)); + EXPECT_EQ(802, message.packed_int64(1)); + EXPECT_EQ(803, message.packed_uint32(1)); + EXPECT_EQ(804, message.packed_uint64(1)); + EXPECT_EQ(805, message.packed_sint32(1)); + EXPECT_EQ(806, message.packed_sint64(1)); + EXPECT_EQ(807, message.packed_fixed32(1)); + EXPECT_EQ(808, message.packed_fixed64(1)); + EXPECT_EQ(809, message.packed_sfixed32(1)); + EXPECT_EQ(810, message.packed_sfixed64(1)); + EXPECT_EQ(811, message.packed_float(1)); + EXPECT_EQ(812, message.packed_double(1)); + EXPECT_EQ(true, message.packed_bool(1)); EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.packed_enum(1)); } @@ -806,116 +806,145 @@ void TestUtilLite::ExpectPackedFieldsModified( // I gave up on the 80-char limit here. Sorry. void TestUtilLite::SetAllExtensions(unittest::TestAllExtensionsLite* message) { - message->SetExtension(unittest::optional_int32_extension_lite , 101); - message->SetExtension(unittest::optional_int64_extension_lite , 102); - message->SetExtension(unittest::optional_uint32_extension_lite , 103); - message->SetExtension(unittest::optional_uint64_extension_lite , 104); - message->SetExtension(unittest::optional_sint32_extension_lite , 105); - message->SetExtension(unittest::optional_sint64_extension_lite , 106); - message->SetExtension(unittest::optional_fixed32_extension_lite , 107); - message->SetExtension(unittest::optional_fixed64_extension_lite , 108); + message->SetExtension(unittest::optional_int32_extension_lite, 101); + message->SetExtension(unittest::optional_int64_extension_lite, 102); + message->SetExtension(unittest::optional_uint32_extension_lite, 103); + message->SetExtension(unittest::optional_uint64_extension_lite, 104); + message->SetExtension(unittest::optional_sint32_extension_lite, 105); + message->SetExtension(unittest::optional_sint64_extension_lite, 106); + message->SetExtension(unittest::optional_fixed32_extension_lite, 107); + message->SetExtension(unittest::optional_fixed64_extension_lite, 108); message->SetExtension(unittest::optional_sfixed32_extension_lite, 109); message->SetExtension(unittest::optional_sfixed64_extension_lite, 110); - message->SetExtension(unittest::optional_float_extension_lite , 111); - message->SetExtension(unittest::optional_double_extension_lite , 112); - message->SetExtension(unittest::optional_bool_extension_lite , true); - message->SetExtension(unittest::optional_string_extension_lite , "115"); - message->SetExtension(unittest::optional_bytes_extension_lite , "116"); - - message->MutableExtension(unittest::optionalgroup_extension_lite )->set_a(117); - message->MutableExtension(unittest::optional_nested_message_extension_lite )->set_bb(118); - message->MutableExtension(unittest::optional_foreign_message_extension_lite )->set_c(119); - message->MutableExtension(unittest::optional_import_message_extension_lite )->set_d(120); - message->MutableExtension(unittest::optional_public_import_message_extension_lite)->set_e(126); - message->MutableExtension(unittest::optional_lazy_message_extension_lite )->set_bb(127); - - message->SetExtension(unittest::optional_nested_enum_extension_lite , unittest::TestAllTypesLite::BAZ ); - message->SetExtension(unittest::optional_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAZ ); - message->SetExtension(unittest::optional_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAZ); + message->SetExtension(unittest::optional_float_extension_lite, 111); + message->SetExtension(unittest::optional_double_extension_lite, 112); + message->SetExtension(unittest::optional_bool_extension_lite, true); + message->SetExtension(unittest::optional_string_extension_lite, "115"); + message->SetExtension(unittest::optional_bytes_extension_lite, "116"); + + message->MutableExtension(unittest::optionalgroup_extension_lite)->set_a(117); + message->MutableExtension(unittest::optional_nested_message_extension_lite) + ->set_bb(118); + message->MutableExtension(unittest::optional_foreign_message_extension_lite) + ->set_c(119); + message->MutableExtension(unittest::optional_import_message_extension_lite) + ->set_d(120); + message + ->MutableExtension( + unittest::optional_public_import_message_extension_lite) + ->set_e(126); + message->MutableExtension(unittest::optional_lazy_message_extension_lite) + ->set_bb(127); + + message->SetExtension(unittest::optional_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAZ); + message->SetExtension(unittest::optional_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); + message->SetExtension(unittest::optional_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->AddExtension(unittest::repeated_int32_extension_lite , 201); - message->AddExtension(unittest::repeated_int64_extension_lite , 202); - message->AddExtension(unittest::repeated_uint32_extension_lite , 203); - message->AddExtension(unittest::repeated_uint64_extension_lite , 204); - message->AddExtension(unittest::repeated_sint32_extension_lite , 205); - message->AddExtension(unittest::repeated_sint64_extension_lite , 206); - message->AddExtension(unittest::repeated_fixed32_extension_lite , 207); - message->AddExtension(unittest::repeated_fixed64_extension_lite , 208); + message->AddExtension(unittest::repeated_int32_extension_lite, 201); + message->AddExtension(unittest::repeated_int64_extension_lite, 202); + message->AddExtension(unittest::repeated_uint32_extension_lite, 203); + message->AddExtension(unittest::repeated_uint64_extension_lite, 204); + message->AddExtension(unittest::repeated_sint32_extension_lite, 205); + message->AddExtension(unittest::repeated_sint64_extension_lite, 206); + message->AddExtension(unittest::repeated_fixed32_extension_lite, 207); + message->AddExtension(unittest::repeated_fixed64_extension_lite, 208); message->AddExtension(unittest::repeated_sfixed32_extension_lite, 209); message->AddExtension(unittest::repeated_sfixed64_extension_lite, 210); - message->AddExtension(unittest::repeated_float_extension_lite , 211); - message->AddExtension(unittest::repeated_double_extension_lite , 212); - message->AddExtension(unittest::repeated_bool_extension_lite , true); - message->AddExtension(unittest::repeated_string_extension_lite , "215"); - message->AddExtension(unittest::repeated_bytes_extension_lite , "216"); - - message->AddExtension(unittest::repeatedgroup_extension_lite )->set_a(217); - message->AddExtension(unittest::repeated_nested_message_extension_lite )->set_bb(218); - message->AddExtension(unittest::repeated_foreign_message_extension_lite)->set_c(219); - message->AddExtension(unittest::repeated_import_message_extension_lite )->set_d(220); - message->AddExtension(unittest::repeated_lazy_message_extension_lite )->set_bb(227); - - message->AddExtension(unittest::repeated_nested_enum_extension_lite , unittest::TestAllTypesLite::BAR ); - message->AddExtension(unittest::repeated_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAR ); - message->AddExtension(unittest::repeated_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAR); + message->AddExtension(unittest::repeated_float_extension_lite, 211); + message->AddExtension(unittest::repeated_double_extension_lite, 212); + message->AddExtension(unittest::repeated_bool_extension_lite, true); + message->AddExtension(unittest::repeated_string_extension_lite, "215"); + message->AddExtension(unittest::repeated_bytes_extension_lite, "216"); + + message->AddExtension(unittest::repeatedgroup_extension_lite)->set_a(217); + message->AddExtension(unittest::repeated_nested_message_extension_lite) + ->set_bb(218); + message->AddExtension(unittest::repeated_foreign_message_extension_lite) + ->set_c(219); + message->AddExtension(unittest::repeated_import_message_extension_lite) + ->set_d(220); + message->AddExtension(unittest::repeated_lazy_message_extension_lite) + ->set_bb(227); + + message->AddExtension(unittest::repeated_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAR); + message->AddExtension(unittest::repeated_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAR); + message->AddExtension(unittest::repeated_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAR); // Add a second one of each field. - message->AddExtension(unittest::repeated_int32_extension_lite , 301); - message->AddExtension(unittest::repeated_int64_extension_lite , 302); - message->AddExtension(unittest::repeated_uint32_extension_lite , 303); - message->AddExtension(unittest::repeated_uint64_extension_lite , 304); - message->AddExtension(unittest::repeated_sint32_extension_lite , 305); - message->AddExtension(unittest::repeated_sint64_extension_lite , 306); - message->AddExtension(unittest::repeated_fixed32_extension_lite , 307); - message->AddExtension(unittest::repeated_fixed64_extension_lite , 308); + message->AddExtension(unittest::repeated_int32_extension_lite, 301); + message->AddExtension(unittest::repeated_int64_extension_lite, 302); + message->AddExtension(unittest::repeated_uint32_extension_lite, 303); + message->AddExtension(unittest::repeated_uint64_extension_lite, 304); + message->AddExtension(unittest::repeated_sint32_extension_lite, 305); + message->AddExtension(unittest::repeated_sint64_extension_lite, 306); + message->AddExtension(unittest::repeated_fixed32_extension_lite, 307); + message->AddExtension(unittest::repeated_fixed64_extension_lite, 308); message->AddExtension(unittest::repeated_sfixed32_extension_lite, 309); message->AddExtension(unittest::repeated_sfixed64_extension_lite, 310); - message->AddExtension(unittest::repeated_float_extension_lite , 311); - message->AddExtension(unittest::repeated_double_extension_lite , 312); - message->AddExtension(unittest::repeated_bool_extension_lite , false); - message->AddExtension(unittest::repeated_string_extension_lite , "315"); - message->AddExtension(unittest::repeated_bytes_extension_lite , "316"); - - message->AddExtension(unittest::repeatedgroup_extension_lite )->set_a(317); - message->AddExtension(unittest::repeated_nested_message_extension_lite )->set_bb(318); - message->AddExtension(unittest::repeated_foreign_message_extension_lite)->set_c(319); - message->AddExtension(unittest::repeated_import_message_extension_lite )->set_d(320); - message->AddExtension(unittest::repeated_lazy_message_extension_lite )->set_bb(327); - - message->AddExtension(unittest::repeated_nested_enum_extension_lite , unittest::TestAllTypesLite::BAZ ); - message->AddExtension(unittest::repeated_foreign_enum_extension_lite, unittest::FOREIGN_LITE_BAZ ); - message->AddExtension(unittest::repeated_import_enum_extension_lite , unittest_import::IMPORT_LITE_BAZ); + message->AddExtension(unittest::repeated_float_extension_lite, 311); + message->AddExtension(unittest::repeated_double_extension_lite, 312); + message->AddExtension(unittest::repeated_bool_extension_lite, false); + message->AddExtension(unittest::repeated_string_extension_lite, "315"); + message->AddExtension(unittest::repeated_bytes_extension_lite, "316"); + + message->AddExtension(unittest::repeatedgroup_extension_lite)->set_a(317); + message->AddExtension(unittest::repeated_nested_message_extension_lite) + ->set_bb(318); + message->AddExtension(unittest::repeated_foreign_message_extension_lite) + ->set_c(319); + message->AddExtension(unittest::repeated_import_message_extension_lite) + ->set_d(320); + message->AddExtension(unittest::repeated_lazy_message_extension_lite) + ->set_bb(327); + + message->AddExtension(unittest::repeated_nested_enum_extension_lite, + unittest::TestAllTypesLite::BAZ); + message->AddExtension(unittest::repeated_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); + message->AddExtension(unittest::repeated_import_enum_extension_lite, + unittest_import::IMPORT_LITE_BAZ); // ----------------------------------------------------------------- - message->SetExtension(unittest::default_int32_extension_lite , 401); - message->SetExtension(unittest::default_int64_extension_lite , 402); - message->SetExtension(unittest::default_uint32_extension_lite , 403); - message->SetExtension(unittest::default_uint64_extension_lite , 404); - message->SetExtension(unittest::default_sint32_extension_lite , 405); - message->SetExtension(unittest::default_sint64_extension_lite , 406); - message->SetExtension(unittest::default_fixed32_extension_lite , 407); - message->SetExtension(unittest::default_fixed64_extension_lite , 408); + message->SetExtension(unittest::default_int32_extension_lite, 401); + message->SetExtension(unittest::default_int64_extension_lite, 402); + message->SetExtension(unittest::default_uint32_extension_lite, 403); + message->SetExtension(unittest::default_uint64_extension_lite, 404); + message->SetExtension(unittest::default_sint32_extension_lite, 405); + message->SetExtension(unittest::default_sint64_extension_lite, 406); + message->SetExtension(unittest::default_fixed32_extension_lite, 407); + message->SetExtension(unittest::default_fixed64_extension_lite, 408); message->SetExtension(unittest::default_sfixed32_extension_lite, 409); message->SetExtension(unittest::default_sfixed64_extension_lite, 410); - message->SetExtension(unittest::default_float_extension_lite , 411); - message->SetExtension(unittest::default_double_extension_lite , 412); - message->SetExtension(unittest::default_bool_extension_lite , false); - message->SetExtension(unittest::default_string_extension_lite , "415"); - message->SetExtension(unittest::default_bytes_extension_lite , "416"); - - message->SetExtension(unittest::default_nested_enum_extension_lite , unittest::TestAllTypesLite::FOO ); - message->SetExtension(unittest::default_foreign_enum_extension_lite, unittest::FOREIGN_LITE_FOO ); - message->SetExtension(unittest::default_import_enum_extension_lite , unittest_import::IMPORT_LITE_FOO); + message->SetExtension(unittest::default_float_extension_lite, 411); + message->SetExtension(unittest::default_double_extension_lite, 412); + message->SetExtension(unittest::default_bool_extension_lite, false); + message->SetExtension(unittest::default_string_extension_lite, "415"); + message->SetExtension(unittest::default_bytes_extension_lite, "416"); + + message->SetExtension(unittest::default_nested_enum_extension_lite, + unittest::TestAllTypesLite::FOO); + message->SetExtension(unittest::default_foreign_enum_extension_lite, + unittest::FOREIGN_LITE_FOO); + message->SetExtension(unittest::default_import_enum_extension_lite, + unittest_import::IMPORT_LITE_FOO); message->SetExtension(unittest::oneof_uint32_extension_lite, 601); - message->MutableExtension(unittest::oneof_nested_message_extension_lite)->set_bb(602);; + message->MutableExtension(unittest::oneof_nested_message_extension_lite) + ->set_bb(602); + ; message->SetExtension(unittest::oneof_string_extension_lite, "603"); message->SetExtension(unittest::oneof_bytes_extension_lite, "604"); } @@ -924,31 +953,40 @@ void TestUtilLite::SetAllExtensions(unittest::TestAllExtensionsLite* message) { void TestUtilLite::ModifyRepeatedExtensions( unittest::TestAllExtensionsLite* message) { - message->SetExtension(unittest::repeated_int32_extension_lite , 1, 501); - message->SetExtension(unittest::repeated_int64_extension_lite , 1, 502); - message->SetExtension(unittest::repeated_uint32_extension_lite , 1, 503); - message->SetExtension(unittest::repeated_uint64_extension_lite , 1, 504); - message->SetExtension(unittest::repeated_sint32_extension_lite , 1, 505); - message->SetExtension(unittest::repeated_sint64_extension_lite , 1, 506); - message->SetExtension(unittest::repeated_fixed32_extension_lite , 1, 507); - message->SetExtension(unittest::repeated_fixed64_extension_lite , 1, 508); + message->SetExtension(unittest::repeated_int32_extension_lite, 1, 501); + message->SetExtension(unittest::repeated_int64_extension_lite, 1, 502); + message->SetExtension(unittest::repeated_uint32_extension_lite, 1, 503); + message->SetExtension(unittest::repeated_uint64_extension_lite, 1, 504); + message->SetExtension(unittest::repeated_sint32_extension_lite, 1, 505); + message->SetExtension(unittest::repeated_sint64_extension_lite, 1, 506); + message->SetExtension(unittest::repeated_fixed32_extension_lite, 1, 507); + message->SetExtension(unittest::repeated_fixed64_extension_lite, 1, 508); message->SetExtension(unittest::repeated_sfixed32_extension_lite, 1, 509); message->SetExtension(unittest::repeated_sfixed64_extension_lite, 1, 510); - message->SetExtension(unittest::repeated_float_extension_lite , 1, 511); - message->SetExtension(unittest::repeated_double_extension_lite , 1, 512); - message->SetExtension(unittest::repeated_bool_extension_lite , 1, true); - message->SetExtension(unittest::repeated_string_extension_lite , 1, "515"); - message->SetExtension(unittest::repeated_bytes_extension_lite , 1, "516"); - - message->MutableExtension(unittest::repeatedgroup_extension_lite , 1)->set_a(517); - message->MutableExtension(unittest::repeated_nested_message_extension_lite , 1)->set_bb(518); - message->MutableExtension(unittest::repeated_foreign_message_extension_lite, 1)->set_c(519); - message->MutableExtension(unittest::repeated_import_message_extension_lite , 1)->set_d(520); - message->MutableExtension(unittest::repeated_lazy_message_extension_lite , 1)->set_bb(527); - - message->SetExtension(unittest::repeated_nested_enum_extension_lite , 1, unittest::TestAllTypesLite::FOO ); - message->SetExtension(unittest::repeated_foreign_enum_extension_lite, 1, unittest::FOREIGN_LITE_FOO ); - message->SetExtension(unittest::repeated_import_enum_extension_lite , 1, unittest_import::IMPORT_LITE_FOO); + message->SetExtension(unittest::repeated_float_extension_lite, 1, 511); + message->SetExtension(unittest::repeated_double_extension_lite, 1, 512); + message->SetExtension(unittest::repeated_bool_extension_lite, 1, true); + message->SetExtension(unittest::repeated_string_extension_lite, 1, "515"); + message->SetExtension(unittest::repeated_bytes_extension_lite, 1, "516"); + + message->MutableExtension(unittest::repeatedgroup_extension_lite, 1) + ->set_a(517); + message->MutableExtension(unittest::repeated_nested_message_extension_lite, 1) + ->set_bb(518); + message + ->MutableExtension(unittest::repeated_foreign_message_extension_lite, 1) + ->set_c(519); + message->MutableExtension(unittest::repeated_import_message_extension_lite, 1) + ->set_d(520); + message->MutableExtension(unittest::repeated_lazy_message_extension_lite, 1) + ->set_bb(527); + + message->SetExtension(unittest::repeated_nested_enum_extension_lite, 1, + unittest::TestAllTypesLite::FOO); + message->SetExtension(unittest::repeated_foreign_enum_extension_lite, 1, + unittest::FOREIGN_LITE_FOO); + message->SetExtension(unittest::repeated_import_enum_extension_lite, 1, + unittest_import::IMPORT_LITE_FOO); } @@ -956,202 +994,351 @@ void TestUtilLite::ModifyRepeatedExtensions( void TestUtilLite::ExpectAllExtensionsSet( const unittest::TestAllExtensionsLite& message) { - EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_int64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_uint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_sint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_sint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_fixed64_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_int64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_uint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_sint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_sint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_fixed64_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed32_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_float_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_double_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_bool_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_TRUE(message.HasExtension(unittest::optionalgroup_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_nested_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_import_message_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_public_import_message_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_lazy_message_extension_lite )); - - EXPECT_TRUE(message.GetExtension(unittest::optionalgroup_extension_lite ).has_a()); - EXPECT_TRUE(message.GetExtension(unittest::optional_nested_message_extension_lite ).has_bb()); - EXPECT_TRUE(message.GetExtension(unittest::optional_foreign_message_extension_lite ).has_c()); - EXPECT_TRUE(message.GetExtension(unittest::optional_import_message_extension_lite ).has_d()); - EXPECT_TRUE(message.GetExtension(unittest::optional_public_import_message_extension_lite).has_e()); - EXPECT_TRUE(message.GetExtension(unittest::optional_lazy_message_extension_lite ).has_bb()); - - EXPECT_TRUE(message.HasExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::optional_import_enum_extension_lite )); - - - EXPECT_EQ(101 , message.GetExtension(unittest::optional_int32_extension_lite )); - EXPECT_EQ(102 , message.GetExtension(unittest::optional_int64_extension_lite )); - EXPECT_EQ(103 , message.GetExtension(unittest::optional_uint32_extension_lite )); - EXPECT_EQ(104 , message.GetExtension(unittest::optional_uint64_extension_lite )); - EXPECT_EQ(105 , message.GetExtension(unittest::optional_sint32_extension_lite )); - EXPECT_EQ(106 , message.GetExtension(unittest::optional_sint64_extension_lite )); - EXPECT_EQ(107 , message.GetExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_EQ(108 , message.GetExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_EQ(109 , message.GetExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_EQ(110 , message.GetExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_EQ(111 , message.GetExtension(unittest::optional_float_extension_lite )); - EXPECT_EQ(112 , message.GetExtension(unittest::optional_double_extension_lite )); - EXPECT_EQ(true , message.GetExtension(unittest::optional_bool_extension_lite )); - EXPECT_EQ("115", message.GetExtension(unittest::optional_string_extension_lite )); - EXPECT_EQ("116", message.GetExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_EQ(117, message.GetExtension(unittest::optionalgroup_extension_lite ).a()); - EXPECT_EQ(118, message.GetExtension(unittest::optional_nested_message_extension_lite ).bb()); - EXPECT_EQ(119, message.GetExtension(unittest::optional_foreign_message_extension_lite ).c()); - EXPECT_EQ(120, message.GetExtension(unittest::optional_import_message_extension_lite ).d()); - EXPECT_EQ(126, message.GetExtension(unittest::optional_public_import_message_extension_lite).e()); - EXPECT_EQ(127, message.GetExtension(unittest::optional_lazy_message_extension_lite ).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.GetExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.GetExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.GetExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::optional_float_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_double_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_bool_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_TRUE(message.HasExtension(unittest::optionalgroup_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_nested_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_import_message_extension_lite)); + EXPECT_TRUE(message.HasExtension( + unittest::optional_public_import_message_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_lazy_message_extension_lite)); + + EXPECT_TRUE( + message.GetExtension(unittest::optionalgroup_extension_lite).has_a()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_nested_message_extension_lite) + .has_bb()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .has_c()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_import_message_extension_lite) + .has_d()); + EXPECT_TRUE( + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .has_e()); + EXPECT_TRUE( + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .has_bb()); + + EXPECT_TRUE( + message.HasExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::optional_import_enum_extension_lite)); + + + EXPECT_EQ(101, message.GetExtension(unittest::optional_int32_extension_lite)); + EXPECT_EQ(102, message.GetExtension(unittest::optional_int64_extension_lite)); + EXPECT_EQ(103, + message.GetExtension(unittest::optional_uint32_extension_lite)); + EXPECT_EQ(104, + message.GetExtension(unittest::optional_uint64_extension_lite)); + EXPECT_EQ(105, + message.GetExtension(unittest::optional_sint32_extension_lite)); + EXPECT_EQ(106, + message.GetExtension(unittest::optional_sint64_extension_lite)); + EXPECT_EQ(107, + message.GetExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_EQ(108, + message.GetExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_EQ(109, + message.GetExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_EQ(110, + message.GetExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_EQ(111, message.GetExtension(unittest::optional_float_extension_lite)); + EXPECT_EQ(112, + message.GetExtension(unittest::optional_double_extension_lite)); + EXPECT_EQ(true, message.GetExtension(unittest::optional_bool_extension_lite)); + EXPECT_EQ("115", + message.GetExtension(unittest::optional_string_extension_lite)); + EXPECT_EQ("116", + message.GetExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_EQ(117, + message.GetExtension(unittest::optionalgroup_extension_lite).a()); + EXPECT_EQ( + 118, + message.GetExtension(unittest::optional_nested_message_extension_lite) + .bb()); + EXPECT_EQ( + 119, + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .c()); + EXPECT_EQ( + 120, + message.GetExtension(unittest::optional_import_message_extension_lite) + .d()); + EXPECT_EQ( + 126, + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .e()); + EXPECT_EQ(127, + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAZ, + message.GetExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAZ, + message.GetExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAZ, + message.GetExtension(unittest::optional_import_enum_extension_lite)); // ----------------------------------------------------------------- - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); - - - EXPECT_EQ(201 , message.GetExtension(unittest::repeated_int32_extension_lite , 0)); - EXPECT_EQ(202 , message.GetExtension(unittest::repeated_int64_extension_lite , 0)); - EXPECT_EQ(203 , message.GetExtension(unittest::repeated_uint32_extension_lite , 0)); - EXPECT_EQ(204 , message.GetExtension(unittest::repeated_uint64_extension_lite , 0)); - EXPECT_EQ(205 , message.GetExtension(unittest::repeated_sint32_extension_lite , 0)); - EXPECT_EQ(206 , message.GetExtension(unittest::repeated_sint64_extension_lite , 0)); - EXPECT_EQ(207 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 0)); - EXPECT_EQ(208 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 0)); - EXPECT_EQ(209 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); - EXPECT_EQ(210 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); - EXPECT_EQ(211 , message.GetExtension(unittest::repeated_float_extension_lite , 0)); - EXPECT_EQ(212 , message.GetExtension(unittest::repeated_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 0)); - EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension_lite , 0)); - EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension_lite , 0)); - - EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension_lite , 0).a()); - EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension_lite , 0).bb()); - EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0).c()); - EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension_lite , 0).d()); - EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 0).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::repeated_import_enum_extension_lite , 0)); - - - EXPECT_EQ(301 , message.GetExtension(unittest::repeated_int32_extension_lite , 1)); - EXPECT_EQ(302 , message.GetExtension(unittest::repeated_int64_extension_lite , 1)); - EXPECT_EQ(303 , message.GetExtension(unittest::repeated_uint32_extension_lite , 1)); - EXPECT_EQ(304 , message.GetExtension(unittest::repeated_uint64_extension_lite , 1)); - EXPECT_EQ(305 , message.GetExtension(unittest::repeated_sint32_extension_lite , 1)); - EXPECT_EQ(306 , message.GetExtension(unittest::repeated_sint64_extension_lite , 1)); - EXPECT_EQ(307 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 1)); - EXPECT_EQ(308 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 1)); - EXPECT_EQ(309 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); - EXPECT_EQ(310 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); - EXPECT_EQ(311 , message.GetExtension(unittest::repeated_float_extension_lite , 1)); - EXPECT_EQ(312 , message.GetExtension(unittest::repeated_double_extension_lite , 1)); - EXPECT_EQ(false, message.GetExtension(unittest::repeated_bool_extension_lite , 1)); - EXPECT_EQ("315", message.GetExtension(unittest::repeated_string_extension_lite , 1)); - EXPECT_EQ("316", message.GetExtension(unittest::repeated_bytes_extension_lite , 1)); - - EXPECT_EQ(317, message.GetExtension(unittest::repeatedgroup_extension_lite , 1).a()); - EXPECT_EQ(318, message.GetExtension(unittest::repeated_nested_message_extension_lite , 1).bb()); - EXPECT_EQ(319, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1).c()); - EXPECT_EQ(320, message.GetExtension(unittest::repeated_import_message_extension_lite , 1).d()); - EXPECT_EQ(327, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 1).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAZ , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 1)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAZ , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAZ, message.GetExtension(unittest::repeated_import_enum_extension_lite , 1)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); + + + EXPECT_EQ(201, + message.GetExtension(unittest::repeated_int32_extension_lite, 0)); + EXPECT_EQ(202, + message.GetExtension(unittest::repeated_int64_extension_lite, 0)); + EXPECT_EQ(203, + message.GetExtension(unittest::repeated_uint32_extension_lite, 0)); + EXPECT_EQ(204, + message.GetExtension(unittest::repeated_uint64_extension_lite, 0)); + EXPECT_EQ(205, + message.GetExtension(unittest::repeated_sint32_extension_lite, 0)); + EXPECT_EQ(206, + message.GetExtension(unittest::repeated_sint64_extension_lite, 0)); + EXPECT_EQ(207, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 0)); + EXPECT_EQ(208, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 0)); + EXPECT_EQ( + 209, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); + EXPECT_EQ( + 210, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); + EXPECT_EQ(211, + message.GetExtension(unittest::repeated_float_extension_lite, 0)); + EXPECT_EQ(212, + message.GetExtension(unittest::repeated_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 0)); + EXPECT_EQ("215", + message.GetExtension(unittest::repeated_string_extension_lite, 0)); + EXPECT_EQ("216", + message.GetExtension(unittest::repeated_bytes_extension_lite, 0)); + + EXPECT_EQ( + 217, message.GetExtension(unittest::repeatedgroup_extension_lite, 0).a()); + EXPECT_EQ( + 218, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 0) + .bb()); + EXPECT_EQ( + 219, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0) + .c()); + EXPECT_EQ( + 220, + message.GetExtension(unittest::repeated_import_message_extension_lite, 0) + .d()); + EXPECT_EQ( + 227, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 0) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 0)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 0)); + + + EXPECT_EQ(301, + message.GetExtension(unittest::repeated_int32_extension_lite, 1)); + EXPECT_EQ(302, + message.GetExtension(unittest::repeated_int64_extension_lite, 1)); + EXPECT_EQ(303, + message.GetExtension(unittest::repeated_uint32_extension_lite, 1)); + EXPECT_EQ(304, + message.GetExtension(unittest::repeated_uint64_extension_lite, 1)); + EXPECT_EQ(305, + message.GetExtension(unittest::repeated_sint32_extension_lite, 1)); + EXPECT_EQ(306, + message.GetExtension(unittest::repeated_sint64_extension_lite, 1)); + EXPECT_EQ(307, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 1)); + EXPECT_EQ(308, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 1)); + EXPECT_EQ( + 309, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); + EXPECT_EQ( + 310, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); + EXPECT_EQ(311, + message.GetExtension(unittest::repeated_float_extension_lite, 1)); + EXPECT_EQ(312, + message.GetExtension(unittest::repeated_double_extension_lite, 1)); + EXPECT_EQ(false, + message.GetExtension(unittest::repeated_bool_extension_lite, 1)); + EXPECT_EQ("315", + message.GetExtension(unittest::repeated_string_extension_lite, 1)); + EXPECT_EQ("316", + message.GetExtension(unittest::repeated_bytes_extension_lite, 1)); + + EXPECT_EQ( + 317, message.GetExtension(unittest::repeatedgroup_extension_lite, 1).a()); + EXPECT_EQ( + 318, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 1) + .bb()); + EXPECT_EQ( + 319, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1) + .c()); + EXPECT_EQ( + 320, + message.GetExtension(unittest::repeated_import_message_extension_lite, 1) + .d()); + EXPECT_EQ( + 327, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 1) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAZ, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 1)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAZ, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAZ, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 1)); // ----------------------------------------------------------------- - EXPECT_TRUE(message.HasExtension(unittest::default_int32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_int64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_uint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_uint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_sint32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_sint64_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_fixed32_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_fixed64_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::default_int32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_int64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_uint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_uint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_sint32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_sint64_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_fixed32_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_fixed64_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::default_sfixed32_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::default_float_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_double_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_bool_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_string_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_bytes_extension_lite )); - - EXPECT_TRUE(message.HasExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_TRUE(message.HasExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_TRUE(message.HasExtension(unittest::default_import_enum_extension_lite )); - - - EXPECT_EQ(401 , message.GetExtension(unittest::default_int32_extension_lite )); - EXPECT_EQ(402 , message.GetExtension(unittest::default_int64_extension_lite )); - EXPECT_EQ(403 , message.GetExtension(unittest::default_uint32_extension_lite )); - EXPECT_EQ(404 , message.GetExtension(unittest::default_uint64_extension_lite )); - EXPECT_EQ(405 , message.GetExtension(unittest::default_sint32_extension_lite )); - EXPECT_EQ(406 , message.GetExtension(unittest::default_sint64_extension_lite )); - EXPECT_EQ(407 , message.GetExtension(unittest::default_fixed32_extension_lite )); - EXPECT_EQ(408 , message.GetExtension(unittest::default_fixed64_extension_lite )); - EXPECT_EQ(409 , message.GetExtension(unittest::default_sfixed32_extension_lite)); - EXPECT_EQ(410 , message.GetExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_EQ(411 , message.GetExtension(unittest::default_float_extension_lite )); - EXPECT_EQ(412 , message.GetExtension(unittest::default_double_extension_lite )); - EXPECT_EQ(false, message.GetExtension(unittest::default_bool_extension_lite )); - EXPECT_EQ("415", message.GetExtension(unittest::default_string_extension_lite )); - EXPECT_EQ("416", message.GetExtension(unittest::default_bytes_extension_lite )); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::default_import_enum_extension_lite )); + EXPECT_TRUE(message.HasExtension(unittest::default_float_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_double_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_bool_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_string_extension_lite)); + EXPECT_TRUE(message.HasExtension(unittest::default_bytes_extension_lite)); + + EXPECT_TRUE( + message.HasExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_TRUE( + message.HasExtension(unittest::default_import_enum_extension_lite)); + + + EXPECT_EQ(401, message.GetExtension(unittest::default_int32_extension_lite)); + EXPECT_EQ(402, message.GetExtension(unittest::default_int64_extension_lite)); + EXPECT_EQ(403, message.GetExtension(unittest::default_uint32_extension_lite)); + EXPECT_EQ(404, message.GetExtension(unittest::default_uint64_extension_lite)); + EXPECT_EQ(405, message.GetExtension(unittest::default_sint32_extension_lite)); + EXPECT_EQ(406, message.GetExtension(unittest::default_sint64_extension_lite)); + EXPECT_EQ(407, + message.GetExtension(unittest::default_fixed32_extension_lite)); + EXPECT_EQ(408, + message.GetExtension(unittest::default_fixed64_extension_lite)); + EXPECT_EQ(409, + message.GetExtension(unittest::default_sfixed32_extension_lite)); + EXPECT_EQ(410, + message.GetExtension(unittest::default_sfixed64_extension_lite)); + EXPECT_EQ(411, message.GetExtension(unittest::default_float_extension_lite)); + EXPECT_EQ(412, message.GetExtension(unittest::default_double_extension_lite)); + EXPECT_EQ(false, message.GetExtension(unittest::default_bool_extension_lite)); + EXPECT_EQ("415", + message.GetExtension(unittest::default_string_extension_lite)); + EXPECT_EQ("416", + message.GetExtension(unittest::default_bytes_extension_lite)); + + EXPECT_EQ(unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::default_import_enum_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_TRUE(message.GetExtension(unittest::oneof_nested_message_extension_lite).has_bb()); + EXPECT_TRUE( + message.GetExtension(unittest::oneof_nested_message_extension_lite) + .has_bb()); EXPECT_TRUE(message.HasExtension(unittest::oneof_string_extension_lite)); EXPECT_TRUE(message.HasExtension(unittest::oneof_bytes_extension_lite)); EXPECT_EQ(601, message.GetExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_EQ(602, message.GetExtension(unittest::oneof_nested_message_extension_lite).bb()); + EXPECT_EQ( + 602, + message.GetExtension(unittest::oneof_nested_message_extension_lite).bb()); EXPECT_EQ("603", message.GetExtension(unittest::oneof_string_extension_lite)); EXPECT_EQ("604", message.GetExtension(unittest::oneof_bytes_extension_lite)); } @@ -1166,145 +1353,213 @@ void TestUtilLite::ExpectExtensionsClear( EXPECT_EQ(0, message.ByteSize()); // has_blah() should initially be false for all optional fields. - EXPECT_FALSE(message.HasExtension(unittest::optional_int32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_uint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_uint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_float_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_double_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_bool_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_string_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_bytes_extension_lite )); - - EXPECT_FALSE(message.HasExtension(unittest::optionalgroup_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_nested_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_import_message_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_public_import_message_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_lazy_message_extension_lite )); - - EXPECT_FALSE(message.HasExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::optional_int32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_uint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_uint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_sint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_sint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_float_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_double_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_bool_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_string_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::optional_bytes_extension_lite)); + + EXPECT_FALSE(message.HasExtension(unittest::optionalgroup_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_nested_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_import_message_extension_lite)); + EXPECT_FALSE(message.HasExtension( + unittest::optional_public_import_message_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_lazy_message_extension_lite)); + + EXPECT_FALSE( + message.HasExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::optional_import_enum_extension_lite)); // Optional fields without defaults are set to zero or something like it. - EXPECT_EQ(0 , message.GetExtension(unittest::optional_int32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_int64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_uint32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_uint64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sint32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sint64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_fixed32_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_fixed64_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sfixed32_extension_lite)); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_sfixed64_extension_lite)); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_float_extension_lite )); - EXPECT_EQ(0 , message.GetExtension(unittest::optional_double_extension_lite )); - EXPECT_EQ(false, message.GetExtension(unittest::optional_bool_extension_lite )); - EXPECT_EQ("" , message.GetExtension(unittest::optional_string_extension_lite )); - EXPECT_EQ("" , message.GetExtension(unittest::optional_bytes_extension_lite )); + EXPECT_EQ(0, message.GetExtension(unittest::optional_int32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_int64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_uint32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_uint64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_sint32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_sint64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_fixed32_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_fixed64_extension_lite)); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_sfixed32_extension_lite)); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_sfixed64_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_float_extension_lite)); + EXPECT_EQ(0, message.GetExtension(unittest::optional_double_extension_lite)); + EXPECT_EQ(false, + message.GetExtension(unittest::optional_bool_extension_lite)); + EXPECT_EQ("", message.GetExtension(unittest::optional_string_extension_lite)); + EXPECT_EQ("", message.GetExtension(unittest::optional_bytes_extension_lite)); // Embedded messages should also be clear. - EXPECT_FALSE(message.GetExtension(unittest::optionalgroup_extension_lite ).has_a()); - EXPECT_FALSE(message.GetExtension(unittest::optional_nested_message_extension_lite ).has_bb()); - EXPECT_FALSE(message.GetExtension(unittest::optional_foreign_message_extension_lite ).has_c()); - EXPECT_FALSE(message.GetExtension(unittest::optional_import_message_extension_lite ).has_d()); - EXPECT_FALSE(message.GetExtension(unittest::optional_public_import_message_extension_lite).has_e()); - EXPECT_FALSE(message.GetExtension(unittest::optional_lazy_message_extension_lite ).has_bb()); - - EXPECT_EQ(0, message.GetExtension(unittest::optionalgroup_extension_lite ).a()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension_lite ).bb()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_foreign_message_extension_lite ).c()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_import_message_extension_lite ).d()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_public_import_message_extension_lite).e()); - EXPECT_EQ(0, message.GetExtension(unittest::optional_lazy_message_extension_lite ).bb()); + EXPECT_FALSE( + message.GetExtension(unittest::optionalgroup_extension_lite).has_a()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_nested_message_extension_lite) + .has_bb()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_foreign_message_extension_lite) + .has_c()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_import_message_extension_lite) + .has_d()); + EXPECT_FALSE( + message + .GetExtension(unittest::optional_public_import_message_extension_lite) + .has_e()); + EXPECT_FALSE( + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .has_bb()); + + EXPECT_EQ(0, + message.GetExtension(unittest::optionalgroup_extension_lite).a()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_nested_message_extension_lite) + .bb()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_foreign_message_extension_lite) + .c()); + EXPECT_EQ( + 0, message.GetExtension(unittest::optional_import_message_extension_lite) + .d()); + EXPECT_EQ(0, message + .GetExtension( + unittest::optional_public_import_message_extension_lite) + .e()); + EXPECT_EQ(0, + message.GetExtension(unittest::optional_lazy_message_extension_lite) + .bb()); // Enums without defaults are set to the first value in the enum. - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::optional_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::optional_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::optional_import_enum_extension_lite )); + EXPECT_EQ( + unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::optional_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::optional_foreign_enum_extension_lite)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::optional_import_enum_extension_lite)); // Repeated fields are empty. - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_float_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_double_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - EXPECT_EQ(0, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + EXPECT_EQ(0, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_float_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_double_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + EXPECT_EQ(0, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + EXPECT_EQ( + 0, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); // has_blah() should also be false for all default fields. - EXPECT_FALSE(message.HasExtension(unittest::default_int32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_int64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_uint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_uint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_sint32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_sint64_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_fixed32_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_fixed64_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::default_int32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_int64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_uint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_uint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_sint32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_sint64_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_fixed32_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_fixed64_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::default_sfixed32_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_float_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_double_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_bool_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_string_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_bytes_extension_lite )); + EXPECT_FALSE(message.HasExtension(unittest::default_float_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_double_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_bool_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_string_extension_lite)); + EXPECT_FALSE(message.HasExtension(unittest::default_bytes_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_FALSE(message.HasExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_FALSE(message.HasExtension(unittest::default_import_enum_extension_lite )); + EXPECT_FALSE( + message.HasExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_FALSE( + message.HasExtension(unittest::default_import_enum_extension_lite)); // Fields with defaults have their default values (duh). - EXPECT_EQ( 41 , message.GetExtension(unittest::default_int32_extension_lite )); - EXPECT_EQ( 42 , message.GetExtension(unittest::default_int64_extension_lite )); - EXPECT_EQ( 43 , message.GetExtension(unittest::default_uint32_extension_lite )); - EXPECT_EQ( 44 , message.GetExtension(unittest::default_uint64_extension_lite )); - EXPECT_EQ(-45 , message.GetExtension(unittest::default_sint32_extension_lite )); - EXPECT_EQ( 46 , message.GetExtension(unittest::default_sint64_extension_lite )); - EXPECT_EQ( 47 , message.GetExtension(unittest::default_fixed32_extension_lite )); - EXPECT_EQ( 48 , message.GetExtension(unittest::default_fixed64_extension_lite )); - EXPECT_EQ( 49 , message.GetExtension(unittest::default_sfixed32_extension_lite)); - EXPECT_EQ(-50 , message.GetExtension(unittest::default_sfixed64_extension_lite)); - EXPECT_EQ( 51.5 , message.GetExtension(unittest::default_float_extension_lite )); - EXPECT_EQ( 52e3 , message.GetExtension(unittest::default_double_extension_lite )); - EXPECT_EQ(true , message.GetExtension(unittest::default_bool_extension_lite )); - EXPECT_EQ("hello", message.GetExtension(unittest::default_string_extension_lite )); - EXPECT_EQ("world", message.GetExtension(unittest::default_bytes_extension_lite )); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::default_nested_enum_extension_lite )); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::default_foreign_enum_extension_lite)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::default_import_enum_extension_lite )); + EXPECT_EQ(41, message.GetExtension(unittest::default_int32_extension_lite)); + EXPECT_EQ(42, message.GetExtension(unittest::default_int64_extension_lite)); + EXPECT_EQ(43, message.GetExtension(unittest::default_uint32_extension_lite)); + EXPECT_EQ(44, message.GetExtension(unittest::default_uint64_extension_lite)); + EXPECT_EQ(-45, message.GetExtension(unittest::default_sint32_extension_lite)); + EXPECT_EQ(46, message.GetExtension(unittest::default_sint64_extension_lite)); + EXPECT_EQ(47, message.GetExtension(unittest::default_fixed32_extension_lite)); + EXPECT_EQ(48, message.GetExtension(unittest::default_fixed64_extension_lite)); + EXPECT_EQ(49, + message.GetExtension(unittest::default_sfixed32_extension_lite)); + EXPECT_EQ(-50, + message.GetExtension(unittest::default_sfixed64_extension_lite)); + EXPECT_EQ(51.5, message.GetExtension(unittest::default_float_extension_lite)); + EXPECT_EQ(52e3, + message.GetExtension(unittest::default_double_extension_lite)); + EXPECT_EQ(true, message.GetExtension(unittest::default_bool_extension_lite)); + EXPECT_EQ("hello", + message.GetExtension(unittest::default_string_extension_lite)); + EXPECT_EQ("world", + message.GetExtension(unittest::default_bytes_extension_lite)); + + EXPECT_EQ(unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::default_nested_enum_extension_lite)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::default_foreign_enum_extension_lite)); + EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::default_import_enum_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::oneof_uint32_extension_lite)); - EXPECT_FALSE(message.GetExtension(unittest::oneof_nested_message_extension_lite).has_bb()); + EXPECT_FALSE( + message.GetExtension(unittest::oneof_nested_message_extension_lite) + .has_bb()); EXPECT_FALSE(message.HasExtension(unittest::oneof_string_extension_lite)); EXPECT_FALSE(message.HasExtension(unittest::oneof_bytes_extension_lite)); } @@ -1316,85 +1571,164 @@ void TestUtilLite::ExpectRepeatedExtensionsModified( // ModifyRepeatedFields only sets the second repeated element of each // field. In addition to verifying this, we also verify that the first // element and size were *not* modified. - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite )); - - ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite )); - - - EXPECT_EQ(201 , message.GetExtension(unittest::repeated_int32_extension_lite , 0)); - EXPECT_EQ(202 , message.GetExtension(unittest::repeated_int64_extension_lite , 0)); - EXPECT_EQ(203 , message.GetExtension(unittest::repeated_uint32_extension_lite , 0)); - EXPECT_EQ(204 , message.GetExtension(unittest::repeated_uint64_extension_lite , 0)); - EXPECT_EQ(205 , message.GetExtension(unittest::repeated_sint32_extension_lite , 0)); - EXPECT_EQ(206 , message.GetExtension(unittest::repeated_sint64_extension_lite , 0)); - EXPECT_EQ(207 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 0)); - EXPECT_EQ(208 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 0)); - EXPECT_EQ(209 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); - EXPECT_EQ(210 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); - EXPECT_EQ(211 , message.GetExtension(unittest::repeated_float_extension_lite , 0)); - EXPECT_EQ(212 , message.GetExtension(unittest::repeated_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 0)); - EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension_lite , 0)); - EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension_lite , 0)); - - EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension_lite , 0).a()); - EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension_lite , 0).bb()); - EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0).c()); - EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension_lite , 0).d()); - EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 0).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::BAR , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 0)); - EXPECT_EQ(unittest::FOREIGN_LITE_BAR , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); - EXPECT_EQ(unittest_import::IMPORT_LITE_BAR, message.GetExtension(unittest::repeated_import_enum_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_fixed64_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed32_extension_lite)); + ASSERT_EQ(2, + message.ExtensionSize(unittest::repeated_sfixed64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension_lite)); + + ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_nested_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_foreign_message_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize( + unittest::repeated_import_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_lazy_message_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_nested_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_foreign_enum_extension_lite)); + ASSERT_EQ( + 2, message.ExtensionSize(unittest::repeated_import_enum_extension_lite)); + + + EXPECT_EQ(201, + message.GetExtension(unittest::repeated_int32_extension_lite, 0)); + EXPECT_EQ(202, + message.GetExtension(unittest::repeated_int64_extension_lite, 0)); + EXPECT_EQ(203, + message.GetExtension(unittest::repeated_uint32_extension_lite, 0)); + EXPECT_EQ(204, + message.GetExtension(unittest::repeated_uint64_extension_lite, 0)); + EXPECT_EQ(205, + message.GetExtension(unittest::repeated_sint32_extension_lite, 0)); + EXPECT_EQ(206, + message.GetExtension(unittest::repeated_sint64_extension_lite, 0)); + EXPECT_EQ(207, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 0)); + EXPECT_EQ(208, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 0)); + EXPECT_EQ( + 209, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 0)); + EXPECT_EQ( + 210, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 0)); + EXPECT_EQ(211, + message.GetExtension(unittest::repeated_float_extension_lite, 0)); + EXPECT_EQ(212, + message.GetExtension(unittest::repeated_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 0)); + EXPECT_EQ("215", + message.GetExtension(unittest::repeated_string_extension_lite, 0)); + EXPECT_EQ("216", + message.GetExtension(unittest::repeated_bytes_extension_lite, 0)); + + EXPECT_EQ( + 217, message.GetExtension(unittest::repeatedgroup_extension_lite, 0).a()); + EXPECT_EQ( + 218, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 0) + .bb()); + EXPECT_EQ( + 219, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 0) + .c()); + EXPECT_EQ( + 220, + message.GetExtension(unittest::repeated_import_message_extension_lite, 0) + .d()); + EXPECT_EQ( + 227, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 0) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::BAR, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 0)); + EXPECT_EQ( + unittest::FOREIGN_LITE_BAR, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 0)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_BAR, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(501 , message.GetExtension(unittest::repeated_int32_extension_lite , 1)); - EXPECT_EQ(502 , message.GetExtension(unittest::repeated_int64_extension_lite , 1)); - EXPECT_EQ(503 , message.GetExtension(unittest::repeated_uint32_extension_lite , 1)); - EXPECT_EQ(504 , message.GetExtension(unittest::repeated_uint64_extension_lite , 1)); - EXPECT_EQ(505 , message.GetExtension(unittest::repeated_sint32_extension_lite , 1)); - EXPECT_EQ(506 , message.GetExtension(unittest::repeated_sint64_extension_lite , 1)); - EXPECT_EQ(507 , message.GetExtension(unittest::repeated_fixed32_extension_lite , 1)); - EXPECT_EQ(508 , message.GetExtension(unittest::repeated_fixed64_extension_lite , 1)); - EXPECT_EQ(509 , message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); - EXPECT_EQ(510 , message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); - EXPECT_EQ(511 , message.GetExtension(unittest::repeated_float_extension_lite , 1)); - EXPECT_EQ(512 , message.GetExtension(unittest::repeated_double_extension_lite , 1)); - EXPECT_EQ(true , message.GetExtension(unittest::repeated_bool_extension_lite , 1)); - EXPECT_EQ("515", message.GetExtension(unittest::repeated_string_extension_lite , 1)); - EXPECT_EQ("516", message.GetExtension(unittest::repeated_bytes_extension_lite , 1)); - - EXPECT_EQ(517, message.GetExtension(unittest::repeatedgroup_extension_lite , 1).a()); - EXPECT_EQ(518, message.GetExtension(unittest::repeated_nested_message_extension_lite , 1).bb()); - EXPECT_EQ(519, message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1).c()); - EXPECT_EQ(520, message.GetExtension(unittest::repeated_import_message_extension_lite , 1).d()); - EXPECT_EQ(527, message.GetExtension(unittest::repeated_lazy_message_extension_lite , 1).bb()); - - EXPECT_EQ(unittest::TestAllTypesLite::FOO , message.GetExtension(unittest::repeated_nested_enum_extension_lite , 1)); - EXPECT_EQ(unittest::FOREIGN_LITE_FOO , message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); - EXPECT_EQ(unittest_import::IMPORT_LITE_FOO, message.GetExtension(unittest::repeated_import_enum_extension_lite , 1)); + EXPECT_EQ(501, + message.GetExtension(unittest::repeated_int32_extension_lite, 1)); + EXPECT_EQ(502, + message.GetExtension(unittest::repeated_int64_extension_lite, 1)); + EXPECT_EQ(503, + message.GetExtension(unittest::repeated_uint32_extension_lite, 1)); + EXPECT_EQ(504, + message.GetExtension(unittest::repeated_uint64_extension_lite, 1)); + EXPECT_EQ(505, + message.GetExtension(unittest::repeated_sint32_extension_lite, 1)); + EXPECT_EQ(506, + message.GetExtension(unittest::repeated_sint64_extension_lite, 1)); + EXPECT_EQ(507, + message.GetExtension(unittest::repeated_fixed32_extension_lite, 1)); + EXPECT_EQ(508, + message.GetExtension(unittest::repeated_fixed64_extension_lite, 1)); + EXPECT_EQ( + 509, message.GetExtension(unittest::repeated_sfixed32_extension_lite, 1)); + EXPECT_EQ( + 510, message.GetExtension(unittest::repeated_sfixed64_extension_lite, 1)); + EXPECT_EQ(511, + message.GetExtension(unittest::repeated_float_extension_lite, 1)); + EXPECT_EQ(512, + message.GetExtension(unittest::repeated_double_extension_lite, 1)); + EXPECT_EQ(true, + message.GetExtension(unittest::repeated_bool_extension_lite, 1)); + EXPECT_EQ("515", + message.GetExtension(unittest::repeated_string_extension_lite, 1)); + EXPECT_EQ("516", + message.GetExtension(unittest::repeated_bytes_extension_lite, 1)); + + EXPECT_EQ( + 517, message.GetExtension(unittest::repeatedgroup_extension_lite, 1).a()); + EXPECT_EQ( + 518, + message.GetExtension(unittest::repeated_nested_message_extension_lite, 1) + .bb()); + EXPECT_EQ( + 519, + message.GetExtension(unittest::repeated_foreign_message_extension_lite, 1) + .c()); + EXPECT_EQ( + 520, + message.GetExtension(unittest::repeated_import_message_extension_lite, 1) + .d()); + EXPECT_EQ( + 527, + message.GetExtension(unittest::repeated_lazy_message_extension_lite, 1) + .bb()); + + EXPECT_EQ( + unittest::TestAllTypesLite::FOO, + message.GetExtension(unittest::repeated_nested_enum_extension_lite, 1)); + EXPECT_EQ( + unittest::FOREIGN_LITE_FOO, + message.GetExtension(unittest::repeated_foreign_enum_extension_lite, 1)); + EXPECT_EQ( + unittest_import::IMPORT_LITE_FOO, + message.GetExtension(unittest::repeated_import_enum_extension_lite, 1)); } @@ -1402,55 +1736,57 @@ void TestUtilLite::ExpectRepeatedExtensionsModified( void TestUtilLite::SetPackedExtensions( unittest::TestPackedExtensionsLite* message) { - message->AddExtension(unittest::packed_int32_extension_lite , 601); - message->AddExtension(unittest::packed_int64_extension_lite , 602); - message->AddExtension(unittest::packed_uint32_extension_lite , 603); - message->AddExtension(unittest::packed_uint64_extension_lite , 604); - message->AddExtension(unittest::packed_sint32_extension_lite , 605); - message->AddExtension(unittest::packed_sint64_extension_lite , 606); - message->AddExtension(unittest::packed_fixed32_extension_lite , 607); - message->AddExtension(unittest::packed_fixed64_extension_lite , 608); + message->AddExtension(unittest::packed_int32_extension_lite, 601); + message->AddExtension(unittest::packed_int64_extension_lite, 602); + message->AddExtension(unittest::packed_uint32_extension_lite, 603); + message->AddExtension(unittest::packed_uint64_extension_lite, 604); + message->AddExtension(unittest::packed_sint32_extension_lite, 605); + message->AddExtension(unittest::packed_sint64_extension_lite, 606); + message->AddExtension(unittest::packed_fixed32_extension_lite, 607); + message->AddExtension(unittest::packed_fixed64_extension_lite, 608); message->AddExtension(unittest::packed_sfixed32_extension_lite, 609); message->AddExtension(unittest::packed_sfixed64_extension_lite, 610); - message->AddExtension(unittest::packed_float_extension_lite , 611); - message->AddExtension(unittest::packed_double_extension_lite , 612); - message->AddExtension(unittest::packed_bool_extension_lite , true); - message->AddExtension(unittest::packed_enum_extension_lite, unittest::FOREIGN_LITE_BAR); + message->AddExtension(unittest::packed_float_extension_lite, 611); + message->AddExtension(unittest::packed_double_extension_lite, 612); + message->AddExtension(unittest::packed_bool_extension_lite, true); + message->AddExtension(unittest::packed_enum_extension_lite, + unittest::FOREIGN_LITE_BAR); // add a second one of each field - message->AddExtension(unittest::packed_int32_extension_lite , 701); - message->AddExtension(unittest::packed_int64_extension_lite , 702); - message->AddExtension(unittest::packed_uint32_extension_lite , 703); - message->AddExtension(unittest::packed_uint64_extension_lite , 704); - message->AddExtension(unittest::packed_sint32_extension_lite , 705); - message->AddExtension(unittest::packed_sint64_extension_lite , 706); - message->AddExtension(unittest::packed_fixed32_extension_lite , 707); - message->AddExtension(unittest::packed_fixed64_extension_lite , 708); + message->AddExtension(unittest::packed_int32_extension_lite, 701); + message->AddExtension(unittest::packed_int64_extension_lite, 702); + message->AddExtension(unittest::packed_uint32_extension_lite, 703); + message->AddExtension(unittest::packed_uint64_extension_lite, 704); + message->AddExtension(unittest::packed_sint32_extension_lite, 705); + message->AddExtension(unittest::packed_sint64_extension_lite, 706); + message->AddExtension(unittest::packed_fixed32_extension_lite, 707); + message->AddExtension(unittest::packed_fixed64_extension_lite, 708); message->AddExtension(unittest::packed_sfixed32_extension_lite, 709); message->AddExtension(unittest::packed_sfixed64_extension_lite, 710); - message->AddExtension(unittest::packed_float_extension_lite , 711); - message->AddExtension(unittest::packed_double_extension_lite , 712); - message->AddExtension(unittest::packed_bool_extension_lite , false); - message->AddExtension(unittest::packed_enum_extension_lite, unittest::FOREIGN_LITE_BAZ); + message->AddExtension(unittest::packed_float_extension_lite, 711); + message->AddExtension(unittest::packed_double_extension_lite, 712); + message->AddExtension(unittest::packed_bool_extension_lite, false); + message->AddExtension(unittest::packed_enum_extension_lite, + unittest::FOREIGN_LITE_BAZ); } // ------------------------------------------------------------------- void TestUtilLite::ModifyPackedExtensions( unittest::TestPackedExtensionsLite* message) { - message->SetExtension(unittest::packed_int32_extension_lite , 1, 801); - message->SetExtension(unittest::packed_int64_extension_lite , 1, 802); - message->SetExtension(unittest::packed_uint32_extension_lite , 1, 803); - message->SetExtension(unittest::packed_uint64_extension_lite , 1, 804); - message->SetExtension(unittest::packed_sint32_extension_lite , 1, 805); - message->SetExtension(unittest::packed_sint64_extension_lite , 1, 806); - message->SetExtension(unittest::packed_fixed32_extension_lite , 1, 807); - message->SetExtension(unittest::packed_fixed64_extension_lite , 1, 808); + message->SetExtension(unittest::packed_int32_extension_lite, 1, 801); + message->SetExtension(unittest::packed_int64_extension_lite, 1, 802); + message->SetExtension(unittest::packed_uint32_extension_lite, 1, 803); + message->SetExtension(unittest::packed_uint64_extension_lite, 1, 804); + message->SetExtension(unittest::packed_sint32_extension_lite, 1, 805); + message->SetExtension(unittest::packed_sint64_extension_lite, 1, 806); + message->SetExtension(unittest::packed_fixed32_extension_lite, 1, 807); + message->SetExtension(unittest::packed_fixed64_extension_lite, 1, 808); message->SetExtension(unittest::packed_sfixed32_extension_lite, 1, 809); message->SetExtension(unittest::packed_sfixed64_extension_lite, 1, 810); - message->SetExtension(unittest::packed_float_extension_lite , 1, 811); - message->SetExtension(unittest::packed_double_extension_lite , 1, 812); - message->SetExtension(unittest::packed_bool_extension_lite , 1, true); - message->SetExtension(unittest::packed_enum_extension_lite , 1, + message->SetExtension(unittest::packed_float_extension_lite, 1, 811); + message->SetExtension(unittest::packed_double_extension_lite, 1, 812); + message->SetExtension(unittest::packed_bool_extension_lite, 1, true); + message->SetExtension(unittest::packed_enum_extension_lite, 1, unittest::FOREIGN_LITE_FOO); } @@ -1458,49 +1794,75 @@ void TestUtilLite::ModifyPackedExtensions( void TestUtilLite::ExpectPackedExtensionsSet( const unittest::TestPackedExtensionsLite& message) { - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite )); - - EXPECT_EQ(601 , message.GetExtension(unittest::packed_int32_extension_lite , 0)); - EXPECT_EQ(602 , message.GetExtension(unittest::packed_int64_extension_lite , 0)); - EXPECT_EQ(603 , message.GetExtension(unittest::packed_uint32_extension_lite , 0)); - EXPECT_EQ(604 , message.GetExtension(unittest::packed_uint64_extension_lite , 0)); - EXPECT_EQ(605 , message.GetExtension(unittest::packed_sint32_extension_lite , 0)); - EXPECT_EQ(606 , message.GetExtension(unittest::packed_sint64_extension_lite , 0)); - EXPECT_EQ(607 , message.GetExtension(unittest::packed_fixed32_extension_lite , 0)); - EXPECT_EQ(608 , message.GetExtension(unittest::packed_fixed64_extension_lite , 0)); - EXPECT_EQ(609 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); - EXPECT_EQ(610 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); - EXPECT_EQ(611 , message.GetExtension(unittest::packed_float_extension_lite , 0)); - EXPECT_EQ(612 , message.GetExtension(unittest::packed_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite)); + + EXPECT_EQ(601, + message.GetExtension(unittest::packed_int32_extension_lite, 0)); + EXPECT_EQ(602, + message.GetExtension(unittest::packed_int64_extension_lite, 0)); + EXPECT_EQ(603, + message.GetExtension(unittest::packed_uint32_extension_lite, 0)); + EXPECT_EQ(604, + message.GetExtension(unittest::packed_uint64_extension_lite, 0)); + EXPECT_EQ(605, + message.GetExtension(unittest::packed_sint32_extension_lite, 0)); + EXPECT_EQ(606, + message.GetExtension(unittest::packed_sint64_extension_lite, 0)); + EXPECT_EQ(607, + message.GetExtension(unittest::packed_fixed32_extension_lite, 0)); + EXPECT_EQ(608, + message.GetExtension(unittest::packed_fixed64_extension_lite, 0)); + EXPECT_EQ(609, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); + EXPECT_EQ(610, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); + EXPECT_EQ(611, + message.GetExtension(unittest::packed_float_extension_lite, 0)); + EXPECT_EQ(612, + message.GetExtension(unittest::packed_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.GetExtension(unittest::packed_enum_extension_lite, 0)); - EXPECT_EQ(701 , message.GetExtension(unittest::packed_int32_extension_lite , 1)); - EXPECT_EQ(702 , message.GetExtension(unittest::packed_int64_extension_lite , 1)); - EXPECT_EQ(703 , message.GetExtension(unittest::packed_uint32_extension_lite , 1)); - EXPECT_EQ(704 , message.GetExtension(unittest::packed_uint64_extension_lite , 1)); - EXPECT_EQ(705 , message.GetExtension(unittest::packed_sint32_extension_lite , 1)); - EXPECT_EQ(706 , message.GetExtension(unittest::packed_sint64_extension_lite , 1)); - EXPECT_EQ(707 , message.GetExtension(unittest::packed_fixed32_extension_lite , 1)); - EXPECT_EQ(708 , message.GetExtension(unittest::packed_fixed64_extension_lite , 1)); - EXPECT_EQ(709 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); - EXPECT_EQ(710 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); - EXPECT_EQ(711 , message.GetExtension(unittest::packed_float_extension_lite , 1)); - EXPECT_EQ(712 , message.GetExtension(unittest::packed_double_extension_lite , 1)); - EXPECT_EQ(false, message.GetExtension(unittest::packed_bool_extension_lite , 1)); + EXPECT_EQ(701, + message.GetExtension(unittest::packed_int32_extension_lite, 1)); + EXPECT_EQ(702, + message.GetExtension(unittest::packed_int64_extension_lite, 1)); + EXPECT_EQ(703, + message.GetExtension(unittest::packed_uint32_extension_lite, 1)); + EXPECT_EQ(704, + message.GetExtension(unittest::packed_uint64_extension_lite, 1)); + EXPECT_EQ(705, + message.GetExtension(unittest::packed_sint32_extension_lite, 1)); + EXPECT_EQ(706, + message.GetExtension(unittest::packed_sint64_extension_lite, 1)); + EXPECT_EQ(707, + message.GetExtension(unittest::packed_fixed32_extension_lite, 1)); + EXPECT_EQ(708, + message.GetExtension(unittest::packed_fixed64_extension_lite, 1)); + EXPECT_EQ(709, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); + EXPECT_EQ(710, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); + EXPECT_EQ(711, + message.GetExtension(unittest::packed_float_extension_lite, 1)); + EXPECT_EQ(712, + message.GetExtension(unittest::packed_double_extension_lite, 1)); + EXPECT_EQ(false, + message.GetExtension(unittest::packed_bool_extension_lite, 1)); EXPECT_EQ(unittest::FOREIGN_LITE_BAZ, message.GetExtension(unittest::packed_enum_extension_lite, 1)); } @@ -1509,70 +1871,96 @@ void TestUtilLite::ExpectPackedExtensionsSet( void TestUtilLite::ExpectPackedExtensionsClear( const unittest::TestPackedExtensionsLite& message) { - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_float_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_double_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_bool_extension_lite )); - EXPECT_EQ(0, message.ExtensionSize(unittest::packed_enum_extension_lite )); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_float_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_double_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_bool_extension_lite)); + EXPECT_EQ(0, message.ExtensionSize(unittest::packed_enum_extension_lite)); } // ------------------------------------------------------------------- void TestUtilLite::ExpectPackedExtensionsModified( const unittest::TestPackedExtensionsLite& message) { - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite )); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension_lite)); ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension_lite)); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite )); - ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite )); - EXPECT_EQ(601 , message.GetExtension(unittest::packed_int32_extension_lite , 0)); - EXPECT_EQ(602 , message.GetExtension(unittest::packed_int64_extension_lite , 0)); - EXPECT_EQ(603 , message.GetExtension(unittest::packed_uint32_extension_lite , 0)); - EXPECT_EQ(604 , message.GetExtension(unittest::packed_uint64_extension_lite , 0)); - EXPECT_EQ(605 , message.GetExtension(unittest::packed_sint32_extension_lite , 0)); - EXPECT_EQ(606 , message.GetExtension(unittest::packed_sint64_extension_lite , 0)); - EXPECT_EQ(607 , message.GetExtension(unittest::packed_fixed32_extension_lite , 0)); - EXPECT_EQ(608 , message.GetExtension(unittest::packed_fixed64_extension_lite , 0)); - EXPECT_EQ(609 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); - EXPECT_EQ(610 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); - EXPECT_EQ(611 , message.GetExtension(unittest::packed_float_extension_lite , 0)); - EXPECT_EQ(612 , message.GetExtension(unittest::packed_double_extension_lite , 0)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 0)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension_lite)); + ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension_lite)); + EXPECT_EQ(601, + message.GetExtension(unittest::packed_int32_extension_lite, 0)); + EXPECT_EQ(602, + message.GetExtension(unittest::packed_int64_extension_lite, 0)); + EXPECT_EQ(603, + message.GetExtension(unittest::packed_uint32_extension_lite, 0)); + EXPECT_EQ(604, + message.GetExtension(unittest::packed_uint64_extension_lite, 0)); + EXPECT_EQ(605, + message.GetExtension(unittest::packed_sint32_extension_lite, 0)); + EXPECT_EQ(606, + message.GetExtension(unittest::packed_sint64_extension_lite, 0)); + EXPECT_EQ(607, + message.GetExtension(unittest::packed_fixed32_extension_lite, 0)); + EXPECT_EQ(608, + message.GetExtension(unittest::packed_fixed64_extension_lite, 0)); + EXPECT_EQ(609, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 0)); + EXPECT_EQ(610, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 0)); + EXPECT_EQ(611, + message.GetExtension(unittest::packed_float_extension_lite, 0)); + EXPECT_EQ(612, + message.GetExtension(unittest::packed_double_extension_lite, 0)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 0)); EXPECT_EQ(unittest::FOREIGN_LITE_BAR, message.GetExtension(unittest::packed_enum_extension_lite, 0)); // Actually verify the second (modified) elements now. - EXPECT_EQ(801 , message.GetExtension(unittest::packed_int32_extension_lite , 1)); - EXPECT_EQ(802 , message.GetExtension(unittest::packed_int64_extension_lite , 1)); - EXPECT_EQ(803 , message.GetExtension(unittest::packed_uint32_extension_lite , 1)); - EXPECT_EQ(804 , message.GetExtension(unittest::packed_uint64_extension_lite , 1)); - EXPECT_EQ(805 , message.GetExtension(unittest::packed_sint32_extension_lite , 1)); - EXPECT_EQ(806 , message.GetExtension(unittest::packed_sint64_extension_lite , 1)); - EXPECT_EQ(807 , message.GetExtension(unittest::packed_fixed32_extension_lite , 1)); - EXPECT_EQ(808 , message.GetExtension(unittest::packed_fixed64_extension_lite , 1)); - EXPECT_EQ(809 , message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); - EXPECT_EQ(810 , message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); - EXPECT_EQ(811 , message.GetExtension(unittest::packed_float_extension_lite , 1)); - EXPECT_EQ(812 , message.GetExtension(unittest::packed_double_extension_lite , 1)); - EXPECT_EQ(true , message.GetExtension(unittest::packed_bool_extension_lite , 1)); + EXPECT_EQ(801, + message.GetExtension(unittest::packed_int32_extension_lite, 1)); + EXPECT_EQ(802, + message.GetExtension(unittest::packed_int64_extension_lite, 1)); + EXPECT_EQ(803, + message.GetExtension(unittest::packed_uint32_extension_lite, 1)); + EXPECT_EQ(804, + message.GetExtension(unittest::packed_uint64_extension_lite, 1)); + EXPECT_EQ(805, + message.GetExtension(unittest::packed_sint32_extension_lite, 1)); + EXPECT_EQ(806, + message.GetExtension(unittest::packed_sint64_extension_lite, 1)); + EXPECT_EQ(807, + message.GetExtension(unittest::packed_fixed32_extension_lite, 1)); + EXPECT_EQ(808, + message.GetExtension(unittest::packed_fixed64_extension_lite, 1)); + EXPECT_EQ(809, + message.GetExtension(unittest::packed_sfixed32_extension_lite, 1)); + EXPECT_EQ(810, + message.GetExtension(unittest::packed_sfixed64_extension_lite, 1)); + EXPECT_EQ(811, + message.GetExtension(unittest::packed_float_extension_lite, 1)); + EXPECT_EQ(812, + message.GetExtension(unittest::packed_double_extension_lite, 1)); + EXPECT_EQ(true, + message.GetExtension(unittest::packed_bool_extension_lite, 1)); EXPECT_EQ(unittest::FOREIGN_LITE_FOO, message.GetExtension(unittest::packed_enum_extension_lite, 1)); } diff --git a/src/google/protobuf/testing/file.cc b/src/google/protobuf/testing/file.cc index 06cc981c24..7d3708e3d6 100644 --- a/src/google/protobuf/testing/file.cc +++ b/src/google/protobuf/testing/file.cc @@ -45,7 +45,7 @@ #endif #include <errno.h> -#include <google/protobuf/stubs/io_win32.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/stubs/logging.h> namespace google { @@ -59,11 +59,11 @@ namespace protobuf { #endif #ifdef _WIN32 -using google::protobuf::internal::win32::access; -using google::protobuf::internal::win32::chdir; -using google::protobuf::internal::win32::fopen; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::stat; +using google::protobuf::io::win32::access; +using google::protobuf::io::win32::chdir; +using google::protobuf::io::win32::fopen; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::stat; #endif bool File::Exists(const string& name) { diff --git a/src/google/protobuf/testing/googletest.cc b/src/google/protobuf/testing/googletest.cc index 5c89c0f41c..aca089c800 100644 --- a/src/google/protobuf/testing/googletest.cc +++ b/src/google/protobuf/testing/googletest.cc @@ -33,7 +33,7 @@ #include <google/protobuf/testing/googletest.h> #include <google/protobuf/testing/file.h> -#include <google/protobuf/stubs/io_win32.h> +#include <google/protobuf/io/io_win32.h> #include <google/protobuf/stubs/strutil.h> #include <sys/stat.h> #include <sys/types.h> @@ -55,11 +55,11 @@ namespace protobuf { #ifdef _WIN32 // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import // them like we do below. -using google::protobuf::internal::win32::close; -using google::protobuf::internal::win32::dup2; -using google::protobuf::internal::win32::dup; -using google::protobuf::internal::win32::mkdir; -using google::protobuf::internal::win32::open; +using google::protobuf::io::win32::close; +using google::protobuf::io::win32::dup2; +using google::protobuf::io::win32::dup; +using google::protobuf::io::win32::mkdir; +using google::protobuf::io::win32::open; #endif #ifndef O_BINARY diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h index 4c7c33b466..f999f4727e 100644 --- a/src/google/protobuf/text_format.h +++ b/src/google/protobuf/text_format.h @@ -552,6 +552,13 @@ class PROTOBUF_EXPORT TextFormat { // a warning message will be generated. void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; } + // When an unknown field is met, parsing will fail if this option is set + // to false(the default). If true, unknown fields will be ignored and + // a warning message will be generated. + // Please aware that set this option true may hide some errors (e.g. + // spelling error on field name). Avoid to use this option if possible. + void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; } + void AllowFieldNumber(bool allow) { allow_field_number_ = allow; } diff --git a/src/google/protobuf/text_format_unittest.cc b/src/google/protobuf/text_format_unittest.cc index 0bed993f33..7faf5c009c 100644 --- a/src/google/protobuf/text_format_unittest.cc +++ b/src/google/protobuf/text_format_unittest.cc @@ -48,9 +48,9 @@ #include <google/protobuf/test_util.h> #include <google/protobuf/test_util2.h> #include <google/protobuf/unittest.pb.h> -#include <google/protobuf/unittest_proto3.pb.h> #include <google/protobuf/unittest_mset.pb.h> #include <google/protobuf/unittest_mset_wire_format.pb.h> +#include <google/protobuf/unittest_proto3.pb.h> #include <google/protobuf/io/tokenizer.h> #include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/stubs/strutil.h> @@ -142,10 +142,11 @@ TEST_F(TextFormatTest, ShortDebugString) { proto_.mutable_optional_nested_message()->set_bb(2); proto_.mutable_optional_foreign_message(); - EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" " - "optional_nested_message { bb: 2 } " - "optional_foreign_message { }", - proto_.ShortDebugString()); + EXPECT_EQ( + "optional_int32: 1 optional_string: \"hello\" " + "optional_nested_message { bb: 2 } " + "optional_foreign_message { }", + proto_.ShortDebugString()); } TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { @@ -164,41 +165,44 @@ TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { std::string text; EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123\n" - "repeated_int32: [456, 789]\n" - "repeated_string: \"foo\"\n" - "repeated_string: \"bar\"\n" - "repeated_nested_message {\n bb: 2\n}\n" - "repeated_nested_message {\n bb: 3\n}\n" - "repeated_nested_enum: [FOO, BAR]\n", - text); + EXPECT_EQ( + "optional_int32: 123\n" + "repeated_int32: [456, 789]\n" + "repeated_string: \"foo\"\n" + "repeated_string: \"bar\"\n" + "repeated_nested_message {\n bb: 2\n}\n" + "repeated_nested_message {\n bb: 3\n}\n" + "repeated_nested_enum: [FOO, BAR]\n", + text); // Verify that any existing data in the string is cleared when PrintToString() // is called. text = "just some data here...\n\nblah blah"; EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123\n" - "repeated_int32: [456, 789]\n" - "repeated_string: \"foo\"\n" - "repeated_string: \"bar\"\n" - "repeated_nested_message {\n bb: 2\n}\n" - "repeated_nested_message {\n bb: 3\n}\n" - "repeated_nested_enum: [FOO, BAR]\n", - text); + EXPECT_EQ( + "optional_int32: 123\n" + "repeated_int32: [456, 789]\n" + "repeated_string: \"foo\"\n" + "repeated_string: \"bar\"\n" + "repeated_nested_message {\n bb: 2\n}\n" + "repeated_nested_message {\n bb: 3\n}\n" + "repeated_nested_enum: [FOO, BAR]\n", + text); // Try in single-line mode. printer.SetSingleLineMode(true); EXPECT_TRUE(printer.PrintToString(proto_, &text)); - EXPECT_EQ("optional_int32: 123 " - "repeated_int32: [456, 789] " - "repeated_string: \"foo\" " - "repeated_string: \"bar\" " - "repeated_nested_message { bb: 2 } " - "repeated_nested_message { bb: 3 } " - "repeated_nested_enum: [FOO, BAR] ", - text); + EXPECT_EQ( + "optional_int32: 123 " + "repeated_int32: [456, 789] " + "repeated_string: \"foo\" " + "repeated_string: \"bar\" " + "repeated_nested_message { bb: 2 } " + "repeated_nested_message { bb: 3 } " + "repeated_nested_enum: [FOO, BAR] ", + text); } @@ -272,17 +276,17 @@ TEST_F(TextFormatTest, PrintUnknownFields) { unknown_fields->AddVarint(8, 3); EXPECT_EQ( - "5: 1\n" - "5: 0x00000002\n" - "5: 0x0000000000000003\n" - "5: \"4\"\n" - "5 {\n" - " 10: 5\n" - "}\n" - "8: 1\n" - "8: 2\n" - "8: 3\n", - message.DebugString()); + "5: 1\n" + "5: 0x00000002\n" + "5: 0x0000000000000003\n" + "5: \"4\"\n" + "5 {\n" + " 10: 5\n" + "}\n" + "8: 1\n" + "8: 2\n" + "8: 3\n", + message.DebugString()); } TEST_F(TextFormatTest, PrintUnknownFieldsHidden) { @@ -345,13 +349,13 @@ TEST_F(TextFormatTest, PrintUnknownMessage) { EXPECT_TRUE(unknown_fields.ParseFromString(data)); EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text)); EXPECT_EQ( - "44: \"abc\"\n" - "44: \"def\"\n" - "44: \"\"\n" - "48 {\n" - " 1: 123\n" - "}\n", - text); + "44: \"abc\"\n" + "44: \"def\"\n" + "44: \"\"\n" + "48 {\n" + " 1: 123\n" + "}\n", + text); } TEST_F(TextFormatTest, PrintMessageWithIndent) { @@ -368,12 +372,12 @@ TEST_F(TextFormatTest, PrintMessageWithIndent) { printer.SetInitialIndentLevel(1); EXPECT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( - " repeated_string: \"abc\"\n" - " repeated_string: \"def\"\n" - " repeated_nested_message {\n" - " bb: 123\n" - " }\n", - text); + " repeated_string: \"abc\"\n" + " repeated_string: \"def\"\n" + " repeated_nested_message {\n" + " bb: 123\n" + " }\n", + text); } TEST_F(TextFormatTest, PrintMessageSingleLine) { @@ -391,9 +395,9 @@ TEST_F(TextFormatTest, PrintMessageSingleLine) { printer.SetSingleLineMode(true); EXPECT_TRUE(printer.PrintToString(message, &text)); EXPECT_EQ( - " repeated_string: \"abc\" repeated_string: \"def\" " - "repeated_nested_message { bb: 123 } ", - text); + " repeated_string: \"abc\" repeated_string: \"def\" " + "repeated_nested_message { bb: 123 } ", + text); } TEST_F(TextFormatTest, PrintBufferTooSmall) { @@ -778,8 +782,7 @@ TEST_F(TextFormatTest, ParseStringEscape) { std::string parse_string = "optional_string: " + kEscapeTestStringEscaped + "\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); // Compare. @@ -790,19 +793,18 @@ TEST_F(TextFormatTest, ParseConcatenatedString) { // Create a parse string with multiple parts on one line. std::string parse_string = "optional_string: \"foo\" \"bar\"\n"; - io::ArrayInputStream input_stream1(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream1, &proto_); // Compare. EXPECT_EQ("foobar", proto_.optional_string()); // Create a parse string with multiple parts on separate lines. - parse_string = "optional_string: \"foo\"\n" - "\"bar\"\n"; + parse_string = + "optional_string: \"foo\"\n" + "\"bar\"\n"; - io::ArrayInputStream input_stream2(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream2, &proto_); // Compare. @@ -816,8 +818,7 @@ TEST_F(TextFormatTest, ParseFloatWithSuffix) { // Have it parse a float with the 'f' suffix. std::string parse_string = "optional_float: 1.0f\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -954,8 +955,7 @@ TEST_F(TextFormatTest, Comments) { "optional_int32: 1 # a comment\n" "optional_int64: 2 # another comment"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -970,8 +970,7 @@ TEST_F(TextFormatTest, OptionalColon) { std::string parse_string = "optional_nested_message: { bb: 1}\n"; - io::ArrayInputStream input_stream(parse_string.data(), - parse_string.size()); + io::ArrayInputStream input_stream(parse_string.data(), parse_string.size()); TextFormat::Parse(&input_stream, &proto_); @@ -1018,16 +1017,17 @@ TEST_F(TextFormatTest, PrintExotic) { // have this problem, so we switched to that instead. EXPECT_EQ( - "repeated_int64: -9223372036854775808\n" - "repeated_uint64: 18446744073709551615\n" - "repeated_double: 123.456\n" - "repeated_double: 1.23e+21\n" - "repeated_double: 1.23e-18\n" - "repeated_double: inf\n" - "repeated_double: -inf\n" - "repeated_double: nan\n" - "repeated_string: \"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", - RemoveRedundantZeros(message.DebugString())); + "repeated_int64: -9223372036854775808\n" + "repeated_uint64: 18446744073709551615\n" + "repeated_double: 123.456\n" + "repeated_double: 1.23e+21\n" + "repeated_double: 1.23e-18\n" + "repeated_double: inf\n" + "repeated_double: -inf\n" + "repeated_double: nan\n" + "repeated_string: " + "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", + RemoveRedundantZeros(message.DebugString())); } TEST_F(TextFormatTest, PrintFloatPrecision) { @@ -1074,49 +1074,48 @@ TEST_F(TextFormatTest, PrintFloatPrecision) { message.add_repeated_double(1.23456789876543e100); EXPECT_EQ( - "repeated_float: 1\n" - "repeated_float: 1.2\n" - "repeated_float: 1.23\n" - "repeated_float: 1.234\n" - "repeated_float: 1.2345\n" - "repeated_float: 1.23456\n" - "repeated_float: 1.2e+10\n" - "repeated_float: 1.23e+10\n" - "repeated_float: 1.234e+10\n" - "repeated_float: 1.2345e+10\n" - "repeated_float: 1.23456e+10\n" - "repeated_double: 1.2\n" - "repeated_double: 1.23\n" - "repeated_double: 1.234\n" - "repeated_double: 1.2345\n" - "repeated_double: 1.23456\n" - "repeated_double: 1.234567\n" - "repeated_double: 1.2345678\n" - "repeated_double: 1.23456789\n" - "repeated_double: 1.234567898\n" - "repeated_double: 1.2345678987\n" - "repeated_double: 1.23456789876\n" - "repeated_double: 1.234567898765\n" - "repeated_double: 1.2345678987654\n" - "repeated_double: 1.23456789876543\n" - "repeated_double: 1.2e+100\n" - "repeated_double: 1.23e+100\n" - "repeated_double: 1.234e+100\n" - "repeated_double: 1.2345e+100\n" - "repeated_double: 1.23456e+100\n" - "repeated_double: 1.234567e+100\n" - "repeated_double: 1.2345678e+100\n" - "repeated_double: 1.23456789e+100\n" - "repeated_double: 1.234567898e+100\n" - "repeated_double: 1.2345678987e+100\n" - "repeated_double: 1.23456789876e+100\n" - "repeated_double: 1.234567898765e+100\n" - "repeated_double: 1.2345678987654e+100\n" - "repeated_double: 1.23456789876543e+100\n", - RemoveRedundantZeros(message.DebugString())); + "repeated_float: 1\n" + "repeated_float: 1.2\n" + "repeated_float: 1.23\n" + "repeated_float: 1.234\n" + "repeated_float: 1.2345\n" + "repeated_float: 1.23456\n" + "repeated_float: 1.2e+10\n" + "repeated_float: 1.23e+10\n" + "repeated_float: 1.234e+10\n" + "repeated_float: 1.2345e+10\n" + "repeated_float: 1.23456e+10\n" + "repeated_double: 1.2\n" + "repeated_double: 1.23\n" + "repeated_double: 1.234\n" + "repeated_double: 1.2345\n" + "repeated_double: 1.23456\n" + "repeated_double: 1.234567\n" + "repeated_double: 1.2345678\n" + "repeated_double: 1.23456789\n" + "repeated_double: 1.234567898\n" + "repeated_double: 1.2345678987\n" + "repeated_double: 1.23456789876\n" + "repeated_double: 1.234567898765\n" + "repeated_double: 1.2345678987654\n" + "repeated_double: 1.23456789876543\n" + "repeated_double: 1.2e+100\n" + "repeated_double: 1.23e+100\n" + "repeated_double: 1.234e+100\n" + "repeated_double: 1.2345e+100\n" + "repeated_double: 1.23456e+100\n" + "repeated_double: 1.234567e+100\n" + "repeated_double: 1.2345678e+100\n" + "repeated_double: 1.23456789e+100\n" + "repeated_double: 1.234567898e+100\n" + "repeated_double: 1.2345678987e+100\n" + "repeated_double: 1.23456789876e+100\n" + "repeated_double: 1.234567898765e+100\n" + "repeated_double: 1.2345678987654e+100\n" + "repeated_double: 1.23456789876543e+100\n", + RemoveRedundantZeros(message.DebugString())); } - TEST_F(TextFormatTest, AllowPartial) { unittest::TestRequired message; TextFormat::Parser parser; @@ -1130,29 +1129,29 @@ TEST_F(TextFormatTest, AllowPartial) { TEST_F(TextFormatTest, ParseExotic) { unittest::TestAllTypes message; ASSERT_TRUE(TextFormat::ParseFromString( - "repeated_int32: -1\n" - "repeated_int32: -2147483648\n" - "repeated_int64: -1\n" - "repeated_int64: -9223372036854775808\n" - "repeated_uint32: 4294967295\n" - "repeated_uint32: 2147483648\n" - "repeated_uint64: 18446744073709551615\n" - "repeated_uint64: 9223372036854775808\n" - "repeated_double: 123.0\n" - "repeated_double: 123.5\n" - "repeated_double: 0.125\n" - "repeated_double: 1.23E17\n" - "repeated_double: 1.235E+22\n" - "repeated_double: 1.235e-18\n" - "repeated_double: 123.456789\n" - "repeated_double: inf\n" - "repeated_double: Infinity\n" - "repeated_double: -inf\n" - "repeated_double: -Infinity\n" - "repeated_double: nan\n" - "repeated_double: NaN\n" - "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", - &message)); + "repeated_int32: -1\n" + "repeated_int32: -2147483648\n" + "repeated_int64: -1\n" + "repeated_int64: -9223372036854775808\n" + "repeated_uint32: 4294967295\n" + "repeated_uint32: 2147483648\n" + "repeated_uint64: 18446744073709551615\n" + "repeated_uint64: 9223372036854775808\n" + "repeated_double: 123.0\n" + "repeated_double: 123.5\n" + "repeated_double: 0.125\n" + "repeated_double: 1.23E17\n" + "repeated_double: 1.235E+22\n" + "repeated_double: 1.235e-18\n" + "repeated_double: 123.456789\n" + "repeated_double: inf\n" + "repeated_double: Infinity\n" + "repeated_double: -inf\n" + "repeated_double: -Infinity\n" + "repeated_double: nan\n" + "repeated_double: NaN\n" + "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", + &message)); ASSERT_EQ(2, message.repeated_int32_size()); EXPECT_EQ(-1, message.repeated_int32(0)); @@ -1184,12 +1183,12 @@ TEST_F(TextFormatTest, ParseExotic) { message.repeated_uint64(1)); ASSERT_EQ(13, message.repeated_double_size()); - EXPECT_EQ(123.0 , message.repeated_double(0)); - EXPECT_EQ(123.5 , message.repeated_double(1)); - EXPECT_EQ(0.125 , message.repeated_double(2)); - EXPECT_EQ(1.23E17 , message.repeated_double(3)); - EXPECT_EQ(1.235E22 , message.repeated_double(4)); - EXPECT_EQ(1.235E-18 , message.repeated_double(5)); + EXPECT_EQ(123.0, message.repeated_double(0)); + EXPECT_EQ(123.5, message.repeated_double(1)); + EXPECT_EQ(0.125, message.repeated_double(2)); + EXPECT_EQ(1.23E17, message.repeated_double(3)); + EXPECT_EQ(1.235E22, message.repeated_double(4)); + EXPECT_EQ(1.235E-18, message.repeated_double(5)); EXPECT_EQ(123.456789, message.repeated_double(6)); EXPECT_EQ(message.repeated_double(7), std::numeric_limits<double>::infinity()); @@ -1207,14 +1206,26 @@ TEST_F(TextFormatTest, ParseExotic) { ASSERT_EQ(1, message.repeated_string_size()); EXPECT_EQ(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), message.repeated_string(0)); + + ASSERT_TRUE( + TextFormat::ParseFromString("repeated_float: 3.4028235e+38\n" + "repeated_float: -3.4028235e+38\n" + "repeated_float: 3.40282351e+38\n" + "repeated_float: -3.40282351e+38\n", + &message)); + EXPECT_EQ(message.repeated_float(0), std::numeric_limits<float>::max()); + EXPECT_EQ(message.repeated_float(1), -std::numeric_limits<float>::max()); + EXPECT_EQ(message.repeated_float(2), std::numeric_limits<float>::infinity()); + EXPECT_EQ(message.repeated_float(3), -std::numeric_limits<float>::infinity()); + } TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { protobuf_unittest::TestFieldOrderings message; // Fields are listed in index order instead of field number. - message.set_my_string("str"); // Field number 11 - message.set_my_int(12345); // Field number 1 - message.set_my_float(0.999); // Field number 101 + message.set_my_string("str"); // Field number 11 + message.set_my_int(12345); // Field number 1 + message.set_my_float(0.999); // Field number 101 // Extensions are listed based on the order of extension number. // Extension number 12. message @@ -1332,8 +1343,8 @@ class TextFormatParserTest : public testing::Test { void ExpectLocation(TextFormat::ParseInfoTree* tree, const Descriptor* d, const std::string& field_name, int index, int line, int column) { - TextFormat::ParseLocation location = tree->GetLocation( - d->FindFieldByName(field_name), index); + TextFormat::ParseLocation location = + tree->GetLocation(d->FindFieldByName(field_name), index); EXPECT_EQ(line, location.line); EXPECT_EQ(column, location.column); } @@ -1349,8 +1360,8 @@ class TextFormatParserTest : public testing::Test { // implements ErrorCollector ------------------------------------- void AddError(int line, int column, const std::string& message) { - strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", - line + 1, column + 1, message); + strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line + 1, column + 1, + message); } void AddWarning(int line, int column, const std::string& message) { @@ -1429,33 +1440,33 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); const Descriptor* d = message->GetDescriptor(); -#define EXPECT_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_EQ(value, message->optional_##name()); \ +#define EXPECT_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_BOOL_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_TRUE(message->optional_##name() == value); \ +#define EXPECT_BOOL_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_TRUE(message->optional_##name() == value); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_FLOAT_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_FLOAT_EQ(value, message->optional_##name()); \ +#define EXPECT_FLOAT_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_FLOAT_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ - EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); \ - EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ +#define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); -#define EXPECT_INVALID(name, valuestring) \ +#define EXPECT_INVALID(name, valuestring) \ EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \ - valuestring, d->FindFieldByName("optional_" #name), message.get())); + valuestring, d->FindFieldByName("optional_" #name), message.get())); // int32 EXPECT_FIELD(int32, 1, "1"); @@ -1545,8 +1556,8 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { // message EXPECT_TRUE(TextFormat::ParseFieldValueFromString( - "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get())); - EXPECT_EQ(12, message->optional_nested_message().bb()); \ + "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get())); + EXPECT_EQ(12, message->optional_nested_message().bb()); EXPECT_TRUE(message->has_optional_nested_message()); EXPECT_INVALID(nested_message, "any"); @@ -1557,15 +1568,13 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { #undef EXPECT_INVALID } - TEST_F(TextFormatParserTest, InvalidToken) { - ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", - 2, 1); + ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", 2, + 1); - ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, - 20); - ExpectFailure("\"some string\"", - "Expected identifier, got: \"some string\"", 1, 1); + ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, 20); + ExpectFailure("\"some string\"", "Expected identifier, got: \"some string\"", + 1, 1); } TEST_F(TextFormatParserTest, InvalidFieldName) { @@ -1615,11 +1624,10 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for a double/float field. ExpectFailure("optional_double: \"hello\"\n", "Expected double, got: \"hello\"", 1, 18); - ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, - 18); + ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, 18); ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18); - ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); // Invalid values for a signed integer field. ExpectFailure("optional_int32: \"hello\"\n", @@ -1627,8 +1635,8 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17); ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17); ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17); - ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", - 1, 16); + ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1, + 16); ExpectFailure("optional_int32: 0x80000000\n", "Integer out of range (0x80000000)", 1, 17); ExpectFailure("optional_int64: 0x8000000000000000\n", @@ -1641,13 +1649,13 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for an unsigned integer field. ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer, got: \"hello\"", 1, 18); - ExpectFailure("optional_uint64: true\n", - "Expected integer, got: true", 1, 18); + ExpectFailure("optional_uint64: true\n", "Expected integer, got: true", 1, + 18); ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18); ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18); ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18); - ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); ExpectFailure("optional_uint32: 0x100000000\n", "Integer out of range (0x100000000)", 1, 18); ExpectFailure("optional_uint64: 0x10000000000000000\n", @@ -1662,19 +1670,19 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure( "optional_bool: meh\n", - "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", - 2, 1); + "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2, + 1); - ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", - 1, 15); + ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1, + 15); // Invalid values for a string field. ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18); ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18); ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18); ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18); - ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", - 1, 17); + ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1, + 17); // Invalid values for an enumeration field. ExpectFailure("optional_nested_enum: \"hello\"\n", @@ -1683,7 +1691,8 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Valid token, but enum value is not defined. ExpectFailure("optional_nested_enum: 5\n", "Unknown enumeration value of \"5\" for field " - "\"optional_nested_enum\".", 2, 1); + "\"optional_nested_enum\".", + 2, 1); // We consume the negative sign, so the error position starts one character // later. ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1, @@ -1691,29 +1700,27 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { ExpectFailure("optional_nested_enum: !\n", "Expected integer or identifier, got: !", 1, 23); - ExpectFailure( - "optional_nested_enum: grah\n", - "Unknown enumeration value of \"grah\" for field " - "\"optional_nested_enum\".", 2, 1); + ExpectFailure("optional_nested_enum: grah\n", + "Unknown enumeration value of \"grah\" for field " + "\"optional_nested_enum\".", + 2, 1); - ExpectFailure( - "optional_nested_enum {\n \n}\n", - "Expected \":\", found \"{\".", 1, 22); + ExpectFailure("optional_nested_enum {\n \n}\n", + "Expected \":\", found \"{\".", 1, 22); } TEST_F(TextFormatParserTest, MessageDelimiters) { // Non-matching delimiters. - ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", - 3, 1); + ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 3, + 1); // Invalid delimiters. - ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", - 1, 15); + ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1, + 15); // Unending message. ExpectFailure("optional_nested_message {\n \nbb: 118\n", - "Expected identifier, got: ", - 4, 1); + "Expected identifier, got: ", 4, 1); } TEST_F(TextFormatParserTest, UnknownExtension) { @@ -1726,23 +1733,22 @@ TEST_F(TextFormatParserTest, UnknownExtension) { TEST_F(TextFormatParserTest, MissingRequired) { unittest::TestRequired message; - ExpectFailure("a: 1", - "Message missing required fields: b, c", - 0, 1, &message); + ExpectFailure("a: 1", "Message missing required fields: b, c", 0, 1, + &message); } TEST_F(TextFormatParserTest, ParseDuplicateRequired) { unittest::TestRequired message; ExpectFailure("a: 1 b: 2 c: 3 a: 1", - "Non-repeated field \"a\" is specified multiple times.", - 1, 17, &message); + "Non-repeated field \"a\" is specified multiple times.", 1, 17, + &message); } TEST_F(TextFormatParserTest, ParseDuplicateOptional) { unittest::ForeignMessage message; ExpectFailure("c: 1 c: 2", - "Non-repeated field \"c\" is specified multiple times.", - 1, 7, &message); + "Non-repeated field \"c\" is specified multiple times.", 1, 7, + &message); } TEST_F(TextFormatParserTest, MergeDuplicateRequired) { @@ -1778,10 +1784,11 @@ TEST_F(TextFormatParserTest, PrintErrorsToStderr) { } ASSERT_EQ(1, errors.size()); - EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " - "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " - "named \"no_such_field\".", - errors[0]); + EXPECT_EQ( + "Error parsing text-format protobuf_unittest.TestAllTypes: " + "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " + "named \"no_such_field\".", + errors[0]); } TEST_F(TextFormatParserTest, FailsOnTokenizationError) { @@ -1795,16 +1802,18 @@ TEST_F(TextFormatParserTest, FailsOnTokenizationError) { } ASSERT_EQ(1, errors.size()); - EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " - "1:1: Invalid control characters encountered in text.", - errors[0]); + EXPECT_EQ( + "Error parsing text-format protobuf_unittest.TestAllTypes: " + "1:1: Invalid control characters encountered in text.", + errors[0]); } TEST_F(TextFormatParserTest, ParseDeprecatedField) { unittest::TestDeprecatedFields message; ExpectMessage("deprecated_int32: 42", "WARNING:text format contains deprecated field " - "\"deprecated_int32\"", 1, 21, &message, true); + "\"deprecated_int32\"", + 1, 21, &message, true); } TEST_F(TextFormatParserTest, SetRecursionLimit) { @@ -1828,25 +1837,24 @@ class TextFormatMessageSetTest : public testing::Test { static const char proto_debug_string_[]; }; const char TextFormatMessageSetTest::proto_debug_string_[] = -"message_set {\n" -" [protobuf_unittest.TestMessageSetExtension1] {\n" -" i: 23\n" -" }\n" -" [protobuf_unittest.TestMessageSetExtension2] {\n" -" str: \"foo\"\n" -" }\n" -"}\n"; - + "message_set {\n" + " [protobuf_unittest.TestMessageSetExtension1] {\n" + " i: 23\n" + " }\n" + " [protobuf_unittest.TestMessageSetExtension2] {\n" + " str: \"foo\"\n" + " }\n" + "}\n"; TEST_F(TextFormatMessageSetTest, Serialize) { protobuf_unittest::TestMessageSetContainer proto; protobuf_unittest::TestMessageSetExtension1* item_a = - proto.mutable_message_set()->MutableExtension( - protobuf_unittest::TestMessageSetExtension1::message_set_extension); + proto.mutable_message_set()->MutableExtension( + protobuf_unittest::TestMessageSetExtension1::message_set_extension); item_a->set_i(23); protobuf_unittest::TestMessageSetExtension2* item_b = - proto.mutable_message_set()->MutableExtension( - protobuf_unittest::TestMessageSetExtension2::message_set_extension); + proto.mutable_message_set()->MutableExtension( + protobuf_unittest::TestMessageSetExtension2::message_set_extension); item_b->set_str("foo"); EXPECT_EQ(proto_debug_string_, proto.DebugString()); } @@ -1854,18 +1862,165 @@ TEST_F(TextFormatMessageSetTest, Serialize) { TEST_F(TextFormatMessageSetTest, Deserialize) { protobuf_unittest::TestMessageSetContainer proto; ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); - EXPECT_EQ(23, proto.message_set().GetExtension( - protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); - EXPECT_EQ("foo", proto.message_set().GetExtension( - protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); + EXPECT_EQ( + 23, + proto.message_set() + .GetExtension( + protobuf_unittest::TestMessageSetExtension1::message_set_extension) + .i()); + EXPECT_EQ( + "foo", + proto.message_set() + .GetExtension( + protobuf_unittest::TestMessageSetExtension2::message_set_extension) + .str()); // Ensure that these are the only entries present. std::vector<const FieldDescriptor*> descriptors; - proto.message_set().GetReflection()->ListFields( - proto.message_set(), &descriptors); + proto.message_set().GetReflection()->ListFields(proto.message_set(), + &descriptors); EXPECT_EQ(2, descriptors.size()); } +TEST(TextFormatUnknownFieldTest, TestUnknownField) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + // Unknown field is not permitted by default. + EXPECT_FALSE(parser.ParseFromString("unknown_field: 12345", &proto)); + EXPECT_FALSE(parser.ParseFromString("12345678: 12345", &proto)); + + parser.AllowUnknownField(true); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 12345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -12345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345f", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345f", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: inf", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: -inf", &proto)); + EXPECT_TRUE(parser.ParseFromString("unknown_field: TYPE_STRING", &proto)); + EXPECT_TRUE( + parser.ParseFromString("unknown_field: \"string value\"", &proto)); + // Invalid field value + EXPECT_FALSE(parser.ParseFromString("unknown_field: -TYPE_STRING", &proto)); + // Two or more unknown fields + EXPECT_TRUE( + parser.ParseFromString("unknown_field1: TYPE_STRING\n" + "unknown_field2: 12345", + &proto)); + // Unknown nested message + EXPECT_TRUE( + parser.ParseFromString("unknown_message1: {}\n" + "unknown_message2 {\n" + " unknown_field: 12345\n" + "}\n" + "unknown_message3 <\n" + " unknown_nested_message {\n" + " unknown_field: 12345\n" + " }\n" + ">", + &proto)); + // Unmatched delimeters for message body + EXPECT_FALSE(parser.ParseFromString("unknown_message: {>", &proto)); + // Unknown extension + EXPECT_TRUE( + parser.ParseFromString("[somewhere.unknown_extension1]: 12345\n" + "[somewhere.unknown_extension2] {\n" + " unknown_field: 12345\n" + "}", + &proto)); + // Unknown fields between known fields + ASSERT_TRUE( + parser.ParseFromString("optional_int32: 1\n" + "unknown_field: 12345\n" + "optional_string: \"string\"\n" + "unknown_message { unknown: 0 }\n" + "optional_nested_message { bb: 2 }", + &proto)); + EXPECT_EQ(1, proto.optional_int32()); + EXPECT_EQ("string", proto.optional_string()); + EXPECT_EQ(2, proto.optional_nested_message().bb()); + + // Unknown field with numeric tag number instead of identifier. + EXPECT_TRUE(parser.ParseFromString("12345678: 12345", &proto)); + + // Nested unknown extensions. + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] <\n" + " unknown_nested_message <\n" + " [test.extension2] <\n" + " unknown_field: 12345\n" + " >\n" + " >\n" + ">", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] {\n" + " unknown_nested_message {\n" + " [test.extension2] {\n" + " unknown_field: 12345\n" + " }\n" + " }\n" + "}", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] <\n" + " some_unknown_fields: <\n" + " unknown_field: 12345\n" + " >\n" + ">", + &proto)); + EXPECT_TRUE( + parser.ParseFromString("[test.extension1] {\n" + " some_unknown_fields: {\n" + " unknown_field: 12345\n" + " }\n" + "}", + &proto)); + + // Unknown field with compact repetition. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [1, 2]", &proto)); + // Unknown field with compact repetition of some unknown enum. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [VAL1, VAL2]", &proto)); + // Unknown field with compact repetition with sub-message. + EXPECT_TRUE(parser.ParseFromString("unknown_field: [{a:1}, <b:2>]", &proto)); +} + +TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + parser.AllowUnknownField(true); + EXPECT_TRUE( + parser.ParseFromString("unknown {\n" + " [type.googleapis.com/foo.bar] {\n" + " }\n" + "}", + &proto)); +} + +TEST(TextFormatUnknownFieldTest, TestUnknownExtension) { + protobuf_unittest::TestAllTypes proto; + TextFormat::Parser parser; + std::string message_with_ext = + "[test.extension1] {\n" + " some_unknown_fields: {\n" + " unknown_field: 12345\n" + " }\n" + "}"; + // Unknown extensions are not permitted by default. + EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto)); + // AllowUnknownField implies AllowUnknownExtension. + parser.AllowUnknownField(true); + EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto)); + + parser.AllowUnknownField(false); + EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto)); + parser.AllowUnknownExtension(true); + EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto)); + // Unknown fields are still not accepted. + EXPECT_FALSE(parser.ParseFromString("unknown_field: 1", &proto)); +} + } // namespace text_format_unittest } // namespace protobuf diff --git a/src/google/protobuf/timestamp.pb.cc b/src/google/protobuf/timestamp.pb.cc index 091caa53f8..6ec599187a 100644 --- a/src/google/protobuf/timestamp.pb.cc +++ b/src/google/protobuf/timestamp.pb.cc @@ -36,10 +36,6 @@ static void InitDefaultsTimestamp_google_2fprotobuf_2ftimestamp_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsTimestamp_google_2fprotobuf_2ftimestamp_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2ftimestamp_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2ftimestamp_2eproto[1]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ftimestamp_2eproto = nullptr; @@ -76,8 +72,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto[] = "Protobuf.WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ftimestamp_2eproto = { - false, InitDefaults_google_2fprotobuf_2ftimestamp_2eproto, - descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2ftimestamp_2eproto, "google/protobuf/timestamp.proto", &assign_descriptors_table_google_2fprotobuf_2ftimestamp_2eproto, 231, }; @@ -85,6 +80,7 @@ void AddDescriptors_google_2fprotobuf_2ftimestamp_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Timestamp_google_2fprotobuf_2ftimestamp_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ftimestamp_2eproto, deps, 0); } @@ -171,39 +167,45 @@ void Timestamp::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Timestamp::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // int64 seconds = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_seconds(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + seconds_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // int32 nanos = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_nanos(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + nanos_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Timestamp::MergePartialFromCodedStream( diff --git a/src/google/protobuf/timestamp.proto b/src/google/protobuf/timestamp.proto index 2b9e26a9b3..cd357864a9 100644 --- a/src/google/protobuf/timestamp.proto +++ b/src/google/protobuf/timestamp.proto @@ -113,17 +113,18 @@ option objc_class_prefix = "GPB"; // 01:30 UTC on January 15, 2017. // // In JavaScript, one can convert a Date object to this format using the -// standard [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) +// standard +// [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) // method. In Python, a standard `datetime.datetime` object can be converted -// to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) -// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one -// can use the Joda Time's [`ISODateTimeFormat.dateTime()`]( +// to this format using +// [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with +// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use +// the Joda Time's [`ISODateTimeFormat.dateTime()`]( // http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D // ) to obtain a formatter capable of generating timestamps in this format. // // message Timestamp { - // Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. diff --git a/src/google/protobuf/type.pb.cc b/src/google/protobuf/type.pb.cc index f30839cee9..d75b30a319 100644 --- a/src/google/protobuf/type.pb.cc +++ b/src/google/protobuf/type.pb.cc @@ -122,14 +122,6 @@ PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_Option_go {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsOption_google_2fprotobuf_2ftype_2eproto}, { &scc_info_Any_google_2fprotobuf_2fany_2eproto.base,}}; -void InitDefaults_google_2fprotobuf_2ftype_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Type_google_2fprotobuf_2ftype_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Field_google_2fprotobuf_2ftype_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Enum_google_2fprotobuf_2ftype_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValue_google_2fprotobuf_2ftype_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Option_google_2fprotobuf_2ftype_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2ftype_2eproto[5]; static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_google_2fprotobuf_2ftype_2eproto[3]; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2ftype_2eproto = nullptr; @@ -252,8 +244,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2ftype_2eproto[] = "le.Protobuf.WellKnownTypesb\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2ftype_2eproto = { - false, InitDefaults_google_2fprotobuf_2ftype_2eproto, - descriptor_table_protodef_google_2fprotobuf_2ftype_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2ftype_2eproto, "google/protobuf/type.proto", &assign_descriptors_table_google_2fprotobuf_2ftype_2eproto, 1594, }; @@ -263,6 +254,11 @@ void AddDescriptors_google_2fprotobuf_2ftype_2eproto() { ::AddDescriptors_google_2fprotobuf_2fany_2eproto, ::AddDescriptors_google_2fprotobuf_2fsource_5fcontext_2eproto, }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Type_google_2fprotobuf_2ftype_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Field_google_2fprotobuf_2ftype_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Enum_google_2fprotobuf_2ftype_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_EnumValue_google_2fprotobuf_2ftype_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Option_google_2fprotobuf_2ftype_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2ftype_2eproto, deps, 2); } @@ -498,61 +494,68 @@ void Type::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Type::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Type.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Field fields = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_fields(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // repeated string oneofs = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(add_oneofs(), ptr, ctx, "google.protobuf.Type.oneofs"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } // repeated .google.protobuf.Option options = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 34 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 34) break; + ptr += 1; + } break; } // .google.protobuf.SourceContext source_context = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 42) goto handle_unusual; ptr = ctx->ParseMessage(mutable_source_context(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.Syntax syntax = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 48) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_syntax(static_cast<PROTOBUF_NAMESPACE_ID::Syntax>(val)); break; } @@ -560,15 +563,20 @@ const char* Type::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Type::MergePartialFromCodedStream( @@ -1090,17 +1098,18 @@ void Field::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Field::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // .google.protobuf.Field.Kind kind = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_kind(static_cast<PROTOBUF_NAMESPACE_ID::Field_Kind>(val)); break; } @@ -1108,82 +1117,89 @@ const char* Field::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_cardinality(static_cast<PROTOBUF_NAMESPACE_ID::Field_Cardinality>(val)); break; } // int32 number = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 24) goto handle_unusual; - set_number(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // string name = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Field.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // string type_url = 6; case 6: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 50) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_type_url(), ptr, ctx, "google.protobuf.Field.type_url"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // int32 oneof_index = 7; case 7: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 56) goto handle_unusual; - set_oneof_index(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + oneof_index_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // bool packed = 8; case 8: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 64) goto handle_unusual; - set_packed(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + packed_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Option options = 9; case 9: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 74) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 74 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 74) break; + ptr += 1; + } break; } // string json_name = 10; case 10: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 82) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_json_name(), ptr, ctx, "google.protobuf.Field.json_name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // string default_value = 11; case 11: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 90) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_default_value(), ptr, ctx, "google.protobuf.Field.default_value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Field::MergePartialFromCodedStream( @@ -1869,51 +1885,56 @@ void Enum::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Enum::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Enum.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // repeated .google.protobuf.EnumValue enumvalue = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_enumvalue(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 18 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 18) break; + ptr += 1; + } break; } // repeated .google.protobuf.Option options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } // .google.protobuf.SourceContext source_context = 4; case 4: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 34) goto handle_unusual; ptr = ctx->ParseMessage(mutable_source_context(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.Syntax syntax = 5; case 5: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 40) goto handle_unusual; ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); set_syntax(static_cast<PROTOBUF_NAMESPACE_ID::Syntax>(val)); break; } @@ -1921,15 +1942,20 @@ const char* Enum::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Enum::MergePartialFromCodedStream( @@ -2368,49 +2394,57 @@ void EnumValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* EnumValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.EnumValue.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // int32 number = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 16) goto handle_unusual; - set_number(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } // repeated .google.protobuf.Option options = 3; case 3: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 26) goto handle_unusual; - do { + while (true) { ptr = ctx->ParseMessage(add_options(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (ctx->Done(&ptr)) return ptr; - } while ((::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint64>(ptr) & 255) == 26 && (ptr += 1)); + CHK_(ptr); + if (!ctx->DataAvailable(ptr)) break; + if (::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<::PROTOBUF_NAMESPACE_ID::uint8>(ptr) != 26) break; + ptr += 1; + } break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool EnumValue::MergePartialFromCodedStream( @@ -2801,39 +2835,45 @@ void Option::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Option::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string name = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_name(), ptr, ctx, "google.protobuf.Option.name"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } // .google.protobuf.Any value = 2; case 2: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 18) goto handle_unusual; ptr = ctx->ParseMessage(mutable_value(), ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Option::MergePartialFromCodedStream( diff --git a/src/google/protobuf/type.pb.h b/src/google/protobuf/type.pb.h index 7e57a50ed3..4cfda7b703 100644 --- a/src/google/protobuf/type.pb.h +++ b/src/google/protobuf/type.pb.h @@ -111,9 +111,13 @@ constexpr Field_Kind Field_Kind_Kind_MAX = Field_Kind_TYPE_SINT64; constexpr int Field_Kind_Kind_ARRAYSIZE = Field_Kind_Kind_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* Field_Kind_descriptor(); -inline const std::string& Field_Kind_Name(Field_Kind value) { +template<typename T> +inline const std::string& Field_Kind_Name(T enum_t_value) { + static_assert(::std::is_same<T, Field_Kind>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Field_Kind_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - Field_Kind_descriptor(), value); + Field_Kind_descriptor(), enum_t_value); } inline bool Field_Kind_Parse( const std::string& name, Field_Kind* value) { @@ -134,9 +138,13 @@ constexpr Field_Cardinality Field_Cardinality_Cardinality_MAX = Field_Cardinalit constexpr int Field_Cardinality_Cardinality_ARRAYSIZE = Field_Cardinality_Cardinality_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* Field_Cardinality_descriptor(); -inline const std::string& Field_Cardinality_Name(Field_Cardinality value) { +template<typename T> +inline const std::string& Field_Cardinality_Name(T enum_t_value) { + static_assert(::std::is_same<T, Field_Cardinality>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Field_Cardinality_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - Field_Cardinality_descriptor(), value); + Field_Cardinality_descriptor(), enum_t_value); } inline bool Field_Cardinality_Parse( const std::string& name, Field_Cardinality* value) { @@ -155,9 +163,13 @@ constexpr Syntax Syntax_MAX = SYNTAX_PROTO3; constexpr int Syntax_ARRAYSIZE = Syntax_MAX + 1; PROTOBUF_EXPORT const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* Syntax_descriptor(); -inline const std::string& Syntax_Name(Syntax value) { +template<typename T> +inline const std::string& Syntax_Name(T enum_t_value) { + static_assert(::std::is_same<T, Syntax>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Syntax_Name."); return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( - Syntax_descriptor(), value); + Syntax_descriptor(), enum_t_value); } inline bool Syntax_Parse( const std::string& name, Syntax* value) { @@ -532,8 +544,12 @@ class PROTOBUF_EXPORT Field final : Kind_descriptor() { return Field_Kind_descriptor(); } - static inline const std::string& Kind_Name(Kind value) { - return Field_Kind_Name(value); + template<typename T> + static inline const std::string& Kind_Name(T enum_t_value) { + static_assert(::std::is_same<T, Kind>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Kind_Name."); + return Field_Kind_Name(enum_t_value); } static inline bool Kind_Parse(const std::string& name, Kind* value) { @@ -562,8 +578,12 @@ class PROTOBUF_EXPORT Field final : Cardinality_descriptor() { return Field_Cardinality_descriptor(); } - static inline const std::string& Cardinality_Name(Cardinality value) { - return Field_Cardinality_Name(value); + template<typename T> + static inline const std::string& Cardinality_Name(T enum_t_value) { + static_assert(::std::is_same<T, Cardinality>::value || + ::std::is_integral<T>::value, + "Incorrect type passed to function Cardinality_Name."); + return Field_Cardinality_Name(enum_t_value); } static inline bool Cardinality_Parse(const std::string& name, Cardinality* value) { diff --git a/src/google/protobuf/type.proto b/src/google/protobuf/type.proto index 624c15ee61..e4b1d3a4ab 100644 --- a/src/google/protobuf/type.proto +++ b/src/google/protobuf/type.proto @@ -64,44 +64,44 @@ message Field { // Basic field types. enum Kind { // Field type unknown. - TYPE_UNKNOWN = 0; + TYPE_UNKNOWN = 0; // Field type double. - TYPE_DOUBLE = 1; + TYPE_DOUBLE = 1; // Field type float. - TYPE_FLOAT = 2; + TYPE_FLOAT = 2; // Field type int64. - TYPE_INT64 = 3; + TYPE_INT64 = 3; // Field type uint64. - TYPE_UINT64 = 4; + TYPE_UINT64 = 4; // Field type int32. - TYPE_INT32 = 5; + TYPE_INT32 = 5; // Field type fixed64. - TYPE_FIXED64 = 6; + TYPE_FIXED64 = 6; // Field type fixed32. - TYPE_FIXED32 = 7; + TYPE_FIXED32 = 7; // Field type bool. - TYPE_BOOL = 8; + TYPE_BOOL = 8; // Field type string. - TYPE_STRING = 9; + TYPE_STRING = 9; // Field type group. Proto2 syntax only, and deprecated. - TYPE_GROUP = 10; + TYPE_GROUP = 10; // Field type message. - TYPE_MESSAGE = 11; + TYPE_MESSAGE = 11; // Field type bytes. - TYPE_BYTES = 12; + TYPE_BYTES = 12; // Field type uint32. - TYPE_UINT32 = 13; + TYPE_UINT32 = 13; // Field type enum. - TYPE_ENUM = 14; + TYPE_ENUM = 14; // Field type sfixed32. - TYPE_SFIXED32 = 15; + TYPE_SFIXED32 = 15; // Field type sfixed64. - TYPE_SFIXED64 = 16; + TYPE_SFIXED64 = 16; // Field type sint32. - TYPE_SINT32 = 17; + TYPE_SINT32 = 17; // Field type sint64. - TYPE_SINT64 = 18; - }; + TYPE_SINT64 = 18; + } // Whether a field is optional, required, or repeated. enum Cardinality { diff --git a/src/google/protobuf/unknown_field_set.h b/src/google/protobuf/unknown_field_set.h index 0d61f89358..4d5cc060e5 100644 --- a/src/google/protobuf/unknown_field_set.h +++ b/src/google/protobuf/unknown_field_set.h @@ -56,20 +56,20 @@ namespace google { namespace protobuf { - namespace io { - class CodedInputStream; // coded_stream.h - class CodedOutputStream; // coded_stream.h - class ZeroCopyInputStream; // zero_copy_stream.h - } - namespace internal { - class InternalMetadataWithArena; // metadata.h - class WireFormat; // wire_format.h - class MessageSetFieldSkipperUsingCord; - // extension_set_heavy.cc - } +namespace io { +class CodedInputStream; // coded_stream.h +class CodedOutputStream; // coded_stream.h +class ZeroCopyInputStream; // zero_copy_stream.h +} // namespace io +namespace internal { +class InternalMetadataWithArena; // metadata.h +class WireFormat; // wire_format.h +class MessageSetFieldSkipperUsingCord; +// extension_set_heavy.cc +} // namespace internal -class Message; // message.h -class UnknownField; // below +class Message; // message.h +class UnknownField; // below // An UnknownFieldSet contains fields that were encountered while parsing a // message but were not defined by its type. Keeping track of these can be @@ -125,9 +125,7 @@ class PROTOBUF_EXPORT UnknownFieldSet { // Version of SpaceUsed() including sizeof(*this). size_t SpaceUsedLong() const; - int SpaceUsed() const { - return internal::ToIntSize(SpaceUsedLong()); - } + int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); } // Returns the number of fields present in the UnknownFieldSet. inline int field_count() const; @@ -172,6 +170,7 @@ class PROTOBUF_EXPORT UnknownFieldSet { } static const UnknownFieldSet* default_instance(); + private: // For InternalMergeFrom friend class UnknownField; @@ -302,9 +301,7 @@ inline void UnknownFieldSet::Clear() { } } -inline bool UnknownFieldSet::empty() const { - return fields_.empty(); -} +inline bool UnknownFieldSet::empty() const { return fields_.empty(); } inline void UnknownFieldSet::Swap(UnknownFieldSet* x) { fields_.swap(x->fields_); diff --git a/src/google/protobuf/unknown_field_set_unittest.cc b/src/google/protobuf/unknown_field_set_unittest.cc index 771413d093..6d76b67b5b 100644 --- a/src/google/protobuf/unknown_field_set_unittest.cc +++ b/src/google/protobuf/unknown_field_set_unittest.cc @@ -35,14 +35,14 @@ // This test is testing a lot more than just the UnknownFieldSet class. It // tests handling of unknown fields throughout the system. -#include <google/protobuf/stubs/mutex.h> #include <google/protobuf/unknown_field_set.h> -#include <google/protobuf/descriptor.h> +#include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> #include <google/protobuf/io/coded_stream.h> #include <google/protobuf/io/zero_copy_stream_impl.h> +#include <google/protobuf/descriptor.h> +#include <google/protobuf/stubs/mutex.h> #include <google/protobuf/wire_format.h> -#include <google/protobuf/unittest.pb.h> -#include <google/protobuf/test_util.h> #include <google/protobuf/stubs/callback.h> #include <google/protobuf/stubs/common.h> @@ -85,7 +85,7 @@ class UnknownFieldSetTest : public testing::Test { std::string GetBizarroData() { unittest::TestEmptyMessage bizarro_message; UnknownFieldSet* bizarro_unknown_fields = - bizarro_message.mutable_unknown_fields(); + bizarro_message.mutable_unknown_fields(); for (int i = 0; i < unknown_fields_->field_count(); i++) { const UnknownField& unknown_field = unknown_fields_->field(i); if (unknown_field.type() == UnknownField::TYPE_VARINT) { @@ -180,7 +180,7 @@ TEST_F(UnknownFieldSetTest, Group) { const UnknownField& nested_field = field->group().field(0); const FieldDescriptor* nested_field_descriptor = - unittest::TestAllTypes::OptionalGroup::descriptor()->FindFieldByName("a"); + unittest::TestAllTypes::OptionalGroup::descriptor()->FindFieldByName("a"); ASSERT_TRUE(nested_field_descriptor != NULL); EXPECT_EQ(nested_field_descriptor->number(), nested_field.number()); @@ -189,8 +189,8 @@ TEST_F(UnknownFieldSetTest, Group) { } TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) { - int size = WireFormat::ComputeUnknownFieldsSize( - empty_message_.unknown_fields()); + int size = + WireFormat::ComputeUnknownFieldsSize(empty_message_.unknown_fields()); std::string slow_buffer; std::string fast_buffer; slow_buffer.resize(size); @@ -198,7 +198,7 @@ TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) { uint8* target = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&fast_buffer)); uint8* result = WireFormat::SerializeUnknownFieldsToArray( - empty_message_.unknown_fields(), target); + empty_message_.unknown_fields(), target); EXPECT_EQ(size, result - target); { @@ -299,13 +299,13 @@ TEST_F(UnknownFieldSetTest, MergeFrom) { destination.MergeFrom(source); EXPECT_EQ( - // Note: The ordering of fields here depends on the ordering of adds - // and merging, above. - "1: 1\n" - "3: 2\n" - "2: 3\n" - "3: 4\n", - destination.DebugString()); + // Note: The ordering of fields here depends on the ordering of adds + // and merging, above. + "1: 1\n" + "3: 2\n" + "2: 3\n" + "3: 4\n", + destination.DebugString()); } @@ -408,14 +408,14 @@ TEST_F(UnknownFieldSetTest, WrongExtensionTypeTreatedAsUnknown) { } TEST_F(UnknownFieldSetTest, UnknownEnumValue) { - using unittest::TestAllTypes; using unittest::TestAllExtensions; + using unittest::TestAllTypes; using unittest::TestEmptyMessage; const FieldDescriptor* singular_field = - TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum"); + TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum"); const FieldDescriptor* repeated_field = - TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum"); + TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum"); ASSERT_TRUE(singular_field != NULL); ASSERT_TRUE(repeated_field != NULL); @@ -572,8 +572,7 @@ void CheckDeleteByNumber(const std::vector<int>& field_numbers, unknown_fields.DeleteByNumber(deleted_number); ASSERT_EQ(expected_field_nubmers.size(), unknown_fields.field_count()); for (int i = 0; i < expected_field_nubmers.size(); ++i) { - EXPECT_EQ(expected_field_nubmers[i], - unknown_fields.field(i).number()); + EXPECT_EQ(expected_field_nubmers[i], unknown_fields.field(i).number()); } } diff --git a/src/google/protobuf/util/delimited_message_util.cc b/src/google/protobuf/util/delimited_message_util.cc index c0d5cb0af2..425dc2cfdf 100644 --- a/src/google/protobuf/util/delimited_message_util.cc +++ b/src/google/protobuf/util/delimited_message_util.cc @@ -1,8 +1,38 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // Adapted from the patch of kenton@google.com (Kenton Varda) // See https://github.com/protocolbuffers/protobuf/pull/710 for details. -#include <google/protobuf/io/coded_stream.h> #include <google/protobuf/util/delimited_message_util.h> +#include <google/protobuf/io/coded_stream.h> namespace google { namespace protobuf { @@ -66,7 +96,9 @@ bool SerializeDelimitedToZeroCopyStream(const MessageLite& message, bool SerializeDelimitedToCodedStream(const MessageLite& message, io::CodedOutputStream* output) { // Write the size. - int size = message.ByteSize(); + size_t size = message.ByteSizeLong(); + if (size > INT_MAX) return false; + output->WriteVarint32(size); // Write the content. diff --git a/src/google/protobuf/util/delimited_message_util.h b/src/google/protobuf/util/delimited_message_util.h index 5f2d94cdca..946bbc999b 100644 --- a/src/google/protobuf/util/delimited_message_util.h +++ b/src/google/protobuf/util/delimited_message_util.h @@ -1,3 +1,33 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // Adapted from the patch of kenton@google.com (Kenton Varda) // See https://github.com/protocolbuffers/protobuf/pull/710 for details. diff --git a/src/google/protobuf/util/delimited_message_util_test.cc b/src/google/protobuf/util/delimited_message_util_test.cc index 5a311c0dec..9ed67784ee 100644 --- a/src/google/protobuf/util/delimited_message_util_test.cc +++ b/src/google/protobuf/util/delimited_message_util_test.cc @@ -1,3 +1,33 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // Adapted from the patch of kenton@google.com (Kenton Varda) // See https://github.com/protocolbuffers/protobuf/pull/710 for details. diff --git a/src/google/protobuf/util/field_comparator.cc b/src/google/protobuf/util/field_comparator.cc index 83d21c9720..3316506b9c 100644 --- a/src/google/protobuf/util/field_comparator.cc +++ b/src/google/protobuf/util/field_comparator.cc @@ -51,8 +51,7 @@ FieldComparator::~FieldComparator() {} DefaultFieldComparator::DefaultFieldComparator() : float_comparison_(EXACT), treat_nan_as_equal_(false), - has_default_tolerance_(false) { -} + has_default_tolerance_(false) {} DefaultFieldComparator::~DefaultFieldComparator() {} @@ -64,19 +63,17 @@ FieldComparator::ComparisonResult DefaultFieldComparator::Compare( const Reflection* reflection_2 = message_2.GetReflection(); switch (field->cpp_type()) { -#define COMPARE_FIELD(METHOD) \ - if (field->is_repeated()) { \ - return ResultFromBoolean(Compare##METHOD( \ - *field, \ - reflection_1->GetRepeated##METHOD(message_1, field, index_1), \ - reflection_2->GetRepeated##METHOD(message_2, field, index_2))); \ - } else { \ - return ResultFromBoolean(Compare##METHOD( \ - *field, \ - reflection_1->Get##METHOD(message_1, field), \ - reflection_2->Get##METHOD(message_2, field))); \ - } \ - break; // Make sure no fall-through is introduced. +#define COMPARE_FIELD(METHOD) \ + if (field->is_repeated()) { \ + return ResultFromBoolean(Compare##METHOD( \ + *field, reflection_1->GetRepeated##METHOD(message_1, field, index_1), \ + reflection_2->GetRepeated##METHOD(message_2, field, index_2))); \ + } else { \ + return ResultFromBoolean( \ + Compare##METHOD(*field, reflection_1->Get##METHOD(message_1, field), \ + reflection_2->Get##METHOD(message_2, field))); \ + } \ + break; // Make sure no fall-through is introduced. case FieldDescriptor::CPPTYPE_BOOL: COMPARE_FIELD(Bool); @@ -97,8 +94,9 @@ FieldComparator::ComparisonResult DefaultFieldComparator::Compare( std::string scratch1; std::string scratch2; return ResultFromBoolean( - CompareString(*field, reflection_1->GetRepeatedStringReference( - message_1, field, index_1, &scratch1), + CompareString(*field, + reflection_1->GetRepeatedStringReference( + message_1, field, index_1, &scratch1), reflection_2->GetRepeatedStringReference( message_2, field, index_2, &scratch2))); } else { @@ -133,8 +131,8 @@ bool DefaultFieldComparator::Compare(MessageDifferencer* differencer, const Message& message1, const Message& message2, const util::FieldContext* field_context) { - return differencer->Compare( - message1, message2, field_context->parent_fields()); + return differencer->Compare(message1, message2, + field_context->parent_fields()); } void DefaultFieldComparator::SetDefaultFractionAndMargin(double fraction, @@ -169,7 +167,7 @@ bool DefaultFieldComparator::CompareFloat(const FieldDescriptor& field, return CompareDoubleOrFloat(field, value_1, value_2); } -template<typename T> +template <typename T> bool DefaultFieldComparator::CompareDoubleOrFloat(const FieldDescriptor& field, T value_1, T value_2) { if (value_1 == value_2) { @@ -177,14 +175,14 @@ bool DefaultFieldComparator::CompareDoubleOrFloat(const FieldDescriptor& field, // themselves), and is a shortcut for finite values. return true; } else if (float_comparison_ == EXACT) { - if (treat_nan_as_equal_ && - MathLimits<T>::IsNaN(value_1) && MathLimits<T>::IsNaN(value_2)) { + if (treat_nan_as_equal_ && MathLimits<T>::IsNaN(value_1) && + MathLimits<T>::IsNaN(value_2)) { return true; } return false; } else { - if (treat_nan_as_equal_ && - MathLimits<T>::IsNaN(value_1) && MathLimits<T>::IsNaN(value_2)) { + if (treat_nan_as_equal_ && MathLimits<T>::IsNaN(value_1) && + MathLimits<T>::IsNaN(value_2)) { return true; } // float_comparison_ == APPROXIMATE covers two use cases. diff --git a/src/google/protobuf/util/field_comparator.h b/src/google/protobuf/util/field_comparator.h index ca78fc5f41..7b5ce52b82 100644 --- a/src/google/protobuf/util/field_comparator.h +++ b/src/google/protobuf/util/field_comparator.h @@ -100,12 +100,12 @@ class PROTOBUF_EXPORT FieldComparator { class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { public: enum FloatComparison { - EXACT, // Floats and doubles are compared exactly. - APPROXIMATE, // Floats and doubles are compared using the - // MathUtil::AlmostEqual method or - // MathUtil::WithinFractionOrMargin method. - // TODO(ksroka): Introduce third value to differenciate uses of AlmostEqual - // and WithinFractionOrMargin. + EXACT, // Floats and doubles are compared exactly. + APPROXIMATE, // Floats and doubles are compared using the + // MathUtil::AlmostEqual method or + // MathUtil::WithinFractionOrMargin method. + // TODO(ksroka): Introduce third value to differentiate uses of AlmostEqual + // and WithinFractionOrMargin. }; // Creates new comparator with float comparison set to EXACT. @@ -122,9 +122,7 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { float_comparison_ = float_comparison; } - FloatComparison float_comparison() const { - return float_comparison_; - } + FloatComparison float_comparison() const { return float_comparison_; } // Set whether the FieldComparator shall treat floats or doubles that are both // NaN as equal (treat_nan_as_equal = true) or as different @@ -133,9 +131,7 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { treat_nan_as_equal_ = treat_nan_as_equal; } - bool treat_nan_as_equal() const { - return treat_nan_as_equal_; - } + bool treat_nan_as_equal() const { return treat_nan_as_equal_; } // Sets the fraction and margin for the float comparison of a given field. // Uses MathUtil::WithinFractionOrMargin to compare the values. @@ -167,12 +163,8 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { struct Tolerance { double fraction; double margin; - Tolerance() - : fraction(0.0), - margin(0.0) {} - Tolerance(double f, double m) - : fraction(f), - margin(m) {} + Tolerance() : fraction(0.0), margin(0.0) {} + Tolerance(double f, double m) : fraction(f), margin(m) {} }; // Defines the map to store the tolerances for floating point comparison. @@ -182,8 +174,8 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { // basic types (instead of submessages). They return true on success. One // can use ResultFromBoolean() to convert that boolean to a ComparisonResult // value. - bool CompareBool(const FieldDescriptor& /* unused */, - bool value_1, bool value_2) { + bool CompareBool(const FieldDescriptor& /* unused */, bool value_1, + bool value_2) { return value_1 == value_2; } @@ -210,8 +202,8 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator { return value_1 == value_2; } - bool CompareString(const FieldDescriptor& /* unused */, const std::string& value_1, - const std::string& value_2) { + bool CompareString(const FieldDescriptor& /* unused */, + const std::string& value_1, const std::string& value_2) { return value_1 == value_2; } diff --git a/src/google/protobuf/util/field_comparator_test.cc b/src/google/protobuf/util/field_comparator_test.cc index 00de96d6cd..6b7ee49a19 100644 --- a/src/google/protobuf/util/field_comparator_test.cc +++ b/src/google/protobuf/util/field_comparator_test.cc @@ -51,9 +51,7 @@ using protobuf_unittest::TestAllTypes; class DefaultFieldComparatorTest : public ::testing::Test { protected: - void SetUp() { - descriptor_ = TestAllTypes::descriptor(); - } + void SetUp() { descriptor_ = TestAllTypes::descriptor(); } const Descriptor* descriptor_; DefaultFieldComparator comparator_; @@ -62,8 +60,7 @@ class DefaultFieldComparatorTest : public ::testing::Test { }; TEST_F(DefaultFieldComparatorTest, RecursesIntoGroup) { - const FieldDescriptor* field = - descriptor_->FindFieldByName("optionalgroup"); + const FieldDescriptor* field = descriptor_->FindFieldByName("optionalgroup"); EXPECT_EQ(FieldComparator::RECURSE, comparator_.Compare(message_1_, message_2_, field, -1, -1, NULL)); } @@ -137,8 +134,7 @@ TEST_F(DefaultFieldComparatorTest, UInt64Comparison) { } TEST_F(DefaultFieldComparatorTest, BooleanComparison) { - const FieldDescriptor* field = - descriptor_->FindFieldByName("optional_bool"); + const FieldDescriptor* field = descriptor_->FindFieldByName("optional_bool"); message_1_.set_optional_bool(true); message_2_.set_optional_bool(true); @@ -189,22 +185,22 @@ TEST_F(DefaultFieldComparatorTest, FloatingPointComparisonExact) { message_1_.set_optional_double(0.1); message_2_.set_optional_double(0.1); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); message_2_.set_optional_float(0.2f); message_2_.set_optional_double(0.2); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); } TEST_F(DefaultFieldComparatorTest, FloatingPointComparisonApproximate) { @@ -229,21 +225,21 @@ TEST_F(DefaultFieldComparatorTest, FloatingPointComparisonApproximate) { // DefaultFieldComparator's default float comparison mode is EXACT. ASSERT_EQ(DefaultFieldComparator::EXACT, comparator_.float_comparison()); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); comparator_.set_float_comparison(DefaultFieldComparator::APPROXIMATE); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); } TEST_F(DefaultFieldComparatorTest, FloatingPointComparisonTreatNaNsAsEqual) { @@ -261,36 +257,36 @@ TEST_F(DefaultFieldComparatorTest, FloatingPointComparisonTreatNaNsAsEqual) { // treating NaNs as different. ASSERT_EQ(DefaultFieldComparator::EXACT, comparator_.float_comparison()); ASSERT_EQ(false, comparator_.treat_nan_as_equal()); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); comparator_.set_float_comparison(DefaultFieldComparator::APPROXIMATE); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); comparator_.set_treat_nan_as_equal(true); ASSERT_EQ(true, comparator_.treat_nan_as_equal()); comparator_.set_float_comparison(DefaultFieldComparator::EXACT); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); comparator_.set_float_comparison(DefaultFieldComparator::APPROXIMATE); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); } TEST_F(DefaultFieldComparatorTest, @@ -306,62 +302,62 @@ TEST_F(DefaultFieldComparatorTest, message_2_.set_optional_double(109.9); comparator_.set_float_comparison(DefaultFieldComparator::APPROXIMATE); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Should fail since the fraction is too low. comparator_.SetFractionAndMargin(field_float, 0.01, 0.0); comparator_.SetFractionAndMargin(field_double, 0.01, 0.0); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Should fail since the margin is too low. comparator_.SetFractionAndMargin(field_float, 0.0, 9.0); comparator_.SetFractionAndMargin(field_double, 0.0, 9.0); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Should succeed since the fraction is high enough. comparator_.SetFractionAndMargin(field_float, 0.2, 0.0); comparator_.SetFractionAndMargin(field_double, 0.2, 0.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Should succeed since the margin is high enough. comparator_.SetFractionAndMargin(field_float, 0.0, 10.0); comparator_.SetFractionAndMargin(field_double, 0.0, 10.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Setting values for one of the fields should not affect the other. comparator_.SetFractionAndMargin(field_double, 0.0, 0.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // +inf should be equal even though they are not technically within margin or // fraction. @@ -371,12 +367,12 @@ TEST_F(DefaultFieldComparatorTest, message_2_.set_optional_double(std::numeric_limits<double>::infinity()); comparator_.SetFractionAndMargin(field_float, 0.0, 0.0); comparator_.SetFractionAndMargin(field_double, 0.0, 0.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // -inf should be equal even though they are not technically within margin or // fraction. @@ -386,12 +382,12 @@ TEST_F(DefaultFieldComparatorTest, message_2_.set_optional_double(-std::numeric_limits<double>::infinity()); comparator_.SetFractionAndMargin(field_float, 0.0, 0.0); comparator_.SetFractionAndMargin(field_double, 0.0, 0.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); } TEST_F(DefaultFieldComparatorTest, @@ -407,23 +403,23 @@ TEST_F(DefaultFieldComparatorTest, message_2_.set_optional_double(109.9); comparator_.set_float_comparison(DefaultFieldComparator::APPROXIMATE); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Set default fraction and margin. comparator_.SetDefaultFractionAndMargin(0.01, 0.0); // Float comparisons should fail since the fraction is too low. - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Set field-specific fraction and margin for one field (field_float) but not // the other (field_double) @@ -431,37 +427,37 @@ TEST_F(DefaultFieldComparatorTest, // The field with the override should succeed, since its field-specific // fraction is high enough. - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); // The field with no override should fail, since the default fraction is too // low - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // Set the default fraction and margin high enough so that fields that use // the default should succeed comparator_.SetDefaultFractionAndMargin(0.2, 0.0); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); // The field with an override should still be OK - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); // Set fraction and margin for the field with an override to be too low comparator_.SetFractionAndMargin(field_float, 0.01, 0.0); // Now our default is high enough but field_float's override is too low. - EXPECT_EQ(FieldComparator::DIFFERENT, - comparator_.Compare(message_1_, message_2_, - field_float, -1, -1, NULL)); - EXPECT_EQ(FieldComparator::SAME, - comparator_.Compare(message_1_, message_2_, - field_double, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::DIFFERENT, + comparator_.Compare(message_1_, message_2_, field_float, -1, -1, NULL)); + EXPECT_EQ( + FieldComparator::SAME, + comparator_.Compare(message_1_, message_2_, field_double, -1, -1, NULL)); } // Simple test checking whether we compare values at correct indices. diff --git a/src/google/protobuf/util/field_mask_util.cc b/src/google/protobuf/util/field_mask_util.cc index 11f62ba44f..c4b2758bf9 100644 --- a/src/google/protobuf/util/field_mask_util.cc +++ b/src/google/protobuf/util/field_mask_util.cc @@ -34,6 +34,8 @@ #include <google/protobuf/stubs/map_util.h> +#include <google/protobuf/port_def.inc> + namespace google { namespace protobuf { namespace util { @@ -160,8 +162,8 @@ bool FieldMaskUtil::GetFieldDescriptors( return true; } -void FieldMaskUtil::GetFieldMaskForAllFields( - const Descriptor* descriptor, FieldMask* out) { +void FieldMaskUtil::GetFieldMaskForAllFields(const Descriptor* descriptor, + FieldMask* out) { for (int i = 0; i < descriptor->field_count(); ++i) { out->add_paths(descriptor->field(i)->name()); } @@ -548,8 +550,8 @@ void FieldMaskTree::MergeMessage(const Node* node, const Message& source, } } -void FieldMaskTree::AddRequiredFieldPath( - Node* node, const Descriptor* descriptor) { +void FieldMaskTree::AddRequiredFieldPath(Node* node, + const Descriptor* descriptor) { const int32 field_count = descriptor->field_count(); for (int index = 0; index < field_count; ++index) { const FieldDescriptor* field = descriptor->field(index); @@ -559,7 +561,7 @@ void FieldMaskTree::AddRequiredFieldPath( if (child == nullptr) { // Add required field path to the tree child = new Node(); - } else if (child->children.empty()){ + } else if (child->children.empty()) { // If the required field is in the tree and does not have any children, // do nothing. continue; diff --git a/src/google/protobuf/util/field_mask_util.h b/src/google/protobuf/util/field_mask_util.h index 7031442d27..3e7a93e25a 100644 --- a/src/google/protobuf/util/field_mask_util.h +++ b/src/google/protobuf/util/field_mask_util.h @@ -35,8 +35,8 @@ #include <string> -#include <google/protobuf/descriptor.h> #include <google/protobuf/field_mask.pb.h> +#include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/stringpiece.h> #include <google/protobuf/port_def.inc> @@ -134,9 +134,8 @@ class PROTOBUF_EXPORT FieldMaskUtil { } // This flavor takes the protobuf type descriptor as an argument. // Useful when the type is not known at compile time. - static void Subtract(const Descriptor* descriptor, - const FieldMask& mask1, const FieldMask& mask2, - FieldMask* out); + static void Subtract(const Descriptor* descriptor, const FieldMask& mask1, + const FieldMask& mask2, FieldMask* out); // Returns true if path is covered by the given FieldMask. Note that path // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc. @@ -177,7 +176,8 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Note that the input can contain characters not allowed in C identifiers. // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz" // successfully. - static bool SnakeCaseToCamelCase(StringPiece input, std::string* output); + static bool SnakeCaseToCamelCase(StringPiece input, + std::string* output); // Converts a field name from camelCase to snake_case: // 1. Every uppercase letter is converted to lowercase with a additional // preceding "-". @@ -190,7 +190,8 @@ class PROTOBUF_EXPORT FieldMaskUtil { // Note that the input can contain characters not allowed in C identifiers. // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz" // successfully. - static bool CamelCaseToSnakeCase(StringPiece input, std::string* output); + static bool CamelCaseToSnakeCase(StringPiece input, + std::string* output); }; class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions { @@ -223,15 +224,12 @@ class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions { class PROTOBUF_EXPORT FieldMaskUtil::TrimOptions { public: - TrimOptions() - : keep_required_fields_(false) {} + TrimOptions() : keep_required_fields_(false) {} // When trimming message fields, the default behavior is to trim required // fields of the present message if they are not specified in the field mask. // If you instead want to keep required fields of the present message even - // they are not speicifed in the field mask, set this flag to true. - void set_keep_required_fields(bool value) { - keep_required_fields_ = value; - } + // they are not specified in the field mask, set this flag to true. + void set_keep_required_fields(bool value) { keep_required_fields_ = value; } bool keep_required_fields() const { return keep_required_fields_; } private: diff --git a/src/google/protobuf/util/field_mask_util_test.cc b/src/google/protobuf/util/field_mask_util_test.cc index 1481c17edc..401a56a4b1 100644 --- a/src/google/protobuf/util/field_mask_util_test.cc +++ b/src/google/protobuf/util/field_mask_util_test.cc @@ -35,8 +35,8 @@ #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> #include <google/protobuf/field_mask.pb.h> -#include <google/protobuf/unittest.pb.h> #include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> #include <gtest/gtest.h> namespace google { @@ -113,11 +113,11 @@ TEST_F(SnakeCaseCamelCaseTest, RoundTripTest) { } while (std::next_permutation(name.begin(), name.end())); } +using google::protobuf::FieldMask; +using protobuf_unittest::NestedTestAllTypes; using protobuf_unittest::TestAllTypes; using protobuf_unittest::TestRequired; using protobuf_unittest::TestRequiredMessage; -using protobuf_unittest::NestedTestAllTypes; -using google::protobuf::FieldMask; TEST(FieldMaskUtilTest, StringFormat) { FieldMask mask; @@ -707,16 +707,14 @@ TEST(FieldMaskUtilTest, TrimMessage) { required_msg_2.clear_repeated_message(); required_msg_2.mutable_required_message()->clear_dummy2(); FieldMaskUtil::TrimMessage(mask, &trimmed_required_msg_2, options); - EXPECT_EQ(trimmed_required_msg_2.DebugString(), - required_msg_2.DebugString()); + EXPECT_EQ(trimmed_required_msg_2.DebugString(), required_msg_2.DebugString()); FieldMaskUtil::FromString("required_message", &mask); required_msg_2.mutable_required_message()->set_dummy2(7890); trimmed_required_msg_2.mutable_required_message()->set_dummy2(7890); required_msg_2.clear_optional_message(); FieldMaskUtil::TrimMessage(mask, &trimmed_required_msg_2, options); - EXPECT_EQ(trimmed_required_msg_2.DebugString(), - required_msg_2.DebugString()); + EXPECT_EQ(trimmed_required_msg_2.DebugString(), required_msg_2.DebugString()); // Test trim required message with keep_required_fields is set false. FieldMaskUtil::FromString("required_message.dummy2", &mask); @@ -725,8 +723,7 @@ TEST(FieldMaskUtilTest, TrimMessage) { required_msg_2.mutable_required_message()->clear_c(); options.set_keep_required_fields(false); FieldMaskUtil::TrimMessage(mask, &trimmed_required_msg_2, options); - EXPECT_EQ(trimmed_required_msg_2.DebugString(), - required_msg_2.DebugString()); + EXPECT_EQ(trimmed_required_msg_2.DebugString(), required_msg_2.DebugString()); // Verify that trimming an empty message has no effect. In particular, fields // mentioned in the field mask should not be created or changed. diff --git a/src/google/protobuf/util/internal/datapiece.cc b/src/google/protobuf/util/internal/datapiece.cc index be474548e6..55af434e97 100644 --- a/src/google/protobuf/util/internal/datapiece.cc +++ b/src/google/protobuf/util/internal/datapiece.cc @@ -47,9 +47,9 @@ namespace converter { ; ; ; -using util::error::Code; using util::Status; using util::StatusOr; +using util::error::Code; namespace { diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.cc b/src/google/protobuf/util/internal/default_value_objectwriter.cc index 3d7f27cfb1..6c3b58bf63 100644 --- a/src/google/protobuf/util/internal/default_value_objectwriter.cc +++ b/src/google/protobuf/util/internal/default_value_objectwriter.cc @@ -481,7 +481,9 @@ DataPiece DefaultValueObjectWriter::CreateDefaultDataPieceForField( case google::protobuf::Field_Kind_TYPE_ENUM: { return FindEnumDefault(field, typeinfo, use_ints_for_enums); } - default: { return DataPiece::NullData(); } + default: { + return DataPiece::NullData(); + } } } diff --git a/src/google/protobuf/util/internal/json_escaping.cc b/src/google/protobuf/util/internal/json_escaping.cc index d167359714..5347c7b2fe 100644 --- a/src/google/protobuf/util/internal/json_escaping.cc +++ b/src/google/protobuf/util/internal/json_escaping.cc @@ -50,39 +50,30 @@ static const char kHex[] = "0123456789abcdef"; // kCommonEscapes[ch] is the escaped string of ch, if escaping is needed; // or an empty string, if escaping is not needed. static const char kCommonEscapes[160][7] = { - // C0 (ASCII and derivatives) control characters - "\\u0000", "\\u0001", "\\u0002", "\\u0003", // 0x00 - "\\u0004", "\\u0005", "\\u0006", "\\u0007", - "\\b", "\\t", "\\n", "\\u000b", - "\\f", "\\r", "\\u000e", "\\u000f", - "\\u0010", "\\u0011", "\\u0012", "\\u0013", // 0x10 - "\\u0014", "\\u0015", "\\u0016", "\\u0017", - "\\u0018", "\\u0019", "\\u001a", "\\u001b", - "\\u001c", "\\u001d", "\\u001e", "\\u001f", - // Escaping of " and \ are required by www.json.org string definition. - // Escaping of < and > are required for HTML security. - "", "", "\\\"", "", "", "", "", "", // 0x20 - "", "", "", "", "", "", "", "", - "", "", "", "", "", "", "", "", // 0x30 - "", "", "", "", "\\u003c", "", "\\u003e", "", - "", "", "", "", "", "", "", "", // 0x40 - "", "", "", "", "", "", "", "", - "", "", "", "", "", "", "", "", // 0x50 - "", "", "", "", "\\\\", "", "", "", - "", "", "", "", "", "", "", "", // 0x60 - "", "", "", "", "", "", "", "", - "", "", "", "", "", "", "", "", // 0x70 - "", "", "", "", "", "", "", "\\u007f", - // C1 (ISO 8859 and Unicode) extended control characters - "\\u0080", "\\u0081", "\\u0082", "\\u0083", // 0x80 - "\\u0084", "\\u0085", "\\u0086", "\\u0087", - "\\u0088", "\\u0089", "\\u008a", "\\u008b", - "\\u008c", "\\u008d", "\\u008e", "\\u008f", - "\\u0090", "\\u0091", "\\u0092", "\\u0093", // 0x90 - "\\u0094", "\\u0095", "\\u0096", "\\u0097", - "\\u0098", "\\u0099", "\\u009a", "\\u009b", - "\\u009c", "\\u009d", "\\u009e", "\\u009f" -}; + // C0 (ASCII and derivatives) control characters + "\\u0000", "\\u0001", "\\u0002", "\\u0003", // 0x00 + "\\u0004", "\\u0005", "\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\u000b", + "\\f", "\\r", "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", + "\\u0013", // 0x10 + "\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", + "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f", + // Escaping of " and \ are required by www.json.org string definition. + // Escaping of < and > are required for HTML security. + "", "", "\\\"", "", "", "", "", "", // 0x20 + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", // 0x30 + "", "", "", "", "\\u003c", "", "\\u003e", "", "", "", "", "", "", "", "", + "", // 0x40 + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", // 0x50 + "", "", "", "", "\\\\", "", "", "", "", "", "", "", "", "", "", "", // 0x60 + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", // 0x70 + "", "", "", "", "", "", "", "\\u007f", + // C1 (ISO 8859 and Unicode) extended control characters + "\\u0080", "\\u0081", "\\u0082", "\\u0083", // 0x80 + "\\u0084", "\\u0085", "\\u0086", "\\u0087", "\\u0088", "\\u0089", "\\u008a", + "\\u008b", "\\u008c", "\\u008d", "\\u008e", "\\u008f", "\\u0090", "\\u0091", + "\\u0092", "\\u0093", // 0x90 + "\\u0094", "\\u0095", "\\u0096", "\\u0097", "\\u0098", "\\u0099", "\\u009a", + "\\u009b", "\\u009c", "\\u009d", "\\u009e", "\\u009f"}; // Determines if the given char value is a unicode surrogate code unit (either // high-surrogate or low-surrogate). @@ -102,9 +93,9 @@ inline bool IsValidCodePoint(uint32 cp) { // Returns the low surrogate for the given unicode code point. The result is // meaningless if the given code point is not a supplementary character. inline uint16 ToLowSurrogate(uint32 cp) { - return (cp & (JsonEscaping::kMaxLowSurrogate - - JsonEscaping::kMinLowSurrogate)) - + JsonEscaping::kMinLowSurrogate; + return (cp & + (JsonEscaping::kMaxLowSurrogate - JsonEscaping::kMinLowSurrogate)) + + JsonEscaping::kMinLowSurrogate; } // Returns the high surrogate for the given unicode code point. The result is @@ -328,10 +319,10 @@ void JsonEscaping::Escape(strings::ByteSource* input, ok = ReadCodePoint(str, i, &cp, &num_left, &num_read); if (num_left > 0 || !ok) break; // case iii or iv escaped = EscapeCodePoint(cp, buffer, cp_was_split); - if (!escaped.empty()) break; // case i or ii + if (!escaped.empty()) break; // case i or ii i += num_read; num_read = 0; - } while (i < str.length()); // case iv + } while (i < str.length()); // case iv // First copy the un-escaped prefix, if any, to the output ByteSink. if (i > 0) input->CopyTo(output, i); if (num_read > 0) input->Skip(num_read); diff --git a/src/google/protobuf/util/internal/json_stream_parser_test.cc b/src/google/protobuf/util/internal/json_stream_parser_test.cc index d0980f94eb..7916085d2a 100644 --- a/src/google/protobuf/util/internal/json_stream_parser_test.cc +++ b/src/google/protobuf/util/internal/json_stream_parser_test.cc @@ -118,7 +118,7 @@ class JsonStreamParserTest : public ::testing::Test { result = parser.FinishParse(); } } - if (result.ok()){ + if (result.ok()) { EXPECT_EQ(parser.recursion_depth(), 0); } return result; @@ -138,8 +138,7 @@ class JsonStreamParserTest : public ::testing::Test { void DoErrorTest(StringPiece json, int split, StringPiece error_prefix, bool coerce_utf8 = false, bool allow_empty_null = false) { - util::Status result = - RunTest(json, split, coerce_utf8, allow_empty_null); + util::Status result = RunTest(json, split, coerce_utf8, allow_empty_null); EXPECT_EQ(util::error::INVALID_ARGUMENT, result.code()); StringPiece error_message(result.error_message()); EXPECT_EQ(error_prefix, error_message.substr(0, error_prefix.size())); diff --git a/src/google/protobuf/util/internal/proto_writer.cc b/src/google/protobuf/util/internal/proto_writer.cc index 222ffc35d1..74fe0f44e8 100644 --- a/src/google/protobuf/util/internal/proto_writer.cc +++ b/src/google/protobuf/util/internal/proto_writer.cc @@ -313,10 +313,10 @@ ProtoWriter::ProtoElement::ProtoElement(ProtoWriter::ProtoElement* parent, typeinfo_(this->parent()->typeinfo_), proto3_(type.syntax() == google::protobuf::SYNTAX_PROTO3), type_(type), - size_index_( - !is_list && field->kind() == google::protobuf::Field_Kind_TYPE_MESSAGE - ? ow_->size_insert_.size() - : -1), + size_index_(!is_list && field->kind() == + google::protobuf::Field_Kind_TYPE_MESSAGE + ? ow_->size_insert_.size() + : -1), array_index_(is_list ? 0 : -1), // oneof_indices_ values are 1-indexed (0 means not present). oneof_indices_(type_.oneofs_size() + 1) { diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc index 7accf051a9..a4e7f34110 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource.cc @@ -64,7 +64,7 @@ using util::StatusOr; namespace error { using util::error::Code; using util::error::INTERNAL; -} +} // namespace error namespace converter { using ::PROTOBUF_NAMESPACE_ID::internal::WireFormat; using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite; diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h index 804f6e6e36..af51a27ef9 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource.h +++ b/src/google/protobuf/util/internal/protostream_objectsource.h @@ -37,9 +37,9 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/type.pb.h> +#include <google/protobuf/util/internal/type_info.h> #include <google/protobuf/util/internal/object_source.h> #include <google/protobuf/util/internal/object_writer.h> -#include <google/protobuf/util/internal/type_info.h> #include <google/protobuf/util/type_resolver.h> #include <google/protobuf/stubs/stringpiece.h> #include <google/protobuf/stubs/hash.h> @@ -175,10 +175,9 @@ class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { // Renders a NWP map. // Returns the next tag after reading all map entries. The caller should use // this tag before reading more tags from the stream. - util::StatusOr<uint32> - RenderMap(const google::protobuf::Field* field, - StringPiece name, uint32 list_tag, - ObjectWriter* ow) const; + util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field, + StringPiece name, uint32 list_tag, + ObjectWriter* ow) const; // Renders a packed repeating field. A packed field is stored as: // {tag length item1 item2 item3} instead of the less efficient diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc index 686aa08cd9..dc2875ab4b 100644 --- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc @@ -57,6 +57,8 @@ namespace protobuf { namespace util { namespace converter { +using io::ArrayInputStream; +using io::CodedInputStream; using proto_util_converter::testing::AnyM; using proto_util_converter::testing::AnyOut; using proto_util_converter::testing::Author; @@ -75,8 +77,6 @@ using proto_util_converter::testing::Primitive; using proto_util_converter::testing::Proto3Message; using proto_util_converter::testing::StructType; using proto_util_converter::testing::TimestampDuration; -using io::ArrayInputStream; -using io::CodedInputStream; using ::testing::_; using util::Status; diff --git a/src/google/protobuf/util/internal/protostream_objectwriter.cc b/src/google/protobuf/util/internal/protostream_objectwriter.cc index a3c3065615..7865f78537 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter.cc @@ -57,10 +57,10 @@ namespace util { namespace converter { using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite; +using std::placeholders::_1; using util::Status; using util::StatusOr; using util::error::INVALID_ARGUMENT; -using std::placeholders::_1; ProtoStreamObjectWriter::ProtoStreamObjectWriter( @@ -359,9 +359,9 @@ void ProtoStreamObjectWriter::AnyWriter::StartAny(const DataPiece& value) { // Create our object writer and initialize it with the first StartObject // call. - ow_.reset(new ProtoStreamObjectWriter( - parent_->typeinfo(), *type, &output_, parent_->listener(), - parent_->options_)); + ow_.reset(new ProtoStreamObjectWriter(parent_->typeinfo(), *type, &output_, + parent_->listener(), + parent_->options_)); // Don't call StartObject() for well-known types yet. Depending on the // type of actual data, we may not need to call StartObject(). For @@ -985,9 +985,9 @@ Status ProtoStreamObjectWriter::RenderFieldMask(ProtoStreamObjectWriter* ow, data.ValueAsStringOrDefault(""))); } -// TODO(tsun): figure out how to do proto descriptor based snake case -// conversions as much as possible. Because ToSnakeCase sometimes returns the -// wrong value. + // TODO(tsun): figure out how to do proto descriptor based snake case + // conversions as much as possible. Because ToSnakeCase sometimes returns the + // wrong value. return DecodeCompactFieldMaskPaths(data.str(), std::bind(&RenderOneFieldPath, ow, _1)); } diff --git a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc index 5eda41cf8f..006e85bad4 100644 --- a/src/google/protobuf/util/internal/protostream_objectwriter_test.cc +++ b/src/google/protobuf/util/internal/protostream_objectwriter_test.cc @@ -91,9 +91,9 @@ std::string GetTypeUrl(const Descriptor* descriptor) { } // namespace #if __cplusplus >= 201103L - using std::get; +using std::get; #else - using std::tr1::get; +using std::tr1::get; #endif class BaseProtoStreamObjectWriterTest @@ -183,9 +183,7 @@ class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { ProtoStreamObjectWriterTest() : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} - void ResetProtoWriter() { - ResetTypeInfo(Book::descriptor()); - } + void ResetProtoWriter() { ResetTypeInfo(Book::descriptor()); } virtual ~ProtoStreamObjectWriterTest() {} }; @@ -1024,9 +1022,7 @@ TEST_P(ProtoStreamObjectWriterTest, AcceptUnknownEnumValue) { expected.set_enum_value(static_cast<Proto3Message::NestedEnum>(12345)); EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); - ow_->StartObject("") - ->RenderInt32("enumValue", 12345) - ->EndObject(); + ow_->StartObject("")->RenderInt32("enumValue", 12345)->EndObject(); CheckOutput(expected); } diff --git a/src/google/protobuf/util/internal/type_info_test_helper.cc b/src/google/protobuf/util/internal/type_info_test_helper.cc index 9106f1111f..cc2dcd73cb 100644 --- a/src/google/protobuf/util/internal/type_info_test_helper.cc +++ b/src/google/protobuf/util/internal/type_info_test_helper.cc @@ -33,7 +33,6 @@ #include <memory> #include <vector> - #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> #include <google/protobuf/descriptor.h> diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index ed50b1d4c7..52ce331005 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -98,8 +98,8 @@ util::Status BinaryToJsonStream(TypeResolver* resolver, converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "", &out_stream); if (options.always_print_primitive_fields) { - converter::DefaultValueObjectWriter default_value_writer( - resolver, type, &json_writer); + converter::DefaultValueObjectWriter default_value_writer(resolver, type, + &json_writer); default_value_writer.set_preserve_proto_field_names( options.preserve_proto_field_names); default_value_writer.set_print_enums_as_ints( @@ -129,9 +129,9 @@ class StatusErrorListener : public converter::ErrorListener { util::Status GetStatus() { return status_; } - virtual void InvalidName(const converter::LocationTrackerInterface& loc, - StringPiece unknown_name, - StringPiece message) { + void InvalidName(const converter::LocationTrackerInterface& loc, + StringPiece unknown_name, + StringPiece message) override { std::string loc_string = GetLocString(loc); if (!loc_string.empty()) { loc_string.append(" "); @@ -141,17 +141,17 @@ class StatusErrorListener : public converter::ErrorListener { StrCat(loc_string, unknown_name, ": ", message)); } - virtual void InvalidValue(const converter::LocationTrackerInterface& loc, - StringPiece type_name, - StringPiece value) { + void InvalidValue(const converter::LocationTrackerInterface& loc, + StringPiece type_name, + StringPiece value) override { status_ = util::Status( util::error::INVALID_ARGUMENT, StrCat(GetLocString(loc), ": invalid value ", string(value), " for type ", string(type_name))); } - virtual void MissingField(const converter::LocationTrackerInterface& loc, - StringPiece missing_name) { + void MissingField(const converter::LocationTrackerInterface& loc, + StringPiece missing_name) override { status_ = util::Status(util::error::INVALID_ARGUMENT, StrCat(GetLocString(loc), ": missing field ", std::string(missing_name))); @@ -188,9 +188,8 @@ util::Status JsonToBinaryStream(TypeResolver* resolver, options.ignore_unknown_fields; proto_writer_options.case_insensitive_enum_parsing = options.case_insensitive_enum_parsing; - converter::ProtoStreamObjectWriter proto_writer(resolver, type, &sink, - &listener, - proto_writer_options); + converter::ProtoStreamObjectWriter proto_writer( + resolver, type, &sink, &listener, proto_writer_options); converter::JsonStreamParser parser(&proto_writer); const void* buffer; @@ -212,8 +211,8 @@ util::Status JsonToBinaryString(TypeResolver* resolver, const JsonParseOptions& options) { io::ArrayInputStream input_stream(json_input.data(), json_input.size()); io::StringOutputStream output_stream(binary_output); - return JsonToBinaryStream( - resolver, type_url, &input_stream, &output_stream, options); + return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream, + options); } namespace { @@ -265,8 +264,8 @@ util::Status JsonStringToMessage(StringPiece input, Message* message, ? GetGeneratedTypeResolver() : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool); std::string binary; - util::Status result = JsonToBinaryString( - resolver, GetTypeUrl(*message), input, &binary, options); + util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message), + input, &binary, options); if (result.ok() && !message->ParseFromString(binary)) { result = util::Status(util::error::INVALID_ARGUMENT, diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h index 2e4061540f..39b6b5aa03 100644 --- a/src/google/protobuf/util/json_util.h +++ b/src/google/protobuf/util/json_util.h @@ -133,8 +133,9 @@ inline util::Status BinaryToJsonStream( } PROTOBUF_EXPORT util::Status BinaryToJsonString( - TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, - std::string* json_output, const JsonPrintOptions& options); + TypeResolver* resolver, const std::string& type_url, + const std::string& binary_input, std::string* json_output, + const JsonPrintOptions& options); inline util::Status BinaryToJsonString(TypeResolver* resolver, const std::string& type_url, @@ -155,8 +156,7 @@ PROTOBUF_EXPORT util::Status JsonToBinaryStream( io::ZeroCopyOutputStream* binary_output, const JsonParseOptions& options); inline util::Status JsonToBinaryStream( - TypeResolver* resolver, - const std::string& type_url, + TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output) { return JsonToBinaryStream(resolver, type_url, json_input, binary_output, diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index dedcff8824..9f6054342b 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -49,14 +49,14 @@ namespace protobuf { namespace util { namespace { -using proto_util_converter::testing::MapIn; using proto3::BAR; using proto3::FOO; +using proto3::TestAny; using proto3::TestEnumValue; using proto3::TestMap; using proto3::TestMessage; using proto3::TestOneof; -using proto3::TestAny; +using proto_util_converter::testing::MapIn; static const char kTypeUrlPrefix[] = "type.googleapis.com"; @@ -67,8 +67,7 @@ static const char kTypeUrlPrefix[] = "type.googleapis.com"; // tests are contained in the //net/proto2/util/converter directory. class JsonUtilTest : public ::testing::Test { protected: - JsonUtilTest() { - } + JsonUtilTest() {} std::string ToJson(const Message& message, const JsonPrintOptions& options) { std::string result; @@ -339,8 +338,9 @@ TEST_F(JsonUtilTest, TestDynamicMessage) { DescriptorPool pool(&database); // A dynamic version of the test proto. DynamicMessageFactory factory; - std::unique_ptr<Message> message(factory.GetPrototype( - pool.FindMessageTypeByName("proto3.TestMessage"))->New()); + std::unique_ptr<Message> message( + factory.GetPrototype(pool.FindMessageTypeByName("proto3.TestMessage")) + ->New()); EXPECT_TRUE(FromJson(input, message.get())); // Convert to generated message for easy inspection. @@ -498,7 +498,9 @@ typedef std::pair<char*, int> Segment; class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream { public: explicit SegmentedZeroCopyOutputStream(std::list<Segment> segments) - : segments_(segments), last_segment_(static_cast<char*>(NULL), 0), byte_count_(0) {} + : segments_(segments), + last_segment_(static_cast<char*>(NULL), 0), + byte_count_(0) {} virtual bool Next(void** buffer, int* length) { if (segments_.empty()) { @@ -627,8 +629,8 @@ TEST_F(JsonUtilTest, TestWrongJsonInput) { TypeResolver* resolver = NewTypeResolverForDescriptorPool( "type.googleapis.com", DescriptorPool::generated_pool()); - auto result_status = util::JsonToBinaryStream( - resolver, message_type, &input_stream, &output_stream); + auto result_status = util::JsonToBinaryStream(resolver, message_type, + &input_stream, &output_stream); delete resolver; diff --git a/src/google/protobuf/util/message_differencer.cc b/src/google/protobuf/util/message_differencer.cc index 57e9987863..92d113d434 100644 --- a/src/google/protobuf/util/message_differencer.cc +++ b/src/google/protobuf/util/message_differencer.cc @@ -53,6 +53,8 @@ #include <google/protobuf/stubs/strutil.h> +#include <google/protobuf/port_def.inc> + namespace google { namespace protobuf { @@ -72,19 +74,25 @@ class NumDiffsReporter : public google::protobuf::util::MessageDifferencer::Repo void ReportAdded( const google::protobuf::Message& message1, const google::protobuf::Message& message2, const std::vector<google::protobuf::util::MessageDifferencer::SpecificField>& - field_path) override { ++num_diffs_; } + field_path) override { + ++num_diffs_; + } // Report that a field has been deleted from Message1. void ReportDeleted( const google::protobuf::Message& message1, const google::protobuf::Message& message2, const std::vector<google::protobuf::util::MessageDifferencer::SpecificField>& - field_path) override { ++num_diffs_; } + field_path) override { + ++num_diffs_; + } // Report that the value of a field has been modified. void ReportModified( const google::protobuf::Message& message1, const google::protobuf::Message& message2, const std::vector<google::protobuf::util::MessageDifferencer::SpecificField>& - field_path) override { ++num_diffs_; } + field_path) override { + ++num_diffs_; + } private: int32 num_diffs_; @@ -101,17 +109,16 @@ class MessageDifferencer::MultipleFieldsMapKeyComparator MultipleFieldsMapKeyComparator( MessageDifferencer* message_differencer, const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths) - : message_differencer_(message_differencer), - key_field_paths_(key_field_paths) { + : message_differencer_(message_differencer), + key_field_paths_(key_field_paths) { GOOGLE_CHECK(!key_field_paths_.empty()); for (int i = 0; i < key_field_paths_.size(); ++i) { GOOGLE_CHECK(!key_field_paths_[i].empty()); } } - MultipleFieldsMapKeyComparator( - MessageDifferencer* message_differencer, - const FieldDescriptor* key) - : message_differencer_(message_differencer) { + MultipleFieldsMapKeyComparator(MessageDifferencer* message_differencer, + const FieldDescriptor* key) + : message_differencer_(message_differencer) { std::vector<const FieldDescriptor*> key_field_path; key_field_path.push_back(key); key_field_paths_.push_back(key_field_path); @@ -129,8 +136,7 @@ class MessageDifferencer::MultipleFieldsMapKeyComparator private: bool IsMatchInternal( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& parent_fields, const std::vector<const FieldDescriptor*>& key_field_path, int path_index) const { @@ -139,12 +145,12 @@ class MessageDifferencer::MultipleFieldsMapKeyComparator if (path_index == key_field_path.size() - 1) { if (field->is_repeated()) { if (!message_differencer_->CompareRepeatedField( - message1, message2, field, ¤t_parent_fields)) { + message1, message2, field, ¤t_parent_fields)) { return false; } } else { if (!message_differencer_->CompareFieldValueUsingParentFields( - message1, message2, field, -1, -1, ¤t_parent_fields)) { + message1, message2, field, -1, -1, ¤t_parent_fields)) { return false; } } @@ -163,12 +169,10 @@ class MessageDifferencer::MultipleFieldsMapKeyComparator SpecificField specific_field; specific_field.field = field; current_parent_fields.push_back(specific_field); - return IsMatchInternal( - reflection1->GetMessage(message1, field), - reflection2->GetMessage(message2, field), - current_parent_fields, - key_field_path, - path_index + 1); + return IsMatchInternal(reflection1->GetMessage(message1, field), + reflection2->GetMessage(message2, field), + current_parent_fields, key_field_path, + path_index + 1); } } MessageDifferencer* message_differencer_; @@ -241,8 +245,7 @@ bool MessageDifferencer::Equivalent(const Message& message1, bool MessageDifferencer::ApproximatelyEquals(const Message& message1, const Message& message2) { MessageDifferencer differencer; - differencer.set_float_comparison( - MessageDifferencer::APPROXIMATE); + differencer.set_float_comparison(MessageDifferencer::APPROXIMATE); return differencer.Compare(message1, message2); } @@ -291,18 +294,14 @@ void MessageDifferencer::set_message_field_comparison( message_field_comparison_ = comparison; } -void MessageDifferencer::set_scope(Scope scope) { - scope_ = scope; -} +void MessageDifferencer::set_scope(Scope scope) { scope_ = scope; } -MessageDifferencer::Scope MessageDifferencer::scope() { - return scope_; -} +MessageDifferencer::Scope MessageDifferencer::scope() { return scope_; } void MessageDifferencer::set_float_comparison(FloatComparison comparison) { default_field_comparator_.set_float_comparison( - comparison == EXACT ? - DefaultFieldComparator::EXACT : DefaultFieldComparator::APPROXIMATE); + comparison == EXACT ? DefaultFieldComparator::EXACT + : DefaultFieldComparator::APPROXIMATE); } void MessageDifferencer::set_repeated_field_comparison( @@ -313,23 +312,19 @@ void MessageDifferencer::set_repeated_field_comparison( void MessageDifferencer::CheckRepeatedFieldComparisons( const FieldDescriptor* field, const RepeatedFieldComparison& new_comparison) { - GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " - << field->full_name(); + GOOGLE_CHECK(field->is_repeated()) + << "Field must be repeated: " << field->full_name(); const MapKeyComparator* key_comparator = GetMapKeyComparator(field); GOOGLE_CHECK(key_comparator == NULL) - << "Cannot treat this repeated field as both MAP and " - << new_comparison + << "Cannot treat this repeated field as both MAP and " << new_comparison << " for" << " comparison. Field name is: " << field->full_name(); GOOGLE_CHECK(repeated_field_comparisons_.find(field) == - repeated_field_comparisons_.end() || + repeated_field_comparisons_.end() || repeated_field_comparisons_[field] == new_comparison) << "Cannot treat the same field as both " - << repeated_field_comparisons_[field] - << " and " - << new_comparison - <<". Field name is: " - << field->full_name(); + << repeated_field_comparisons_[field] << " and " << new_comparison + << ". Field name is: " << field->full_name(); } void MessageDifferencer::TreatAsSet(const FieldDescriptor* field) { @@ -360,8 +355,7 @@ void MessageDifferencer::TreatAsSmartList(const FieldDescriptor* field) { void MessageDifferencer::TreatAsMap(const FieldDescriptor* field, const FieldDescriptor* key) { GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type()) - << "Field has to be message type. Field name is: " - << field->full_name(); + << "Field has to be message type. Field name is: " << field->full_name(); GOOGLE_CHECK(key->containing_type() == field->message_type()) << key->full_name() << " must be a direct subfield within the repeated field " @@ -370,8 +364,7 @@ void MessageDifferencer::TreatAsMap(const FieldDescriptor* field, repeated_field_comparisons_.end()) << "Cannot treat the same field as both " << repeated_field_comparisons_[field] - << " and MAP. Field name is: " - << field->full_name(); + << " and MAP. Field name is: " << field->full_name(); MapKeyComparator* key_comparator = new MultipleFieldsMapKeyComparator(this, key); owned_key_comparators_.push_back(key_comparator); @@ -393,11 +386,10 @@ void MessageDifferencer::TreatAsMapWithMultipleFieldsAsKey( void MessageDifferencer::TreatAsMapWithMultipleFieldPathsAsKey( const FieldDescriptor* field, const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths) { - GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " - << field->full_name(); + GOOGLE_CHECK(field->is_repeated()) + << "Field must be repeated: " << field->full_name(); GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type()) - << "Field has to be message type. Field name is: " - << field->full_name(); + << "Field has to be message type. Field name is: " << field->full_name(); for (int i = 0; i < key_field_paths.size(); ++i) { const std::vector<const FieldDescriptor*>& key_field_path = key_field_paths[i]; @@ -419,10 +411,9 @@ void MessageDifferencer::TreatAsMapWithMultipleFieldPathsAsKey( } GOOGLE_CHECK(repeated_field_comparisons_.find(field) == repeated_field_comparisons_.end()) - << "Cannot treat the same field as both " - << repeated_field_comparisons_[field] - << " and MAP. Field name is: " - << field->full_name(); + << "Cannot treat the same field as both " + << repeated_field_comparisons_[field] + << " and MAP. Field name is: " << field->full_name(); MapKeyComparator* key_comparator = new MultipleFieldsMapKeyComparator(this, key_field_paths); owned_key_comparators_.push_back(key_comparator); @@ -430,16 +421,14 @@ void MessageDifferencer::TreatAsMapWithMultipleFieldPathsAsKey( } void MessageDifferencer::TreatAsMapUsingKeyComparator( - const FieldDescriptor* field, - const MapKeyComparator* key_comparator) { - GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " - << field->full_name(); + const FieldDescriptor* field, const MapKeyComparator* key_comparator) { + GOOGLE_CHECK(field->is_repeated()) + << "Field must be repeated: " << field->full_name(); GOOGLE_CHECK(repeated_field_comparisons_.find(field) == repeated_field_comparisons_.end()) << "Cannot treat the same field as both " << repeated_field_comparisons_[field] - << " and MAP. Field name is: " - << field->full_name(); + << " and MAP. Field name is: " << field->full_name(); map_field_key_comparator_[field] = key_comparator; } @@ -510,8 +499,7 @@ bool MessageDifferencer::Compare(const Message& message1, } bool MessageDifferencer::CompareWithFields( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<const FieldDescriptor*>& message1_fields_arg, const std::vector<const FieldDescriptor*>& message2_fields_arg) { if (message1.GetDescriptor() != message2.GetDescriptor()) { @@ -549,16 +537,14 @@ bool MessageDifferencer::CompareWithFields( return result; } -bool MessageDifferencer::Compare( - const Message& message1, - const Message& message2, - std::vector<SpecificField>* parent_fields) { +bool MessageDifferencer::Compare(const Message& message1, + const Message& message2, + std::vector<SpecificField>* parent_fields) { const Descriptor* descriptor1 = message1.GetDescriptor(); const Descriptor* descriptor2 = message2.GetDescriptor(); if (descriptor1 != descriptor2) { GOOGLE_LOG(DFATAL) << "Comparison between two messages with different " - << "descriptors. " - << descriptor1->full_name() << " vs " + << "descriptors. " << descriptor1->full_name() << " vs " << descriptor2->full_name(); return false; } @@ -577,11 +563,6 @@ bool MessageDifferencer::Compare( const Reflection* reflection1 = message1.GetReflection(); const Reflection* reflection2 = message2.GetReflection(); - std::vector<const FieldDescriptor*> message1_fields = - RetrieveFields(message1, true); - std::vector<const FieldDescriptor*> message2_fields = - RetrieveFields(message2, false); - bool unknown_compare_result = true; // Ignore unknown fields in EQUIVALENT mode if (message_field_comparison_ != EQUIVALENT) { @@ -589,9 +570,8 @@ bool MessageDifferencer::Compare( &reflection1->GetUnknownFields(message1); const UnknownFieldSet* unknown_field_set2 = &reflection2->GetUnknownFields(message2); - if (!CompareUnknownFields(message1, message2, - *unknown_field_set1, *unknown_field_set2, - parent_fields)) { + if (!CompareUnknownFields(message1, message2, *unknown_field_set1, + *unknown_field_set2, parent_fields)) { if (reporter_ == NULL) { return false; }; @@ -599,6 +579,11 @@ bool MessageDifferencer::Compare( } } + std::vector<const FieldDescriptor*> message1_fields = + RetrieveFields(message1, true); + std::vector<const FieldDescriptor*> message2_fields = + RetrieveFields(message2, false); + return CompareRequestedFieldsUsingSettings( message1, message2, message1_fields, message2_fields, @@ -751,8 +736,9 @@ bool MessageDifferencer::CompareWithFieldsInternal( if (reporter_ != NULL) { assert(field1 != NULL); - int count = field1->is_repeated() ? - reflection1->FieldSize(message1, field1) : 1; + int count = field1->is_repeated() + ? reflection1->FieldSize(message1, field1) + : 1; for (int i = 0; i < count; ++i) { SpecificField specific_field; @@ -791,8 +777,9 @@ bool MessageDifferencer::CompareWithFieldsInternal( } if (reporter_ != NULL) { - int count = field2->is_repeated() ? - reflection2->FieldSize(message2, field2) : 1; + int count = field2->is_repeated() + ? reflection2->FieldSize(message2, field2) + : 1; for (int i = 0; i < count; ++i) { SpecificField specific_field; @@ -837,8 +824,8 @@ bool MessageDifferencer::CompareWithFieldsInternal( bool fieldDifferent = false; assert(field1 != NULL); if (field1->is_repeated()) { - fieldDifferent = !CompareRepeatedField(message1, message2, field1, - parent_fields); + fieldDifferent = + !CompareRepeatedField(message1, message2, field1, parent_fields); if (fieldDifferent) { if (reporter_ == NULL) return false; isDifferent = true; @@ -881,9 +868,9 @@ bool MessageDifferencer::IsMatch( Reporter* reporter, int index1, int index2) { std::vector<SpecificField> current_parent_fields(parent_fields); if (repeated_field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { - return CompareFieldValueUsingParentFields( - *message1, *message2, repeated_field, index1, index2, - ¤t_parent_fields); + return CompareFieldValueUsingParentFields(*message1, *message2, + repeated_field, index1, index2, + ¤t_parent_fields); } // Back up the Reporter and output_string_. They will be reset in the // following code. @@ -894,9 +881,9 @@ bool MessageDifferencer::IsMatch( bool match; if (key_comparator == NULL) { - match = CompareFieldValueUsingParentFields( - *message1, *message2, repeated_field, index1, index2, - ¤t_parent_fields); + match = CompareFieldValueUsingParentFields(*message1, *message2, + repeated_field, index1, index2, + ¤t_parent_fields); } else { const Reflection* reflection1 = message1->GetReflection(); const Reflection* reflection2 = message2->GetReflection(); @@ -918,8 +905,7 @@ bool MessageDifferencer::IsMatch( } bool MessageDifferencer::CompareRepeatedField( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const FieldDescriptor* repeated_field, std::vector<SpecificField>* parent_fields) { // the input FieldDescriptor is guaranteed to be repeated field. @@ -1048,8 +1034,7 @@ bool MessageDifferencer::CompareRepeatedField( bool MessageDifferencer::CompareFieldValue(const Message& message1, const Message& message2, const FieldDescriptor* field, - int index1, - int index2) { + int index1, int index2) { return CompareFieldValueUsingParentFields(message1, message2, field, index1, index2, NULL); } @@ -1068,12 +1053,14 @@ bool MessageDifferencer::CompareFieldValueUsingParentFields( // methods. const Reflection* reflection1 = message1.GetReflection(); const Reflection* reflection2 = message2.GetReflection(); - const Message& m1 = field->is_repeated() ? - reflection1->GetRepeatedMessage(message1, field, index1) : - reflection1->GetMessage(message1, field); - const Message& m2 = field->is_repeated() ? - reflection2->GetRepeatedMessage(message2, field, index2) : - reflection2->GetMessage(message2, field); + const Message& m1 = + field->is_repeated() + ? reflection1->GetRepeatedMessage(message1, field, index1) + : reflection1->GetMessage(message1, field); + const Message& m2 = + field->is_repeated() + ? reflection2->GetRepeatedMessage(message2, field, index2) + : reflection2->GetMessage(message2, field); // parent_fields is used in calls to Reporter methods. if (parent_fields != NULL) { @@ -1134,12 +1121,11 @@ bool MessageDifferencer::IsTreatedAsSmartList(const FieldDescriptor* field) { bool MessageDifferencer::IsTreatedAsSubset(const FieldDescriptor* field) { return scope_ == PARTIAL && - (IsTreatedAsSet(field) || GetMapKeyComparator(field) != NULL); + (IsTreatedAsSet(field) || GetMapKeyComparator(field) != NULL); } bool MessageDifferencer::IsIgnored( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const FieldDescriptor* field, const std::vector<SpecificField>& parent_fields) { if (ignored_fields_.find(field) != ignored_fields_.end()) { @@ -1282,8 +1268,13 @@ bool MessageDifferencer::CompareUnknownFields( int index1 = 0; int index2 = 0; while (index1 < fields1.size() || index2 < fields2.size()) { - enum { ADDITION, DELETION, MODIFICATION, COMPARE_GROUPS, - NO_CHANGE } change_type; + enum { + ADDITION, + DELETION, + MODIFICATION, + COMPARE_GROUPS, + NO_CHANGE + } change_type; // focus_field is the field we're currently reporting on. (In the case // of a modification, it's the field on the left side.) @@ -1292,7 +1283,7 @@ bool MessageDifferencer::CompareUnknownFields( if (index2 == fields2.size() || (index1 < fields1.size() && - is_before(fields1[index1], fields2[index2]))) { + is_before(fields1[index1], fields2[index2]))) { // fields1[index1] is not present in fields2. change_type = DELETION; focus_field = fields1[index1].second; @@ -1414,10 +1405,9 @@ bool MessageDifferencer::CompareUnknownFields( ++index2; break; case COMPARE_GROUPS: - if (!CompareUnknownFields(message1, message2, - fields1[index1].second->group(), - fields2[index2].second->group(), - parent_field)) { + if (!CompareUnknownFields( + message1, message2, fields1[index1].second->group(), + fields2[index2].second->group(), parent_field)) { if (reporter_ == NULL) return false; is_different = true; reporter_->ReportModified(message1, message2, *parent_field); @@ -1461,6 +1451,7 @@ class MaximumMatcher { // If early_return is true, this method will return 0 immediately when it // finds that not all nodes on the left side can be matched. int FindMaximumMatch(bool early_return); + private: // Determines whether the node on the left side of the bipartial graph // matches the one on the right side. @@ -1582,20 +1573,17 @@ bool MessageDifferencer::MatchRepeatedFieldIndices( bool success = true; // Find potential match if this is a special repeated field. - if (key_comparator != nullptr || - IsTreatedAsSet(repeated_field) || + if (key_comparator != nullptr || IsTreatedAsSet(repeated_field) || IsTreatedAsSmartSet(repeated_field) || IsTreatedAsSmartList(repeated_field)) { if (scope_ == PARTIAL) { // When partial matching is enabled, Compare(a, b) && Compare(a, c) // doesn't necessarily imply Compare(b, c). Therefore a naive greedy // algorithm will fail to find a maximum matching. - // Here we use the argumenting path algorithm. - MaximumMatcher::NodeMatchCallback* callback = - ::google::protobuf::NewPermanentCallback( - this, &MessageDifferencer::IsMatch, - repeated_field, key_comparator, - &message1, &message2, parent_fields, nullptr); + // Here we use the augmenting path algorithm. + MaximumMatcher::NodeMatchCallback* callback = ::google::protobuf::NewPermanentCallback( + this, &MessageDifferencer::IsMatch, repeated_field, key_comparator, + &message1, &message2, parent_fields, nullptr); MaximumMatcher matcher(count1, count2, callback, match_list1, match_list2); // If diff info is not needed, we should end the matching process as @@ -1637,13 +1625,12 @@ bool MessageDifferencer::MatchRepeatedFieldIndices( if (is_treated_as_smart_set) { num_diffs_reporter.Reset(); - match = IsMatch(repeated_field, key_comparator, - &message1, &message2, parent_fields, - &num_diffs_reporter, i, j); + match = + IsMatch(repeated_field, key_comparator, &message1, &message2, + parent_fields, &num_diffs_reporter, i, j); } else { - match = IsMatch(repeated_field, key_comparator, - &message1, &message2, parent_fields, - nullptr, i, j); + match = IsMatch(repeated_field, key_comparator, &message1, + &message2, parent_fields, nullptr, i, j); } if (is_treated_as_smart_set) { @@ -1704,15 +1691,16 @@ FieldComparator::ComparisonResult MessageDifferencer::GetFieldComparisonResult( const Message& message1, const Message& message2, const FieldDescriptor* field, int index1, int index2, const FieldContext* field_context) { - FieldComparator* comparator = field_comparator_ != NULL ? - field_comparator_ : &default_field_comparator_; - return comparator->Compare(message1, message2, field, - index1, index2, field_context); + FieldComparator* comparator = field_comparator_ != NULL + ? field_comparator_ + : &default_field_comparator_; + return comparator->Compare(message1, message2, field, index1, index2, + field_context); } // =========================================================================== -MessageDifferencer::Reporter::Reporter() { } +MessageDifferencer::Reporter::Reporter() {} MessageDifferencer::Reporter::~Reporter() {} // =========================================================================== @@ -1730,14 +1718,15 @@ MessageDifferencer::IgnoreCriteria::~IgnoreCriteria() {} // Note that the printer's delimiter is not used, because if we are given a // printer, we don't know its delimiter. MessageDifferencer::StreamReporter::StreamReporter( - io::ZeroCopyOutputStream* output) : printer_(new io::Printer(output, '$')), - delete_printer_(true), - report_modified_aggregates_(false) { } + io::ZeroCopyOutputStream* output) + : printer_(new io::Printer(output, '$')), + delete_printer_(true), + report_modified_aggregates_(false) {} -MessageDifferencer::StreamReporter::StreamReporter( - io::Printer* printer) : printer_(printer), - delete_printer_(false), - report_modified_aggregates_(false) { } +MessageDifferencer::StreamReporter::StreamReporter(io::Printer* printer) + : printer_(printer), + delete_printer_(false), + report_modified_aggregates_(false) {} MessageDifferencer::StreamReporter::~StreamReporter() { if (delete_printer_) delete printer_; @@ -1754,8 +1743,7 @@ void MessageDifferencer::StreamReporter::PrintPath( if (specific_field.field != NULL) { if (specific_field.field->is_extension()) { - printer_->Print("($name$)", "name", - specific_field.field->full_name()); + printer_->Print("($name$)", "name", specific_field.field->full_name()); } else { printer_->PrintRaw(specific_field.field->name()); } @@ -1782,10 +1770,9 @@ void MessageDifferencer::StreamReporter::PrintPath( PrintPath(field_path, left_side); } -void MessageDifferencer:: -StreamReporter::PrintValue(const Message& message, - const std::vector<SpecificField>& field_path, - bool left_side) { +void MessageDifferencer::StreamReporter::PrintValue( + const Message& message, const std::vector<SpecificField>& field_path, + bool left_side) { const SpecificField& specific_field = field_path.back(); const FieldDescriptor* field = specific_field.field; if (field != NULL) { @@ -1793,9 +1780,10 @@ StreamReporter::PrintValue(const Message& message, int index = left_side ? specific_field.index : specific_field.new_index; if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { const Reflection* reflection = message.GetReflection(); - const Message& field_message = field->is_repeated() ? - reflection->GetRepeatedMessage(message, field, index) : - reflection->GetMessage(message, field); + const Message& field_message = + field->is_repeated() + ? reflection->GetRepeatedMessage(message, field, index) + : reflection->GetMessage(message, field); output = field_message.ShortDebugString(); if (output.empty()) { printer_->Print("{ }"); @@ -1808,19 +1796,17 @@ StreamReporter::PrintValue(const Message& message, } } else { const UnknownFieldSet* unknown_fields = - (left_side ? - specific_field.unknown_field_set1 : - specific_field.unknown_field_set2); - const UnknownField* unknown_field = &unknown_fields->field( - left_side ? - specific_field.unknown_field_index1 : - specific_field.unknown_field_index2); + (left_side ? specific_field.unknown_field_set1 + : specific_field.unknown_field_set2); + const UnknownField* unknown_field = + &unknown_fields->field(left_side ? specific_field.unknown_field_index1 + : specific_field.unknown_field_index2); PrintUnknownFieldValue(unknown_field); } } -void MessageDifferencer:: -StreamReporter::PrintUnknownFieldValue(const UnknownField* unknown_field) { +void MessageDifferencer::StreamReporter::PrintUnknownFieldValue( + const UnknownField* unknown_field) { GOOGLE_CHECK(unknown_field != NULL) << " Cannot print NULL unknown_field."; std::string output; @@ -1837,8 +1823,8 @@ StreamReporter::PrintUnknownFieldValue(const UnknownField* unknown_field) { "0x", strings::Hex(unknown_field->fixed64(), strings::ZERO_PAD_16)); break; case UnknownField::TYPE_LENGTH_DELIMITED: - output = StringPrintf("\"%s\"", - CEscape(unknown_field->length_delimited()).c_str()); + output = StringPrintf( + "\"%s\"", CEscape(unknown_field->length_delimited()).c_str()); break; case UnknownField::TYPE_GROUP: // TODO(kenton): Print the contents of the group like we do for @@ -1855,8 +1841,7 @@ void MessageDifferencer::StreamReporter::Print(const std::string& str) { } void MessageDifferencer::StreamReporter::ReportAdded( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { printer_->Print("added: "); PrintPath(field_path, false, message2); @@ -1866,8 +1851,7 @@ void MessageDifferencer::StreamReporter::ReportAdded( } void MessageDifferencer::StreamReporter::ReportDeleted( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { printer_->Print("deleted: "); PrintPath(field_path, true, message1); @@ -1877,8 +1861,7 @@ void MessageDifferencer::StreamReporter::ReportDeleted( } void MessageDifferencer::StreamReporter::ReportModified( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { if (!report_modified_aggregates_ && field_path.back().field == NULL) { if (field_path.back().unknown_field_type == UnknownField::TYPE_GROUP) { @@ -1907,8 +1890,7 @@ void MessageDifferencer::StreamReporter::ReportModified( } void MessageDifferencer::StreamReporter::ReportMoved( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { printer_->Print("moved: "); PrintPath(field_path, true, message1); @@ -1920,8 +1902,7 @@ void MessageDifferencer::StreamReporter::ReportMoved( } void MessageDifferencer::StreamReporter::ReportMatched( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { printer_->Print("matched: "); PrintPath(field_path, true, message1); @@ -1935,8 +1916,7 @@ void MessageDifferencer::StreamReporter::ReportMatched( } void MessageDifferencer::StreamReporter::ReportIgnored( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) { printer_->Print("ignored: "); PrintPath(field_path, true, message1); @@ -1965,7 +1945,6 @@ MessageDifferencer::CreateMultipleFieldsMapKeyComparator( return new MultipleFieldsMapKeyComparator(this, key_field_paths); } - } // namespace util } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/util/message_differencer.h b/src/google/protobuf/util/message_differencer.h index 7504227622..e36a72b9b5 100644 --- a/src/google/protobuf/util/message_differencer.h +++ b/src/google/protobuf/util/message_differencer.h @@ -48,7 +48,7 @@ #include <string> #include <vector> #include <google/protobuf/descriptor.h> // FieldDescriptor -#include <google/protobuf/message.h> // Message +#include <google/protobuf/message.h> // Message #include <google/protobuf/unknown_field_set.h> #include <google/protobuf/util/field_comparator.h> @@ -70,7 +70,6 @@ namespace util { class DefaultFieldComparator; class FieldContext; // declared below MessageDifferencer - // A basic differencer that can be used to determine // the differences between two specified Protocol Messages. If any differences // are found, the Compare method will return false, and any differencer reporter @@ -224,20 +223,17 @@ class PROTOBUF_EXPORT MessageDifferencer { virtual ~Reporter(); // Reports that a field has been added into Message2. - virtual void ReportAdded( - const Message& message1, const Message& message2, - const std::vector<SpecificField>& field_path) = 0; + virtual void ReportAdded(const Message& message1, const Message& message2, + const std::vector<SpecificField>& field_path) = 0; // Reports that a field has been deleted from Message1. virtual void ReportDeleted( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) = 0; // Reports that the value of a field has been modified. virtual void ReportModified( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const std::vector<SpecificField>& field_path) = 0; // Reports that a repeated field has been moved to another location. This @@ -246,18 +242,16 @@ class PROTOBUF_EXPORT MessageDifferencer { // mutually exclusive. If a field has been both moved and modified, then // only ReportModified will be called. virtual void ReportMoved( - const Message& /* message1 */, - const Message& /* message2 */, - const std::vector<SpecificField>& /* field_path */) { } + const Message& /* message1 */, const Message& /* message2 */, + const std::vector<SpecificField>& /* field_path */) {} // Reports that two fields match. Useful for doing side-by-side diffs. // This function is mutually exclusive with ReportModified and ReportMoved. // Note that you must call set_report_matches(true) before calling Compare // to make use of this function. virtual void ReportMatched( - const Message& /* message1 */, - const Message& /* message2 */, - const std::vector<SpecificField>& /* field_path */) { } + const Message& /* message1 */, const Message& /* message2 */, + const std::vector<SpecificField>& /* field_path */) {} // Reports that two fields would have been compared, but the // comparison has been skipped because the field was marked as @@ -279,9 +273,8 @@ class PROTOBUF_EXPORT MessageDifferencer { // the fields are equal or not (perhaps with a second call to // Compare()), if it cares. virtual void ReportIgnored( - const Message& /* message1 */, - const Message& /* message2 */, - const std::vector<SpecificField>& /* field_path */) { } + const Message& /* message1 */, const Message& /* message2 */, + const std::vector<SpecificField>& /* field_path */) {} // Report that an unknown field is ignored. (see comment above). // Note this is a different function since the last SpecificField in field @@ -302,8 +295,7 @@ class PROTOBUF_EXPORT MessageDifferencer { virtual ~MapKeyComparator(); virtual bool IsMatch( - const Message& /* message1 */, - const Message& /* message2 */, + const Message& /* message1 */, const Message& /* message2 */, const std::vector<SpecificField>& /* parent_fields */) const { GOOGLE_CHECK(false) << "IsMatch() is not implemented."; return false; @@ -326,8 +318,7 @@ class PROTOBUF_EXPORT MessageDifferencer { // Returns true if the field should be ignored. virtual bool IsIgnored( - const Message& /* message1 */, - const Message& /* message2 */, + const Message& /* message1 */, const Message& /* message2 */, const FieldDescriptor* /* field */, const std::vector<SpecificField>& /* parent_fields */) = 0; @@ -372,13 +363,13 @@ class PROTOBUF_EXPORT MessageDifferencer { }; enum RepeatedFieldComparison { - AS_LIST, // Repeated fields are compared in order. Differing values at - // the same index are reported using ReportModified(). If the - // repeated fields have different numbers of elements, the - // unpaired elements are reported using ReportAdded() or - // ReportDeleted(). - AS_SET, // Treat all the repeated fields as sets. - // See TreatAsSet(), as below. + AS_LIST, // Repeated fields are compared in order. Differing values at + // the same index are reported using ReportModified(). If the + // repeated fields have different numbers of elements, the + // unpaired elements are reported using ReportAdded() or + // ReportDeleted(). + AS_SET, // Treat all the repeated fields as sets. + // See TreatAsSet(), as below. AS_SMART_LIST, // Similar to AS_SET, but preserve the order and find the // longest matching sequence from the first matching // element. To use an optimal solution, call @@ -484,9 +475,8 @@ class PROTOBUF_EXPORT MessageDifferencer { // first key matching step. Rather than comparing some specified fields, it // will invoke the IsMatch method of the given 'key_comparator' to decide if // two elements have the same key. - void TreatAsMapUsingKeyComparator( - const FieldDescriptor* field, - const MapKeyComparator* key_comparator); + void TreatAsMapUsingKeyComparator(const FieldDescriptor* field, + const MapKeyComparator* key_comparator); // Initiates and returns a new instance of MultipleFieldsMapKeyComparator. MapKeyComparator* CreateMultipleFieldsMapKeyComparator( @@ -543,9 +533,7 @@ class PROTOBUF_EXPORT MessageDifferencer { // Tells the differencer whether or not to report moves (in a set or map // repeated field). This method must be called before Compare. The default for // a new differencer is true. - void set_report_moves(bool report_moves) { - report_moves_ = report_moves; - } + void set_report_moves(bool report_moves) { report_moves_ = report_moves; } // Tells the differencer whether or not to report ignored values. This method // must be called before Compare. The default for a new differencer is true. @@ -757,11 +745,8 @@ class PROTOBUF_EXPORT MessageDifferencer { std::vector<SpecificField>* parent_fields); // Shorthand for CompareFieldValueUsingParentFields with NULL parent_fields. - bool CompareFieldValue(const Message& message1, - const Message& message2, - const FieldDescriptor* field, - int index1, - int index2); + bool CompareFieldValue(const Message& message1, const Message& message2, + const FieldDescriptor* field, int index1, int index2); // Compares the specified field on the two messages, returning // true if they are the same, false otherwise. For repeated fields, @@ -773,11 +758,8 @@ class PROTOBUF_EXPORT MessageDifferencer { // To avoid confusing users you should not set it to NULL unless you modified // Reporter to handle the change of parent_fields correctly. bool CompareFieldValueUsingParentFields( - const Message& message1, - const Message& message2, - const FieldDescriptor* field, - int index1, - int index2, + const Message& message1, const Message& message2, + const FieldDescriptor* field, int index1, int index2, std::vector<SpecificField>* parent_fields); // Compares the specified field on the two messages, returning comparison @@ -791,8 +773,8 @@ class PROTOBUF_EXPORT MessageDifferencer { // if the key_comprator is NULL, this function returns true when the two // elements are equal. bool IsMatch(const FieldDescriptor* repeated_field, - const MapKeyComparator* key_comparator, - const Message* message1, const Message* message2, + const MapKeyComparator* key_comparator, const Message* message1, + const Message* message2, const std::vector<SpecificField>& parent_fields, Reporter* reporter, int index1, int index2); @@ -816,11 +798,9 @@ class PROTOBUF_EXPORT MessageDifferencer { // Returns true if this field is to be ignored when this // MessageDifferencer compares messages. - bool IsIgnored( - const Message& message1, - const Message& message2, - const FieldDescriptor* field, - const std::vector<SpecificField>& parent_fields); + bool IsIgnored(const Message& message1, const Message& message2, + const FieldDescriptor* field, + const std::vector<SpecificField>& parent_fields); // Returns true if this unknown field is to be ignored when this // MessageDifferencer compares messages. @@ -841,12 +821,10 @@ class PROTOBUF_EXPORT MessageDifferencer { // that the comparison succeeds when this method returns true (you need to // double-check in this case). bool MatchRepeatedFieldIndices( - const Message& message1, - const Message& message2, + const Message& message1, const Message& message2, const FieldDescriptor* repeated_field, const std::vector<SpecificField>& parent_fields, - std::vector<int>* match_list1, - std::vector<int>* match_list2); + std::vector<int>* match_list1, std::vector<int>* match_list2); // If "any" is of type google.protobuf.Any, extract its payload using // DynamicMessageFactory and store in "data". @@ -863,8 +841,8 @@ class PROTOBUF_EXPORT MessageDifferencer { // Defines a map between field descriptors and their MapKeyComparators. // Used for repeated fields when they are configured as TreatAsMap. - typedef std::map<const FieldDescriptor*, - const MapKeyComparator*> FieldKeyComparatorMap; + typedef std::map<const FieldDescriptor*, const MapKeyComparator*> + FieldKeyComparatorMap; // Defines a set to store field descriptors. Used for repeated fields when // they are configured as TreatAsSet. @@ -899,8 +877,8 @@ class PROTOBUF_EXPORT MessageDifferencer { std::string* output_string_; // Callback to post-process the matched indices to support SMART_LIST. - std::function<void(std::vector<int>*, - std::vector<int>*)> match_indices_for_smart_list_callback_; + std::function<void(std::vector<int>*, std::vector<int>*)> + match_indices_for_smart_list_callback_; std::unique_ptr<DynamicMessageFactory> dynamic_message_factory_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageDifferencer); diff --git a/src/google/protobuf/util/message_differencer_unittest.cc b/src/google/protobuf/util/message_differencer_unittest.cc index cacfc1f04d..702f99fbd6 100644 --- a/src/google/protobuf/util/message_differencer_unittest.cc +++ b/src/google/protobuf/util/message_differencer_unittest.cc @@ -41,18 +41,18 @@ #include <google/protobuf/stubs/strutil.h> -#include <google/protobuf/util/field_comparator.h> -#include <google/protobuf/util/message_differencer.h> -#include <google/protobuf/util/message_differencer_unittest.pb.h> -#include <google/protobuf/text_format.h> -#include <google/protobuf/wire_format.h> -#include <google/protobuf/io/zero_copy_stream_impl.h> -#include <google/protobuf/io/coded_stream.h> #include <google/protobuf/any_test.pb.h> -#include <google/protobuf/map_unittest.pb.h> -#include <google/protobuf/unittest.pb.h> #include <google/protobuf/map_test_util.h> +#include <google/protobuf/map_unittest.pb.h> #include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> +#include <google/protobuf/io/coded_stream.h> +#include <google/protobuf/io/zero_copy_stream_impl.h> +#include <google/protobuf/text_format.h> +#include <google/protobuf/wire_format.h> +#include <google/protobuf/util/message_differencer_unittest.pb.h> +#include <google/protobuf/util/field_comparator.h> +#include <google/protobuf/util/message_differencer.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> @@ -79,8 +79,7 @@ const FieldDescriptor* GetFieldDescriptor(const Message& message, } void ExpectEqualsWithDifferencer(util::MessageDifferencer* differencer, - const Message& msg1, - const Message& msg2) { + const Message& msg1, const Message& msg2) { differencer->set_scope(util::MessageDifferencer::FULL); EXPECT_TRUE(differencer->Compare(msg1, msg2)); @@ -298,7 +297,6 @@ TEST(MessageDifferencerTest, BasicInequivalencyTest) { EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2)); } - TEST(MessageDifferencerTest, BasicEquivalencyNonSetTest) { // Create the testing protos unittest::TestAllTypes msg1; @@ -308,7 +306,6 @@ TEST(MessageDifferencerTest, BasicEquivalencyNonSetTest) { EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2)); } - TEST(MessageDifferencerTest, BasicInequivalencyNonSetTest) { // Create the testing protos unittest::TestAllTypes msg1; @@ -320,7 +317,6 @@ TEST(MessageDifferencerTest, BasicInequivalencyNonSetTest) { EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2)); } - TEST(MessageDifferencerTest, BasicPartialEquivalencyTest) { // Create the testing protos unittest::TestAllTypes msg1; @@ -479,8 +475,7 @@ TEST(MessageDifferencerTest, ApproximateEquivalencyTest) { TestUtil::SetAllFields(&msg2); // Compare - EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); } TEST(MessageDifferencerTest, ApproximateModifiedEquivalencyTest) { @@ -501,8 +496,7 @@ TEST(MessageDifferencerTest, ApproximateModifiedEquivalencyTest) { ASSERT_NE(v1, v2) << "Should not be the same: " << v1 << ", " << v2; ASSERT_FLOAT_EQ(v1, v2) << "Should be approx. equal: " << v1 << ", " << v2; EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2)); - EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); // Modify the equivalency requirement too msg1.clear_optional_int32(); @@ -512,8 +506,7 @@ TEST(MessageDifferencerTest, ApproximateModifiedEquivalencyTest) { EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2)); EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2)); EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2)); - EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); } TEST(MessageDifferencerTest, ApproximateInequivalencyTest) { @@ -526,22 +519,19 @@ TEST(MessageDifferencerTest, ApproximateInequivalencyTest) { // Should fail on equivalency msg1.set_optional_int32(-1); - EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); // Make these fields the same again. msg1.set_optional_int32(0); msg2.set_optional_int32(0); - EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); // Should fail on approximate equality check const float v1 = 2.3f; const float v2 = 9.3f; msg1.set_optional_float(v1); msg2.set_optional_float(v2); - EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, - msg2)); + EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1, msg2)); } TEST(MessageDifferencerTest, WithinFractionOrMarginFloatTest) { @@ -715,7 +705,6 @@ TEST(MessageDifferencerTest, BasicFieldOrderingsTest) { EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2)); } - TEST(MessageDifferencerTest, BasicFieldOrderingInequalityTest) { // Create the testing protos unittest::TestFieldOrderings msg1; @@ -743,7 +732,6 @@ TEST(MessageDifferencerTest, BasicExtensionTest) { EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2)); } - TEST(MessageDifferencerTest, BasicExtensionInequalityTest) { // Create the testing protos unittest::TestAllExtensions msg1; @@ -845,12 +833,12 @@ TEST(MessageDifferencerTest, SpecifiedFieldsEmptyListAlwaysSucceeds) { std::vector<const FieldDescriptor*> empty_fields; util::MessageDifferencer differencer; - EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, - empty_fields, empty_fields)); + EXPECT_TRUE( + differencer.CompareWithFields(msg1, msg2, empty_fields, empty_fields)); TestUtil::SetAllFields(&msg2); - EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, - empty_fields, empty_fields)); + EXPECT_TRUE( + differencer.CompareWithFields(msg1, msg2, empty_fields, empty_fields)); } TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) { @@ -871,8 +859,8 @@ TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) { compare_fields.push_back(fields[i]); } } - EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, - compare_fields, compare_fields)); + EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, compare_fields, + compare_fields)); } { // Specify a different set of fields to compare, even though we're using the @@ -887,8 +875,8 @@ TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) { compare_fields2.push_back(fields[i]); } } - EXPECT_FALSE(differencer.CompareWithFields( - msg1, msg1, compare_fields1, compare_fields2)); + EXPECT_FALSE(differencer.CompareWithFields(msg1, msg1, compare_fields1, + compare_fields2)); } } @@ -934,8 +922,8 @@ TEST(MessageDifferencerTest, SpecifiedFieldsSubsetEqualityTest) { } util::MessageDifferencer differencer; - EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, - compare_fields, compare_fields)); + EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, compare_fields, + compare_fields)); } TEST(MessageDifferencerTest, @@ -964,8 +952,8 @@ TEST(MessageDifferencerTest, util::MessageDifferencer differencer; EXPECT_FALSE(differencer.Compare(msg1, msg2)); - EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, - compare_fields, compare_fields)); + EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, compare_fields, + compare_fields)); } TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) { @@ -989,8 +977,8 @@ TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) { } util::MessageDifferencer differencer; - EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, - compare_fields, compare_fields)); + EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, compare_fields, + compare_fields)); } TEST(MessageDifferencerTest, SpecifiedFieldsEquivalenceAllTest) { @@ -1080,18 +1068,20 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartListTest) { differencer.set_repeated_field_comparison( util::MessageDifferencer::AS_SMART_LIST); EXPECT_FALSE(differencer.Compare(msg1, msg2)); - EXPECT_EQ("deleted: rv[0]: 1\n" - "added: rv[0]: 9\n" - "added: rv[1]: 0\n" - "moved: rv[1] -> rv[2] : 2\n" - "added: rv[3]: 7\n" - "moved: rv[2] -> rv[4] : 3\n" - "deleted: rv[3]: 9\n" - "moved: rv[4] -> rv[5] : 4\n" - "moved: rv[5] -> rv[6] : 5\n" - "deleted: rv[6]: 7\n" - "added: rv[7]: 6\n" - "moved: rv[7] -> rv[8] : 2\n", diff_report); + EXPECT_EQ( + "deleted: rv[0]: 1\n" + "added: rv[0]: 9\n" + "added: rv[1]: 0\n" + "moved: rv[1] -> rv[2] : 2\n" + "added: rv[3]: 7\n" + "moved: rv[2] -> rv[4] : 3\n" + "deleted: rv[3]: 9\n" + "moved: rv[4] -> rv[5] : 4\n" + "moved: rv[5] -> rv[6] : 5\n" + "deleted: rv[6]: 7\n" + "added: rv[7]: 6\n" + "moved: rv[7] -> rv[8] : 2\n", + diff_report); // Compare two sub messages // a: 1, 2, 3, 4, 5 @@ -1110,12 +1100,14 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartListTest) { differencer.set_repeated_field_comparison( util::MessageDifferencer::AS_SMART_LIST); EXPECT_FALSE(differencer.Compare(msg1, msg2)); - EXPECT_EQ("deleted: rm[0]: { a: 1 }\n" - "moved: rm[1] -> rm[0] : { a: 2 }\n" - "deleted: rm[2]: { a: 3 }\n" - "added: rm[1]: { a: 6 }\n" - "moved: rm[3] -> rm[2] : { a: 4 }\n" - "deleted: rm[4]: { a: 5 }\n", diff_report); + EXPECT_EQ( + "deleted: rm[0]: { a: 1 }\n" + "moved: rm[1] -> rm[0] : { a: 2 }\n" + "deleted: rm[2]: { a: 3 }\n" + "added: rm[1]: { a: 6 }\n" + "moved: rm[3] -> rm[2] : { a: 4 }\n" + "deleted: rm[4]: { a: 5 }\n", + diff_report); } TEST(MessageDifferencerTest, RepeatedFieldSmartSetTest) { @@ -1126,15 +1118,24 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSetTest) { protobuf_unittest::TestField elem1_2, elem2_2, elem3_2; // Only one field is different for each pair of elememts - elem1_1.set_a(1); elem1_2.set_a(0); - elem1_1.set_b(1); elem1_2.set_b(1); - elem1_1.set_c(1); elem1_2.set_c(1); - elem2_1.set_a(2); elem2_2.set_a(2); - elem2_1.set_b(2); elem2_2.set_b(0); - elem2_1.set_c(2); elem2_2.set_c(2); - elem3_1.set_a(3); elem3_2.set_a(3); - elem3_1.set_b(3); elem3_2.set_b(0); - elem3_1.set_c(3); elem3_2.set_c(3); + elem1_1.set_a(1); + elem1_2.set_a(0); + elem1_1.set_b(1); + elem1_2.set_b(1); + elem1_1.set_c(1); + elem1_2.set_c(1); + elem2_1.set_a(2); + elem2_2.set_a(2); + elem2_1.set_b(2); + elem2_2.set_b(0); + elem2_1.set_c(2); + elem2_2.set_c(2); + elem3_1.set_a(3); + elem3_2.set_a(3); + elem3_1.set_b(3); + elem3_2.set_b(0); + elem3_1.set_c(3); + elem3_2.set_c(3); *msg1.add_rm() = elem1_1; *msg1.add_rm() = elem2_1; @@ -1150,9 +1151,11 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSetTest) { differencer.set_repeated_field_comparison( util::MessageDifferencer::AS_SMART_SET); EXPECT_FALSE(differencer.Compare(msg1, msg2)); - EXPECT_EQ("modified: rm[0].a -> rm[1].a: 1 -> 0\n" - "modified: rm[1].b -> rm[2].b: 2 -> 0\n" - "modified: rm[2].b -> rm[0].b: 3 -> 0\n", diff_report); + EXPECT_EQ( + "modified: rm[0].a -> rm[1].a: 1 -> 0\n" + "modified: rm[1].b -> rm[2].b: 2 -> 0\n" + "modified: rm[2].b -> rm[0].b: 3 -> 0\n", + diff_report); } TEST(MessageDifferencerTest, RepeatedFieldSmartSet_MultipleMatches) { @@ -1166,14 +1169,18 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSet_MultipleMatches) { elem1_1.set_a(1); elem1_1.set_b(1); elem1_1.set_c(1); - elem2_1.set_a(2); elem2_2.set_a(2); - elem2_1.set_b(2); elem2_2.set_b(0); - elem2_1.set_c(2); elem2_2.set_c(2); - elem3_1.set_a(3); elem3_2.set_a(3); - elem3_1.set_b(3); elem3_2.set_b(0); - elem3_1.set_c(3); elem3_2.set_c(3); - - + elem2_1.set_a(2); + elem2_2.set_a(2); + elem2_1.set_b(2); + elem2_2.set_b(0); + elem2_1.set_c(2); + elem2_2.set_c(2); + elem3_1.set_a(3); + elem3_2.set_a(3); + elem3_1.set_b(3); + elem3_2.set_b(0); + elem3_1.set_c(3); + elem3_2.set_c(3); // In this testcase, elem1_1 will match with elem2_2 first and then get // reverted because elem2_1 matches with elem2_2 later. @@ -1189,9 +1196,11 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSet_MultipleMatches) { differencer.set_repeated_field_comparison( util::MessageDifferencer::AS_SMART_SET); EXPECT_FALSE(differencer.Compare(msg1, msg2)); - EXPECT_EQ("modified: rm[1].b -> rm[0].b: 2 -> 0\n" - "modified: rm[2].b -> rm[1].b: 3 -> 0\n" - "deleted: rm[0]: { c: 1 a: 1 b: 1 }\n", diff_report); + EXPECT_EQ( + "modified: rm[1].b -> rm[0].b: 2 -> 0\n" + "modified: rm[2].b -> rm[1].b: 3 -> 0\n" + "deleted: rm[0]: { c: 1 a: 1 b: 1 }\n", + diff_report); } TEST(MessageDifferencerTest, RepeatedFieldSmartSet_MultipleMatchesNoReporter) { @@ -1222,9 +1231,12 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSet_NonMessageTypeTest) { protobuf_unittest::TestDiffMessage msg2; // Create 3 elements, but in different order. - msg1.add_rw("b"); msg2.add_rw("a"); - msg1.add_rw("x"); msg2.add_rw("x"); - msg1.add_rw("a"); msg2.add_rw("b"); + msg1.add_rw("b"); + msg2.add_rw("a"); + msg1.add_rw("x"); + msg2.add_rw("x"); + msg1.add_rw("a"); + msg2.add_rw("b"); std::string diff_report; util::MessageDifferencer differencer; @@ -1232,8 +1244,10 @@ TEST(MessageDifferencerTest, RepeatedFieldSmartSet_NonMessageTypeTest) { differencer.set_repeated_field_comparison( util::MessageDifferencer::AS_SMART_SET); EXPECT_TRUE(differencer.Compare(msg1, msg2)); - EXPECT_EQ("moved: rw[0] -> rw[2] : \"b\"\n" - "moved: rw[2] -> rw[0] : \"a\"\n", diff_report); + EXPECT_EQ( + "moved: rw[0] -> rw[2] : \"b\"\n" + "moved: rw[2] -> rw[0] : \"a\"\n", + diff_report); } TEST(MessageDifferencerTest, RepeatedFieldSetTest_SetOfSet) { @@ -1242,22 +1256,34 @@ TEST(MessageDifferencerTest, RepeatedFieldSetTest_SetOfSet) { protobuf_unittest::TestDiffMessage msg2; protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); - item->add_ra(1); item->add_ra(2); item->add_ra(3); + item->add_ra(1); + item->add_ra(2); + item->add_ra(3); item = msg1.add_item(); - item->add_ra(5); item->add_ra(6); + item->add_ra(5); + item->add_ra(6); item = msg1.add_item(); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg1.add_item(); - item->add_ra(6); item->add_ra(7); item->add_ra(8); + item->add_ra(6); + item->add_ra(7); + item->add_ra(8); item = msg2.add_item(); - item->add_ra(6); item->add_ra(5); + item->add_ra(6); + item->add_ra(5); item = msg2.add_item(); - item->add_ra(6); item->add_ra(8); item->add_ra(7); + item->add_ra(6); + item->add_ra(8); + item->add_ra(7); item = msg2.add_item(); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg2.add_item(); - item->add_ra(3); item->add_ra(2); item->add_ra(1); + item->add_ra(3); + item->add_ra(2); + item->add_ra(1); // Compare util::MessageDifferencer differencer; @@ -1275,29 +1301,41 @@ TEST(MessageDifferencerTest, RepeatedFieldSetTest_Combination) { // Treat "rw" as List protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); item->set_a(3); - item->add_ra(1); item->add_ra(2); item->add_ra(3); + item->add_ra(1); + item->add_ra(2); + item->add_ra(3); item = msg1.add_item(); item->set_a(4); - item->add_ra(5); item->add_ra(6); + item->add_ra(5); + item->add_ra(6); item = msg1.add_item(); item->set_a(1); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg1.add_item(); item->set_a(2); - item->add_ra(6); item->add_ra(7); item->add_ra(8); + item->add_ra(6); + item->add_ra(7); + item->add_ra(8); item = msg2.add_item(); item->set_a(4); - item->add_ra(6); item->add_ra(5); + item->add_ra(6); + item->add_ra(5); item = msg2.add_item(); item->set_a(2); - item->add_ra(6); item->add_ra(8); item->add_ra(7); + item->add_ra(6); + item->add_ra(8); + item->add_ra(7); item = msg2.add_item(); item->set_a(1); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg2.add_item(); item->set_a(3); - item->add_ra(3); item->add_ra(2); item->add_ra(1); + item->add_ra(3); + item->add_ra(2); + item->add_ra(1); msg1.add_rv(3); msg1.add_rv(4); @@ -1308,9 +1346,12 @@ TEST(MessageDifferencerTest, RepeatedFieldSetTest_Combination) { msg2.add_rv(0); msg2.add_rv(7); - msg1.add_rw("nothing"); msg2.add_rw("nothing"); - msg1.add_rw("should"); msg2.add_rw("should"); - msg1.add_rw("change"); msg2.add_rw("change"); + msg1.add_rw("nothing"); + msg2.add_rw("nothing"); + msg1.add_rw("should"); + msg2.add_rw("should"); + msg1.add_rw("change"); + msg2.add_rw("change"); // Compare util::MessageDifferencer differencer1; @@ -1711,9 +1752,8 @@ class ValueProductMapKeyComparator : public util::MessageDifferencer::MapKeyComparator { public: typedef util::MessageDifferencer::SpecificField SpecificField; - virtual bool IsMatch( - const Message &message1, const Message &message2, - const std::vector<SpecificField>& parent_fields) const { + virtual bool IsMatch(const Message& message1, const Message& message2, + const std::vector<SpecificField>& parent_fields) const { const Reflection* reflection1 = message1.GetReflection(); const Reflection* reflection2 = message2.GetReflection(); // FieldDescriptor for item.ra @@ -1746,8 +1786,8 @@ TEST(MessageDifferencerTest, RepeatedFieldMapTest_CustomMapKeyComparator) { item->set_b("hello"); util::MessageDifferencer differencer; ValueProductMapKeyComparator key_comparator; - differencer.TreatAsMapUsingKeyComparator( - GetFieldDescriptor(msg1, "item"), &key_comparator); + differencer.TreatAsMapUsingKeyComparator(GetFieldDescriptor(msg1, "item"), + &key_comparator); std::string output; differencer.ReportDifferencesToString(&output); // Though the above two messages have different values for item.ra, they @@ -1936,19 +1976,19 @@ TEST(MessageDifferencerTest, IgnoreField_Multiple) { const FieldDescriptor* c = GetFieldDescriptor(msg1, "c"); const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc"); - { // Ignore c + { // Ignore c util::MessageDifferencer differencer; differencer.IgnoreField(c); EXPECT_FALSE(differencer.Compare(msg1, msg2)); } - { // Ignore rc + { // Ignore rc util::MessageDifferencer differencer; differencer.IgnoreField(rc); EXPECT_FALSE(differencer.Compare(msg1, msg2)); } - { // Ignore both + { // Ignore both util::MessageDifferencer differencer; differencer.IgnoreField(c); differencer.IgnoreField(rc); @@ -2176,8 +2216,7 @@ class ParentSavingFieldComparator : public util::FieldComparator { const FieldDescriptor* field, int index_1, int index_2, const util::FieldContext* field_context) { - if (field_context) - parent_fields_ = *(field_context->parent_fields()); + if (field_context) parent_fields_ = *(field_context->parent_fields()); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { return RECURSE; } else { @@ -2229,10 +2268,10 @@ class ComparisonTest : public testing::Test { unknown2_ = empty2_.mutable_unknown_fields(); } - ~ComparisonTest() { } + ~ComparisonTest() {} void SetSpecialFieldOption(const Message& message, - util::MessageDifferencer* d) { + util::MessageDifferencer* d) { if (!ignored_field_.empty()) { d->IgnoreField(GetFieldDescriptor(message, ignored_field_)); } @@ -2281,19 +2320,15 @@ class ComparisonTest : public testing::Test { std::string RunUn() { return Run(empty1_, empty2_); } - void use_equivalency() { - use_equivalency_ = true; - } + void use_equivalency() { use_equivalency_ = true; } - void repeated_field_as_set() { - repeated_field_as_set_ = true; - } + void repeated_field_as_set() { repeated_field_as_set_ = true; } void field_as_set(const std::string& field) { set_field_ = field; } void field_as_map(const string& field, const std::string& key) { map_field_ = field; - map_key_ = key; + map_key_ = key; } void ignore_field(const std::string& field) { ignored_field_ = field; } @@ -2332,44 +2367,40 @@ class ComparisonTest : public testing::Test { TEST_F(ComparisonTest, AdditionTest) { proto1_.clear_optional_int32(); - EXPECT_EQ("added: optional_int32: 101\n", - Run()); + EXPECT_EQ("added: optional_int32: 101\n", Run()); } TEST_F(ComparisonTest, Addition_OrderTest) { orderings_proto1_.clear_my_int(); - EXPECT_EQ("added: my_int: 1\n", - RunOrder()); + EXPECT_EQ("added: my_int: 1\n", RunOrder()); } TEST_F(ComparisonTest, DeletionTest) { proto2_.clear_optional_int32(); - EXPECT_EQ("deleted: optional_int32: 101\n", - Run()); + EXPECT_EQ("deleted: optional_int32: 101\n", Run()); } TEST_F(ComparisonTest, Deletion_OrderTest) { orderings_proto2_.clear_my_string(); - EXPECT_EQ("deleted: my_string: \"foo\"\n", - RunOrder()); + EXPECT_EQ("deleted: my_string: \"foo\"\n", RunOrder()); } TEST_F(ComparisonTest, RepeatedDeletionTest) { proto2_.clear_repeated_int32(); - EXPECT_EQ("deleted: repeated_int32[0]: 201\n" - "deleted: repeated_int32[1]: 301\n", - Run()); + EXPECT_EQ( + "deleted: repeated_int32[0]: 201\n" + "deleted: repeated_int32[1]: 301\n", + Run()); } TEST_F(ComparisonTest, ModificationTest) { proto1_.set_optional_int32(-1); - EXPECT_EQ("modified: optional_int32: -1 -> 101\n", - Run()); + EXPECT_EQ("modified: optional_int32: -1 -> 101\n", Run()); } // Basic equivalency tests. @@ -2378,8 +2409,7 @@ TEST_F(ComparisonTest, EquivalencyAdditionTest) { proto1_.clear_optional_int32(); - EXPECT_EQ("modified: optional_int32: 0 -> 101\n", - Run()); + EXPECT_EQ("modified: optional_int32: 0 -> 101\n", Run()); } TEST_F(ComparisonTest, EquivalencyDeletionTest) { @@ -2387,45 +2417,39 @@ TEST_F(ComparisonTest, EquivalencyDeletionTest) { proto2_.clear_optional_int32(); - EXPECT_EQ("modified: optional_int32: 101 -> 0\n", - Run()); + EXPECT_EQ("modified: optional_int32: 101 -> 0\n", Run()); } // Group tests. TEST_F(ComparisonTest, GroupAdditionTest) { proto1_.mutable_optionalgroup()->clear_a(); - EXPECT_EQ("added: optionalgroup.a: 117\n", - Run()); + EXPECT_EQ("added: optionalgroup.a: 117\n", Run()); } TEST_F(ComparisonTest, GroupDeletionTest) { proto2_.mutable_optionalgroup()->clear_a(); - EXPECT_EQ("deleted: optionalgroup.a: 117\n", - Run()); + EXPECT_EQ("deleted: optionalgroup.a: 117\n", Run()); } TEST_F(ComparisonTest, GroupModificationTest) { proto1_.mutable_optionalgroup()->set_a(2); - EXPECT_EQ("modified: optionalgroup.a: 2 -> 117\n", - Run()); + EXPECT_EQ("modified: optionalgroup.a: 2 -> 117\n", Run()); } TEST_F(ComparisonTest, GroupFullAdditionTest) { proto1_.clear_optionalgroup(); // Note the difference in the output between this and GroupAdditionTest. - EXPECT_EQ("added: optionalgroup: { a: 117 }\n", - Run()); + EXPECT_EQ("added: optionalgroup: { a: 117 }\n", Run()); } TEST_F(ComparisonTest, GroupFullDeletionTest) { proto2_.clear_optionalgroup(); - EXPECT_EQ("deleted: optionalgroup: { a: 117 }\n", - Run()); + EXPECT_EQ("deleted: optionalgroup: { a: 117 }\n", Run()); } TEST_F(ComparisonTest, RepeatedSetOptionTest) { @@ -2440,11 +2464,12 @@ TEST_F(ComparisonTest, RepeatedSetOptionTest) { proto1_.add_repeatedgroup()->set_a(907); proto1_.add_repeatedgroup()->set_a(909); - EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n" - "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" - "deleted: repeatedgroup[0]: { a: 317 }\n" - "deleted: repeatedgroup[1]: { a: 904 }\n", - Run()); + EXPECT_EQ( + "moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n" + "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" + "deleted: repeatedgroup[0]: { a: 317 }\n" + "deleted: repeatedgroup[1]: { a: 904 }\n", + Run()); } TEST_F(ComparisonTest, RepeatedSetOptionTest_Ex) { @@ -2463,12 +2488,13 @@ TEST_F(ComparisonTest, RepeatedSetOptionTest_Ex) { proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension) ->set_bb(909); - EXPECT_EQ("moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->" - " (protobuf_unittest.repeated_nested_message_extension)[0] :" - " { bb: 909 }\n" - "deleted: (protobuf_unittest.repeated_nested_message_extension)[0]:" - " { bb: 904 }\n", - RunEx()); + EXPECT_EQ( + "moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->" + " (protobuf_unittest.repeated_nested_message_extension)[0] :" + " { bb: 909 }\n" + "deleted: (protobuf_unittest.repeated_nested_message_extension)[0]:" + " { bb: 904 }\n", + RunEx()); } TEST_F(ComparisonTest, RepeatedMapFieldTest_Group) { @@ -2485,11 +2511,12 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_Group) { proto2_.add_repeatedgroup()->set_a(318); // added proto2_.add_repeatedgroup()->set_a(907); // moved to - EXPECT_EQ("moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" - "added: repeatedgroup[1]: { a: 318 }\n" - "deleted: repeatedgroup[0]: { a: 317 }\n" - "deleted: repeatedgroup[1]: { a: 904 }\n", - Run()); + EXPECT_EQ( + "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" + "added: repeatedgroup[1]: { a: 318 }\n" + "deleted: repeatedgroup[0]: { a: 317 }\n" + "deleted: repeatedgroup[1]: { a: 904 }\n", + Run()); } TEST_F(ComparisonTest, RepeatedMapFieldTest_MessageKey) { @@ -2507,10 +2534,11 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_MessageKey) { item = msg1.add_item(); item->mutable_m()->set_c(2); item->set_b("second"); - item = msg1.add_item(); item->set_b("null"); // empty key moved + item = msg1.add_item(); + item->set_b("null"); // empty key moved item = msg1.add_item(); item->mutable_m()->set_c(3); - item->set_b("third"); // deletion + item->set_b("third"); // deletion item = msg1.add_item(); item->mutable_m()->set_c(2); item->set_b("second"); // duplicated key ( deletion ) @@ -2522,8 +2550,9 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_MessageKey) { item->set_b("fourth"); // addition item = msg2.add_item(); item->mutable_m()->set_c(0); - item->set_b("fist"); // move with change - item = msg2.add_item(); item->set_b("null"); + item->set_b("fist"); // move with change + item = msg2.add_item(); + item->set_b("null"); EXPECT_EQ( "modified: item[0].b -> item[2].b: \"first\" -> \"fist\"\n" @@ -2542,33 +2571,45 @@ TEST_F(ComparisonTest, RepeatedFieldSetTest_SetOfSet) { protobuf_unittest::TestDiffMessage msg2; protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); - item->add_ra(1); item->add_ra(2); item->add_ra(3); + item->add_ra(1); + item->add_ra(2); + item->add_ra(3); item = msg1.add_item(); - item->add_ra(5); item->add_ra(6); + item->add_ra(5); + item->add_ra(6); item = msg1.add_item(); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg1.add_item(); - item->add_ra(6); item->add_ra(7); item->add_ra(8); + item->add_ra(6); + item->add_ra(7); + item->add_ra(8); item = msg2.add_item(); - item->add_ra(6); item->add_ra(5); + item->add_ra(6); + item->add_ra(5); item = msg2.add_item(); - item->add_ra(6); item->add_ra(8); + item->add_ra(6); + item->add_ra(8); item = msg2.add_item(); - item->add_ra(1); item->add_ra(3); + item->add_ra(1); + item->add_ra(3); item = msg2.add_item(); - item->add_ra(3); item->add_ra(2); item->add_ra(1); + item->add_ra(3); + item->add_ra(2); + item->add_ra(1); // Compare - EXPECT_EQ("moved: item[0].ra[0] -> item[3].ra[2] : 1\n" - "moved: item[0].ra[2] -> item[3].ra[0] : 3\n" - "moved: item[0] -> item[3] : { ra: 1 ra: 2 ra: 3 }\n" - "moved: item[1].ra[0] -> item[0].ra[1] : 5\n" - "moved: item[1].ra[1] -> item[0].ra[0] : 6\n" - "moved: item[1] -> item[0] : { ra: 5 ra: 6 }\n" - "added: item[1]: { ra: 6 ra: 8 }\n" - "deleted: item[3]: { ra: 6 ra: 7 ra: 8 }\n", - Run(msg1, msg2)); + EXPECT_EQ( + "moved: item[0].ra[0] -> item[3].ra[2] : 1\n" + "moved: item[0].ra[2] -> item[3].ra[0] : 3\n" + "moved: item[0] -> item[3] : { ra: 1 ra: 2 ra: 3 }\n" + "moved: item[1].ra[0] -> item[0].ra[1] : 5\n" + "moved: item[1].ra[1] -> item[0].ra[0] : 6\n" + "moved: item[1] -> item[0] : { ra: 5 ra: 6 }\n" + "added: item[1]: { ra: 6 ra: 8 }\n" + "deleted: item[3]: { ra: 6 ra: 7 ra: 8 }\n", + Run(msg1, msg2)); } TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedKey) { @@ -2592,12 +2633,12 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedKey) { item->add_rb("a"); item->set_b("fist"); - - EXPECT_EQ("modified: item[0].b -> item[1].b: \"first\" -> \"fist\"\n" - "moved: item[0].rb[0] -> item[1].rb[1] : \"a\"\n" - "moved: item[0].rb[1] -> item[1].rb[0] : \"b\"\n" - "added: item[0]: { b: \"second\" rb: \"c\" }\n", - Run(msg1, msg2)); + EXPECT_EQ( + "modified: item[0].b -> item[1].b: \"first\" -> \"fist\"\n" + "moved: item[0].rb[0] -> item[1].rb[1] : \"a\"\n" + "moved: item[0].rb[1] -> item[1].rb[0] : \"b\"\n" + "added: item[0]: { b: \"second\" rb: \"c\" }\n", + Run(msg1, msg2)); } TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedMessageKey) { @@ -2607,9 +2648,14 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedMessageKey) { protobuf_unittest::TestDiffMessage msg2; protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); protobuf_unittest::TestField* key = item->add_rm(); - key->set_c(2); key->add_rc(10); key->add_rc(10); - item = msg1.add_item(); key = item->add_rm(); - key->set_c(0); key->add_rc(1); key->add_rc(2); + key->set_c(2); + key->add_rc(10); + key->add_rc(10); + item = msg1.add_item(); + key = item->add_rm(); + key->set_c(0); + key->add_rc(1); + key->add_rc(2); key = item->add_rm(); key->set_c(0); item->add_rb("first"); @@ -2618,9 +2664,10 @@ TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedMessageKey) { item->CopyFrom(msg1.item(1)); item->add_rb("second"); - EXPECT_EQ("added: item[0].rb[1]: \"second\"\n" - "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n", - Run(msg1, msg2)); + EXPECT_EQ( + "added: item[0].rb[1]: \"second\"\n" + "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n", + Run(msg1, msg2)); } TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) { @@ -2632,9 +2679,10 @@ TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) { unknown2_->AddGroup(245)->AddFixed32(248, 1); // We expect it behaves the same as normal comparison. - EXPECT_EQ("modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n" - "added: 245[1]: { ... }\n", - RunUn()); + EXPECT_EQ( + "modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n" + "added: 245[1]: { ... }\n", + RunUn()); } TEST_F(ComparisonTest, Matching_Unknown) { @@ -2646,9 +2694,10 @@ TEST_F(ComparisonTest, Matching_Unknown) { unknown2_->AddGroup(246)->AddFixed32(248, 4); // report_match is false so only added/modified fields are expected. - EXPECT_EQ("added: 242[0]: \"cat\"\n" - "added: 246[0]: { ... }\n", - RunUn()); + EXPECT_EQ( + "added: 242[0]: \"cat\"\n" + "added: 246[0]: { ... }\n", + RunUn()); } TEST_F(ComparisonTest, RepeatedSetFieldTest) { @@ -2663,55 +2712,50 @@ TEST_F(ComparisonTest, RepeatedSetFieldTest) { proto1_.add_repeatedgroup()->set_a(907); proto1_.add_repeatedgroup()->set_a(909); - EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n" - "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" - "deleted: repeatedgroup[0]: { a: 317 }\n" - "deleted: repeatedgroup[1]: { a: 904 }\n", - Run()); + EXPECT_EQ( + "moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n" + "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n" + "deleted: repeatedgroup[0]: { a: 317 }\n" + "deleted: repeatedgroup[1]: { a: 904 }\n", + Run()); } // Embedded message tests. TEST_F(ComparisonTest, EmbeddedAdditionTest) { proto1_.mutable_optional_nested_message()->clear_bb(); - EXPECT_EQ("added: optional_nested_message.bb: 118\n", - Run()); + EXPECT_EQ("added: optional_nested_message.bb: 118\n", Run()); } TEST_F(ComparisonTest, EmbeddedDeletionTest) { proto2_.mutable_optional_nested_message()->clear_bb(); - EXPECT_EQ("deleted: optional_nested_message.bb: 118\n", - Run()); + EXPECT_EQ("deleted: optional_nested_message.bb: 118\n", Run()); } TEST_F(ComparisonTest, EmbeddedModificationTest) { proto1_.mutable_optional_nested_message()->set_bb(2); - EXPECT_EQ("modified: optional_nested_message.bb: 2 -> 118\n", - Run()); + EXPECT_EQ("modified: optional_nested_message.bb: 2 -> 118\n", Run()); } TEST_F(ComparisonTest, EmbeddedFullAdditionTest) { proto1_.clear_optional_nested_message(); - EXPECT_EQ("added: optional_nested_message: { bb: 118 }\n", - Run()); + EXPECT_EQ("added: optional_nested_message: { bb: 118 }\n", Run()); } TEST_F(ComparisonTest, EmbeddedPartialAdditionTest) { proto1_.clear_optional_nested_message(); proto2_.mutable_optional_nested_message()->clear_bb(); - EXPECT_EQ("added: optional_nested_message: { }\n", - Run()); + EXPECT_EQ("added: optional_nested_message: { }\n", Run()); } TEST_F(ComparisonTest, EmbeddedFullDeletionTest) { proto2_.clear_optional_nested_message(); - EXPECT_EQ("deleted: optional_nested_message: { bb: 118 }\n", - Run()); + EXPECT_EQ("deleted: optional_nested_message: { bb: 118 }\n", Run()); } // Repeated element tests. @@ -2730,10 +2774,11 @@ TEST_F(ComparisonTest, BasicRepeatedTest) { proto2_.add_repeated_int32(502); proto2_.add_repeated_int32(504); - EXPECT_EQ("modified: repeated_int32[1]: 501 -> 509\n" - "modified: repeated_int32[3]: 503 -> 504\n" - "deleted: repeated_int32[4]: 500\n", - Run()); + EXPECT_EQ( + "modified: repeated_int32[1]: 501 -> 509\n" + "modified: repeated_int32[3]: 503 -> 504\n" + "deleted: repeated_int32[4]: 500\n", + Run()); } TEST_F(ComparisonTest, BasicRepeatedTest_SetOption) { @@ -2753,13 +2798,14 @@ TEST_F(ComparisonTest, BasicRepeatedTest_SetOption) { proto2_.add_repeated_int32(502); proto2_.add_repeated_int32(504); - EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n" - "moved: repeated_int32[3] -> repeated_int32[0] : 500\n" - "added: repeated_int32[1]: 509\n" - "added: repeated_int32[4]: 504\n" - "deleted: repeated_int32[0]: 501\n" - "deleted: repeated_int32[4]: 500\n", - Run()); + EXPECT_EQ( + "moved: repeated_int32[1] -> repeated_int32[3] : 502\n" + "moved: repeated_int32[3] -> repeated_int32[0] : 500\n" + "added: repeated_int32[1]: 509\n" + "added: repeated_int32[4]: 504\n" + "deleted: repeated_int32[0]: 501\n" + "deleted: repeated_int32[4]: 500\n", + Run()); } TEST_F(ComparisonTest, BasicRepeatedTest_SetField) { @@ -2779,13 +2825,14 @@ TEST_F(ComparisonTest, BasicRepeatedTest_SetField) { proto2_.add_repeated_int32(502); proto2_.add_repeated_int32(504); - EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n" - "moved: repeated_int32[3] -> repeated_int32[0] : 500\n" - "added: repeated_int32[1]: 509\n" - "added: repeated_int32[4]: 504\n" - "deleted: repeated_int32[0]: 501\n" - "deleted: repeated_int32[4]: 500\n", - Run()); + EXPECT_EQ( + "moved: repeated_int32[1] -> repeated_int32[3] : 502\n" + "moved: repeated_int32[3] -> repeated_int32[0] : 500\n" + "added: repeated_int32[1]: 509\n" + "added: repeated_int32[4]: 504\n" + "deleted: repeated_int32[0]: 501\n" + "deleted: repeated_int32[4]: 500\n", + Run()); } // Multiple action tests. @@ -2793,18 +2840,20 @@ TEST_F(ComparisonTest, AddDeleteTest) { proto1_.clear_optional_int32(); proto2_.clear_optional_int64(); - EXPECT_EQ("added: optional_int32: 101\n" - "deleted: optional_int64: 102\n", - Run()); + EXPECT_EQ( + "added: optional_int32: 101\n" + "deleted: optional_int64: 102\n", + Run()); } TEST_F(ComparisonTest, AddDelete_FieldOrderingTest) { orderings_proto1_.ClearExtension(unittest::my_extension_string); orderings_proto2_.clear_my_int(); - EXPECT_EQ("deleted: my_int: 1\n" - "added: (protobuf_unittest.my_extension_string): \"bar\"\n", - RunOrder()); + EXPECT_EQ( + "deleted: my_int: 1\n" + "added: (protobuf_unittest.my_extension_string): \"bar\"\n", + RunOrder()); } TEST_F(ComparisonTest, AllThreeTest) { @@ -2812,10 +2861,11 @@ TEST_F(ComparisonTest, AllThreeTest) { proto2_.clear_optional_float(); proto2_.set_optional_string("hello world!"); - EXPECT_EQ("added: optional_int32: 101\n" - "deleted: optional_float: 111\n" - "modified: optional_string: \"115\" -> \"hello world!\"\n", - Run()); + EXPECT_EQ( + "added: optional_int32: 101\n" + "deleted: optional_float: 111\n" + "modified: optional_string: \"115\" -> \"hello world!\"\n", + Run()); } TEST_F(ComparisonTest, SandwhichTest) { @@ -2824,10 +2874,11 @@ TEST_F(ComparisonTest, SandwhichTest) { proto2_.clear_optional_uint64(); - EXPECT_EQ("added: optional_int64: 102\n" - "added: optional_uint32: 103\n" - "deleted: optional_uint64: 104\n", - Run()); + EXPECT_EQ( + "added: optional_int64: 102\n" + "added: optional_uint32: 103\n" + "deleted: optional_uint64: 104\n", + Run()); } TEST_F(ComparisonTest, IgnoredNoChangeTest) { @@ -2837,9 +2888,10 @@ TEST_F(ComparisonTest, IgnoredNoChangeTest) { ignore_field("v"); - EXPECT_EQ("ignored: v\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: v\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredAddTest) { @@ -2848,9 +2900,10 @@ TEST_F(ComparisonTest, IgnoredAddTest) { ignore_field("v"); - EXPECT_EQ("ignored: v\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: v\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredDeleteTest) { @@ -2859,9 +2912,10 @@ TEST_F(ComparisonTest, IgnoredDeleteTest) { ignore_field("v"); - EXPECT_EQ("ignored: v\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: v\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredModifyTest) { @@ -2871,9 +2925,10 @@ TEST_F(ComparisonTest, IgnoredModifyTest) { ignore_field("v"); - EXPECT_EQ("ignored: v\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: v\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredRepeatedAddTest) { @@ -2888,9 +2943,10 @@ TEST_F(ComparisonTest, IgnoredRepeatedAddTest) { ignore_field("rv"); - EXPECT_EQ("ignored: rv\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: rv\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredRepeatedDeleteTest) { @@ -2905,9 +2961,10 @@ TEST_F(ComparisonTest, IgnoredRepeatedDeleteTest) { ignore_field("rv"); - EXPECT_EQ("ignored: rv\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: rv\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredRepeatedModifyTest) { @@ -2921,9 +2978,10 @@ TEST_F(ComparisonTest, IgnoredRepeatedModifyTest) { ignore_field("rv"); - EXPECT_EQ("ignored: rv\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: rv\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredWholeNestedMessage) { @@ -2934,9 +2992,10 @@ TEST_F(ComparisonTest, IgnoredWholeNestedMessage) { ignore_field("m"); - EXPECT_EQ("added: w: \"foo\"\n" - "ignored: m\n", - RunDiff()); + EXPECT_EQ( + "added: w: \"foo\"\n" + "ignored: m\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredNestedField) { @@ -2947,9 +3006,10 @@ TEST_F(ComparisonTest, IgnoredNestedField) { ignore_field("m.c"); - EXPECT_EQ("added: w: \"foo\"\n" - "ignored: m.c\n", - RunDiff()); + EXPECT_EQ( + "added: w: \"foo\"\n" + "ignored: m.c\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredRepeatedNested) { @@ -2962,10 +3022,11 @@ TEST_F(ComparisonTest, IgnoredRepeatedNested) { ignore_field("rm.c"); - EXPECT_EQ("ignored: rm[0].c\n" - "ignored: rm[1].c\n" - "added: w: \"foo\"\n", - RunDiff()); + EXPECT_EQ( + "ignored: rm[0].c\n" + "ignored: rm[1].c\n" + "added: w: \"foo\"\n", + RunDiff()); } TEST_F(ComparisonTest, IgnoredNestedRepeated) { @@ -2977,9 +3038,10 @@ TEST_F(ComparisonTest, IgnoredNestedRepeated) { ignore_field("m.rc"); - EXPECT_EQ("added: w: \"foo\"\n" - "ignored: m.rc\n", - RunDiff()); + EXPECT_EQ( + "added: w: \"foo\"\n" + "ignored: m.rc\n", + RunDiff()); } TEST_F(ComparisonTest, ExtensionTest) { @@ -3111,8 +3173,8 @@ TEST_F(ComparisonTest, MapRoundTripSyncTest) { unittest::TestMap map_reflection1; // By setting via reflection, data exists in repeated field. - ASSERT_TRUE(parser.ParseFromString( - "map_int32_foreign_message { key: 1 }", &map_reflection1)); + ASSERT_TRUE(parser.ParseFromString("map_int32_foreign_message { key: 1 }", + &map_reflection1)); // During copy, data is synced from repeated field to map. unittest::TestMap map_reflection2 = map_reflection1; @@ -3224,11 +3286,9 @@ class MatchingTest : public testing::Test { typedef util::MessageDifferencer MessageDifferencer; protected: - MatchingTest() { - } + MatchingTest() {} - ~MatchingTest() { - } + ~MatchingTest() {} std::string RunWithResult(MessageDifferencer* differencer, const Message& msg1, const Message& msg2, @@ -3385,7 +3445,7 @@ TEST_F(MatchingTest, MatchesAppearInPostTraversalOrderForMovedFields) { "moved: item[0].rm[1].rc[2] -> item[0].rm[0].rc[0] : 7\n" "moved: item[0].rm[1] -> item[0].rm[0] : { c: 4 rc: 5 rc: 6 rc: 7 }\n" "matched: item[0] : { rm { c: 1 rc: 2 rc: 3 }" - " rm { c: 4 rc: 5 rc: 6 rc: 7 } }\n" + " rm { c: 4 rc: 5 rc: 6 rc: 7 } }\n" "matched: item[1].rm[0].c : 8\n" "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 10\n" "moved: item[1].rm[0].rc[1] -> item[1].rm[0].rc[2] : 11\n" @@ -3464,13 +3524,13 @@ TEST_F(MatchingTest, MatchAndModifiedInterleaveProperly) { "added: item[0].rm[0].rc[1]: 8\n" "deleted: item[0].rm[1].rc[1]: 7\n" "modified: item[0].rm[1] -> item[0].rm[0]: { c: 5 rc: 6 rc: 7 rc: 8 } ->" - " { c: 5 rc: 8 rc: 8 rc: 6 }\n" + " { c: 5 rc: 8 rc: 8 rc: 6 }\n" "added: item[0].rm[1]: { c: 3 rc: 2 rc: 4 }\n" "deleted: item[0].rm[0]: { c: 2 rc: 3 rc: 4 }\n" "modified: item[0]: { a: 1 rm { c: 2 rc: 3 rc: 4 }" - " rm { c: 5 rc: 6 rc: 7 rc: 8 } } ->" - " { a: 1 rm { c: 5 rc: 8 rc: 8 rc: 6 }" - " rm { c: 3 rc: 2 rc: 4 } }\n" + " rm { c: 5 rc: 6 rc: 7 rc: 8 } } ->" + " { a: 1 rm { c: 5 rc: 8 rc: 8 rc: 6 }" + " rm { c: 3 rc: 2 rc: 4 } }\n" "matched: item[1].a : 9\n" "matched: item[1].rm[0].c : 10\n" "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 11\n" @@ -3513,20 +3573,20 @@ TEST_F(MatchingTest, MatchingWorksWithExtensions) { EXPECT_EQ( "matched: (protobuf_unittest.repeated_nested_message_extension)[0].bb ->" - " (protobuf_unittest.repeated_nested_message_extension)[2].bb : 7\n" + " (protobuf_unittest.repeated_nested_message_extension)[2].bb : 7\n" "moved: (protobuf_unittest.repeated_nested_message_extension)[0] ->" - " (protobuf_unittest.repeated_nested_message_extension)[2] :" - " { bb: 7 }\n" + " (protobuf_unittest.repeated_nested_message_extension)[2] :" + " { bb: 7 }\n" "matched: (protobuf_unittest.repeated_nested_message_extension)[1].bb :" - " 13\n" + " 13\n" "matched: (protobuf_unittest.repeated_nested_message_extension)[1] :" - " { bb: 13 }\n" + " { bb: 13 }\n" "matched: (protobuf_unittest.repeated_nested_message_extension)[2].bb ->" - " (protobuf_unittest.repeated_nested_message_extension)[0].bb :" - " 11\n" + " (protobuf_unittest.repeated_nested_message_extension)[0].bb :" + " 11\n" "moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->" - " (protobuf_unittest.repeated_nested_message_extension)[0] :" - " { bb: 11 }\n", + " (protobuf_unittest.repeated_nested_message_extension)[0] :" + " { bb: 11 }\n", RunWithResult(&differencer, msg1, msg2, true)); } diff --git a/src/google/protobuf/util/time_util.cc b/src/google/protobuf/util/time_util.cc index 8f9d939000..68a3e675bb 100644 --- a/src/google/protobuf/util/time_util.cc +++ b/src/google/protobuf/util/time_util.cc @@ -45,8 +45,8 @@ namespace google { namespace protobuf { namespace util { -using google::protobuf::Timestamp; using google::protobuf::Duration; +using google::protobuf::Timestamp; namespace { static const int kNanosPerSecond = 1000000000; diff --git a/src/google/protobuf/util/time_util.h b/src/google/protobuf/util/time_util.h index bbda5380e4..756e23b396 100644 --- a/src/google/protobuf/util/time_util.h +++ b/src/google/protobuf/util/time_util.h @@ -224,15 +224,15 @@ inline Duration operator-(const Duration& d1, const Duration& d2) { return result -= d2; } // Multiplicative operators -template<typename T> +template <typename T> inline Duration operator*(Duration d, T r) { return d *= r; } -template<typename T> +template <typename T> inline Duration operator*(T r, Duration d) { return d *= r; } -template<typename T> +template <typename T> inline Duration operator/(Duration d, T r) { return d /= r; } diff --git a/src/google/protobuf/util/time_util_test.cc b/src/google/protobuf/util/time_util_test.cc index 285740abe1..9d6c564fce 100644 --- a/src/google/protobuf/util/time_util_test.cc +++ b/src/google/protobuf/util/time_util_test.cc @@ -32,8 +32,8 @@ #include <ctime> -#include <google/protobuf/timestamp.pb.h> #include <google/protobuf/duration.pb.h> +#include <google/protobuf/timestamp.pb.h> #include <google/protobuf/testing/googletest.h> #include <gtest/gtest.h> @@ -41,8 +41,8 @@ namespace google { namespace protobuf { namespace util { -using google::protobuf::Timestamp; using google::protobuf::Duration; +using google::protobuf::Timestamp; namespace { diff --git a/src/google/protobuf/util/type_resolver_util_test.cc b/src/google/protobuf/util/type_resolver_util_test.cc index 42fb7f6918..c453955d39 100644 --- a/src/google/protobuf/util/type_resolver_util_test.cc +++ b/src/google/protobuf/util/type_resolver_util_test.cc @@ -88,8 +88,8 @@ class DescriptorPoolTypeResolverTest : public testing::Test { if (field == NULL) { return false; } - return field->cardinality() == cardinality && - field->kind() == kind && field->number() == number; + return field->cardinality() == cardinality && field->kind() == kind && + field->number() == number; } bool CheckFieldTypeUrl(const Type& type, const std::string& name, @@ -177,156 +177,158 @@ class DescriptorPoolTypeResolverTest : public testing::Test { TEST_F(DescriptorPoolTypeResolverTest, TestAllTypes) { Type type; - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type).ok()); + ASSERT_TRUE(resolver_ + ->ResolveMessageType( + GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type) + .ok()); // Check all optional fields. - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_INT32, "optional_int32", 1)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_INT64, "optional_int64", 2)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_UINT32, "optional_uint32", 3)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_UINT64, "optional_uint64", 4)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_SINT32, "optional_sint32", 5)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_SINT64, "optional_sint64", 6)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_FIXED32, "optional_fixed32", 7)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_FIXED64, "optional_fixed64", 8)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_SFIXED32, "optional_sfixed32", 9)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_SFIXED64, "optional_sfixed64", 10)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_FLOAT, "optional_float", 11)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_DOUBLE, "optional_double", 12)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_BOOL, "optional_bool", 13)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_STRING, "optional_string", 14)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_BYTES, "optional_bytes", 15)); - - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_GROUP, "optionalgroup", 16)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_INT32, + "optional_int32", 1)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_INT64, + "optional_int64", 2)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_UINT32, + "optional_uint32", 3)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_UINT64, + "optional_uint64", 4)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_SINT32, + "optional_sint32", 5)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_SINT64, + "optional_sint64", 6)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_FIXED32, + "optional_fixed32", 7)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_FIXED64, + "optional_fixed64", 8)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_SFIXED32, + "optional_sfixed32", 9)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_SFIXED64, + "optional_sfixed64", 10)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_FLOAT, + "optional_float", 11)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_DOUBLE, + "optional_double", 12)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_BOOL, + "optional_bool", 13)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_STRING, + "optional_string", 14)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_BYTES, + "optional_bytes", 15)); + + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_GROUP, + "optionalgroup", 16)); EXPECT_TRUE(CheckFieldTypeUrl( type, "optionalgroup", GetTypeUrl<protobuf_unittest::TestAllTypes::OptionalGroup>())); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_MESSAGE, "optional_nested_message", 18)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_MESSAGE, "optional_foreign_message", 19)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_MESSAGE, + "optional_nested_message", 18)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_MESSAGE, + "optional_foreign_message", 19)); EXPECT_TRUE(CheckFieldTypeUrl( type, "optional_nested_message", GetTypeUrl<protobuf_unittest::TestAllTypes::NestedMessage>())); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "optional_foreign_message", - GetTypeUrl<protobuf_unittest::ForeignMessage>())); + EXPECT_TRUE(CheckFieldTypeUrl(type, "optional_foreign_message", + GetTypeUrl<protobuf_unittest::ForeignMessage>())); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_ENUM, "optional_nested_enum", 21)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_ENUM, "optional_foreign_enum", 22)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_ENUM, + "optional_nested_enum", 21)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_ENUM, + "optional_foreign_enum", 22)); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "optional_nested_enum", - GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"))); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "optional_foreign_enum", - GetTypeUrl("protobuf_unittest.ForeignEnum"))); + EXPECT_TRUE( + CheckFieldTypeUrl(type, "optional_nested_enum", + GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"))); + EXPECT_TRUE(CheckFieldTypeUrl(type, "optional_foreign_enum", + GetTypeUrl("protobuf_unittest.ForeignEnum"))); // Check all repeated fields. - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_INT32, "repeated_int32", 31)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_INT64, "repeated_int64", 32)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_UINT32, "repeated_uint32", 33)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_UINT64, "repeated_uint64", 34)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_SINT32, "repeated_sint32", 35)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_SINT64, "repeated_sint64", 36)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_FIXED32, "repeated_fixed32", 37)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_FIXED64, "repeated_fixed64", 38)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_SFIXED32, "repeated_sfixed32", 39)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_SFIXED64, "repeated_sfixed64", 40)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_FLOAT, "repeated_float", 41)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_DOUBLE, "repeated_double", 42)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_BOOL, "repeated_bool", 43)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_STRING, "repeated_string", 44)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_BYTES, "repeated_bytes", 45)); - - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_GROUP, "repeatedgroup", 46)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_INT32, + "repeated_int32", 31)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_INT64, + "repeated_int64", 32)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_UINT32, + "repeated_uint32", 33)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_UINT64, + "repeated_uint64", 34)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_SINT32, + "repeated_sint32", 35)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_SINT64, + "repeated_sint64", 36)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_FIXED32, + "repeated_fixed32", 37)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_FIXED64, + "repeated_fixed64", 38)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_SFIXED32, + "repeated_sfixed32", 39)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_SFIXED64, + "repeated_sfixed64", 40)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_FLOAT, + "repeated_float", 41)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_DOUBLE, + "repeated_double", 42)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_BOOL, + "repeated_bool", 43)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_STRING, + "repeated_string", 44)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_BYTES, + "repeated_bytes", 45)); + + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_GROUP, + "repeatedgroup", 46)); EXPECT_TRUE(CheckFieldTypeUrl( type, "repeatedgroup", GetTypeUrl<protobuf_unittest::TestAllTypes::RepeatedGroup>())); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_MESSAGE, "repeated_nested_message", 48)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_MESSAGE, "repeated_foreign_message", 49)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_MESSAGE, + "repeated_nested_message", 48)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_MESSAGE, + "repeated_foreign_message", 49)); EXPECT_TRUE(CheckFieldTypeUrl( type, "repeated_nested_message", GetTypeUrl<protobuf_unittest::TestAllTypes::NestedMessage>())); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "repeated_foreign_message", - GetTypeUrl<protobuf_unittest::ForeignMessage>())); + EXPECT_TRUE(CheckFieldTypeUrl(type, "repeated_foreign_message", + GetTypeUrl<protobuf_unittest::ForeignMessage>())); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_ENUM, "repeated_nested_enum", 51)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_ENUM, "repeated_foreign_enum", 52)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_ENUM, + "repeated_nested_enum", 51)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_ENUM, + "repeated_foreign_enum", 52)); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "repeated_nested_enum", - GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"))); - EXPECT_TRUE(CheckFieldTypeUrl( - type, "repeated_foreign_enum", - GetTypeUrl("protobuf_unittest.ForeignEnum"))); + EXPECT_TRUE( + CheckFieldTypeUrl(type, "repeated_nested_enum", + GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"))); + EXPECT_TRUE(CheckFieldTypeUrl(type, "repeated_foreign_enum", + GetTypeUrl("protobuf_unittest.ForeignEnum"))); } TEST_F(DescriptorPoolTypeResolverTest, TestPackedField) { Type type; - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<protobuf_unittest::TestPackedTypes>(), &type).ok()); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_INT32, "packed_int32", 90)); + ASSERT_TRUE(resolver_ + ->ResolveMessageType( + GetTypeUrl<protobuf_unittest::TestPackedTypes>(), &type) + .ok()); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_INT32, + "packed_int32", 90)); EXPECT_TRUE(IsPacked(type, "packed_int32")); } TEST_F(DescriptorPoolTypeResolverTest, TestOneof) { Type type; - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type).ok()); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_UINT32, "oneof_uint32", 111)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_MESSAGE, "oneof_nested_message", 112)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_STRING, "oneof_string", 113)); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, - Field::TYPE_BYTES, "oneof_bytes", 114)); + ASSERT_TRUE(resolver_ + ->ResolveMessageType( + GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type) + .ok()); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_UINT32, + "oneof_uint32", 111)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_MESSAGE, + "oneof_nested_message", 112)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_STRING, + "oneof_string", 113)); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_OPTIONAL, Field::TYPE_BYTES, + "oneof_bytes", 114)); EXPECT_TRUE(FieldInOneof(type, "oneof_uint32", "oneof_field")); EXPECT_TRUE(FieldInOneof(type, "oneof_nested_message", "oneof_field")); EXPECT_TRUE(FieldInOneof(type, "oneof_string", "oneof_field")); @@ -335,17 +337,21 @@ TEST_F(DescriptorPoolTypeResolverTest, TestOneof) { TEST_F(DescriptorPoolTypeResolverTest, TestMap) { Type type; - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<protobuf_unittest::TestMap>(), &type).ok()); - EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, - Field::TYPE_MESSAGE, "map_int32_int32", 1)); + ASSERT_TRUE( + resolver_ + ->ResolveMessageType(GetTypeUrl<protobuf_unittest::TestMap>(), &type) + .ok()); + EXPECT_TRUE(HasField(type, Field::CARDINALITY_REPEATED, Field::TYPE_MESSAGE, + "map_int32_int32", 1)); EXPECT_TRUE(CheckFieldTypeUrl( type, "map_int32_int32", GetTypeUrl("protobuf_unittest.TestMap.MapInt32Int32Entry"))); - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl("protobuf_unittest.TestMap.MapInt32Int32Entry"), - &type).ok()); + ASSERT_TRUE( + resolver_ + ->ResolveMessageType( + GetTypeUrl("protobuf_unittest.TestMap.MapInt32Int32Entry"), &type) + .ok()); EXPECT_TRUE(HasBoolOption(type.options(), "map_entry", true)); } @@ -377,8 +383,11 @@ TEST_F(DescriptorPoolTypeResolverTest, TestCustomFieldOptions) { TEST_F(DescriptorPoolTypeResolverTest, TestEnum) { Enum type; - ASSERT_TRUE(resolver_->ResolveEnumType( - GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"), &type).ok()); + ASSERT_TRUE( + resolver_ + ->ResolveEnumType( + GetTypeUrl("protobuf_unittest.TestAllTypes.NestedEnum"), &type) + .ok()); EnumHasValue(type, "FOO", 1); EnumHasValue(type, "BAR", 2); EnumHasValue(type, "BAZ", 3); @@ -413,14 +422,16 @@ TEST_F(DescriptorPoolTypeResolverTest, TestCustomValueOptions) { TEST_F(DescriptorPoolTypeResolverTest, TestJsonName) { Type type; - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type) + ASSERT_TRUE(resolver_ + ->ResolveMessageType( + GetTypeUrl<protobuf_unittest::TestAllTypes>(), &type) .ok()); EXPECT_EQ("optionalInt32", FindField(type, "optional_int32")->json_name()); - ASSERT_TRUE(resolver_->ResolveMessageType( - GetTypeUrl<proto3::TestCustomJsonName>(), &type) - .ok()); + ASSERT_TRUE( + resolver_ + ->ResolveMessageType(GetTypeUrl<proto3::TestCustomJsonName>(), &type) + .ok()); EXPECT_EQ("@value", FindField(type, "value")->json_name()); } diff --git a/src/google/protobuf/wire_format.cc b/src/google/protobuf/wire_format.cc index 7a25207fdc..fe0714b9d0 100644 --- a/src/google/protobuf/wire_format.cc +++ b/src/google/protobuf/wire_format.cc @@ -68,8 +68,8 @@ static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field, // =================================================================== -bool UnknownFieldSetFieldSkipper::SkipField( - io::CodedInputStream* input, uint32 tag) { +bool UnknownFieldSetFieldSkipper::SkipField(io::CodedInputStream* input, + uint32 tag) { return WireFormat::SkipField(input, tag, unknown_fields_); } @@ -77,8 +77,7 @@ bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) { return WireFormat::SkipMessage(input, unknown_fields_); } -void UnknownFieldSetFieldSkipper::SkipUnknownEnum( - int field_number, int value) { +void UnknownFieldSetFieldSkipper::SkipUnknownEnum(int field_number, int value) { unknown_fields_->AddVarint(field_number, value); } @@ -116,15 +115,16 @@ bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag, } case WireFormatLite::WIRETYPE_START_GROUP: { if (!input->IncrementRecursionDepth()) return false; - if (!SkipMessage(input, (unknown_fields == NULL) ? - NULL : unknown_fields->AddGroup(number))) { + if (!SkipMessage(input, (unknown_fields == NULL) + ? NULL + : unknown_fields->AddGroup(number))) { return false; } input->DecrementRecursionDepth(); // Check that the ending tag matched the starting tag. - if (!input->LastTagWas(WireFormatLite::MakeTag( - WireFormatLite::GetTagFieldNumber(tag), - WireFormatLite::WIRETYPE_END_GROUP))) { + if (!input->LastTagWas( + WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag), + WireFormatLite::WIRETYPE_END_GROUP))) { return false; } return true; @@ -188,63 +188,61 @@ bool WireFormat::ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input, return true; } - void WireFormat::SerializeUnknownFields(const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); switch (field.type()) { case UnknownField::TYPE_VARINT: - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_VARINT)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_VARINT)); output->WriteVarint64(field.varint()); break; case UnknownField::TYPE_FIXED32: - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_FIXED32)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_FIXED32)); output->WriteLittleEndian32(field.fixed32()); break; case UnknownField::TYPE_FIXED64: - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_FIXED64)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_FIXED64)); output->WriteLittleEndian64(field.fixed64()); break; case UnknownField::TYPE_LENGTH_DELIMITED: - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); output->WriteVarint32(field.length_delimited().size()); output->WriteRawMaybeAliased(field.length_delimited().data(), field.length_delimited().size()); break; case UnknownField::TYPE_GROUP: - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_START_GROUP)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_START_GROUP)); SerializeUnknownFields(field.group(), output); - output->WriteVarint32(WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_END_GROUP)); + output->WriteVarint32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_END_GROUP)); break; } } } uint8* WireFormat::SerializeUnknownFieldsToArray( - const UnknownFieldSet& unknown_fields, - uint8* target) { + const UnknownFieldSet& unknown_fields, uint8* target) { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); switch (field.type()) { case UnknownField::TYPE_VARINT: - target = WireFormatLite::WriteInt64ToArray( - field.number(), field.varint(), target); + target = WireFormatLite::WriteInt64ToArray(field.number(), + field.varint(), target); break; case UnknownField::TYPE_FIXED32: - target = WireFormatLite::WriteFixed32ToArray( - field.number(), field.fixed32(), target); + target = WireFormatLite::WriteFixed32ToArray(field.number(), + field.fixed32(), target); break; case UnknownField::TYPE_FIXED64: - target = WireFormatLite::WriteFixed64ToArray( - field.number(), field.fixed64(), target); + target = WireFormatLite::WriteFixed64ToArray(field.number(), + field.fixed64(), target); break; case UnknownField::TYPE_LENGTH_DELIMITED: target = WireFormatLite::WriteBytesToArray( @@ -263,8 +261,7 @@ uint8* WireFormat::SerializeUnknownFieldsToArray( } void WireFormat::SerializeUnknownMessageSetItems( - const UnknownFieldSet& unknown_fields, - io::CodedOutputStream* output) { + const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); // The only unknown fields that are allowed to exist in a MessageSet are @@ -288,8 +285,7 @@ void WireFormat::SerializeUnknownMessageSetItems( } uint8* WireFormat::SerializeUnknownMessageSetItemsToArray( - const UnknownFieldSet& unknown_fields, - uint8* target) { + const UnknownFieldSet& unknown_fields, uint8* target) { for (int i = 0; i < unknown_fields.field_count(); i++) { const UnknownField& field = unknown_fields.field(i); @@ -303,8 +299,8 @@ uint8* WireFormat::SerializeUnknownMessageSetItemsToArray( // Write type ID. target = io::CodedOutputStream::WriteTagToArray( WireFormatLite::kMessageSetTypeIdTag, target); - target = io::CodedOutputStream::WriteVarint32ToArray( - field.number(), target); + target = + io::CodedOutputStream::WriteVarint32ToArray(field.number(), target); // Write message. target = io::CodedOutputStream::WriteTagToArray( @@ -328,39 +324,33 @@ size_t WireFormat::ComputeUnknownFieldsSize( switch (field.type()) { case UnknownField::TYPE_VARINT: - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_VARINT)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_VARINT)); size += io::CodedOutputStream::VarintSize64(field.varint()); break; case UnknownField::TYPE_FIXED32: - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_FIXED32)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_FIXED32)); size += sizeof(int32); break; case UnknownField::TYPE_FIXED64: - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_FIXED64)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_FIXED64)); size += sizeof(int64); break; case UnknownField::TYPE_LENGTH_DELIMITED: - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); size += io::CodedOutputStream::VarintSize32( field.length_delimited().size()); size += field.length_delimited().size(); break; case UnknownField::TYPE_GROUP: - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_START_GROUP)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_START_GROUP)); size += ComputeUnknownFieldsSize(field.group()); - size += io::CodedOutputStream::VarintSize32( - WireFormatLite::MakeTag(field.number(), - WireFormatLite::WIRETYPE_END_GROUP)); + size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( + field.number(), WireFormatLite::WIRETYPE_END_GROUP)); break; } } @@ -396,7 +386,7 @@ bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input, const Descriptor* descriptor = message->GetDescriptor(); const Reflection* message_reflection = message->GetReflection(); - while(true) { + while (true) { uint32 tag = input->ReadTag(); if (tag == 0) { // End of input. This is a valid place to end, so return true. @@ -420,15 +410,14 @@ bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input, if (input->GetExtensionPool() == NULL) { field = message_reflection->FindKnownExtensionByNumber(field_number); } else { - field = input->GetExtensionPool() - ->FindExtensionByNumber(descriptor, field_number); + field = input->GetExtensionPool()->FindExtensionByNumber( + descriptor, field_number); } } // If that failed, but we're a MessageSet, and this is the tag for a // MessageSet item, then parse that. - if (field == NULL && - descriptor->options().message_set_wire_format() && + if (field == NULL && descriptor->options().message_set_wire_format() && tag == WireFormatLite::kMessageSetItemStartTag) { if (!ParseAndMergeMessageSetItem(input, message)) { return false; @@ -448,8 +437,8 @@ bool WireFormat::SkipMessageSetField(io::CodedInputStream* input, UnknownFieldSet* unknown_fields) { uint32 length; if (!input->ReadVarint32(&length)) return false; - return input->ReadString( - unknown_fields->AddLengthDelimited(field_number), length); + return input->ReadString(unknown_fields->AddLengthDelimited(field_number), + length); } bool WireFormat::ParseAndMergeMessageSetField(uint32 field_number, @@ -480,9 +469,8 @@ static bool StrictUtf8Check(const FieldDescriptor* field) { bool WireFormat::ParseAndMergeField( uint32 tag, - const FieldDescriptor* field, // May be NULL for unknown - Message* message, - io::CodedInputStream* input) { + const FieldDescriptor* field, // May be NULL for unknown + Message* message, io::CodedInputStream* input) { const Reflection* message_reflection = message->GetReflection(); enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format; @@ -494,7 +482,7 @@ bool WireFormat::ParseAndMergeField( value_format = NORMAL_FORMAT; } else if (field->is_packable() && WireFormatLite::GetTagWireType(tag) == - WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { value_format = PACKED_FORMAT; } else { // We don't recognize this field. Either the field number is unknown @@ -512,30 +500,31 @@ bool WireFormat::ParseAndMergeField( switch (field->type()) { #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: { \ - while (input->BytesUntilLimit() > 0) { \ - CPPTYPE value; \ - if (!WireFormatLite::ReadPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \ - return false; \ - message_reflection->Add##CPPTYPE_METHOD(message, field, value); \ - } \ - break; \ - } + case FieldDescriptor::TYPE_##TYPE: { \ + while (input->BytesUntilLimit() > 0) { \ + CPPTYPE value; \ + if (!WireFormatLite::ReadPrimitive<CPPTYPE, \ + WireFormatLite::TYPE_##TYPE>(input, \ + &value)) \ + return false; \ + message_reflection->Add##CPPTYPE_METHOD(message, field, value); \ + } \ + break; \ + } - HANDLE_PACKED_TYPE( INT32, int32, Int32) - HANDLE_PACKED_TYPE( INT64, int64, Int64) - HANDLE_PACKED_TYPE(SINT32, int32, Int32) - HANDLE_PACKED_TYPE(SINT64, int64, Int64) + HANDLE_PACKED_TYPE(INT32, int32, Int32) + HANDLE_PACKED_TYPE(INT64, int64, Int64) + HANDLE_PACKED_TYPE(SINT32, int32, Int32) + HANDLE_PACKED_TYPE(SINT64, int64, Int64) HANDLE_PACKED_TYPE(UINT32, uint32, UInt32) HANDLE_PACKED_TYPE(UINT64, uint64, UInt64) - HANDLE_PACKED_TYPE( FIXED32, uint32, UInt32) - HANDLE_PACKED_TYPE( FIXED64, uint64, UInt64) - HANDLE_PACKED_TYPE(SFIXED32, int32, Int32) - HANDLE_PACKED_TYPE(SFIXED64, int64, Int64) + HANDLE_PACKED_TYPE(FIXED32, uint32, UInt32) + HANDLE_PACKED_TYPE(FIXED64, uint64, UInt64) + HANDLE_PACKED_TYPE(SFIXED32, int32, Int32) + HANDLE_PACKED_TYPE(SFIXED64, int64, Int64) - HANDLE_PACKED_TYPE(FLOAT , float , Float ) + HANDLE_PACKED_TYPE(FLOAT, float, Float) HANDLE_PACKED_TYPE(DOUBLE, double, Double) HANDLE_PACKED_TYPE(BOOL, bool, Bool) @@ -545,7 +534,8 @@ bool WireFormat::ParseAndMergeField( while (input->BytesUntilLimit() > 0) { int value; if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( - input, &value)) return false; + input, &value)) + return false; if (message->GetDescriptor()->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { message_reflection->AddEnumValue(message, field, value); @@ -558,10 +548,8 @@ bool WireFormat::ParseAndMergeField( // The enum value is not one of the known values. Add it to the // UnknownFieldSet. int64 sign_extended_value = static_cast<int64>(value); - message_reflection->MutableUnknownFields(message) - ->AddVarint( - WireFormatLite::GetTagFieldNumber(tag), - sign_extended_value); + message_reflection->MutableUnknownFields(message)->AddVarint( + WireFormatLite::GetTagFieldNumber(tag), sign_extended_value); } } } @@ -584,32 +572,32 @@ bool WireFormat::ParseAndMergeField( // Non-packed value (value_format == NORMAL_FORMAT) switch (field->type()) { #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: { \ - CPPTYPE value; \ - if (!WireFormatLite::ReadPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \ - return false; \ - if (field->is_repeated()) { \ - message_reflection->Add##CPPTYPE_METHOD(message, field, value); \ - } else { \ - message_reflection->Set##CPPTYPE_METHOD(message, field, value); \ - } \ - break; \ - } + case FieldDescriptor::TYPE_##TYPE: { \ + CPPTYPE value; \ + if (!WireFormatLite::ReadPrimitive<CPPTYPE, WireFormatLite::TYPE_##TYPE>( \ + input, &value)) \ + return false; \ + if (field->is_repeated()) { \ + message_reflection->Add##CPPTYPE_METHOD(message, field, value); \ + } else { \ + message_reflection->Set##CPPTYPE_METHOD(message, field, value); \ + } \ + break; \ + } - HANDLE_TYPE( INT32, int32, Int32) - HANDLE_TYPE( INT64, int64, Int64) - HANDLE_TYPE(SINT32, int32, Int32) - HANDLE_TYPE(SINT64, int64, Int64) + HANDLE_TYPE(INT32, int32, Int32) + HANDLE_TYPE(INT64, int64, Int64) + HANDLE_TYPE(SINT32, int32, Int32) + HANDLE_TYPE(SINT64, int64, Int64) HANDLE_TYPE(UINT32, uint32, UInt32) HANDLE_TYPE(UINT64, uint64, UInt64) - HANDLE_TYPE( FIXED32, uint32, UInt32) - HANDLE_TYPE( FIXED64, uint64, UInt64) - HANDLE_TYPE(SFIXED32, int32, Int32) - HANDLE_TYPE(SFIXED64, int64, Int64) + HANDLE_TYPE(FIXED32, uint32, UInt32) + HANDLE_TYPE(FIXED64, uint64, UInt64) + HANDLE_TYPE(SFIXED32, int32, Int32) + HANDLE_TYPE(SFIXED64, int64, Int64) - HANDLE_TYPE(FLOAT , float , Float ) + HANDLE_TYPE(FLOAT, float, Float) HANDLE_TYPE(DOUBLE, double, Double) HANDLE_TYPE(BOOL, bool, Bool) @@ -618,7 +606,8 @@ bool WireFormat::ParseAndMergeField( case FieldDescriptor::TYPE_ENUM: { int value; if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( - input, &value)) return false; + input, &value)) + return false; if (field->is_repeated()) { message_reflection->AddEnumValue(message, field, value); } else { @@ -633,9 +622,9 @@ bool WireFormat::ParseAndMergeField( std::string value; if (!WireFormatLite::ReadString(input, &value)) return false; if (strict_utf8_check) { - if (!WireFormatLite::VerifyUtf8String( - value.data(), value.length(), WireFormatLite::PARSE, - field->full_name().c_str())) { + if (!WireFormatLite::VerifyUtf8String(value.data(), value.length(), + WireFormatLite::PARSE, + field->full_name().c_str())) { return false; } } else { @@ -696,9 +685,8 @@ bool WireFormat::ParseAndMergeField( return true; } -bool WireFormat::ParseAndMergeMessageSetItem( - io::CodedInputStream* input, - Message* message) { +bool WireFormat::ParseAndMergeMessageSetItem(io::CodedInputStream* input, + Message* message) { struct MSReflective { bool ParseField(int type_id, io::CodedInputStream* input) { const FieldDescriptor* field = @@ -720,9 +708,8 @@ bool WireFormat::ParseAndMergeMessageSetItem( // =================================================================== -void WireFormat::SerializeWithCachedSizes( - const Message& message, - int size, io::CodedOutputStream* output) { +void WireFormat::SerializeWithCachedSizes(const Message& message, int size, + io::CodedOutputStream* output) { const Descriptor* descriptor = message.GetDescriptor(); const Reflection* message_reflection = message.GetReflection(); int expected_endpoint = output->ByteCount() + size; @@ -746,14 +733,14 @@ void WireFormat::SerializeWithCachedSizes( SerializeUnknownMessageSetItems( message_reflection->GetUnknownFields(message), output); } else { - SerializeUnknownFields( - message_reflection->GetUnknownFields(message), output); + SerializeUnknownFields(message_reflection->GetUnknownFields(message), + output); } GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint) - << ": Protocol message serialized to a size different from what was " - "originally expected. Perhaps it was modified by another thread " - "during serialization?"; + << ": Protocol message serialized to a size different from what was " + "originally expected. Perhaps it was modified by another thread " + "during serialization?"; } static void SerializeMapKeyWithCachedSizes(const FieldDescriptor* field, @@ -879,10 +866,9 @@ static void SerializeMapEntry(const FieldDescriptor* field, const MapKey& key, SerializeMapValueRefWithCachedSizes(value_field, value, output); } -void WireFormat::SerializeFieldWithCachedSizes( - const FieldDescriptor* field, - const Message& message, - io::CodedOutputStream* output) { +void WireFormat::SerializeFieldWithCachedSizes(const FieldDescriptor* field, + const Message& message, + io::CodedOutputStream* output) { const Reflection* message_reflection = message.GetReflection(); if (field->is_extension() && @@ -956,67 +942,68 @@ void WireFormat::SerializeFieldWithCachedSizes( const bool is_packed = field->is_packed(); if (is_packed && count > 0) { WireFormatLite::WriteTag(field->number(), - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); const size_t data_size = FieldDataOnlyByteSize(field, message); output->WriteVarint32(data_size); } for (int j = 0; j < count; j++) { switch (field->type()) { -#define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: { \ - const CPPTYPE value = field->is_repeated() ? \ - message_reflection->GetRepeated##CPPTYPE_METHOD( \ - message, field, j) : \ - message_reflection->Get##CPPTYPE_METHOD( \ - message, field); \ - if (is_packed) { \ - WireFormatLite::Write##TYPE_METHOD##NoTag(value, output); \ - } else { \ - WireFormatLite::Write##TYPE_METHOD(field->number(), value, output); \ - } \ - break; \ - } +#define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \ + case FieldDescriptor::TYPE_##TYPE: { \ + const CPPTYPE value = \ + field->is_repeated() \ + ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \ + j) \ + : message_reflection->Get##CPPTYPE_METHOD(message, field); \ + if (is_packed) { \ + WireFormatLite::Write##TYPE_METHOD##NoTag(value, output); \ + } else { \ + WireFormatLite::Write##TYPE_METHOD(field->number(), value, output); \ + } \ + break; \ + } - HANDLE_PRIMITIVE_TYPE( INT32, int32, Int32, Int32) - HANDLE_PRIMITIVE_TYPE( INT64, int64, Int64, Int64) - HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32) - HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64) + HANDLE_PRIMITIVE_TYPE(INT32, int32, Int32, Int32) + HANDLE_PRIMITIVE_TYPE(INT64, int64, Int64, Int64) + HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32) + HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64) HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32) HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64) - HANDLE_PRIMITIVE_TYPE( FIXED32, uint32, Fixed32, UInt32) - HANDLE_PRIMITIVE_TYPE( FIXED64, uint64, Fixed64, UInt64) - HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32) - HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64) + HANDLE_PRIMITIVE_TYPE(FIXED32, uint32, Fixed32, UInt32) + HANDLE_PRIMITIVE_TYPE(FIXED64, uint64, Fixed64, UInt64) + HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32) + HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64) - HANDLE_PRIMITIVE_TYPE(FLOAT , float , Float , Float ) + HANDLE_PRIMITIVE_TYPE(FLOAT, float, Float, Float) HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double) HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool) #undef HANDLE_PRIMITIVE_TYPE -#define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: \ - WireFormatLite::Write##TYPE_METHOD( \ - field->number(), \ - field->is_repeated() ? \ - (map_entries.empty() ? \ - message_reflection->GetRepeated##CPPTYPE_METHOD( \ - message, field, j) : \ - *map_entries[j]) : \ - message_reflection->Get##CPPTYPE_METHOD(message, field), \ - output); \ - break; +#define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \ + case FieldDescriptor::TYPE_##TYPE: \ + WireFormatLite::Write##TYPE_METHOD( \ + field->number(), \ + field->is_repeated() \ + ? (map_entries.empty() \ + ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, \ + field, j) \ + : *map_entries[j]) \ + : message_reflection->Get##CPPTYPE_METHOD(message, field), \ + output); \ + break; - HANDLE_TYPE(GROUP , Group , Message) + HANDLE_TYPE(GROUP, Group, Message) HANDLE_TYPE(MESSAGE, Message, Message) #undef HANDLE_TYPE case FieldDescriptor::TYPE_ENUM: { - const EnumValueDescriptor* value = field->is_repeated() ? - message_reflection->GetRepeatedEnum(message, field, j) : - message_reflection->GetEnum(message, field); + const EnumValueDescriptor* value = + field->is_repeated() + ? message_reflection->GetRepeatedEnum(message, field, j) + : message_reflection->GetEnum(message, field); if (is_packed) { WireFormatLite::WriteEnumNoTag(value->number(), output); } else { @@ -1064,8 +1051,7 @@ void WireFormat::SerializeFieldWithCachedSizes( } void WireFormat::SerializeMessageSetItemWithCachedSizes( - const FieldDescriptor* field, - const Message& message, + const FieldDescriptor* field, const Message& message, io::CodedOutputStream* output) { const Reflection* message_reflection = message.GetReflection(); @@ -1112,18 +1098,17 @@ size_t WireFormat::ByteSize(const Message& message) { if (descriptor->options().message_set_wire_format()) { our_size += ComputeUnknownMessageSetItemsSize( - message_reflection->GetUnknownFields(message)); + message_reflection->GetUnknownFields(message)); } else { - our_size += ComputeUnknownFieldsSize( - message_reflection->GetUnknownFields(message)); + our_size += + ComputeUnknownFieldsSize(message_reflection->GetUnknownFields(message)); } return our_size; } -size_t WireFormat::FieldByteSize( - const FieldDescriptor* field, - const Message& message) { +size_t WireFormat::FieldByteSize(const FieldDescriptor* field, + const Message& message) { const Reflection* message_reflection = message.GetReflection(); if (field->is_extension() && @@ -1240,9 +1225,8 @@ static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field, return 0; } -size_t WireFormat::FieldDataOnlyByteSize( - const FieldDescriptor* field, - const Message& message) { +size_t WireFormat::FieldDataOnlyByteSize(const FieldDescriptor* field, + const Message& message) { const Reflection* message_reflection = message.GetReflection(); size_t data_size = 0; @@ -1278,43 +1262,43 @@ size_t WireFormat::FieldDataOnlyByteSize( } switch (field->type()) { -#define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: \ - if (field->is_repeated()) { \ - for (int j = 0; j < count; j++) { \ - data_size += WireFormatLite::TYPE_METHOD##Size( \ - message_reflection->GetRepeated##CPPTYPE_METHOD( \ - message, field, j)); \ - } \ - } else { \ - data_size += WireFormatLite::TYPE_METHOD##Size( \ - message_reflection->Get##CPPTYPE_METHOD(message, field)); \ - } \ - break; +#define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \ + case FieldDescriptor::TYPE_##TYPE: \ + if (field->is_repeated()) { \ + for (int j = 0; j < count; j++) { \ + data_size += WireFormatLite::TYPE_METHOD##Size( \ + message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \ + j)); \ + } \ + } else { \ + data_size += WireFormatLite::TYPE_METHOD##Size( \ + message_reflection->Get##CPPTYPE_METHOD(message, field)); \ + } \ + break; -#define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \ - case FieldDescriptor::TYPE_##TYPE: \ - data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \ - break; +#define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \ + case FieldDescriptor::TYPE_##TYPE: \ + data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \ + break; - HANDLE_TYPE( INT32, Int32, Int32) - HANDLE_TYPE( INT64, Int64, Int64) - HANDLE_TYPE(SINT32, SInt32, Int32) - HANDLE_TYPE(SINT64, SInt64, Int64) + HANDLE_TYPE(INT32, Int32, Int32) + HANDLE_TYPE(INT64, Int64, Int64) + HANDLE_TYPE(SINT32, SInt32, Int32) + HANDLE_TYPE(SINT64, SInt64, Int64) HANDLE_TYPE(UINT32, UInt32, UInt32) HANDLE_TYPE(UINT64, UInt64, UInt64) - HANDLE_FIXED_TYPE( FIXED32, Fixed32) - HANDLE_FIXED_TYPE( FIXED64, Fixed64) + HANDLE_FIXED_TYPE(FIXED32, Fixed32) + HANDLE_FIXED_TYPE(FIXED64, Fixed64) HANDLE_FIXED_TYPE(SFIXED32, SFixed32) HANDLE_FIXED_TYPE(SFIXED64, SFixed64) - HANDLE_FIXED_TYPE(FLOAT , Float ) + HANDLE_FIXED_TYPE(FLOAT, Float) HANDLE_FIXED_TYPE(DOUBLE, Double) HANDLE_FIXED_TYPE(BOOL, Bool) - HANDLE_TYPE(GROUP , Group , Message) + HANDLE_TYPE(GROUP, Group, Message) HANDLE_TYPE(MESSAGE, Message, Message) #undef HANDLE_TYPE #undef HANDLE_FIXED_TYPE @@ -1323,11 +1307,11 @@ size_t WireFormat::FieldDataOnlyByteSize( if (field->is_repeated()) { for (int j = 0; j < count; j++) { data_size += WireFormatLite::EnumSize( - message_reflection->GetRepeatedEnum(message, field, j)->number()); + message_reflection->GetRepeatedEnum(message, field, j)->number()); } } else { data_size += WireFormatLite::EnumSize( - message_reflection->GetEnum(message, field)->number()); + message_reflection->GetEnum(message, field)->number()); } break; } @@ -1352,9 +1336,8 @@ size_t WireFormat::FieldDataOnlyByteSize( return data_size; } -size_t WireFormat::MessageSetItemByteSize( - const FieldDescriptor* field, - const Message& message) { +size_t WireFormat::MessageSetItemByteSize(const FieldDescriptor* field, + const Message& message) { const Reflection* message_reflection = message.GetReflection(); size_t our_size = WireFormatLite::kMessageSetItemTagsSize; diff --git a/src/google/protobuf/wire_format_lite.cc b/src/google/protobuf/wire_format_lite.cc index db058c2a34..5189a91cf6 100644 --- a/src/google/protobuf/wire_format_lite.cc +++ b/src/google/protobuf/wire_format_lite.cc @@ -50,7 +50,6 @@ namespace google { namespace protobuf { namespace internal { - #if !defined(_MSC_VER) || _MSC_VER >= 1900 // Old version of MSVC doesn't like definitions of inline constants, GCC // requires them. @@ -63,64 +62,63 @@ const int WireFormatLite::kMessageSetMessageTag; // IBM xlC requires prefixing constants with WireFormatLite:: const size_t WireFormatLite::kMessageSetItemTagsSize = - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetItemStartTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetItemEndTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetTypeIdTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetMessageTag>::value; + io::CodedOutputStream::StaticVarintSize32< + WireFormatLite::kMessageSetItemStartTag>::value + + io::CodedOutputStream::StaticVarintSize32< + WireFormatLite::kMessageSetItemEndTag>::value + + io::CodedOutputStream::StaticVarintSize32< + WireFormatLite::kMessageSetTypeIdTag>::value + + io::CodedOutputStream::StaticVarintSize32< + WireFormatLite::kMessageSetMessageTag>::value; const WireFormatLite::CppType -WireFormatLite::kFieldTypeToCppTypeMap[MAX_FIELD_TYPE + 1] = { - static_cast<CppType>(0), // 0 is reserved for errors - - CPPTYPE_DOUBLE, // TYPE_DOUBLE - CPPTYPE_FLOAT, // TYPE_FLOAT - CPPTYPE_INT64, // TYPE_INT64 - CPPTYPE_UINT64, // TYPE_UINT64 - CPPTYPE_INT32, // TYPE_INT32 - CPPTYPE_UINT64, // TYPE_FIXED64 - CPPTYPE_UINT32, // TYPE_FIXED32 - CPPTYPE_BOOL, // TYPE_BOOL - CPPTYPE_STRING, // TYPE_STRING - CPPTYPE_MESSAGE, // TYPE_GROUP - CPPTYPE_MESSAGE, // TYPE_MESSAGE - CPPTYPE_STRING, // TYPE_BYTES - CPPTYPE_UINT32, // TYPE_UINT32 - CPPTYPE_ENUM, // TYPE_ENUM - CPPTYPE_INT32, // TYPE_SFIXED32 - CPPTYPE_INT64, // TYPE_SFIXED64 - CPPTYPE_INT32, // TYPE_SINT32 - CPPTYPE_INT64, // TYPE_SINT64 + WireFormatLite::kFieldTypeToCppTypeMap[MAX_FIELD_TYPE + 1] = { + static_cast<CppType>(0), // 0 is reserved for errors + + CPPTYPE_DOUBLE, // TYPE_DOUBLE + CPPTYPE_FLOAT, // TYPE_FLOAT + CPPTYPE_INT64, // TYPE_INT64 + CPPTYPE_UINT64, // TYPE_UINT64 + CPPTYPE_INT32, // TYPE_INT32 + CPPTYPE_UINT64, // TYPE_FIXED64 + CPPTYPE_UINT32, // TYPE_FIXED32 + CPPTYPE_BOOL, // TYPE_BOOL + CPPTYPE_STRING, // TYPE_STRING + CPPTYPE_MESSAGE, // TYPE_GROUP + CPPTYPE_MESSAGE, // TYPE_MESSAGE + CPPTYPE_STRING, // TYPE_BYTES + CPPTYPE_UINT32, // TYPE_UINT32 + CPPTYPE_ENUM, // TYPE_ENUM + CPPTYPE_INT32, // TYPE_SFIXED32 + CPPTYPE_INT64, // TYPE_SFIXED64 + CPPTYPE_INT32, // TYPE_SINT32 + CPPTYPE_INT64, // TYPE_SINT64 }; const WireFormatLite::WireType -WireFormatLite::kWireTypeForFieldType[MAX_FIELD_TYPE + 1] = { - static_cast<WireFormatLite::WireType>(-1), // invalid - WireFormatLite::WIRETYPE_FIXED64, // TYPE_DOUBLE - WireFormatLite::WIRETYPE_FIXED32, // TYPE_FLOAT - WireFormatLite::WIRETYPE_VARINT, // TYPE_INT64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_INT32 - WireFormatLite::WIRETYPE_FIXED64, // TYPE_FIXED64 - WireFormatLite::WIRETYPE_FIXED32, // TYPE_FIXED32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_BOOL - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_STRING - WireFormatLite::WIRETYPE_START_GROUP, // TYPE_GROUP - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_MESSAGE - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_BYTES - WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_ENUM - WireFormatLite::WIRETYPE_FIXED32, // TYPE_SFIXED32 - WireFormatLite::WIRETYPE_FIXED64, // TYPE_SFIXED64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT64 + WireFormatLite::kWireTypeForFieldType[MAX_FIELD_TYPE + 1] = { + static_cast<WireFormatLite::WireType>(-1), // invalid + WireFormatLite::WIRETYPE_FIXED64, // TYPE_DOUBLE + WireFormatLite::WIRETYPE_FIXED32, // TYPE_FLOAT + WireFormatLite::WIRETYPE_VARINT, // TYPE_INT64 + WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT64 + WireFormatLite::WIRETYPE_VARINT, // TYPE_INT32 + WireFormatLite::WIRETYPE_FIXED64, // TYPE_FIXED64 + WireFormatLite::WIRETYPE_FIXED32, // TYPE_FIXED32 + WireFormatLite::WIRETYPE_VARINT, // TYPE_BOOL + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_STRING + WireFormatLite::WIRETYPE_START_GROUP, // TYPE_GROUP + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_MESSAGE + WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_BYTES + WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT32 + WireFormatLite::WIRETYPE_VARINT, // TYPE_ENUM + WireFormatLite::WIRETYPE_FIXED32, // TYPE_SFIXED32 + WireFormatLite::WIRETYPE_FIXED64, // TYPE_SFIXED64 + WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT32 + WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT64 }; -bool WireFormatLite::SkipField( - io::CodedInputStream* input, uint32 tag) { +bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag) { // Field number 0 is illegal. if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false; switch (WireFormatLite::GetTagWireType(tag)) { @@ -145,9 +143,9 @@ bool WireFormatLite::SkipField( if (!SkipMessage(input)) return false; input->DecrementRecursionDepth(); // Check that the ending tag matched the starting tag. - if (!input->LastTagWas(WireFormatLite::MakeTag( - WireFormatLite::GetTagFieldNumber(tag), - WireFormatLite::WIRETYPE_END_GROUP))) { + if (!input->LastTagWas( + WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag), + WireFormatLite::WIRETYPE_END_GROUP))) { return false; } return true; @@ -166,8 +164,8 @@ bool WireFormatLite::SkipField( } } -bool WireFormatLite::SkipField( - io::CodedInputStream* input, uint32 tag, io::CodedOutputStream* output) { +bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag, + io::CodedOutputStream* output) { // Field number 0 is illegal. if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false; switch (WireFormatLite::GetTagWireType(tag)) { @@ -202,9 +200,9 @@ bool WireFormatLite::SkipField( if (!SkipMessage(input, output)) return false; input->DecrementRecursionDepth(); // Check that the ending tag matched the starting tag. - if (!input->LastTagWas(WireFormatLite::MakeTag( - WireFormatLite::GetTagFieldNumber(tag), - WireFormatLite::WIRETYPE_END_GROUP))) { + if (!input->LastTagWas( + WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag), + WireFormatLite::WIRETYPE_END_GROUP))) { return false; } return true; @@ -245,7 +243,7 @@ bool WireFormatLite::SkipMessage(io::CodedInputStream* input) { } bool WireFormatLite::SkipMessage(io::CodedInputStream* input, - io::CodedOutputStream* output) { + io::CodedOutputStream* output) { while (true) { uint32 tag = input->ReadTag(); if (tag == 0) { @@ -265,8 +263,7 @@ bool WireFormatLite::SkipMessage(io::CodedInputStream* input, } } -bool FieldSkipper::SkipField( - io::CodedInputStream* input, uint32 tag) { +bool FieldSkipper::SkipField(io::CodedInputStream* input, uint32 tag) { return WireFormatLite::SkipField(input, tag); } @@ -274,13 +271,12 @@ bool FieldSkipper::SkipMessage(io::CodedInputStream* input) { return WireFormatLite::SkipMessage(input); } -void FieldSkipper::SkipUnknownEnum( - int /* field_number */, int /* value */) { +void FieldSkipper::SkipUnknownEnum(int /* field_number */, int /* value */) { // Nothing. } -bool CodedOutputStreamFieldSkipper::SkipField( - io::CodedInputStream* input, uint32 tag) { +bool CodedOutputStreamFieldSkipper::SkipField(io::CodedInputStream* input, + uint32 tag) { return WireFormatLite::SkipField(input, tag, unknown_fields_); } @@ -288,8 +284,8 @@ bool CodedOutputStreamFieldSkipper::SkipMessage(io::CodedInputStream* input) { return WireFormatLite::SkipMessage(input, unknown_fields_); } -void CodedOutputStreamFieldSkipper::SkipUnknownEnum( - int field_number, int value) { +void CodedOutputStreamFieldSkipper::SkipUnknownEnum(int field_number, + int value) { unknown_fields_->WriteVarint32(field_number); unknown_fields_->WriteVarint64(value); } @@ -314,11 +310,8 @@ bool WireFormatLite::ReadPackedEnumNoInline(io::CodedInputStream* input, } bool WireFormatLite::ReadPackedEnumPreserveUnknowns( - io::CodedInputStream* input, - int field_number, - bool (*is_valid)(int), - io::CodedOutputStream* unknown_fields_stream, - RepeatedField<int>* values) { + io::CodedInputStream* input, int field_number, bool (*is_valid)(int), + io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values) { uint32 length; if (!input->ReadVarint32(&length)) return false; io::CodedInputStream::Limit limit = input->PushLimit(length); @@ -385,7 +378,7 @@ static void WriteArray(const CType* a, int n, io::CodedOutputStream* output) { int to_do = std::min(kAtATime, n - i); uint8* ptr = buf; for (int j = 0; j < to_do; j++) { - EncodeFixedSizeValue(a[i+j], ptr); + EncodeFixedSizeValue(a[i + j], ptr); ptr += sizeof(a[0]); } output->WriteRaw(buf, to_do * sizeof(a[0])); @@ -399,12 +392,12 @@ void WireFormatLite::WriteFloatArray(const float* a, int n, } void WireFormatLite::WriteDoubleArray(const double* a, int n, - io::CodedOutputStream* output) { + io::CodedOutputStream* output) { WriteArray<double>(a, n, output); } void WireFormatLite::WriteFixed32Array(const uint32* a, int n, - io::CodedOutputStream* output) { + io::CodedOutputStream* output) { WriteArray<uint32>(a, n, output); } @@ -414,12 +407,12 @@ void WireFormatLite::WriteFixed64Array(const uint64* a, int n, } void WireFormatLite::WriteSFixed32Array(const int32* a, int n, - io::CodedOutputStream* output) { + io::CodedOutputStream* output) { WriteArray<int32>(a, n, output); } void WireFormatLite::WriteSFixed64Array(const int64* a, int n, - io::CodedOutputStream* output) { + io::CodedOutputStream* output) { WriteArray<int64>(a, n, output); } @@ -533,16 +526,14 @@ void WireFormatLite::WriteBytesMaybeAliased(int field_number, } -void WireFormatLite::WriteGroup(int field_number, - const MessageLite& value, +void WireFormatLite::WriteGroup(int field_number, const MessageLite& value, io::CodedOutputStream* output) { WriteTag(field_number, WIRETYPE_START_GROUP, output); value.SerializeWithCachedSizes(output); WriteTag(field_number, WIRETYPE_END_GROUP, output); } -void WireFormatLite::WriteMessage(int field_number, - const MessageLite& value, +void WireFormatLite::WriteMessage(int field_number, const MessageLite& value, io::CodedOutputStream* output) { WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); const int size = value.GetCachedSize(); @@ -587,7 +578,7 @@ inline static bool ReadBytesToString(io::CodedInputStream* input, std::string* value) { uint32 length; return input->ReadVarint32(&length) && - input->InternalReadStringInline(value, length); + input->InternalReadStringInline(value, length); } bool WireFormatLite::ReadBytes(io::CodedInputStream* input, @@ -615,9 +606,7 @@ void PrintUTF8ErrorLog(const char* field_name, const char* operation_str, << "bytes. " << stacktrace; } -bool WireFormatLite::VerifyUtf8String(const char* data, - int size, - Operation op, +bool WireFormatLite::VerifyUtf8String(const char* data, int size, Operation op, const char* field_name) { if (!IsStructurallyValidUTF8(data, size)) { const char* operation_str = NULL; @@ -628,7 +617,7 @@ bool WireFormatLite::VerifyUtf8String(const char* data, case SERIALIZE: operation_str = "serializing"; break; - // no default case: have the compiler warn if a case is not covered. + // no default case: have the compiler warn if a case is not covered. } PrintUTF8ErrorLog(field_name, operation_str, false); return false; @@ -638,18 +627,18 @@ bool WireFormatLite::VerifyUtf8String(const char* data, // this code is deliberately written such that clang makes it into really // efficient SSE code. -template<bool ZigZag, bool SignExtended, typename T> +template <bool ZigZag, bool SignExtended, typename T> static size_t VarintSize(const T* data, const int n) { #if __cplusplus >= 201103L static_assert(sizeof(T) == 4, "This routine only works for 32 bit integers"); // is_unsigned<T> => !ZigZag - static_assert((std::is_unsigned<T>::value ^ ZigZag) || - std::is_signed<T>::value, - "Cannot ZigZag encode unsigned types"); + static_assert( + (std::is_unsigned<T>::value ^ ZigZag) || std::is_signed<T>::value, + "Cannot ZigZag encode unsigned types"); // is_unsigned<T> => !SignExtended - static_assert((std::is_unsigned<T>::value ^ SignExtended) || - std::is_signed<T>::value, - "Cannot SignExtended unsigned types"); + static_assert( + (std::is_unsigned<T>::value ^ SignExtended) || std::is_signed<T>::value, + "Cannot SignExtended unsigned types"); static_assert(!(SignExtended && ZigZag), "Cannot SignExtended and ZigZag on the same type"); #endif @@ -675,7 +664,7 @@ static size_t VarintSize(const T* data, const int n) { return sum; } -template<bool ZigZag, typename T> +template <bool ZigZag, typename T> static size_t VarintSize64(const T* data, const int n) { #if __cplusplus >= 201103L static_assert(sizeof(T) == 8, "This routine only works for 64 bit integers"); @@ -771,7 +760,7 @@ size_t WireFormatLite::EnumSize(const RepeatedField<int>& value) { // enable this. #define USE_SSE_FOR_64_BIT_INTEGER_ARRAYS 0 #if USE_SSE_FOR_64_BIT_INTEGER_ARRAYS -size_t WireFormatLite::Int64Size (const RepeatedField< int64>& value) { +size_t WireFormatLite::Int64Size(const RepeatedField<int64>& value) { return VarintSize64<false>(value.data(), value.size()); } @@ -779,13 +768,13 @@ size_t WireFormatLite::UInt64Size(const RepeatedField<uint64>& value) { return VarintSize64<false>(value.data(), value.size()); } -size_t WireFormatLite::SInt64Size(const RepeatedField< int64>& value) { +size_t WireFormatLite::SInt64Size(const RepeatedField<int64>& value) { return VarintSize64<true>(value.data(), value.size()); } #else -size_t WireFormatLite::Int64Size (const RepeatedField< int64>& value) { +size_t WireFormatLite::Int64Size(const RepeatedField<int64>& value) { size_t out = 0; const int n = value.size(); for (int i = 0; i < n; i++) { @@ -803,7 +792,7 @@ size_t WireFormatLite::UInt64Size(const RepeatedField<uint64>& value) { return out; } -size_t WireFormatLite::SInt64Size(const RepeatedField< int64>& value) { +size_t WireFormatLite::SInt64Size(const RepeatedField<int64>& value) { size_t out = 0; const int n = value.size(); for (int i = 0; i < n; i++) { diff --git a/src/google/protobuf/wire_format_lite.h b/src/google/protobuf/wire_format_lite.h index 2468163158..e0972d9827 100644 --- a/src/google/protobuf/wire_format_lite.h +++ b/src/google/protobuf/wire_format_lite.h @@ -909,8 +909,7 @@ inline size_t ComputeUnknownMessageSetItemsSize( template <> inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>( - io::CodedInputStream* input, - int32* value) { + io::CodedInputStream* input, int32* value) { uint32 temp; if (!input->ReadVarint32(&temp)) return false; *value = static_cast<int32>(temp); @@ -918,8 +917,7 @@ inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>( } template <> inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>( - io::CodedInputStream* input, - int64* value) { + io::CodedInputStream* input, int64* value) { uint64 temp; if (!input->ReadVarint64(&temp)) return false; *value = static_cast<int64>(temp); @@ -927,20 +925,17 @@ inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>( } template <> inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>( - io::CodedInputStream* input, - uint32* value) { + io::CodedInputStream* input, uint32* value) { return input->ReadVarint32(value); } template <> inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>( - io::CodedInputStream* input, - uint64* value) { + io::CodedInputStream* input, uint64* value) { return input->ReadVarint64(value); } template <> inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>( - io::CodedInputStream* input, - int32* value) { + io::CodedInputStream* input, int32* value) { uint32 temp; if (!input->ReadVarint32(&temp)) return false; *value = ZigZagDecode32(temp); @@ -948,8 +943,7 @@ inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>( } template <> inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>( - io::CodedInputStream* input, - int64* value) { + io::CodedInputStream* input, int64* value) { uint64 temp; if (!input->ReadVarint64(&temp)) return false; *value = ZigZagDecode64(temp); @@ -957,20 +951,17 @@ inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>( } template <> inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>( - io::CodedInputStream* input, - uint32* value) { + io::CodedInputStream* input, uint32* value) { return input->ReadLittleEndian32(value); } template <> inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>( - io::CodedInputStream* input, - uint64* value) { + io::CodedInputStream* input, uint64* value) { return input->ReadLittleEndian64(value); } template <> inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>( - io::CodedInputStream* input, - int32* value) { + io::CodedInputStream* input, int32* value) { uint32 temp; if (!input->ReadLittleEndian32(&temp)) return false; *value = static_cast<int32>(temp); @@ -978,8 +969,7 @@ inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>( } template <> inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>( - io::CodedInputStream* input, - int64* value) { + io::CodedInputStream* input, int64* value) { uint64 temp; if (!input->ReadLittleEndian64(&temp)) return false; *value = static_cast<int64>(temp); @@ -987,8 +977,7 @@ inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>( } template <> inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>( - io::CodedInputStream* input, - float* value) { + io::CodedInputStream* input, float* value) { uint32 temp; if (!input->ReadLittleEndian32(&temp)) return false; *value = DecodeFloat(temp); @@ -996,8 +985,7 @@ inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>( } template <> inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>( - io::CodedInputStream* input, - double* value) { + io::CodedInputStream* input, double* value) { uint64 temp; if (!input->ReadLittleEndian64(&temp)) return false; *value = DecodeDouble(temp); @@ -1005,8 +993,7 @@ inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>( } template <> inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>( - io::CodedInputStream* input, - bool* value) { + io::CodedInputStream* input, bool* value) { uint64 temp; if (!input->ReadVarint64(&temp)) return false; *value = temp != 0; @@ -1014,8 +1001,7 @@ inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>( } template <> inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( - io::CodedInputStream* input, - int* value) { + io::CodedInputStream* input, int* value) { uint32 temp; if (!input->ReadVarint32(&temp)) return false; *value = static_cast<int>(temp); @@ -1023,54 +1009,48 @@ inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - uint32, WireFormatLite::TYPE_FIXED32>( - const uint8* buffer, - uint32* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<uint32, WireFormatLite::TYPE_FIXED32>( + const uint8* buffer, uint32* value) { return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value); } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - uint64, WireFormatLite::TYPE_FIXED64>( - const uint8* buffer, - uint64* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<uint64, WireFormatLite::TYPE_FIXED64>( + const uint8* buffer, uint64* value) { return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value); } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - int32, WireFormatLite::TYPE_SFIXED32>( - const uint8* buffer, - int32* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<int32, WireFormatLite::TYPE_SFIXED32>( + const uint8* buffer, int32* value) { uint32 temp; buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp); *value = static_cast<int32>(temp); return buffer; } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - int64, WireFormatLite::TYPE_SFIXED64>( - const uint8* buffer, - int64* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<int64, WireFormatLite::TYPE_SFIXED64>( + const uint8* buffer, int64* value) { uint64 temp; buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp); *value = static_cast<int64>(temp); return buffer; } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - float, WireFormatLite::TYPE_FLOAT>( - const uint8* buffer, - float* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<float, WireFormatLite::TYPE_FLOAT>( + const uint8* buffer, float* value) { uint32 temp; buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp); *value = DecodeFloat(temp); return buffer; } template <> -inline const uint8* WireFormatLite::ReadPrimitiveFromArray< - double, WireFormatLite::TYPE_DOUBLE>( - const uint8* buffer, - double* value) { +inline const uint8* +WireFormatLite::ReadPrimitiveFromArray<double, WireFormatLite::TYPE_DOUBLE>( + const uint8* buffer, double* value) { uint64 temp; buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp); *value = DecodeDouble(temp); @@ -1080,9 +1060,7 @@ inline const uint8* WireFormatLite::ReadPrimitiveFromArray< template <typename CType, enum WireFormatLite::FieldType DeclaredType> inline bool WireFormatLite::ReadRepeatedPrimitive( int, // tag_size, unused. - uint32 tag, - io::CodedInputStream* input, - RepeatedField<CType>* values) { + uint32 tag, io::CodedInputStream* input, RepeatedField<CType>* values) { CType value; if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; values->Add(value); @@ -1097,14 +1075,11 @@ inline bool WireFormatLite::ReadRepeatedPrimitive( template <typename CType, enum WireFormatLite::FieldType DeclaredType> inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( - int tag_size, - uint32 tag, - io::CodedInputStream* input, + int tag_size, uint32 tag, io::CodedInputStream* input, RepeatedField<CType>* values) { GOOGLE_DCHECK_EQ(UInt32Size(tag), static_cast<size_t>(tag_size)); CType value; - if (!ReadPrimitive<CType, DeclaredType>(input, &value)) - return false; + if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; values->Add(value); // For fixed size values, repeated values can be read more quickly by @@ -1129,8 +1104,8 @@ inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( (std::min)(values->Capacity() - values->size(), size / per_value_size); int num_read = 0; while (num_read < elements_available && - (buffer = io::CodedInputStream::ExpectTagFromArray( - buffer, tag)) != NULL) { + (buffer = io::CodedInputStream::ExpectTagFromArray(buffer, tag)) != + NULL) { buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value); values->AddAlreadyReserved(value); ++num_read; @@ -1145,18 +1120,16 @@ inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( // Specializations of ReadRepeatedPrimitive for the fixed size types, which use // the optimized code path. -#define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ -template <> \ -inline bool WireFormatLite::ReadRepeatedPrimitive< \ - CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ - int tag_size, \ - uint32 tag, \ - io::CodedInputStream* input, \ - RepeatedField<CPPTYPE>* values) { \ - return ReadRepeatedFixedSizePrimitive< \ - CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ - tag_size, tag, input, values); \ -} +#define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ + template <> \ + inline bool WireFormatLite::ReadRepeatedPrimitive< \ + CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ + int tag_size, uint32 tag, io::CodedInputStream* input, \ + RepeatedField<CPPTYPE>* values) { \ + return ReadRepeatedFixedSizePrimitive<CPPTYPE, \ + WireFormatLite::DECLARED_TYPE>( \ + tag_size, tag, input, values); \ + } READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32) READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64) @@ -1169,12 +1142,10 @@ READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE) template <typename CType, enum WireFormatLite::FieldType DeclaredType> bool WireFormatLite::ReadRepeatedPrimitiveNoInline( - int tag_size, - uint32 tag, - io::CodedInputStream* input, + int tag_size, uint32 tag, io::CodedInputStream* input, RepeatedField<CType>* value) { - return ReadRepeatedPrimitive<CType, DeclaredType>( - tag_size, tag, input, value); + return ReadRepeatedPrimitive<CType, DeclaredType>(tag_size, tag, input, + value); } template <typename CType, enum WireFormatLite::FieldType DeclaredType> @@ -1255,14 +1226,14 @@ inline bool WireFormatLite::ReadPackedFixedSizePrimitive( // Specializations of ReadPackedPrimitive for the fixed size types, which use // an optimized code path. #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ -template <> \ -inline bool WireFormatLite::ReadPackedPrimitive< \ - CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ - io::CodedInputStream* input, \ - RepeatedField<CPPTYPE>* values) { \ - return ReadPackedFixedSizePrimitive< \ - CPPTYPE, WireFormatLite::DECLARED_TYPE>(input, values); \ -} + template <> \ + inline bool \ + WireFormatLite::ReadPackedPrimitive<CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ + io::CodedInputStream * input, RepeatedField<CPPTYPE> * values) { \ + return ReadPackedFixedSizePrimitive<CPPTYPE, \ + WireFormatLite::DECLARED_TYPE>( \ + input, values); \ + } READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32) READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64) @@ -1280,10 +1251,10 @@ bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input, } -template<typename MessageType> -inline bool WireFormatLite::ReadGroup( - int field_number, io::CodedInputStream* input, - MessageType* value) { +template <typename MessageType> +inline bool WireFormatLite::ReadGroup(int field_number, + io::CodedInputStream* input, + MessageType* value) { if (!input->IncrementRecursionDepth()) return false; if (!value->MergePartialFromCodedStream(input)) return false; input->UnsafeDecrementRecursionDepth(); @@ -1293,9 +1264,9 @@ inline bool WireFormatLite::ReadGroup( } return true; } -template<typename MessageType> -inline bool WireFormatLite::ReadMessage( - io::CodedInputStream* input, MessageType* value) { +template <typename MessageType> +inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input, + MessageType* value) { int length; if (!input->ReadVarintSizeAsInt(&length)) return false; std::pair<io::CodedInputStream::Limit, int> p = @@ -1372,7 +1343,7 @@ inline void WireFormatLite::WriteEnumNoTag(int value, // See comment on ReadGroupNoVirtual to understand the need for this template // parameter name. -template<typename MessageType_WorkAroundCppLookupDefect> +template <typename MessageType_WorkAroundCppLookupDefect> inline void WireFormatLite::WriteGroupNoVirtual( int field_number, const MessageType_WorkAroundCppLookupDefect& value, io::CodedOutputStream* output) { @@ -1380,7 +1351,7 @@ inline void WireFormatLite::WriteGroupNoVirtual( value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output); WriteTag(field_number, WIRETYPE_END_GROUP, output); } -template<typename MessageType_WorkAroundCppLookupDefect> +template <typename MessageType_WorkAroundCppLookupDefect> inline void WireFormatLite::WriteMessageNoVirtual( int field_number, const MessageType_WorkAroundCppLookupDefect& value, io::CodedOutputStream* output) { @@ -1392,8 +1363,7 @@ inline void WireFormatLite::WriteMessageNoVirtual( // =================================================================== -inline uint8* WireFormatLite::WriteTagToArray(int field_number, - WireType type, +inline uint8* WireFormatLite::WriteTagToArray(int field_number, WireType type, uint8* target) { return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type), target); @@ -1405,8 +1375,8 @@ inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value, } inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value, uint8* target) { - return io::CodedOutputStream::WriteVarint64ToArray( - static_cast<uint64>(value), target); + return io::CodedOutputStream::WriteVarint64ToArray(static_cast<uint64>(value), + target); } inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value, uint8* target) { @@ -1454,19 +1424,16 @@ inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value, return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value), target); } -inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value, - uint8* target) { +inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value, uint8* target) { return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target); } -inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value, - uint8* target) { +inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value, uint8* target) { return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target); } -template<typename T> +template <typename T> inline uint8* WireFormatLite::WritePrimitiveNoTagToArray( - const RepeatedField<T>& value, - uint8* (*Writer)(T, uint8*), uint8* target) { + const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*), uint8* target) { const int n = value.size(); GOOGLE_DCHECK_GT(n, 0); @@ -1479,12 +1446,11 @@ inline uint8* WireFormatLite::WritePrimitiveNoTagToArray( return target; } -template<typename T> +template <typename T> inline uint8* WireFormatLite::WriteFixedNoTagToArray( - const RepeatedField<T>& value, - uint8* (*Writer)(T, uint8*), uint8* target) { + const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*), uint8* target) { #if defined(PROTOBUF_LITTLE_ENDIAN) - (void) Writer; + (void)Writer; const int n = value.size(); GOOGLE_DCHECK_GT(n, 0); @@ -1499,11 +1465,11 @@ inline uint8* WireFormatLite::WriteFixedNoTagToArray( } inline uint8* WireFormatLite::WriteInt32NoTagToArray( - const RepeatedField< int32>& value, uint8* target) { + const RepeatedField<int32>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteInt32NoTagToArray, target); } inline uint8* WireFormatLite::WriteInt64NoTagToArray( - const RepeatedField< int64>& value, uint8* target) { + const RepeatedField<int64>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteInt64NoTagToArray, target); } inline uint8* WireFormatLite::WriteUInt32NoTagToArray( @@ -1515,11 +1481,11 @@ inline uint8* WireFormatLite::WriteUInt64NoTagToArray( return WritePrimitiveNoTagToArray(value, WriteUInt64NoTagToArray, target); } inline uint8* WireFormatLite::WriteSInt32NoTagToArray( - const RepeatedField< int32>& value, uint8* target) { + const RepeatedField<int32>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteSInt32NoTagToArray, target); } inline uint8* WireFormatLite::WriteSInt64NoTagToArray( - const RepeatedField< int64>& value, uint8* target) { + const RepeatedField<int64>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteSInt64NoTagToArray, target); } inline uint8* WireFormatLite::WriteFixed32NoTagToArray( @@ -1531,15 +1497,15 @@ inline uint8* WireFormatLite::WriteFixed64NoTagToArray( return WriteFixedNoTagToArray(value, WriteFixed64NoTagToArray, target); } inline uint8* WireFormatLite::WriteSFixed32NoTagToArray( - const RepeatedField< int32>& value, uint8* target) { + const RepeatedField<int32>& value, uint8* target) { return WriteFixedNoTagToArray(value, WriteSFixed32NoTagToArray, target); } inline uint8* WireFormatLite::WriteSFixed64NoTagToArray( - const RepeatedField< int64>& value, uint8* target) { + const RepeatedField<int64>& value, uint8* target) { return WriteFixedNoTagToArray(value, WriteSFixed64NoTagToArray, target); } inline uint8* WireFormatLite::WriteFloatNoTagToArray( - const RepeatedField< float>& value, uint8* target) { + const RepeatedField<float>& value, uint8* target) { return WriteFixedNoTagToArray(value, WriteFloatNoTagToArray, target); } inline uint8* WireFormatLite::WriteDoubleNoTagToArray( @@ -1547,103 +1513,88 @@ inline uint8* WireFormatLite::WriteDoubleNoTagToArray( return WriteFixedNoTagToArray(value, WriteDoubleNoTagToArray, target); } inline uint8* WireFormatLite::WriteBoolNoTagToArray( - const RepeatedField< bool>& value, uint8* target) { + const RepeatedField<bool>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteBoolNoTagToArray, target); } inline uint8* WireFormatLite::WriteEnumNoTagToArray( - const RepeatedField< int>& value, uint8* target) { + const RepeatedField<int>& value, uint8* target) { return WritePrimitiveNoTagToArray(value, WriteEnumNoTagToArray, target); } -inline uint8* WireFormatLite::WriteInt32ToArray(int field_number, - int32 value, +inline uint8* WireFormatLite::WriteInt32ToArray(int field_number, int32 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteInt32NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteInt64ToArray(int field_number, - int64 value, +inline uint8* WireFormatLite::WriteInt64ToArray(int field_number, int64 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteInt64NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number, - uint32 value, +inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number, uint32 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteUInt32NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number, - uint64 value, +inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number, uint64 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteUInt64NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number, - int32 value, +inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number, int32 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteSInt32NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number, - int64 value, +inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number, int64 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteSInt64NoTagToArray(value, target); } inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number, - uint32 value, - uint8* target) { + uint32 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); return WriteFixed32NoTagToArray(value, target); } inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number, - uint64 value, - uint8* target) { + uint64 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); return WriteFixed64NoTagToArray(value, target); } inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number, - int32 value, - uint8* target) { + int32 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); return WriteSFixed32NoTagToArray(value, target); } inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number, - int64 value, - uint8* target) { + int64 value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); return WriteSFixed64NoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteFloatToArray(int field_number, - float value, +inline uint8* WireFormatLite::WriteFloatToArray(int field_number, float value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); return WriteFloatNoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteDoubleToArray(int field_number, - double value, +inline uint8* WireFormatLite::WriteDoubleToArray(int field_number, double value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); return WriteDoubleNoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteBoolToArray(int field_number, - bool value, +inline uint8* WireFormatLite::WriteBoolToArray(int field_number, bool value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteBoolNoTagToArray(value, target); } -inline uint8* WireFormatLite::WriteEnumToArray(int field_number, - int value, +inline uint8* WireFormatLite::WriteEnumToArray(int field_number, int value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); return WriteEnumNoTagToArray(value, target); } -template<typename T> +template <typename T> inline uint8* WireFormatLite::WritePrimitiveToArray( - int field_number, - const RepeatedField<T>& value, + int field_number, const RepeatedField<T>& value, uint8* (*Writer)(int, T, uint8*), uint8* target) { const int n = value.size(); if (n == 0) { @@ -1660,11 +1611,11 @@ inline uint8* WireFormatLite::WritePrimitiveToArray( } inline uint8* WireFormatLite::WriteInt32ToArray( - int field_number, const RepeatedField< int32>& value, uint8* target) { + int field_number, const RepeatedField<int32>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteInt32ToArray, target); } inline uint8* WireFormatLite::WriteInt64ToArray( - int field_number, const RepeatedField< int64>& value, uint8* target) { + int field_number, const RepeatedField<int64>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteInt64ToArray, target); } inline uint8* WireFormatLite::WriteUInt32ToArray( @@ -1676,47 +1627,49 @@ inline uint8* WireFormatLite::WriteUInt64ToArray( return WritePrimitiveToArray(field_number, value, WriteUInt64ToArray, target); } inline uint8* WireFormatLite::WriteSInt32ToArray( - int field_number, const RepeatedField< int32>& value, uint8* target) { + int field_number, const RepeatedField<int32>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteSInt32ToArray, target); } inline uint8* WireFormatLite::WriteSInt64ToArray( - int field_number, const RepeatedField< int64>& value, uint8* target) { + int field_number, const RepeatedField<int64>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteSInt64ToArray, target); } inline uint8* WireFormatLite::WriteFixed32ToArray( int field_number, const RepeatedField<uint32>& value, uint8* target) { - return WritePrimitiveToArray( - field_number, value, WriteFixed32ToArray, target); + return WritePrimitiveToArray(field_number, value, WriteFixed32ToArray, + target); } inline uint8* WireFormatLite::WriteFixed64ToArray( int field_number, const RepeatedField<uint64>& value, uint8* target) { - return WritePrimitiveToArray( - field_number, value, WriteFixed64ToArray, target); + return WritePrimitiveToArray(field_number, value, WriteFixed64ToArray, + target); } inline uint8* WireFormatLite::WriteSFixed32ToArray( - int field_number, const RepeatedField< int32>& value, uint8* target) { - return WritePrimitiveToArray( - field_number, value, WriteSFixed32ToArray, target); + int field_number, const RepeatedField<int32>& value, uint8* target) { + return WritePrimitiveToArray(field_number, value, WriteSFixed32ToArray, + target); } inline uint8* WireFormatLite::WriteSFixed64ToArray( - int field_number, const RepeatedField< int64>& value, uint8* target) { - return WritePrimitiveToArray( - field_number, value, WriteSFixed64ToArray, target); + int field_number, const RepeatedField<int64>& value, uint8* target) { + return WritePrimitiveToArray(field_number, value, WriteSFixed64ToArray, + target); } inline uint8* WireFormatLite::WriteFloatToArray( - int field_number, const RepeatedField< float>& value, uint8* target) { + int field_number, const RepeatedField<float>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteFloatToArray, target); } inline uint8* WireFormatLite::WriteDoubleToArray( int field_number, const RepeatedField<double>& value, uint8* target) { return WritePrimitiveToArray(field_number, value, WriteDoubleToArray, target); } -inline uint8* WireFormatLite::WriteBoolToArray( - int field_number, const RepeatedField< bool>& value, uint8* target) { +inline uint8* WireFormatLite::WriteBoolToArray(int field_number, + const RepeatedField<bool>& value, + uint8* target) { return WritePrimitiveToArray(field_number, value, WriteBoolToArray, target); } -inline uint8* WireFormatLite::WriteEnumToArray( - int field_number, const RepeatedField< int>& value, uint8* target) { +inline uint8* WireFormatLite::WriteEnumToArray(int field_number, + const RepeatedField<int>& value, + uint8* target) { return WritePrimitiveToArray(field_number, value, WriteEnumToArray, target); } inline uint8* WireFormatLite::WriteStringToArray(int field_number, @@ -1749,7 +1702,7 @@ inline uint8* WireFormatLite::InternalWriteMessageToArray( int field_number, const MessageType& value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); target = io::CodedOutputStream::WriteVarint32ToArray( - static_cast<uint32>(value.GetCachedSize()), target); + static_cast<uint32>(value.GetCachedSize()), target); return value.InternalSerializeWithCachedSizesToArray(target); } @@ -1770,9 +1723,9 @@ inline uint8* WireFormatLite::InternalWriteMessageNoVirtualToArray( uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); target = io::CodedOutputStream::WriteVarint32ToArray( - static_cast<uint32>( - value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()), - target); + static_cast<uint32>( + value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()), + target); return value.MessageType_WorkAroundCppLookupDefect:: InternalSerializeWithCachedSizesToArray(target); } @@ -1809,23 +1762,23 @@ inline size_t WireFormatLite::BytesSize(const std::string& value) { } -template<typename MessageType> +template <typename MessageType> inline size_t WireFormatLite::GroupSize(const MessageType& value) { return value.ByteSizeLong(); } -template<typename MessageType> +template <typename MessageType> inline size_t WireFormatLite::MessageSize(const MessageType& value) { return LengthDelimitedSize(value.ByteSizeLong()); } // See comment on ReadGroupNoVirtual to understand the need for this template // parameter name. -template<typename MessageType_WorkAroundCppLookupDefect> +template <typename MessageType_WorkAroundCppLookupDefect> inline size_t WireFormatLite::GroupSizeNoVirtual( const MessageType_WorkAroundCppLookupDefect& value) { return value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong(); } -template<typename MessageType_WorkAroundCppLookupDefect> +template <typename MessageType_WorkAroundCppLookupDefect> inline size_t WireFormatLite::MessageSizeNoVirtual( const MessageType_WorkAroundCppLookupDefect& value) { return LengthDelimitedSize( @@ -1837,8 +1790,8 @@ inline size_t WireFormatLite::LengthDelimitedSize(size_t length) { // but is not technically correct--if length is too large to fit in a uint32 // then it will be silently truncated. We will need to fix this if we ever // decide to start supporting serialized messages greater than 2 GiB in size. - return length + io::CodedOutputStream::VarintSize32( - static_cast<uint32>(length)); + return length + + io::CodedOutputStream::VarintSize32(static_cast<uint32>(length)); } template <typename MS> @@ -1885,8 +1838,8 @@ bool ParseMessageSetItemImpl(io::CodedInputStream* input, MS ms) { uint32 length; if (!input->ReadVarint32(&length)) return false; if (static_cast<int32>(length) < 0) return false; - uint32 size = static_cast<uint32>(length + - io::CodedOutputStream::VarintSize32(length)); + uint32 size = static_cast<uint32>( + length + io::CodedOutputStream::VarintSize32(length)); message_data.resize(size); auto ptr = reinterpret_cast<uint8*>(&message_data[0]); ptr = io::CodedOutputStream::WriteVarint32ToArray(length, ptr); diff --git a/src/google/protobuf/wire_format_unittest.cc b/src/google/protobuf/wire_format_unittest.cc index 762aa66150..cf0ccdcf04 100644 --- a/src/google/protobuf/wire_format_unittest.cc +++ b/src/google/protobuf/wire_format_unittest.cc @@ -33,14 +33,14 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include <google/protobuf/wire_format.h> -#include <google/protobuf/descriptor.h> -#include <google/protobuf/io/zero_copy_stream_impl.h> -#include <google/protobuf/io/coded_stream.h> +#include <google/protobuf/test_util.h> #include <google/protobuf/unittest.pb.h> -#include <google/protobuf/unittest_proto3_arena.pb.h> #include <google/protobuf/unittest_mset.pb.h> #include <google/protobuf/unittest_mset_wire_format.pb.h> -#include <google/protobuf/test_util.h> +#include <google/protobuf/unittest_proto3_arena.pb.h> +#include <google/protobuf/io/coded_stream.h> +#include <google/protobuf/io/zero_copy_stream_impl.h> +#include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> @@ -232,8 +232,7 @@ TEST(WireFormatTest, ByteSizeExtensions) { unittest::TestAllExtensions message; TestUtil::SetAllExtensions(&message); - EXPECT_EQ(message.ByteSize(), - WireFormat::ByteSize(message)); + EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); message.Clear(); EXPECT_EQ(0, message.ByteSize()); EXPECT_EQ(0, WireFormat::ByteSize(message)); @@ -253,8 +252,7 @@ TEST(WireFormatTest, ByteSizePackedExtensions) { unittest::TestPackedExtensions message; TestUtil::SetPackedExtensions(&message); - EXPECT_EQ(message.ByteSize(), - WireFormat::ByteSize(message)); + EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); message.Clear(); EXPECT_EQ(0, message.ByteSize()); EXPECT_EQ(0, WireFormat::ByteSize(message)); @@ -264,8 +262,7 @@ TEST(WireFormatTest, ByteSizeOneof) { unittest::TestOneof2 message; TestUtil::SetOneof1(&message); - EXPECT_EQ(message.ByteSize(), - WireFormat::ByteSize(message)); + EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); message.Clear(); EXPECT_EQ(0, message.ByteSize()); @@ -425,12 +422,16 @@ const int kUnknownTypeId = 1550055; TEST(WireFormatTest, SerializeMessageSet) { // Set up a TestMessageSet with two known messages and an unknown one. proto2_wireformat_unittest::TestMessageSet message_set; - message_set.MutableExtension( - unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); - message_set.MutableExtension( - unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); - message_set.mutable_unknown_fields()->AddLengthDelimited( - kUnknownTypeId, "bar"); + message_set + .MutableExtension( + unittest::TestMessageSetExtension1::message_set_extension) + ->set_i(123); + message_set + .MutableExtension( + unittest::TestMessageSetExtension2::message_set_extension) + ->set_str("foo"); + message_set.mutable_unknown_fields()->AddLengthDelimited(kUnknownTypeId, + "bar"); std::string data; ASSERT_TRUE(message_set.SerializeToString(&data)); @@ -443,11 +444,11 @@ TEST(WireFormatTest, SerializeMessageSet) { ASSERT_EQ(3, raw.item_size()); EXPECT_EQ( - unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(), - raw.item(0).type_id()); + unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(), + raw.item(0).type_id()); EXPECT_EQ( - unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(), - raw.item(1).type_id()); + unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(), + raw.item(1).type_id()); EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id()); unittest::TestMessageSetExtension1 message1; @@ -468,12 +469,16 @@ TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) { // above. proto2_wireformat_unittest::TestMessageSet message_set; - message_set.MutableExtension( - unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); - message_set.MutableExtension( - unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); - message_set.mutable_unknown_fields()->AddLengthDelimited( - kUnknownTypeId, "bar"); + message_set + .MutableExtension( + unittest::TestMessageSetExtension1::message_set_extension) + ->set_i(123); + message_set + .MutableExtension( + unittest::TestMessageSetExtension2::message_set_extension) + ->set_str("foo"); + message_set.mutable_unknown_fields()->AddLengthDelimited(kUnknownTypeId, + "bar"); int size = message_set.ByteSize(); EXPECT_EQ(size, message_set.GetCachedSize()); @@ -519,8 +524,9 @@ TEST(WireFormatTest, ParseMessageSet) { { unittest::RawMessageSet::Item* item = raw.add_item(); - item->set_type_id( - unittest::TestMessageSetExtension1::descriptor()->extension(0)->number()); + item->set_type_id(unittest::TestMessageSetExtension1::descriptor() + ->extension(0) + ->number()); unittest::TestMessageSetExtension1 message; message.set_i(123); message.SerializeToString(item->mutable_message()); @@ -528,8 +534,9 @@ TEST(WireFormatTest, ParseMessageSet) { { unittest::RawMessageSet::Item* item = raw.add_item(); - item->set_type_id( - unittest::TestMessageSetExtension2::descriptor()->extension(0)->number()); + item->set_type_id(unittest::TestMessageSetExtension2::descriptor() + ->extension(0) + ->number()); unittest::TestMessageSetExtension2 message; message.set_str("foo"); message.SerializeToString(item->mutable_message()); @@ -548,10 +555,16 @@ TEST(WireFormatTest, ParseMessageSet) { proto2_wireformat_unittest::TestMessageSet message_set; ASSERT_TRUE(message_set.ParseFromString(data)); - EXPECT_EQ(123, message_set.GetExtension( - unittest::TestMessageSetExtension1::message_set_extension).i()); - EXPECT_EQ("foo", message_set.GetExtension( - unittest::TestMessageSetExtension2::message_set_extension).str()); + EXPECT_EQ(123, + message_set + .GetExtension( + unittest::TestMessageSetExtension1::message_set_extension) + .i()); + EXPECT_EQ("foo", + message_set + .GetExtension( + unittest::TestMessageSetExtension2::message_set_extension) + .str()); ASSERT_EQ(1, message_set.unknown_fields().field_count()); ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, @@ -592,8 +605,11 @@ TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { proto2_wireformat_unittest::TestMessageSet message_set; ASSERT_TRUE(message_set.ParseFromString(data)); - EXPECT_EQ(123, message_set.GetExtension( - unittest::TestMessageSetExtension1::message_set_extension).i()); + EXPECT_EQ(123, + message_set + .GetExtension( + unittest::TestMessageSetExtension1::message_set_extension) + .i()); } { // Test parse the message via Reflection. @@ -603,8 +619,11 @@ TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set)); EXPECT_TRUE(input.ConsumedEntireMessage()); - EXPECT_EQ(123, message_set.GetExtension( - unittest::TestMessageSetExtension1::message_set_extension).i()); + EXPECT_EQ(123, + message_set + .GetExtension( + unittest::TestMessageSetExtension1::message_set_extension) + .i()); } } @@ -699,11 +718,11 @@ TEST(WireFormatTest, RecursionLimit) { TEST(WireFormatTest, UnknownFieldRecursionLimit) { unittest::TestEmptyMessage message; message.mutable_unknown_fields() - ->AddGroup(1234) - ->AddGroup(1234) - ->AddGroup(1234) - ->AddGroup(1234) - ->AddVarint(1234, 123); + ->AddGroup(1234) + ->AddGroup(1234) + ->AddGroup(1234) + ->AddGroup(1234) + ->AddVarint(1234, 123); std::string data; message.SerializeToString(&data); @@ -733,27 +752,27 @@ TEST(WireFormatTest, ZigZag) { #define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x) #define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x) - EXPECT_EQ(0u, ZigZagEncode32( 0)); + EXPECT_EQ(0u, ZigZagEncode32(0)); EXPECT_EQ(1u, ZigZagEncode32(-1)); - EXPECT_EQ(2u, ZigZagEncode32( 1)); + EXPECT_EQ(2u, ZigZagEncode32(1)); EXPECT_EQ(3u, ZigZagEncode32(-2)); EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF)); EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000)); EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF)); EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000)); - EXPECT_EQ( 0, ZigZagDecode32(0u)); + EXPECT_EQ(0, ZigZagDecode32(0u)); EXPECT_EQ(-1, ZigZagDecode32(1u)); - EXPECT_EQ( 1, ZigZagDecode32(2u)); + EXPECT_EQ(1, ZigZagDecode32(2u)); EXPECT_EQ(-2, ZigZagDecode32(3u)); EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu)); EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu)); EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu)); EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu)); - EXPECT_EQ(0u, ZigZagEncode64( 0)); + EXPECT_EQ(0u, ZigZagEncode64(0)); EXPECT_EQ(1u, ZigZagEncode64(-1)); - EXPECT_EQ(2u, ZigZagEncode64( 1)); + EXPECT_EQ(2u, ZigZagEncode64(1)); EXPECT_EQ(3u, ZigZagEncode64(-2)); EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF))); EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000))); @@ -762,9 +781,9 @@ TEST(WireFormatTest, ZigZag) { EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF))); EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000))); - EXPECT_EQ( 0, ZigZagDecode64(0u)); + EXPECT_EQ(0, ZigZagDecode64(0u)); EXPECT_EQ(-1, ZigZagDecode64(1u)); - EXPECT_EQ( 1, ZigZagDecode64(2u)); + EXPECT_EQ(1, ZigZagDecode64(2u)); EXPECT_EQ(-2, ZigZagDecode64(3u)); EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE))); EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF))); @@ -775,22 +794,22 @@ TEST(WireFormatTest, ZigZag) { // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1) // were chosen semi-randomly via keyboard bashing. - EXPECT_EQ( 0, ZigZagDecode32(ZigZagEncode32( 0))); - EXPECT_EQ( 1, ZigZagDecode32(ZigZagEncode32( 1))); - EXPECT_EQ( -1, ZigZagDecode32(ZigZagEncode32( -1))); + EXPECT_EQ(0, ZigZagDecode32(ZigZagEncode32(0))); + EXPECT_EQ(1, ZigZagDecode32(ZigZagEncode32(1))); + EXPECT_EQ(-1, ZigZagDecode32(ZigZagEncode32(-1))); EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927))); EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612))); - EXPECT_EQ( 0, ZigZagDecode64(ZigZagEncode64( 0))); - EXPECT_EQ( 1, ZigZagDecode64(ZigZagEncode64( 1))); - EXPECT_EQ( -1, ZigZagDecode64(ZigZagEncode64( -1))); + EXPECT_EQ(0, ZigZagDecode64(ZigZagEncode64(0))); + EXPECT_EQ(1, ZigZagDecode64(ZigZagEncode64(1))); + EXPECT_EQ(-1, ZigZagDecode64(ZigZagEncode64(-1))); EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927))); EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612))); - EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64( - LL(856912304801416)))); - EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64( - LL(-75123905439571256)))); + EXPECT_EQ(LL(856912304801416), + ZigZagDecode64(ZigZagEncode64(LL(856912304801416)))); + EXPECT_EQ(LL(-75123905439571256), + ZigZagDecode64(ZigZagEncode64(LL(-75123905439571256)))); } TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) { @@ -1029,8 +1048,8 @@ class WireFormatInvalidInputTest : public testing::Test { // contains exactly the given bytes, which may be invalid. std::string MakeInvalidEmbeddedMessage(const char* bytes, int size) { const FieldDescriptor* field = - unittest::TestAllTypes::descriptor()->FindFieldByName( - "optional_nested_message"); + unittest::TestAllTypes::descriptor()->FindFieldByName( + "optional_nested_message"); GOOGLE_CHECK(field != NULL); std::string result; @@ -1052,8 +1071,7 @@ class WireFormatInvalidInputTest : public testing::Test { std::string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) { const FieldDescriptor* field = - unittest::TestAllTypes::descriptor()->FindFieldByName( - "optionalgroup"); + unittest::TestAllTypes::descriptor()->FindFieldByName("optionalgroup"); GOOGLE_CHECK(field != NULL); std::string result; @@ -1066,7 +1084,7 @@ class WireFormatInvalidInputTest : public testing::Test { output.WriteString(std::string(bytes, size)); if (include_end_tag) { output.WriteVarint32(WireFormatLite::MakeTag( - field->number(), WireFormatLite::WIRETYPE_END_GROUP)); + field->number(), WireFormatLite::WIRETYPE_END_GROUP)); } } @@ -1115,7 +1133,6 @@ TEST_F(WireFormatInvalidInputTest, InvalidMessageWithExtraZero) { } } - TEST_F(WireFormatInvalidInputTest, InvalidGroup) { unittest::TestAllTypes message; @@ -1191,10 +1208,10 @@ TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) { // ReadValidUTF8String: fine. // WriteAnyBytes: fine. // ReadAnyBytes: fine. -const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0"; +const char* kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0"; // This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to // interpret \u differently from GCC. -const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214"; +const char* kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214"; template <typename T> bool WriteMessage(const char* value, T* message, std::string* wire_buffer) { diff --git a/src/google/protobuf/wrappers.pb.cc b/src/google/protobuf/wrappers.pb.cc index c7277d9c20..bedf4e1d8a 100644 --- a/src/google/protobuf/wrappers.pb.cc +++ b/src/google/protobuf/wrappers.pb.cc @@ -180,18 +180,6 @@ static void InitDefaultsBytesValue_google_2fprotobuf_2fwrappers_2eproto() { PROTOBUF_EXPORT ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_BytesValue_google_2fprotobuf_2fwrappers_2eproto = {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsBytesValue_google_2fprotobuf_2fwrappers_2eproto}, {}}; -void InitDefaults_google_2fprotobuf_2fwrappers_2eproto() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DoubleValue_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FloatValue_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Int64Value_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UInt64Value_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Int32Value_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UInt32Value_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_BoolValue_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_StringValue_google_2fprotobuf_2fwrappers_2eproto.base); - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_BytesValue_google_2fprotobuf_2fwrappers_2eproto.base); -} - static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_google_2fprotobuf_2fwrappers_2eproto[9]; static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_google_2fprotobuf_2fwrappers_2eproto = nullptr; static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_google_2fprotobuf_2fwrappers_2eproto = nullptr; @@ -297,8 +285,7 @@ const char descriptor_table_protodef_google_2fprotobuf_2fwrappers_2eproto[] = "\006proto3" ; static ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fwrappers_2eproto = { - false, InitDefaults_google_2fprotobuf_2fwrappers_2eproto, - descriptor_table_protodef_google_2fprotobuf_2fwrappers_2eproto, + false, descriptor_table_protodef_google_2fprotobuf_2fwrappers_2eproto, "google/protobuf/wrappers.proto", &assign_descriptors_table_google_2fprotobuf_2fwrappers_2eproto, 447, }; @@ -306,6 +293,15 @@ void AddDescriptors_google_2fprotobuf_2fwrappers_2eproto() { static constexpr ::PROTOBUF_NAMESPACE_ID::internal::InitFunc deps[1] = { }; + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_DoubleValue_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_FloatValue_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Int64Value_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UInt64Value_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_Int32Value_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_UInt32Value_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_BoolValue_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_StringValue_google_2fprotobuf_2fwrappers_2eproto.base); + ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_BytesValue_google_2fprotobuf_2fwrappers_2eproto.base); ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_google_2fprotobuf_2fwrappers_2eproto, deps, 0); } @@ -385,16 +381,17 @@ void DoubleValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* DoubleValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // double value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 9) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<double>(ptr)); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<double>(ptr); ptr += sizeof(double); break; } @@ -402,15 +399,20 @@ const char* DoubleValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool DoubleValue::MergePartialFromCodedStream( @@ -665,16 +667,17 @@ void FloatValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* FloatValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // float value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 13) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<float>(ptr)); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad<float>(ptr); ptr += sizeof(float); break; } @@ -682,15 +685,20 @@ const char* FloatValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID: handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool FloatValue::MergePartialFromCodedStream( @@ -945,32 +953,38 @@ void Int64Value::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Int64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // int64 value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Int64Value::MergePartialFromCodedStream( @@ -1227,32 +1241,38 @@ void UInt64Value::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* UInt64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // uint64 value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UInt64Value::MergePartialFromCodedStream( @@ -1509,32 +1529,38 @@ void Int32Value::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* Int32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // int32 value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool Int32Value::MergePartialFromCodedStream( @@ -1791,32 +1817,38 @@ void UInt32Value::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* UInt32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // uint32 value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool UInt32Value::MergePartialFromCodedStream( @@ -2073,32 +2105,38 @@ void BoolValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* BoolValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // bool value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 8) goto handle_unusual; - set_value(::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr)); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + value_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool BoolValue::MergePartialFromCodedStream( @@ -2360,32 +2398,38 @@ void StringValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* StringValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // string value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParserUTF8(mutable_value(), ptr, ctx, "google.protobuf.StringValue.value"); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool StringValue::MergePartialFromCodedStream( @@ -2663,32 +2707,38 @@ void BytesValue::Clear() { #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER const char* BytesValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { +#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure ::PROTOBUF_NAMESPACE_ID::Arena* arena = GetArenaNoVirtual(); (void)arena; while (!ctx->Done(&ptr)) { ::PROTOBUF_NAMESPACE_ID::uint32 tag; ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); switch (tag >> 3) { // bytes value = 1; case 1: { if (static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) != 10) goto handle_unusual; ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(mutable_value(), ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); + CHK_(ptr); break; } default: { handle_unusual: if ((tag & 7) == 4 || tag == 0) { ctx->SetLastTag(tag); - return ptr; + goto success; } ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); + CHK_(ptr != nullptr); break; } } // switch } // while +success: return ptr; +failure: + ptr = nullptr; + goto success; +#undef CHK_ } #else // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER bool BytesValue::MergePartialFromCodedStream(