Browse Source

Merge tag 'refs/tags/sync-piper' into sync-stage

Joshua Haberman 4 years ago
parent
commit
77334e39a7
100 changed files with 1232 additions and 738 deletions
  1. 0 1
      Makefile.am
  2. 21 0
      conformance/text_format_conformance_suite.cc
  3. BIN
      csharp/src/Google.Protobuf.Test/testprotos.pb
  4. 48 47
      csharp/src/Google.Protobuf/Reflection/Descriptor.cs
  5. 4 2
      java/core/src/main/java/com/google/protobuf/ByteString.java
  6. 6 1
      java/core/src/main/java/com/google/protobuf/RopeByteString.java
  7. 2 2
      java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java
  8. 1 1
      java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java
  9. 1 0
      java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java
  10. 2 2
      java/core/src/test/java/com/google/protobuf/MapForProto2Test.java
  11. 2 2
      java/core/src/test/java/com/google/protobuf/MapTest.java
  12. 4 2
      java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java
  13. 4 2
      java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java
  14. 0 56
      java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto
  15. 20 18
      java/lite/src/test/java/com/google/protobuf/LiteTest.java
  16. 55 0
      python/google/protobuf/internal/text_format_test.py
  17. 9 6
      python/google/protobuf/pyext/descriptor.cc
  18. 3 1
      python/google/protobuf/pyext/descriptor_database.cc
  19. 4 2
      python/google/protobuf/pyext/extension_dict.cc
  20. 4 3
      python/google/protobuf/pyext/map_container.cc
  21. 2 1
      python/google/protobuf/pyext/map_container.h
  22. 5 4
      python/google/protobuf/pyext/message.cc
  23. 5 4
      python/google/protobuf/pyext/message.h
  24. 5 4
      python/google/protobuf/pyext/repeated_scalar_container.cc
  25. 1 3
      python/google/protobuf/text_format.py
  26. 2 2
      src/google/protobuf/any.pb.cc
  27. 6 6
      src/google/protobuf/api.pb.cc
  28. 8 0
      src/google/protobuf/arenastring.cc
  29. 8 4
      src/google/protobuf/arenastring.h
  30. 3 2
      src/google/protobuf/compiler/annotation_test_util.cc
  31. 3 1
      src/google/protobuf/compiler/command_line_interface.cc
  32. 2 1
      src/google/protobuf/compiler/command_line_interface.h
  33. 3 1
      src/google/protobuf/compiler/command_line_interface_unittest.cc
  34. 7 3
      src/google/protobuf/compiler/cpp/cpp_enum.cc
  35. 3 1
      src/google/protobuf/compiler/cpp/cpp_field.cc
  36. 2 1
      src/google/protobuf/compiler/cpp/cpp_field.h
  37. 21 19
      src/google/protobuf/compiler/cpp/cpp_helpers.cc
  38. 3 4
      src/google/protobuf/compiler/cpp/cpp_helpers.h
  39. 92 37
      src/google/protobuf/compiler/cpp/cpp_message.cc
  40. 3 1
      src/google/protobuf/compiler/cpp/cpp_message.h
  41. 5 0
      src/google/protobuf/compiler/cpp/cpp_names.h
  42. 18 14
      src/google/protobuf/compiler/cpp/cpp_primitive_field.cc
  43. 29 23
      src/google/protobuf/compiler/cpp/cpp_unittest.inc
  44. 4 2
      src/google/protobuf/compiler/java/java_enum_field.cc
  45. 7 5
      src/google/protobuf/compiler/java/java_enum_field_lite.cc
  46. 5 3
      src/google/protobuf/compiler/java/java_enum_field_lite.h
  47. 2 1
      src/google/protobuf/compiler/java/java_field.h
  48. 9 8
      src/google/protobuf/compiler/java/java_helpers.cc
  49. 7 4
      src/google/protobuf/compiler/java/java_helpers.h
  50. 3 1
      src/google/protobuf/compiler/java/java_map_field_lite.cc
  51. 3 1
      src/google/protobuf/compiler/java/java_map_field_lite.h
  52. 5 4
      src/google/protobuf/compiler/java/java_message.cc
  53. 6 4
      src/google/protobuf/compiler/java/java_message_field_lite.cc
  54. 5 3
      src/google/protobuf/compiler/java/java_message_field_lite.h
  55. 3 2
      src/google/protobuf/compiler/java/java_message_lite.cc
  56. 4 2
      src/google/protobuf/compiler/java/java_primitive_field.cc
  57. 5 4
      src/google/protobuf/compiler/java/java_primitive_field_lite.cc
  58. 5 3
      src/google/protobuf/compiler/java/java_primitive_field_lite.h
  59. 4 2
      src/google/protobuf/compiler/java/java_string_field.cc
  60. 7 5
      src/google/protobuf/compiler/java/java_string_field_lite.cc
  61. 5 3
      src/google/protobuf/compiler/java/java_string_field_lite.h
  62. 3 2
      src/google/protobuf/compiler/mock_code_generator.cc
  63. 3 2
      src/google/protobuf/compiler/mock_code_generator.h
  64. 30 23
      src/google/protobuf/compiler/parser.cc
  65. 7 3
      src/google/protobuf/compiler/parser.h
  66. 8 8
      src/google/protobuf/compiler/plugin.pb.cc
  67. 19 16
      src/google/protobuf/compiler/zip_writer.cc
  68. 5 3
      src/google/protobuf/compiler/zip_writer.h
  69. 107 106
      src/google/protobuf/descriptor.pb.cc
  70. 2 0
      src/google/protobuf/descriptor.proto
  71. 2 2
      src/google/protobuf/duration.pb.cc
  72. 2 2
      src/google/protobuf/empty.pb.cc
  73. 2 2
      src/google/protobuf/field_mask.pb.cc
  74. 20 0
      src/google/protobuf/io/coded_stream.cc
  75. 24 7
      src/google/protobuf/io/coded_stream.h
  76. 32 0
      src/google/protobuf/message_unittest.inc
  77. 12 0
      src/google/protobuf/parse_context.cc
  78. 14 6
      src/google/protobuf/parse_context.h
  79. 42 9
      src/google/protobuf/repeated_field.h
  80. 87 0
      src/google/protobuf/repeated_field_unittest.cc
  81. 2 2
      src/google/protobuf/source_context.pb.cc
  82. 6 6
      src/google/protobuf/struct.pb.cc
  83. 2 2
      src/google/protobuf/timestamp.pb.cc
  84. 10 10
      src/google/protobuf/type.pb.cc
  85. 30 0
      src/google/protobuf/unittest.proto
  86. 21 22
      src/google/protobuf/util/field_comparator.cc
  87. 48 21
      src/google/protobuf/util/field_comparator.h
  88. 5 2
      src/google/protobuf/util/field_mask_util.cc
  89. 2 1
      src/google/protobuf/util/field_mask_util.h
  90. 2 1
      src/google/protobuf/util/field_mask_util_test.cc
  91. 2 1
      src/google/protobuf/util/json_util_test.cc
  92. 30 13
      src/google/protobuf/util/message_differencer.cc
  93. 1 1
      src/google/protobuf/util/message_differencer.h
  94. 5 1
      src/google/protobuf/util/message_differencer_unittest.cc
  95. 65 62
      src/google/protobuf/util/time_util.cc
  96. 30 29
      src/google/protobuf/util/time_util.h
  97. 13 11
      src/google/protobuf/util/time_util_test.cc
  98. 3 2
      src/google/protobuf/util/type_resolver_util_test.cc
  99. 1 1
      src/google/protobuf/wire_format_lite.h
  100. 18 18
      src/google/protobuf/wrappers.pb.cc

+ 0 - 1
Makefile.am

@@ -529,7 +529,6 @@ java_EXTRA_DIST=
   java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto               \
   java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto               \
   java/core/src/test/proto/com/google/protobuf/test_check_utf8_size.proto          \
   java/core/src/test/proto/com/google/protobuf/test_check_utf8_size.proto          \
   java/core/src/test/proto/com/google/protobuf/test_custom_options.proto           \
   java/core/src/test/proto/com/google/protobuf/test_custom_options.proto           \
-  java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto         \
   java/core/src/test/proto/com/google/protobuf/wrappers_test.proto                 \
   java/core/src/test/proto/com/google/protobuf/wrappers_test.proto                 \
   java/lite.md                                                                     \
   java/lite.md                                                                     \
   java/lite/BUILD                                                                  \
   java/lite/BUILD                                                                  \

+ 21 - 0
conformance/text_format_conformance_suite.cc

@@ -451,6 +451,27 @@ void TextFormatConformanceTestSuite::RunSuiteImpl() {
       }
       }
       )",
       )",
                                     prototype);
                                     prototype);
+
+  prototype.Clear();
+  ConformanceRequestSetting setting_map(
+      REQUIRED, conformance::TEXT_FORMAT, conformance::PROTOBUF,
+      conformance::TEXT_FORMAT_TEST, prototype, "DuplicateMapKey", R"(
+      map_string_nested_message {
+        key: "duplicate"
+        value: { a: 123 }
+      }
+      map_string_nested_message {
+        key: "duplicate"
+        value: { corecursive: {} }
+      }
+      )");
+  // The last-specified value will be retained in a parsed map
+  RunValidInputTest(setting_map, R"(
+      map_string_nested_message {
+        key: "duplicate"
+        value: { corecursive: {} }
+      }
+      )");
 }
 }
 
 
 }  // namespace protobuf
 }  // namespace protobuf

BIN
csharp/src/Google.Protobuf.Test/testprotos.pb


+ 48 - 47
csharp/src/Google.Protobuf/Reflection/Descriptor.cs

@@ -107,57 +107,58 @@ namespace Google.Protobuf.Reflection {
             "eV9wYWNrYWdlGC0gASgJEkMKFHVuaW50ZXJwcmV0ZWRfb3B0aW9uGOcHIAMo",
             "eV9wYWNrYWdlGC0gASgJEkMKFHVuaW50ZXJwcmV0ZWRfb3B0aW9uGOcHIAMo",
             "CzIkLmdvb2dsZS5wcm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9uIjoKDE9w",
             "CzIkLmdvb2dsZS5wcm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9uIjoKDE9w",
             "dGltaXplTW9kZRIJCgVTUEVFRBABEg0KCUNPREVfU0laRRACEhAKDExJVEVf",
             "dGltaXplTW9kZRIJCgVTUEVFRBABEg0KCUNPREVfU0laRRACEhAKDExJVEVf",
-            "UlVOVElNRRADKgkI6AcQgICAgAJKBAgmECci8gEKDk1lc3NhZ2VPcHRpb25z",
+            "UlVOVElNRRADKgkI6AcQgICAgAJKBAgmECcihAIKDk1lc3NhZ2VPcHRpb25z",
             "EiYKF21lc3NhZ2Vfc2V0X3dpcmVfZm9ybWF0GAEgASgIOgVmYWxzZRIuCh9u",
             "EiYKF21lc3NhZ2Vfc2V0X3dpcmVfZm9ybWF0GAEgASgIOgVmYWxzZRIuCh9u",
             "b19zdGFuZGFyZF9kZXNjcmlwdG9yX2FjY2Vzc29yGAIgASgIOgVmYWxzZRIZ",
             "b19zdGFuZGFyZF9kZXNjcmlwdG9yX2FjY2Vzc29yGAIgASgIOgVmYWxzZRIZ",
             "CgpkZXByZWNhdGVkGAMgASgIOgVmYWxzZRIRCgltYXBfZW50cnkYByABKAgS",
             "CgpkZXByZWNhdGVkGAMgASgIOgVmYWxzZRIRCgltYXBfZW50cnkYByABKAgS",
             "QwoUdW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3Rv",
             "QwoUdW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3Rv",
-            "YnVmLlVuaW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICAAkoECAgQCUoECAkQ",
-            "CiKeAwoMRmllbGRPcHRpb25zEjoKBWN0eXBlGAEgASgOMiMuZ29vZ2xlLnBy",
-            "b3RvYnVmLkZpZWxkT3B0aW9ucy5DVHlwZToGU1RSSU5HEg4KBnBhY2tlZBgC",
-            "IAEoCBI/CgZqc3R5cGUYBiABKA4yJC5nb29nbGUucHJvdG9idWYuRmllbGRP",
-            "cHRpb25zLkpTVHlwZToJSlNfTk9STUFMEhMKBGxhenkYBSABKAg6BWZhbHNl",
-            "EhkKCmRlcHJlY2F0ZWQYAyABKAg6BWZhbHNlEhMKBHdlYWsYCiABKAg6BWZh",
-            "bHNlEkMKFHVuaW50ZXJwcmV0ZWRfb3B0aW9uGOcHIAMoCzIkLmdvb2dsZS5w",
-            "cm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9uIi8KBUNUeXBlEgoKBlNUUklO",
-            "RxAAEggKBENPUkQQARIQCgxTVFJJTkdfUElFQ0UQAiI1CgZKU1R5cGUSDQoJ",
-            "SlNfTk9STUFMEAASDQoJSlNfU1RSSU5HEAESDQoJSlNfTlVNQkVSEAIqCQjo",
-            "BxCAgICAAkoECAQQBSJeCgxPbmVvZk9wdGlvbnMSQwoUdW5pbnRlcnByZXRl",
-            "ZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3RvYnVmLlVuaW50ZXJwcmV0",
-            "ZWRPcHRpb24qCQjoBxCAgICAAiKTAQoLRW51bU9wdGlvbnMSEwoLYWxsb3df",
-            "YWxpYXMYAiABKAgSGQoKZGVwcmVjYXRlZBgDIAEoCDoFZmFsc2USQwoUdW5p",
-            "bnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3RvYnVmLlVu",
-            "aW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICAAkoECAUQBiJ9ChBFbnVtVmFs",
-            "dWVPcHRpb25zEhkKCmRlcHJlY2F0ZWQYASABKAg6BWZhbHNlEkMKFHVuaW50",
-            "ZXJwcmV0ZWRfb3B0aW9uGOcHIAMoCzIkLmdvb2dsZS5wcm90b2J1Zi5Vbmlu",
-            "dGVycHJldGVkT3B0aW9uKgkI6AcQgICAgAIiewoOU2VydmljZU9wdGlvbnMS",
-            "GQoKZGVwcmVjYXRlZBghIAEoCDoFZmFsc2USQwoUdW5pbnRlcnByZXRlZF9v",
-            "cHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3RvYnVmLlVuaW50ZXJwcmV0ZWRP",
-            "cHRpb24qCQjoBxCAgICAAiKtAgoNTWV0aG9kT3B0aW9ucxIZCgpkZXByZWNh",
-            "dGVkGCEgASgIOgVmYWxzZRJfChFpZGVtcG90ZW5jeV9sZXZlbBgiIAEoDjIv",
-            "Lmdvb2dsZS5wcm90b2J1Zi5NZXRob2RPcHRpb25zLklkZW1wb3RlbmN5TGV2",
-            "ZWw6E0lERU1QT1RFTkNZX1VOS05PV04SQwoUdW5pbnRlcnByZXRlZF9vcHRp",
-            "b24Y5wcgAygLMiQuZ29vZ2xlLnByb3RvYnVmLlVuaW50ZXJwcmV0ZWRPcHRp",
-            "b24iUAoQSWRlbXBvdGVuY3lMZXZlbBIXChNJREVNUE9URU5DWV9VTktOT1dO",
-            "EAASEwoPTk9fU0lERV9FRkZFQ1RTEAESDgoKSURFTVBPVEVOVBACKgkI6AcQ",
-            "gICAgAIingIKE1VuaW50ZXJwcmV0ZWRPcHRpb24SOwoEbmFtZRgCIAMoCzIt",
-            "Lmdvb2dsZS5wcm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9uLk5hbWVQYXJ0",
-            "EhgKEGlkZW50aWZpZXJfdmFsdWUYAyABKAkSGgoScG9zaXRpdmVfaW50X3Zh",
-            "bHVlGAQgASgEEhoKEm5lZ2F0aXZlX2ludF92YWx1ZRgFIAEoAxIUCgxkb3Vi",
-            "bGVfdmFsdWUYBiABKAESFAoMc3RyaW5nX3ZhbHVlGAcgASgMEhcKD2FnZ3Jl",
-            "Z2F0ZV92YWx1ZRgIIAEoCRozCghOYW1lUGFydBIRCgluYW1lX3BhcnQYASAC",
-            "KAkSFAoMaXNfZXh0ZW5zaW9uGAIgAigIItUBCg5Tb3VyY2VDb2RlSW5mbxI6",
-            "Cghsb2NhdGlvbhgBIAMoCzIoLmdvb2dsZS5wcm90b2J1Zi5Tb3VyY2VDb2Rl",
-            "SW5mby5Mb2NhdGlvbhqGAQoITG9jYXRpb24SEAoEcGF0aBgBIAMoBUICEAES",
-            "EAoEc3BhbhgCIAMoBUICEAESGAoQbGVhZGluZ19jb21tZW50cxgDIAEoCRIZ",
-            "ChF0cmFpbGluZ19jb21tZW50cxgEIAEoCRIhChlsZWFkaW5nX2RldGFjaGVk",
-            "X2NvbW1lbnRzGAYgAygJIqcBChFHZW5lcmF0ZWRDb2RlSW5mbxJBCgphbm5v",
-            "dGF0aW9uGAEgAygLMi0uZ29vZ2xlLnByb3RvYnVmLkdlbmVyYXRlZENvZGVJ",
-            "bmZvLkFubm90YXRpb24aTwoKQW5ub3RhdGlvbhIQCgRwYXRoGAEgAygFQgIQ",
-            "ARITCgtzb3VyY2VfZmlsZRgCIAEoCRINCgViZWdpbhgDIAEoBRILCgNlbmQY",
-            "BCABKAVCfgoTY29tLmdvb2dsZS5wcm90b2J1ZkIQRGVzY3JpcHRvclByb3Rv",
-            "c0gBWi1nb29nbGUuZ29sYW5nLm9yZy9wcm90b2J1Zi90eXBlcy9kZXNjcmlw",
-            "dG9ycGL4AQGiAgNHUEKqAhpHb29nbGUuUHJvdG9idWYuUmVmbGVjdGlvbg=="));
+            "YnVmLlVuaW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICAAkoECAQQBUoECAUQ",
+            "BkoECAYQB0oECAgQCUoECAkQCiKeAwoMRmllbGRPcHRpb25zEjoKBWN0eXBl",
+            "GAEgASgOMiMuZ29vZ2xlLnByb3RvYnVmLkZpZWxkT3B0aW9ucy5DVHlwZToG",
+            "U1RSSU5HEg4KBnBhY2tlZBgCIAEoCBI/CgZqc3R5cGUYBiABKA4yJC5nb29n",
+            "bGUucHJvdG9idWYuRmllbGRPcHRpb25zLkpTVHlwZToJSlNfTk9STUFMEhMK",
+            "BGxhenkYBSABKAg6BWZhbHNlEhkKCmRlcHJlY2F0ZWQYAyABKAg6BWZhbHNl",
+            "EhMKBHdlYWsYCiABKAg6BWZhbHNlEkMKFHVuaW50ZXJwcmV0ZWRfb3B0aW9u",
+            "GOcHIAMoCzIkLmdvb2dsZS5wcm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9u",
+            "Ii8KBUNUeXBlEgoKBlNUUklORxAAEggKBENPUkQQARIQCgxTVFJJTkdfUElF",
+            "Q0UQAiI1CgZKU1R5cGUSDQoJSlNfTk9STUFMEAASDQoJSlNfU1RSSU5HEAES",
+            "DQoJSlNfTlVNQkVSEAIqCQjoBxCAgICAAkoECAQQBSJeCgxPbmVvZk9wdGlv",
+            "bnMSQwoUdW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnBy",
+            "b3RvYnVmLlVuaW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICAAiKTAQoLRW51",
+            "bU9wdGlvbnMSEwoLYWxsb3dfYWxpYXMYAiABKAgSGQoKZGVwcmVjYXRlZBgD",
+            "IAEoCDoFZmFsc2USQwoUdW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQu",
+            "Z29vZ2xlLnByb3RvYnVmLlVuaW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICA",
+            "AkoECAUQBiJ9ChBFbnVtVmFsdWVPcHRpb25zEhkKCmRlcHJlY2F0ZWQYASAB",
+            "KAg6BWZhbHNlEkMKFHVuaW50ZXJwcmV0ZWRfb3B0aW9uGOcHIAMoCzIkLmdv",
+            "b2dsZS5wcm90b2J1Zi5VbmludGVycHJldGVkT3B0aW9uKgkI6AcQgICAgAIi",
+            "ewoOU2VydmljZU9wdGlvbnMSGQoKZGVwcmVjYXRlZBghIAEoCDoFZmFsc2US",
+            "QwoUdW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3Rv",
+            "YnVmLlVuaW50ZXJwcmV0ZWRPcHRpb24qCQjoBxCAgICAAiKtAgoNTWV0aG9k",
+            "T3B0aW9ucxIZCgpkZXByZWNhdGVkGCEgASgIOgVmYWxzZRJfChFpZGVtcG90",
+            "ZW5jeV9sZXZlbBgiIAEoDjIvLmdvb2dsZS5wcm90b2J1Zi5NZXRob2RPcHRp",
+            "b25zLklkZW1wb3RlbmN5TGV2ZWw6E0lERU1QT1RFTkNZX1VOS05PV04SQwoU",
+            "dW5pbnRlcnByZXRlZF9vcHRpb24Y5wcgAygLMiQuZ29vZ2xlLnByb3RvYnVm",
+            "LlVuaW50ZXJwcmV0ZWRPcHRpb24iUAoQSWRlbXBvdGVuY3lMZXZlbBIXChNJ",
+            "REVNUE9URU5DWV9VTktOT1dOEAASEwoPTk9fU0lERV9FRkZFQ1RTEAESDgoK",
+            "SURFTVBPVEVOVBACKgkI6AcQgICAgAIingIKE1VuaW50ZXJwcmV0ZWRPcHRp",
+            "b24SOwoEbmFtZRgCIAMoCzItLmdvb2dsZS5wcm90b2J1Zi5VbmludGVycHJl",
+            "dGVkT3B0aW9uLk5hbWVQYXJ0EhgKEGlkZW50aWZpZXJfdmFsdWUYAyABKAkS",
+            "GgoScG9zaXRpdmVfaW50X3ZhbHVlGAQgASgEEhoKEm5lZ2F0aXZlX2ludF92",
+            "YWx1ZRgFIAEoAxIUCgxkb3VibGVfdmFsdWUYBiABKAESFAoMc3RyaW5nX3Zh",
+            "bHVlGAcgASgMEhcKD2FnZ3JlZ2F0ZV92YWx1ZRgIIAEoCRozCghOYW1lUGFy",
+            "dBIRCgluYW1lX3BhcnQYASACKAkSFAoMaXNfZXh0ZW5zaW9uGAIgAigIItUB",
+            "Cg5Tb3VyY2VDb2RlSW5mbxI6Cghsb2NhdGlvbhgBIAMoCzIoLmdvb2dsZS5w",
+            "cm90b2J1Zi5Tb3VyY2VDb2RlSW5mby5Mb2NhdGlvbhqGAQoITG9jYXRpb24S",
+            "EAoEcGF0aBgBIAMoBUICEAESEAoEc3BhbhgCIAMoBUICEAESGAoQbGVhZGlu",
+            "Z19jb21tZW50cxgDIAEoCRIZChF0cmFpbGluZ19jb21tZW50cxgEIAEoCRIh",
+            "ChlsZWFkaW5nX2RldGFjaGVkX2NvbW1lbnRzGAYgAygJIqcBChFHZW5lcmF0",
+            "ZWRDb2RlSW5mbxJBCgphbm5vdGF0aW9uGAEgAygLMi0uZ29vZ2xlLnByb3Rv",
+            "YnVmLkdlbmVyYXRlZENvZGVJbmZvLkFubm90YXRpb24aTwoKQW5ub3RhdGlv",
+            "bhIQCgRwYXRoGAEgAygFQgIQARITCgtzb3VyY2VfZmlsZRgCIAEoCRINCgVi",
+            "ZWdpbhgDIAEoBRILCgNlbmQYBCABKAVCfgoTY29tLmdvb2dsZS5wcm90b2J1",
+            "ZkIQRGVzY3JpcHRvclByb3Rvc0gBWi1nb29nbGUuZ29sYW5nLm9yZy9wcm90",
+            "b2J1Zi90eXBlcy9kZXNjcmlwdG9ycGL4AQGiAgNHUEKqAhpHb29nbGUuUHJv",
+            "dG9idWYuUmVmbGVjdGlvbg=="));
       descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
       descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
           new pbr::FileDescriptor[] { },
           new pbr::FileDescriptor[] { },
           new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
           new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {

+ 4 - 2
java/core/src/main/java/com/google/protobuf/ByteString.java

@@ -1043,8 +1043,10 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
     }
     }
 
 
     /**
     /**
-     * Creates a byte string. Its size is the current size of this output stream and its output has
-     * been copied to it.
+     * Creates a byte string with the size and contents of this output stream. This does not create
+     * a new copy of the underlying bytes. If the stream size grows dynamically, the runtime is
+     * O(log n) in respect to the number of bytes written to the {@link Output}. If the stream size
+     * stays within the initial capacity, the runtime is O(1).
      *
      *
      * @return the current contents of this output stream, as a byte string.
      * @return the current contents of this output stream, as a byte string.
      */
      */

+ 6 - 1
java/core/src/main/java/com/google/protobuf/RopeByteString.java

@@ -603,7 +603,12 @@ final class RopeByteString extends ByteString {
 
 
   @Override
   @Override
   public CodedInputStream newCodedInput() {
   public CodedInputStream newCodedInput() {
-    return CodedInputStream.newInstance(new RopeInputStream());
+    // Passing along direct references to internal ByteBuffers can support more efficient parsing
+    // via aliasing in CodedInputStream for users who wish to use it.
+    //
+    // Otherwise we force data copies, both in copying as an input stream and in buffering in the
+    // CodedInputSteam.
+    return CodedInputStream.newInstance(asReadOnlyByteBufferList(), /* bufferIsImmutable= */ true);
   }
   }
 
 
   @Override
   @Override

+ 2 - 2
java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java

@@ -283,7 +283,7 @@ public class AbstractMessageTest extends TestCase {
     Message.Builder abstractMessageBuilder =
     Message.Builder abstractMessageBuilder =
         new AbstractMessageWrapper.Builder(TestRequiredForeign.newBuilder());
         new AbstractMessageWrapper.Builder(TestRequiredForeign.newBuilder());
     // mergeFrom() should not throw initialization error.
     // mergeFrom() should not throw initialization error.
-    abstractMessageBuilder.mergeFrom(bytes).buildPartial();
+    Message unused1 = abstractMessageBuilder.mergeFrom(bytes).buildPartial();
     try {
     try {
       abstractMessageBuilder.mergeFrom(bytes).build();
       abstractMessageBuilder.mergeFrom(bytes).build();
       fail();
       fail();
@@ -295,7 +295,7 @@ public class AbstractMessageTest extends TestCase {
     Message.Builder dynamicMessageBuilder =
     Message.Builder dynamicMessageBuilder =
         DynamicMessage.newBuilder(TestRequiredForeign.getDescriptor());
         DynamicMessage.newBuilder(TestRequiredForeign.getDescriptor());
     // mergeFrom() should not throw initialization error.
     // mergeFrom() should not throw initialization error.
-    dynamicMessageBuilder.mergeFrom(bytes).buildPartial();
+    Message unused2 = dynamicMessageBuilder.mergeFrom(bytes).buildPartial();
     try {
     try {
       dynamicMessageBuilder.mergeFrom(bytes).build();
       dynamicMessageBuilder.mergeFrom(bytes).build();
       fail();
       fail();

+ 1 - 1
java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java

@@ -69,7 +69,7 @@ public class DynamicMessageTest extends TestCase {
     Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
     Message.Builder builder = DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
     Message firstMessage = builder.build();
     Message firstMessage = builder.build();
     // double build()
     // double build()
-    builder.build();
+    Message unused = builder.build();
     // clear() after build()
     // clear() after build()
     builder.clear();
     builder.clear();
     // setters after build()
     // setters after build()

+ 1 - 0
java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java

@@ -76,6 +76,7 @@ import junit.framework.TestCase;
  *
  *
  * @author kenton@google.com Kenton Varda
  * @author kenton@google.com Kenton Varda
  */
  */
+@SuppressWarnings({"ProtoBuilderReturnValueIgnored", "ReturnValueIgnored"})
 public class GeneratedMessageTest extends TestCase {
 public class GeneratedMessageTest extends TestCase {
   TestUtil.ReflectionTester reflectionTester =
   TestUtil.ReflectionTester reflectionTester =
       new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);
       new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);

+ 2 - 2
java/core/src/test/java/com/google/protobuf/MapForProto2Test.java

@@ -1163,8 +1163,8 @@ public class MapForProto2Test extends TestCase {
   }
   }
 
 
   public void testReservedWordsFieldNames() {
   public void testReservedWordsFieldNames() {
-    ReservedAsMapField.newBuilder().build();
-    ReservedAsMapFieldWithEnumValue.newBuilder().build();
+    ReservedAsMapField unused1 = ReservedAsMapField.newBuilder().build();
+    ReservedAsMapFieldWithEnumValue unused2 = ReservedAsMapFieldWithEnumValue.newBuilder().build();
   }
   }
 
 
   public void testGetMap() {
   public void testGetMap() {

+ 2 - 2
java/core/src/test/java/com/google/protobuf/MapTest.java

@@ -1294,8 +1294,8 @@ public class MapTest extends TestCase {
   }
   }
 
 
   public void testReservedWordsFieldNames() {
   public void testReservedWordsFieldNames() {
-    ReservedAsMapField.newBuilder().build();
-    ReservedAsMapFieldWithEnumValue.newBuilder().build();
+    ReservedAsMapField unused1 = ReservedAsMapField.newBuilder().build();
+    ReservedAsMapFieldWithEnumValue unused2 = ReservedAsMapFieldWithEnumValue.newBuilder().build();
   }
   }
 
 
   public void testDeterministicSerialziation() throws Exception {
   public void testDeterministicSerialziation() throws Exception {

+ 4 - 2
java/core/src/test/java/com/google/protobuf/TestBadIdentifiers.java

@@ -43,8 +43,10 @@ public class TestBadIdentifiers extends TestCase {
 
 
   public void testCompilation() {
   public void testCompilation() {
     // If this compiles, it means the generation was correct.
     // If this compiles, it means the generation was correct.
-    TestBadIdentifiersProto.Deprecated.newBuilder();
-    TestBadIdentifiersProto.Override.newBuilder();
+    TestBadIdentifiersProto.Deprecated unused1 =
+        TestBadIdentifiersProto.Deprecated.newBuilder().build();
+    TestBadIdentifiersProto.Override unused2 =
+        TestBadIdentifiersProto.Override.getDefaultInstance();
   }
   }
 
 
   public void testGetDescriptor() {
   public void testGetDescriptor() {

+ 4 - 2
java/core/src/test/java/com/google/protobuf/TestBadIdentifiersLite.java

@@ -45,8 +45,10 @@ public final class TestBadIdentifiersLite extends TestCase {
 
 
   public void testCompilation() {
   public void testCompilation() {
     // If this compiles, it means the generation was correct.
     // If this compiles, it means the generation was correct.
-    TestBadIdentifiersProto.Deprecated.newBuilder();
-    TestBadIdentifiersProto.Override.newBuilder();
+    TestBadIdentifiersProto.Deprecated.Builder builder1 =
+        TestBadIdentifiersProto.Deprecated.newBuilder();
+    TestBadIdentifiersProto.Override.Builder builder2 =
+        TestBadIdentifiersProto.Override.newBuilder();
   }
   }
 
 
   public void testConflictingFieldNames() throws Exception {
   public void testConflictingFieldNames() throws Exception {

+ 0 - 56
java/core/src/test/proto/com/google/protobuf/test_extra_interfaces.proto

@@ -1,56 +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.
-
-// Author: Darick Tong (darick@google.com)
-syntax = "proto2";
-
-package protobuf_unittest;
-
-message Proto1 {
-  option experimental_java_message_interface =
-      "com.google.protobuf.ExtraInterfaces.HasBoolValue";
-  option experimental_java_message_interface =
-      "com.google.protobuf.ExtraInterfaces.HasStringValue<Proto1>";
-  option experimental_java_builder_interface =
-      "com.google.protobuf.ExtraInterfaces.HasStringValueBuilder"
-      "<Proto1, Builder>";
-
-  optional string string_value = 1;
-  optional bool bool_value = 2;
-  optional bytes byte_value = 3;
-  optional int32 int_value = 4;
-}
-
-message Proto2 {
-  option experimental_java_message_interface =
-      "com.google.protobuf.ExtraInterfaces.HasBoolValue";
-
-  optional bool bool_value = 1;
-}

+ 20 - 18
java/lite/src/test/java/com/google/protobuf/LiteTest.java

@@ -2406,24 +2406,26 @@ public class LiteTest extends TestCase {
   }
   }
 
 
   public void testAddAllIteratesOnce() {
   public void testAddAllIteratesOnce() {
-    TestAllTypesLite.newBuilder()
-        .addAllRepeatedBool(new OneTimeIterableList<>(false))
-        .addAllRepeatedInt32(new OneTimeIterableList<>(0))
-        .addAllRepeatedInt64(new OneTimeIterableList<>(0L))
-        .addAllRepeatedFloat(new OneTimeIterableList<>(0f))
-        .addAllRepeatedDouble(new OneTimeIterableList<>(0d))
-        .addAllRepeatedBytes(new OneTimeIterableList<>(ByteString.EMPTY))
-        .addAllRepeatedString(new OneTimeIterableList<>(""))
-        .addAllRepeatedNestedMessage(new OneTimeIterableList<>(NestedMessage.getDefaultInstance()))
-        .addAllRepeatedBool(new OneTimeIterable<>(false))
-        .addAllRepeatedInt32(new OneTimeIterable<>(0))
-        .addAllRepeatedInt64(new OneTimeIterable<>(0L))
-        .addAllRepeatedFloat(new OneTimeIterable<>(0f))
-        .addAllRepeatedDouble(new OneTimeIterable<>(0d))
-        .addAllRepeatedBytes(new OneTimeIterable<>(ByteString.EMPTY))
-        .addAllRepeatedString(new OneTimeIterable<>(""))
-        .addAllRepeatedNestedMessage(new OneTimeIterable<>(NestedMessage.getDefaultInstance()))
-        .build();
+    TestAllTypesLite unused =
+        TestAllTypesLite.newBuilder()
+            .addAllRepeatedBool(new OneTimeIterableList<>(false))
+            .addAllRepeatedInt32(new OneTimeIterableList<>(0))
+            .addAllRepeatedInt64(new OneTimeIterableList<>(0L))
+            .addAllRepeatedFloat(new OneTimeIterableList<>(0f))
+            .addAllRepeatedDouble(new OneTimeIterableList<>(0d))
+            .addAllRepeatedBytes(new OneTimeIterableList<>(ByteString.EMPTY))
+            .addAllRepeatedString(new OneTimeIterableList<>(""))
+            .addAllRepeatedNestedMessage(
+                new OneTimeIterableList<>(NestedMessage.getDefaultInstance()))
+            .addAllRepeatedBool(new OneTimeIterable<>(false))
+            .addAllRepeatedInt32(new OneTimeIterable<>(0))
+            .addAllRepeatedInt64(new OneTimeIterable<>(0L))
+            .addAllRepeatedFloat(new OneTimeIterable<>(0f))
+            .addAllRepeatedDouble(new OneTimeIterable<>(0d))
+            .addAllRepeatedBytes(new OneTimeIterable<>(ByteString.EMPTY))
+            .addAllRepeatedString(new OneTimeIterable<>(""))
+            .addAllRepeatedNestedMessage(new OneTimeIterable<>(NestedMessage.getDefaultInstance()))
+            .build();
   }
   }
 
 
   public void testAddAllIteratesOnce_throwsOnNull() {
   public void testAddAllIteratesOnce_throwsOnNull() {

+ 55 - 0
python/google/protobuf/internal/text_format_test.py

@@ -1176,6 +1176,54 @@ class OnlyWorksWithProto2RightNowTests(TextFormatBase):
         '  }\n'
         '  }\n'
         '}\n')
         '}\n')
 
 
+  def testDuplicateMapKey(self):
+    message = map_unittest_pb2.TestMap()
+    text = (
+        'map_uint64_uint64 {\n'
+        '  key: 123\n'
+        '  value: 17179869184\n'
+        '}\n'
+        'map_string_string {\n'
+        '  key: "abc"\n'
+        '  value: "first"\n'
+        '}\n'
+        'map_int32_foreign_message {\n'
+        '  key: 111\n'
+        '  value {\n'
+        '    c: 5\n'
+        '  }\n'
+        '}\n'
+        'map_uint64_uint64 {\n'
+        '  key: 123\n'
+        '  value: 321\n'
+        '}\n'
+        'map_string_string {\n'
+        '  key: "abc"\n'
+        '  value: "second"\n'
+        '}\n'
+        'map_int32_foreign_message {\n'
+        '  key: 111\n'
+        '  value {\n'
+        '    d: 5\n'
+        '  }\n'
+        '}\n')
+    text_format.Parse(text, message)
+    self.CompareToGoldenText(
+        text_format.MessageToString(message), 'map_uint64_uint64 {\n'
+        '  key: 123\n'
+        '  value: 321\n'
+        '}\n'
+        'map_string_string {\n'
+        '  key: "abc"\n'
+        '  value: "second"\n'
+        '}\n'
+        'map_int32_foreign_message {\n'
+        '  key: 111\n'
+        '  value {\n'
+        '    d: 5\n'
+        '  }\n'
+        '}\n')
+
   # In cpp implementation, __str__ calls the cpp implementation of text format.
   # In cpp implementation, __str__ calls the cpp implementation of text format.
   def testPrintMapUsingCppImplementation(self):
   def testPrintMapUsingCppImplementation(self):
     message = map_unittest_pb2.TestMap()
     message = map_unittest_pb2.TestMap()
@@ -2348,6 +2396,13 @@ class OptionalColonMessageToStringTest(unittest.TestCase):
                 '}\n')
                 '}\n')
     self.assertEqual(expected, output)
     self.assertEqual(expected, output)
 
 
+  def testPrintShortFormatRepeatedFields(self):
+    message = unittest_pb2.TestAllTypes()
+    message.repeated_int32.append(1)
+    output = text_format.MessageToString(
+        message, use_short_repeated_primitives=True, force_colon=True)
+    self.assertEqual('repeated_int32: [1]\n', output)
+
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
   unittest.main()
   unittest.main()

+ 9 - 6
python/google/protobuf/pyext/descriptor.cc

@@ -30,15 +30,18 @@
 
 
 // Author: petar@google.com (Petar Petrov)
 // Author: petar@google.com (Petar Petrov)
 
 
+#include <google/protobuf/pyext/descriptor.h>
+
 #include <Python.h>
 #include <Python.h>
 #include <frameobject.h>
 #include <frameobject.h>
+
+#include <cstdint>
 #include <string>
 #include <string>
 #include <unordered_map>
 #include <unordered_map>
 
 
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/dynamic_message.h>
 #include <google/protobuf/dynamic_message.h>
-#include <google/protobuf/pyext/descriptor.h>
 #include <google/protobuf/pyext/descriptor_containers.h>
 #include <google/protobuf/pyext/descriptor_containers.h>
 #include <google/protobuf/pyext/descriptor_pool.h>
 #include <google/protobuf/pyext/descriptor_pool.h>
 #include <google/protobuf/pyext/message.h>
 #include <google/protobuf/pyext/message.h>
@@ -195,7 +198,7 @@ bool Reparse(
   std::string serialized;
   std::string serialized;
   from.SerializeToString(&serialized);
   from.SerializeToString(&serialized);
   io::CodedInputStream input(
   io::CodedInputStream input(
-      reinterpret_cast<const uint8*>(serialized.c_str()), serialized.size());
+      reinterpret_cast<const uint8_t*>(serialized.c_str()), serialized.size());
   input.SetExtensionRegistry(message_factory->pool->pool,
   input.SetExtensionRegistry(message_factory->pool->pool,
                              message_factory->message_factory);
                              message_factory->message_factory);
   bool success = to->ParseFromCodedStream(&input);
   bool success = to->ParseFromCodedStream(&input);
@@ -804,22 +807,22 @@ static PyObject* GetDefaultValue(PyBaseDescriptor *self, void *closure) {
 
 
   switch (_GetDescriptor(self)->cpp_type()) {
   switch (_GetDescriptor(self)->cpp_type()) {
     case FieldDescriptor::CPPTYPE_INT32: {
     case FieldDescriptor::CPPTYPE_INT32: {
-      int32 value = _GetDescriptor(self)->default_value_int32();
+      int32_t value = _GetDescriptor(self)->default_value_int32();
       result = PyInt_FromLong(value);
       result = PyInt_FromLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_INT64: {
     case FieldDescriptor::CPPTYPE_INT64: {
-      int64 value = _GetDescriptor(self)->default_value_int64();
+      int64_t value = _GetDescriptor(self)->default_value_int64();
       result = PyLong_FromLongLong(value);
       result = PyLong_FromLongLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT32: {
     case FieldDescriptor::CPPTYPE_UINT32: {
-      uint32 value = _GetDescriptor(self)->default_value_uint32();
+      uint32_t value = _GetDescriptor(self)->default_value_uint32();
       result = PyInt_FromSize_t(value);
       result = PyInt_FromSize_t(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT64: {
     case FieldDescriptor::CPPTYPE_UINT64: {
-      uint64 value = _GetDescriptor(self)->default_value_uint64();
+      uint64_t value = _GetDescriptor(self)->default_value_uint64();
       result = PyLong_FromUnsignedLongLong(value);
       result = PyLong_FromUnsignedLongLong(value);
       break;
       break;
     }
     }

+ 3 - 1
python/google/protobuf/pyext/descriptor_database.cc

@@ -33,6 +33,8 @@
 
 
 #include <google/protobuf/pyext/descriptor_database.h>
 #include <google/protobuf/pyext/descriptor_database.h>
 
 
+#include <cstdint>
+
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.pb.h>
@@ -164,7 +166,7 @@ bool PyDescriptorDatabase::FindAllExtensionNumbers(
     return false;
     return false;
   }
   }
   Py_ssize_t size = PyList_Size(py_list.get());
   Py_ssize_t size = PyList_Size(py_list.get());
-  int64 item_value;
+  int64_t item_value;
   for (Py_ssize_t i = 0 ; i < size; ++i) {
   for (Py_ssize_t i = 0 ; i < size; ++i) {
     ScopedPyObjectPtr item(PySequence_GetItem(py_list.get(), i));
     ScopedPyObjectPtr item(PySequence_GetItem(py_list.get(), i));
     item_value = PyLong_AsLong(item.get());
     item_value = PyLong_AsLong(item.get());

+ 4 - 2
python/google/protobuf/pyext/extension_dict.cc

@@ -32,14 +32,16 @@
 // Author: tibell@google.com (Johan Tibell)
 // Author: tibell@google.com (Johan Tibell)
 
 
 #include <google/protobuf/pyext/extension_dict.h>
 #include <google/protobuf/pyext/extension_dict.h>
+
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
+#include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/dynamic_message.h>
 #include <google/protobuf/dynamic_message.h>
 #include <google/protobuf/message.h>
 #include <google/protobuf/message.h>
-#include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/pyext/descriptor.h>
 #include <google/protobuf/pyext/descriptor.h>
 #include <google/protobuf/pyext/message.h>
 #include <google/protobuf/pyext/message.h>
 #include <google/protobuf/pyext/message_factory.h>
 #include <google/protobuf/pyext/message_factory.h>
@@ -263,7 +265,7 @@ PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* arg) {
 }
 }
 
 
 PyObject* _FindExtensionByNumber(ExtensionDict* self, PyObject* arg) {
 PyObject* _FindExtensionByNumber(ExtensionDict* self, PyObject* arg) {
-  int64 number = PyLong_AsLong(arg);
+  int64_t number = PyLong_AsLong(arg);
   if (number == -1 && PyErr_Occurred()) {
   if (number == -1 && PyErr_Occurred()) {
     return NULL;
     return NULL;
   }
   }

+ 4 - 3
python/google/protobuf/pyext/map_container.cc

@@ -32,15 +32,16 @@
 
 
 #include <google/protobuf/pyext/map_container.h>
 #include <google/protobuf/pyext/map_container.h>
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/logging.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
-#include <google/protobuf/map_field.h>
 #include <google/protobuf/map.h>
 #include <google/protobuf/map.h>
+#include <google/protobuf/map_field.h>
 #include <google/protobuf/message.h>
 #include <google/protobuf/message.h>
-#include <google/protobuf/pyext/message_factory.h>
 #include <google/protobuf/pyext/message.h>
 #include <google/protobuf/pyext/message.h>
+#include <google/protobuf/pyext/message_factory.h>
 #include <google/protobuf/pyext/repeated_composite_container.h>
 #include <google/protobuf/pyext/repeated_composite_container.h>
 #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
 #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
 #include <google/protobuf/stubs/map_util.h>
 #include <google/protobuf/stubs/map_util.h>
@@ -99,7 +100,7 @@ struct MapIterator {
   //
   //
   // We store this so that if the map is modified during iteration we can throw
   // We store this so that if the map is modified during iteration we can throw
   // an error.
   // an error.
-  uint64 version;
+  uint64_t version;
 };
 };
 
 
 Message* MapContainer::GetMutableMessage() {
 Message* MapContainer::GetMutableMessage() {

+ 2 - 1
python/google/protobuf/pyext/map_container.h

@@ -33,6 +33,7 @@
 
 
 #include <Python.h>
 #include <Python.h>
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/descriptor.h>
@@ -56,7 +57,7 @@ struct MapContainer : public ContainerBase {
 
 
   // We bump this whenever we perform a mutation, to invalidate existing
   // We bump this whenever we perform a mutation, to invalidate existing
   // iterators.
   // iterators.
-  uint64 version;
+  uint64_t version;
 };
 };
 
 
 struct MessageMapContainer : public MapContainer {
 struct MessageMapContainer : public MapContainer {

+ 5 - 4
python/google/protobuf/pyext/message.cc

@@ -35,6 +35,7 @@
 
 
 #include <structmember.h>  // A Python header file.
 #include <structmember.h>  // A Python header file.
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
@@ -2191,22 +2192,22 @@ PyObject* InternalGetScalar(const Message* message,
   PyObject* result = NULL;
   PyObject* result = NULL;
   switch (field_descriptor->cpp_type()) {
   switch (field_descriptor->cpp_type()) {
     case FieldDescriptor::CPPTYPE_INT32: {
     case FieldDescriptor::CPPTYPE_INT32: {
-      int32 value = reflection->GetInt32(*message, field_descriptor);
+      int32_t value = reflection->GetInt32(*message, field_descriptor);
       result = PyInt_FromLong(value);
       result = PyInt_FromLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_INT64: {
     case FieldDescriptor::CPPTYPE_INT64: {
-      int64 value = reflection->GetInt64(*message, field_descriptor);
+      int64_t value = reflection->GetInt64(*message, field_descriptor);
       result = PyLong_FromLongLong(value);
       result = PyLong_FromLongLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT32: {
     case FieldDescriptor::CPPTYPE_UINT32: {
-      uint32 value = reflection->GetUInt32(*message, field_descriptor);
+      uint32_t value = reflection->GetUInt32(*message, field_descriptor);
       result = PyInt_FromSize_t(value);
       result = PyInt_FromSize_t(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT64: {
     case FieldDescriptor::CPPTYPE_UINT64: {
-      uint64 value = reflection->GetUInt64(*message, field_descriptor);
+      uint64_t value = reflection->GetUInt64(*message, field_descriptor);
       result = PyLong_FromUnsignedLongLong(value);
       result = PyLong_FromUnsignedLongLong(value);
       break;
       break;
     }
     }

+ 5 - 4
python/google/protobuf/pyext/message.h

@@ -36,6 +36,7 @@
 
 
 #include <Python.h>
 #include <Python.h>
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
 #include <unordered_map>
 #include <unordered_map>
@@ -286,25 +287,25 @@ PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg);
   ((field_descriptor)->label() == FieldDescriptor::LABEL_REPEATED)
   ((field_descriptor)->label() == FieldDescriptor::LABEL_REPEATED)
 
 
 #define GOOGLE_CHECK_GET_INT32(arg, value, err)  \
 #define GOOGLE_CHECK_GET_INT32(arg, value, err)  \
-  int32 value;                            \
+  int32_t value;                          \
   if (!CheckAndGetInteger(arg, &value)) { \
   if (!CheckAndGetInteger(arg, &value)) { \
     return err;                           \
     return err;                           \
   }
   }
 
 
 #define GOOGLE_CHECK_GET_INT64(arg, value, err)  \
 #define GOOGLE_CHECK_GET_INT64(arg, value, err)  \
-  int64 value;                            \
+  int64_t value;                          \
   if (!CheckAndGetInteger(arg, &value)) { \
   if (!CheckAndGetInteger(arg, &value)) { \
     return err;                           \
     return err;                           \
   }
   }
 
 
 #define GOOGLE_CHECK_GET_UINT32(arg, value, err) \
 #define GOOGLE_CHECK_GET_UINT32(arg, value, err) \
-  uint32 value;                           \
+  uint32_t value;                         \
   if (!CheckAndGetInteger(arg, &value)) { \
   if (!CheckAndGetInteger(arg, &value)) { \
     return err;                           \
     return err;                           \
   }
   }
 
 
 #define GOOGLE_CHECK_GET_UINT64(arg, value, err) \
 #define GOOGLE_CHECK_GET_UINT64(arg, value, err) \
-  uint64 value;                           \
+  uint64_t value;                         \
   if (!CheckAndGetInteger(arg, &value)) { \
   if (!CheckAndGetInteger(arg, &value)) { \
     return err;                           \
     return err;                           \
   }
   }

+ 5 - 4
python/google/protobuf/pyext/repeated_scalar_container.cc

@@ -33,6 +33,7 @@
 
 
 #include <google/protobuf/pyext/repeated_scalar_container.h>
 #include <google/protobuf/pyext/repeated_scalar_container.h>
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
@@ -207,25 +208,25 @@ static PyObject* Item(PyObject* pself, Py_ssize_t index) {
   PyObject* result = nullptr;
   PyObject* result = nullptr;
   switch (field_descriptor->cpp_type()) {
   switch (field_descriptor->cpp_type()) {
     case FieldDescriptor::CPPTYPE_INT32: {
     case FieldDescriptor::CPPTYPE_INT32: {
-      int32 value =
+      int32_t value =
           reflection->GetRepeatedInt32(*message, field_descriptor, index);
           reflection->GetRepeatedInt32(*message, field_descriptor, index);
       result = PyInt_FromLong(value);
       result = PyInt_FromLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_INT64: {
     case FieldDescriptor::CPPTYPE_INT64: {
-      int64 value =
+      int64_t value =
           reflection->GetRepeatedInt64(*message, field_descriptor, index);
           reflection->GetRepeatedInt64(*message, field_descriptor, index);
       result = PyLong_FromLongLong(value);
       result = PyLong_FromLongLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT32: {
     case FieldDescriptor::CPPTYPE_UINT32: {
-      uint32 value =
+      uint32_t value =
           reflection->GetRepeatedUInt32(*message, field_descriptor, index);
           reflection->GetRepeatedUInt32(*message, field_descriptor, index);
       result = PyLong_FromLongLong(value);
       result = PyLong_FromLongLong(value);
       break;
       break;
     }
     }
     case FieldDescriptor::CPPTYPE_UINT64: {
     case FieldDescriptor::CPPTYPE_UINT64: {
-      uint64 value =
+      uint64_t value =
           reflection->GetRepeatedUInt64(*message, field_descriptor, index);
           reflection->GetRepeatedUInt64(*message, field_descriptor, index);
       result = PyLong_FromUnsignedLongLong(value);
       result = PyLong_FromUnsignedLongLong(value);
       break;
       break;

+ 1 - 3
python/google/protobuf/text_format.py

@@ -567,8 +567,6 @@ class _Printer(object):
       self.out.write(', ')
       self.out.write(', ')
     self.PrintFieldValue(field, value[-1])
     self.PrintFieldValue(field, value[-1])
     self.out.write(']')
     self.out.write(']')
-    if self.force_colon:
-      self.out.write(':')
     self.out.write(' ' if self.as_one_line else '\n')
     self.out.write(' ' if self.as_one_line else '\n')
 
 
   def _PrintMessageFieldValue(self, value):
   def _PrintMessageFieldValue(self, value):
@@ -1060,7 +1058,7 @@ class _Parser(object):
       value_cpptype = field.message_type.fields_by_name['value'].cpp_type
       value_cpptype = field.message_type.fields_by_name['value'].cpp_type
       if value_cpptype == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
       if value_cpptype == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
         value = getattr(message, field.name)[sub_message.key]
         value = getattr(message, field.name)[sub_message.key]
-        value.MergeFrom(sub_message.value)
+        value.CopyFrom(sub_message.value)
       else:
       else:
         getattr(message, field.name)[sub_message.key] = sub_message.value
         getattr(message, field.name)[sub_message.key] = sub_message.value
 
 

+ 2 - 2
src/google/protobuf/any.pb.cc

@@ -163,7 +163,6 @@ const char* Any::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::intern
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string type_url = 1;
       // string type_url = 1;
       case 1:
       case 1:
@@ -184,7 +183,8 @@ const char* Any::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::intern
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 6 - 6
src/google/protobuf/api.pb.cc

@@ -268,7 +268,6 @@ const char* Api::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::intern
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -341,7 +340,8 @@ const char* Api::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::intern
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -673,7 +673,6 @@ const char* Method::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -738,7 +737,8 @@ const char* Method::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1043,7 +1043,6 @@ const char* Mixin::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -1065,7 +1064,8 @@ const char* Mixin::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 8 - 0
src/google/protobuf/arenastring.cc

@@ -62,6 +62,14 @@ const std::string& LazyString::Init() const {
 }
 }
 
 
 
 
+std::string* ArenaStringPtr::SetAndReturnNewString() {
+  std::string* new_string = new std::string();
+  tagged_ptr_.Set(new_string);
+  return new_string;
+}
+
+void ArenaStringPtr::DestroyNoArenaSlowPath() { delete UnsafeMutablePointer(); }
+
 void ArenaStringPtr::Set(const std::string* default_value,
 void ArenaStringPtr::Set(const std::string* default_value,
                          ConstStringParam value, ::google::protobuf::Arena* arena) {
                          ConstStringParam value, ::google::protobuf::Arena* arena) {
   if (IsDefault(default_value)) {
   if (IsDefault(default_value)) {

+ 8 - 4
src/google/protobuf/arenastring.h

@@ -328,6 +328,12 @@ struct PROTOBUF_EXPORT ArenaStringPtr {
   template <typename... Lazy>
   template <typename... Lazy>
   std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
   std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
 
 
+  // Sets value to a newly allocated string and returns it
+  std::string* SetAndReturnNewString();
+
+  // Destroys the non-default string value out-of-line
+  void DestroyNoArenaSlowPath();
+
 };
 };
 
 
 inline void ArenaStringPtr::UnsafeSetDefault(const std::string* value) {
 inline void ArenaStringPtr::UnsafeSetDefault(const std::string* value) {
@@ -379,9 +385,7 @@ inline std::string* ArenaStringPtr::MutableNoArenaNoDefault(
   // static global) and a branch to the slowpath (which calls operator new and
   // static global) and a branch to the slowpath (which calls operator new and
   // the ctor). DO NOT add any tagged-pointer operations here.
   // the ctor). DO NOT add any tagged-pointer operations here.
   if (IsDefault(default_value)) {
   if (IsDefault(default_value)) {
-    std::string* new_string = new std::string();
-    tagged_ptr_.Set(new_string);
-    return new_string;
+    return SetAndReturnNewString();
   } else {
   } else {
     return UnsafeMutablePointer();
     return UnsafeMutablePointer();
   }
   }
@@ -389,7 +393,7 @@ inline std::string* ArenaStringPtr::MutableNoArenaNoDefault(
 
 
 inline void ArenaStringPtr::DestroyNoArena(const std::string* default_value) {
 inline void ArenaStringPtr::DestroyNoArena(const std::string* default_value) {
   if (!IsDefault(default_value)) {
   if (!IsDefault(default_value)) {
-    delete UnsafeMutablePointer();
+    DestroyNoArenaSlowPath();
   }
   }
 }
 }
 
 

+ 3 - 2
src/google/protobuf/compiler/annotation_test_util.cc

@@ -30,6 +30,7 @@
 
 
 #include <google/protobuf/compiler/annotation_test_util.h>
 #include <google/protobuf/compiler/annotation_test_util.h>
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/testing/file.h>
 #include <google/protobuf/testing/file.h>
@@ -141,8 +142,8 @@ bool AtLeastOneAnnotationMatchesSubstring(
            e = annotations.end();
            e = annotations.end();
        i != e; ++i) {
        i != e; ++i) {
     const GeneratedCodeInfo::Annotation* annotation = *i;
     const GeneratedCodeInfo::Annotation* annotation = *i;
-    uint32 begin = annotation->begin();
-    uint32 end = annotation->end();
+    uint32_t begin = annotation->begin();
+    uint32_t end = annotation->end();
     if (end < begin || end > file_content.size()) {
     if (end < begin || end > file_content.size()) {
       return false;
       return false;
     }
     }

+ 3 - 1
src/google/protobuf/compiler/command_line_interface.cc

@@ -34,6 +34,8 @@
 
 
 #include <google/protobuf/compiler/command_line_interface.h>
 #include <google/protobuf/compiler/command_line_interface.h>
 
 
+#include <cstdint>
+
 #include <google/protobuf/stubs/platform_macros.h>
 #include <google/protobuf/stubs/platform_macros.h>
 
 
 #include <stdio.h>
 #include <stdio.h>
@@ -2105,7 +2107,7 @@ Parse PROTO_FILES and generate output based on the options given:
 }
 }
 
 
 bool CommandLineInterface::EnforceProto3OptionalSupport(
 bool CommandLineInterface::EnforceProto3OptionalSupport(
-    const std::string& codegen_name, uint64 supported_features,
+    const std::string& codegen_name, uint64_t supported_features,
     const std::vector<const FileDescriptor*>& parsed_files) const {
     const std::vector<const FileDescriptor*>& parsed_files) const {
   bool supports_proto3_optional =
   bool supports_proto3_optional =
       supported_features & CodeGenerator::FEATURE_PROTO3_OPTIONAL;
       supported_features & CodeGenerator::FEATURE_PROTO3_OPTIONAL;

+ 2 - 1
src/google/protobuf/compiler/command_line_interface.h

@@ -38,6 +38,7 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <set>
 #include <set>
@@ -229,7 +230,7 @@ class PROTOC_EXPORT CommandLineInterface {
   // Fails if these files use proto3 optional and the code generator doesn't
   // Fails if these files use proto3 optional and the code generator doesn't
   // support it. This is a permanent check.
   // support it. This is a permanent check.
   bool EnforceProto3OptionalSupport(
   bool EnforceProto3OptionalSupport(
-      const std::string& codegen_name, uint64 supported_features,
+      const std::string& codegen_name, uint64_t supported_features,
       const std::vector<const FileDescriptor*>& parsed_files) const;
       const std::vector<const FileDescriptor*>& parsed_files) const;
 
 
 
 

+ 3 - 1
src/google/protobuf/compiler/command_line_interface_unittest.cc

@@ -36,6 +36,8 @@
 #include <sys/stat.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/types.h>
 
 
+#include <cstdint>
+
 #ifndef _MSC_VER
 #ifndef _MSC_VER
 #include <unistd.h>
 #include <unistd.h>
 #endif
 #endif
@@ -216,7 +218,7 @@ class CommandLineInterfaceTest : public testing::Test {
   // code generator that omits the given feature(s).
   // code generator that omits the given feature(s).
   void CreateGeneratorWithMissingFeatures(const std::string& name,
   void CreateGeneratorWithMissingFeatures(const std::string& name,
                                           const std::string& description,
                                           const std::string& description,
-                                          uint64 features) {
+                                          uint64_t features) {
     MockCodeGenerator* generator = new MockCodeGenerator(name);
     MockCodeGenerator* generator = new MockCodeGenerator(name);
     generator->SuppressFeatures(features);
     generator->SuppressFeatures(features);
     mock_generators_to_delete_.push_back(generator);
     mock_generators_to_delete_.push_back(generator);

+ 7 - 3
src/google/protobuf/compiler/cpp/cpp_enum.cc

@@ -32,10 +32,14 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/cpp/cpp_enum.h>
+
+#include <cstdint>
+#include <limits>
 #include <map>
 #include <map>
 
 
-#include <google/protobuf/compiler/cpp/cpp_enum.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
+#include <google/protobuf/compiler/cpp/cpp_names.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
 
 
@@ -49,13 +53,13 @@ namespace {
 // is kint32max, GOOGLE_ARRAYSIZE will overflow. In such cases we should omit the
 // is kint32max, GOOGLE_ARRAYSIZE will overflow. In such cases we should omit the
 // generation of the GOOGLE_ARRAYSIZE constant.
 // generation of the GOOGLE_ARRAYSIZE constant.
 bool ShouldGenerateArraySize(const EnumDescriptor* descriptor) {
 bool ShouldGenerateArraySize(const EnumDescriptor* descriptor) {
-  int32 max_value = descriptor->value(0)->number();
+  int32_t max_value = descriptor->value(0)->number();
   for (int i = 0; i < descriptor->value_count(); i++) {
   for (int i = 0; i < descriptor->value_count(); i++) {
     if (descriptor->value(i)->number() > max_value) {
     if (descriptor->value(i)->number() > max_value) {
       max_value = descriptor->value(i)->number();
       max_value = descriptor->value(i)->number();
     }
     }
   }
   }
-  return max_value != kint32max;
+  return max_value != std::numeric_limits<int32_t>::max();
 }
 }
 
 
 // Returns the number of unique numeric enum values. This is less than
 // Returns the number of unique numeric enum values. This is less than

+ 3 - 1
src/google/protobuf/compiler/cpp/cpp_field.cc

@@ -33,6 +33,8 @@
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
 #include <google/protobuf/compiler/cpp/cpp_field.h>
 #include <google/protobuf/compiler/cpp/cpp_field.h>
+
+#include <cstdint>
 #include <memory>
 #include <memory>
 
 
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
@@ -89,7 +91,7 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor,
   (*variables)["}"] = "";
   (*variables)["}"] = "";
 }
 }
 
 
-void FieldGenerator::SetHasBitIndex(int32 has_bit_index) {
+void FieldGenerator::SetHasBitIndex(int32_t has_bit_index) {
   if (!HasHasbit(descriptor_)) {
   if (!HasHasbit(descriptor_)) {
     GOOGLE_CHECK_EQ(has_bit_index, -1);
     GOOGLE_CHECK_EQ(has_bit_index, -1);
     return;
     return;

+ 2 - 1
src/google/protobuf/compiler/cpp/cpp_field.h

@@ -35,6 +35,7 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
@@ -180,7 +181,7 @@ class FieldGenerator {
   // are placed in the message's ByteSize() method.
   // are placed in the message's ByteSize() method.
   virtual void GenerateByteSize(io::Printer* printer) const = 0;
   virtual void GenerateByteSize(io::Printer* printer) const = 0;
 
 
-  void SetHasBitIndex(int32 has_bit_index);
+  void SetHasBitIndex(int32_t has_bit_index);
 
 
  protected:
  protected:
   const FieldDescriptor* descriptor_;
   const FieldDescriptor* descriptor_;

+ 21 - 19
src/google/protobuf/compiler/cpp/cpp_helpers.cc

@@ -34,6 +34,7 @@
 
 
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 
 
+#include <cstdint>
 #include <functional>
 #include <functional>
 #include <limits>
 #include <limits>
 #include <map>
 #include <map>
@@ -642,7 +643,7 @@ const char* DeclaredTypeMethodName(FieldDescriptor::Type type) {
 }
 }
 
 
 std::string Int32ToString(int number) {
 std::string Int32ToString(int number) {
-  if (number == kint32min) {
+  if (number == std::numeric_limits<int32_t>::min()) {
     // This needs to be special-cased, see explanation here:
     // This needs to be special-cased, see explanation here:
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
     return StrCat(number + 1, " - 1");
     return StrCat(number + 1, " - 1");
@@ -651,8 +652,8 @@ std::string Int32ToString(int number) {
   }
   }
 }
 }
 
 
-std::string Int64ToString(const std::string& macro_prefix, int64 number) {
-  if (number == kint64min) {
+std::string Int64ToString(const std::string& macro_prefix, int64_t number) {
+  if (number == std::numeric_limits<int64_t>::min()) {
     // This needs to be special-cased, see explanation here:
     // This needs to be special-cased, see explanation here:
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
     return StrCat(macro_prefix, "_LONGLONG(", number + 1, ") - 1");
     return StrCat(macro_prefix, "_LONGLONG(", number + 1, ") - 1");
@@ -660,7 +661,7 @@ std::string Int64ToString(const std::string& macro_prefix, int64 number) {
   return StrCat(macro_prefix, "_LONGLONG(", number, ")");
   return StrCat(macro_prefix, "_LONGLONG(", number, ")");
 }
 }
 
 
-std::string UInt64ToString(const std::string& macro_prefix, uint64 number) {
+std::string UInt64ToString(const std::string& macro_prefix, uint64_t number) {
   return StrCat(macro_prefix, "_ULONGLONG(", number, ")");
   return StrCat(macro_prefix, "_ULONGLONG(", number, ")");
 }
 }
 
 
@@ -748,7 +749,8 @@ std::string FilenameIdentifier(const std::string& filename) {
     } else {
     } else {
       // Not alphanumeric.  To avoid any possibility of name conflicts we
       // Not alphanumeric.  To avoid any possibility of name conflicts we
       // use the hex code for the character.
       // use the hex code for the character.
-      StrAppend(&result, "_", strings::Hex(static_cast<uint8>(filename[i])));
+      StrAppend(&result, "_",
+                      strings::Hex(static_cast<uint8_t>(filename[i])));
     }
     }
   }
   }
   return result;
   return result;
@@ -1612,7 +1614,7 @@ class ParseLoopGenerator {
   }
   }
 
 
   // Convert a 1 or 2 byte varint into the equivalent value upon a direct load.
   // Convert a 1 or 2 byte varint into the equivalent value upon a direct load.
-  static uint32 SmallVarintValue(uint32 x) {
+  static uint32_t SmallVarintValue(uint32_t x) {
     GOOGLE_DCHECK(x < 128 * 128);
     GOOGLE_DCHECK(x < 128 * 128);
     if (x >= 128) x += (x & 0xFF80) + 128;
     if (x >= 128) x += (x & 0xFF80) + 128;
     return x;
     return x;
@@ -1629,7 +1631,7 @@ class ParseLoopGenerator {
 
 
   void GenerateFieldBody(internal::WireFormatLite::WireType wiretype,
   void GenerateFieldBody(internal::WireFormatLite::WireType wiretype,
                          const FieldDescriptor* field) {
                          const FieldDescriptor* field) {
-    uint32 tag = WireFormatLite::MakeTag(field->number(), wiretype);
+    uint32_t tag = WireFormatLite::MakeTag(field->number(), wiretype);
     switch (wiretype) {
     switch (wiretype) {
       case WireFormatLite::WIRETYPE_VARINT: {
       case WireFormatLite::WIRETYPE_VARINT: {
         std::string type = PrimitiveTypeName(options_, field->cpp_type());
         std::string type = PrimitiveTypeName(options_, field->cpp_type());
@@ -1724,16 +1726,16 @@ class ParseLoopGenerator {
 
 
   // Returns the tag for this field and in case of repeated packable fields,
   // Returns the tag for this field and in case of repeated packable fields,
   // sets a fallback tag in fallback_tag_ptr.
   // sets a fallback tag in fallback_tag_ptr.
-  static uint32 ExpectedTag(const FieldDescriptor* field,
-                            uint32* fallback_tag_ptr) {
-    uint32 expected_tag;
+  static uint32_t ExpectedTag(const FieldDescriptor* field,
+                              uint32_t* fallback_tag_ptr) {
+    uint32_t expected_tag;
     if (field->is_packable()) {
     if (field->is_packable()) {
       auto expected_wiretype = WireFormat::WireTypeForFieldType(field->type());
       auto expected_wiretype = WireFormat::WireTypeForFieldType(field->type());
       expected_tag =
       expected_tag =
           WireFormatLite::MakeTag(field->number(), expected_wiretype);
           WireFormatLite::MakeTag(field->number(), expected_wiretype);
       GOOGLE_CHECK(expected_wiretype != WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
       GOOGLE_CHECK(expected_wiretype != WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
       auto fallback_wiretype = WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
       auto fallback_wiretype = WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
-      uint32 fallback_tag =
+      uint32_t fallback_tag =
           WireFormatLite::MakeTag(field->number(), fallback_wiretype);
           WireFormatLite::MakeTag(field->number(), fallback_wiretype);
 
 
       if (field->is_packed()) std::swap(expected_tag, fallback_tag);
       if (field->is_packed()) std::swap(expected_tag, fallback_tag);
@@ -1752,8 +1754,7 @@ class ParseLoopGenerator {
     format_(
     format_(
         "while (!ctx->Done(&ptr)) {\n"
         "while (!ctx->Done(&ptr)) {\n"
         "  $uint32$ tag;\n"
         "  $uint32$ tag;\n"
-        "  ptr = $pi_ns$::ReadTag(ptr, &tag);\n"
-        "  CHK_(ptr);\n");
+        "  ptr = $pi_ns$::ReadTag(ptr, &tag);\n");
     if (!ordered_fields.empty()) format_("  switch (tag >> 3) {\n");
     if (!ordered_fields.empty()) format_("  switch (tag >> 3) {\n");
 
 
     format_.Indent();
     format_.Indent();
@@ -1763,14 +1764,14 @@ class ParseLoopGenerator {
       PrintFieldComment(format_, field);
       PrintFieldComment(format_, field);
       format_("case $1$:\n", field->number());
       format_("case $1$:\n", field->number());
       format_.Indent();
       format_.Indent();
-      uint32 fallback_tag = 0;
-      uint32 expected_tag = ExpectedTag(field, &fallback_tag);
+      uint32_t fallback_tag = 0;
+      uint32_t expected_tag = ExpectedTag(field, &fallback_tag);
       format_(
       format_(
           "if (PROTOBUF_PREDICT_TRUE(static_cast<$uint8$>(tag) == $1$)) {\n",
           "if (PROTOBUF_PREDICT_TRUE(static_cast<$uint8$>(tag) == $1$)) {\n",
           expected_tag & 0xFF);
           expected_tag & 0xFF);
       format_.Indent();
       format_.Indent();
       auto wiretype = WireFormatLite::GetTagWireType(expected_tag);
       auto wiretype = WireFormatLite::GetTagWireType(expected_tag);
-      uint32 tag = WireFormatLite::MakeTag(field->number(), wiretype);
+      uint32_t tag = WireFormatLite::MakeTag(field->number(), wiretype);
       int tag_size = io::CodedOutputStream::VarintSize32(tag);
       int tag_size = io::CodedOutputStream::VarintSize32(tag);
       bool is_repeat = ShouldRepeat(field, wiretype);
       bool is_repeat = ShouldRepeat(field, wiretype);
       if (is_repeat) {
       if (is_repeat) {
@@ -1807,7 +1808,8 @@ class ParseLoopGenerator {
     if (!ordered_fields.empty()) format_("default: {\n");
     if (!ordered_fields.empty()) format_("default: {\n");
     if (!ordered_fields.empty()) format_("handle_unusual:\n");
     if (!ordered_fields.empty()) format_("handle_unusual:\n");
     format_(
     format_(
-        "  if ((tag & 7) == 4 || tag == 0) {\n"
+        "  if ((tag == 0) || ((tag & 7) == 4)) {\n"
+        "    CHK_(ptr);\n"
         "    ctx->SetLastTag(tag);\n"
         "    ctx->SetLastTag(tag);\n"
         "    goto success;\n"
         "    goto success;\n"
         "  }\n");
         "  }\n");
@@ -1821,9 +1823,9 @@ class ParseLoopGenerator {
               descriptor->extension_range(i);
               descriptor->extension_range(i);
           if (i > 0) format_(" ||\n    ");
           if (i > 0) format_(" ||\n    ");
 
 
-          uint32 start_tag = WireFormatLite::MakeTag(
+          uint32_t start_tag = WireFormatLite::MakeTag(
               range->start, static_cast<WireFormatLite::WireType>(0));
               range->start, static_cast<WireFormatLite::WireType>(0));
-          uint32 end_tag = WireFormatLite::MakeTag(
+          uint32_t end_tag = WireFormatLite::MakeTag(
               range->end, static_cast<WireFormatLite::WireType>(0));
               range->end, static_cast<WireFormatLite::WireType>(0));
 
 
           if (range->end > FieldDescriptor::kMaxNumber) {
           if (range->end > FieldDescriptor::kMaxNumber) {

+ 3 - 4
src/google/protobuf/compiler/cpp/cpp_helpers.h

@@ -36,11 +36,13 @@
 #define GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
 #define GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <iterator>
 #include <iterator>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
 #include <google/protobuf/compiler/cpp/cpp_options.h>
 #include <google/protobuf/compiler/cpp/cpp_options.h>
+#include <google/protobuf/compiler/cpp/cpp_names.h>
 #include <google/protobuf/compiler/scc.h>
 #include <google/protobuf/compiler/scc.h>
 #include <google/protobuf/compiler/code_generator.h>
 #include <google/protobuf/compiler/code_generator.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.pb.h>
@@ -184,9 +186,6 @@ std::string ResolveKeyword(const std::string& name);
 // anyway, so normally this just returns field->name().
 // anyway, so normally this just returns field->name().
 std::string FieldName(const FieldDescriptor* field);
 std::string FieldName(const FieldDescriptor* field);
 
 
-// Get the sanitized name that should be used for the given enum in C++ code.
-std::string EnumValueName(const EnumValueDescriptor* enum_value);
-
 // Returns an estimate of the compiler's alignment for the field.  This
 // Returns an estimate of the compiler's alignment for the field.  This
 // can't guarantee to be correct because the generated code could be compiled on
 // can't guarantee to be correct because the generated code could be compiled on
 // different systems with different alignment rules.  The estimates below assume
 // different systems with different alignment rules.  The estimates below assume
@@ -222,7 +221,7 @@ const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
 std::string Int32ToString(int number);
 std::string Int32ToString(int number);
 
 
 // Return the code that evaluates to the number when compiled.
 // Return the code that evaluates to the number when compiled.
-std::string Int64ToString(const Options& options, int64 number);
+std::string Int64ToString(const Options& options, int64_t number);
 
 
 // Get code that evaluates to the field's default value.
 // Get code that evaluates to the field's default value.
 std::string DefaultValue(const Options& options, const FieldDescriptor* field);
 std::string DefaultValue(const Options& options, const FieldDescriptor* field);

+ 92 - 37
src/google/protobuf/compiler/cpp/cpp_message.cc

@@ -35,6 +35,7 @@
 #include <google/protobuf/compiler/cpp/cpp_message.h>
 #include <google/protobuf/compiler/cpp/cpp_message.h>
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <functional>
 #include <functional>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
@@ -76,7 +77,7 @@ static constexpr int kNoHasbit = -1;
 // masks is allowed to be shorter than _has_bits_, but at least one element of
 // masks is allowed to be shorter than _has_bits_, but at least one element of
 // masks must be non-zero.
 // masks must be non-zero.
 std::string ConditionalToCheckBitmasks(
 std::string ConditionalToCheckBitmasks(
-    const std::vector<uint32>& masks, bool return_success = true,
+    const std::vector<uint32_t>& masks, bool return_success = true,
     StringPiece has_bits_var = "_has_bits_") {
     StringPiece has_bits_var = "_has_bits_") {
   std::vector<std::string> parts;
   std::vector<std::string> parts;
   for (int i = 0; i < masks.size(); i++) {
   for (int i = 0; i < masks.size(); i++) {
@@ -405,23 +406,23 @@ std::vector<std::vector<const FieldDescriptor*>> CollectFields(
 // Returns a bit mask based on has_bit index of "fields" that are typically on
 // Returns a bit mask based on has_bit index of "fields" that are typically on
 // the same chunk. It is used in a group presence check where _has_bits_ is
 // the same chunk. It is used in a group presence check where _has_bits_ is
 // masked to tell if any thing in "fields" is present.
 // masked to tell if any thing in "fields" is present.
-uint32 GenChunkMask(const std::vector<const FieldDescriptor*>& fields,
-                    const std::vector<int>& has_bit_indices) {
+uint32_t GenChunkMask(const std::vector<const FieldDescriptor*>& fields,
+                      const std::vector<int>& has_bit_indices) {
   GOOGLE_CHECK(!fields.empty());
   GOOGLE_CHECK(!fields.empty());
   int first_index_offset = has_bit_indices[fields.front()->index()] / 32;
   int first_index_offset = has_bit_indices[fields.front()->index()] / 32;
-  uint32 chunk_mask = 0;
+  uint32_t chunk_mask = 0;
   for (auto field : fields) {
   for (auto field : fields) {
     // "index" defines where in the _has_bits_ the field appears.
     // "index" defines where in the _has_bits_ the field appears.
     int index = has_bit_indices[field->index()];
     int index = has_bit_indices[field->index()];
     GOOGLE_CHECK_EQ(first_index_offset, index / 32);
     GOOGLE_CHECK_EQ(first_index_offset, index / 32);
-    chunk_mask |= static_cast<uint32>(1) << (index % 32);
+    chunk_mask |= static_cast<uint32_t>(1) << (index % 32);
   }
   }
   GOOGLE_CHECK_NE(0, chunk_mask);
   GOOGLE_CHECK_NE(0, chunk_mask);
   return chunk_mask;
   return chunk_mask;
 }
 }
 
 
 // Return the number of bits set in n, a non-negative integer.
 // Return the number of bits set in n, a non-negative integer.
-static int popcnt(uint32 n) {
+static int popcnt(uint32_t n) {
   int result = 0;
   int result = 0;
   while (n != 0) {
   while (n != 0) {
     result += (n & 1);
     result += (n & 1);
@@ -507,7 +508,7 @@ void ColdChunkSkipper::OnStartChunk(int chunk, int cached_has_word_index,
   format("if (PROTOBUF_PREDICT_FALSE(");
   format("if (PROTOBUF_PREDICT_FALSE(");
   int first_word = HasbitWord(chunk, 0);
   int first_word = HasbitWord(chunk, 0);
   while (chunk < limit_chunk_) {
   while (chunk < limit_chunk_) {
-    uint32 mask = 0;
+    uint32_t mask = 0;
     int this_word = HasbitWord(chunk, 0);
     int this_word = HasbitWord(chunk, 0);
     // Generate mask for chunks on the same word.
     // Generate mask for chunks on the same word.
     for (; chunk < limit_chunk_ && HasbitWord(chunk, 0) == this_word; chunk++) {
     for (; chunk < limit_chunk_ && HasbitWord(chunk, 0) == this_word; chunk++) {
@@ -1654,8 +1655,8 @@ namespace {
 
 
 // We need to calculate for each field what function the table driven code
 // We need to calculate for each field what function the table driven code
 // should use to serialize it. This returns the index in a lookup table.
 // should use to serialize it. This returns the index in a lookup table.
-uint32 CalcFieldNum(const FieldGenerator& generator,
-                    const FieldDescriptor* field, const Options& options) {
+uint32_t CalcFieldNum(const FieldGenerator& generator,
+                      const FieldDescriptor* field, const Options& options) {
   bool is_a_map = IsMapEntryMessage(field->containing_type());
   bool is_a_map = IsMapEntryMessage(field->containing_type());
   int type = field->type();
   int type = field->type();
   if (type == FieldDescriptor::TYPE_STRING ||
   if (type == FieldDescriptor::TYPE_STRING ||
@@ -1707,7 +1708,7 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) {
       const FieldDescriptor* field = sorted[i];
       const FieldDescriptor* field = sorted[i];
       const FieldGenerator& generator = field_generators_.get(field);
       const FieldGenerator& generator = field_generators_.get(field);
 
 
-      uint32 tag = internal::WireFormatLite::MakeTag(
+      uint32_t tag = internal::WireFormatLite::MakeTag(
           field->number(), WireFormat::WireTypeForFieldType(field->type()));
           field->number(), WireFormat::WireTypeForFieldType(field->type()));
 
 
       std::map<std::string, std::string> vars;
       std::map<std::string, std::string> vars;
@@ -1764,7 +1765,7 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) {
     if (i == sorted.size()) break;
     if (i == sorted.size()) break;
     const FieldDescriptor* field = sorted[i];
     const FieldDescriptor* field = sorted[i];
 
 
-    uint32 tag = internal::WireFormatLite::MakeTag(
+    uint32_t tag = internal::WireFormatLite::MakeTag(
         field->number(), WireFormat::WireTypeForFieldType(field->type()));
         field->number(), WireFormat::WireTypeForFieldType(field->type()));
     if (field->is_packed()) {
     if (field->is_packed()) {
       tag = internal::WireFormatLite::MakeTag(
       tag = internal::WireFormatLite::MakeTag(
@@ -1827,7 +1828,7 @@ int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) {
           tag);
           tag);
     } else if (field->real_containing_oneof()) {
     } else if (field->real_containing_oneof()) {
       format.Set("oneofoffset",
       format.Set("oneofoffset",
-                 sizeof(uint32) * field->containing_oneof()->index());
+                 sizeof(uint32_t) * field->containing_oneof()->index());
       format(
       format(
           "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$,"
           "{PROTOBUF_FIELD_OFFSET($classtype$, $field_name$_), $1$,"
           " PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_) + "
           " PROTOBUF_FIELD_OFFSET($classtype$, _oneof_case_) + "
@@ -1933,7 +1934,7 @@ void MessageGenerator::GenerateClassMethods(io::Printer* printer) {
     }
     }
   }
   }
   if (num_required_fields_ > 0) {
   if (num_required_fields_ > 0) {
-    const std::vector<uint32> masks_for_has_bits = RequiredFieldsBitMask();
+    const std::vector<uint32_t> masks_for_has_bits = RequiredFieldsBitMask();
     format(
     format(
         "static bool MissingRequiredFields(const HasBits& has_bits) "
         "static bool MissingRequiredFields(const HasBits& has_bits) "
         "{\n"
         "{\n"
@@ -2751,7 +2752,7 @@ void MessageGenerator::GenerateClear(io::Printer* printer) {
 
 
     if (have_outer_if) {
     if (have_outer_if) {
       // Emit an if() that will let us skip the whole chunk if none are set.
       // Emit an if() that will let us skip the whole chunk if none are set.
-      uint32 chunk_mask = GenChunkMask(chunk, has_bit_indices_);
+      uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_);
       std::string chunk_mask_str =
       std::string chunk_mask_str =
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
 
 
@@ -3055,7 +3056,7 @@ void MessageGenerator::GenerateClassSpecificMergeFrom(io::Printer* printer) {
 
 
     if (have_outer_if) {
     if (have_outer_if) {
       // Emit an if() that will let us skip the whole chunk if none are set.
       // Emit an if() that will let us skip the whole chunk if none are set.
-      uint32 chunk_mask = GenChunkMask(chunk, has_bit_indices_);
+      uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_);
       std::string chunk_mask_str =
       std::string chunk_mask_str =
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
 
 
@@ -3445,6 +3446,12 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(
       }
       }
     }
     }
 
 
+    void EmitIfNotNull(const FieldDescriptor* field) {
+      if (field != nullptr) {
+        Emit(field);
+      }
+    }
+
     void Flush() {
     void Flush() {
       if (!v_.empty()) {
       if (!v_.empty()) {
         mg_->GenerateSerializeOneofFields(format_.printer(), v_);
         mg_->GenerateSerializeOneofFields(format_.printer(), v_);
@@ -3471,6 +3478,61 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(
     int cached_has_bit_index_;
     int cached_has_bit_index_;
   };
   };
 
 
+  class LazyExtensionRangeEmitter {
+   public:
+    LazyExtensionRangeEmitter(MessageGenerator* mg, io::Printer* printer)
+        : mg_(mg), format_(printer) {}
+
+    void AddToRange(const Descriptor::ExtensionRange* range) {
+      if (!has_current_range_) {
+        current_combined_range_ = *range;
+        has_current_range_ = true;
+      } else {
+        current_combined_range_.start =
+            std::min(current_combined_range_.start, range->start);
+        current_combined_range_.end =
+            std::max(current_combined_range_.end, range->end);
+      }
+    }
+
+    void Flush() {
+      if (has_current_range_) {
+        mg_->GenerateSerializeOneExtensionRange(format_.printer(),
+                                                &current_combined_range_);
+      }
+      has_current_range_ = false;
+    }
+
+   private:
+    MessageGenerator* mg_;
+    Formatter format_;
+    bool has_current_range_ = false;
+    Descriptor::ExtensionRange current_combined_range_;
+  };
+
+  // We need to track the largest weak field, because weak fields are serialized
+  // differently than normal fields.  The WeakFieldMap::FieldWriter will
+  // serialize all weak fields that are ordinally between the last serialized
+  // weak field and the current field.  In order to guarantee that all weak
+  // fields are serialized, we need to make sure to emit the code to serialize
+  // the largest weak field present at some point.
+  class LargestWeakFieldHolder {
+   public:
+    const FieldDescriptor* Release() {
+      const FieldDescriptor* result = field_;
+      field_ = nullptr;
+      return result;
+    }
+    void ReplaceIfLarger(const FieldDescriptor* field) {
+      if (field_ == nullptr || field_->number() < field->number()) {
+        field_ = field;
+      }
+    }
+
+   private:
+    const FieldDescriptor* field_ = nullptr;
+  };
+
   std::vector<const FieldDescriptor*> ordered_fields =
   std::vector<const FieldDescriptor*> ordered_fields =
       SortFieldsByNumber(descriptor_);
       SortFieldsByNumber(descriptor_);
 
 
@@ -3494,7 +3556,8 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(
   // Merge the fields and the extension ranges, both sorted by field number.
   // Merge the fields and the extension ranges, both sorted by field number.
   {
   {
     LazySerializerEmitter e(this, printer);
     LazySerializerEmitter e(this, printer);
-    const FieldDescriptor* last_weak_field = nullptr;
+    LazyExtensionRangeEmitter re(this, printer);
+    LargestWeakFieldHolder largest_weak_field;
     int i, j;
     int i, j;
     for (i = 0, j = 0;
     for (i = 0, j = 0;
          i < ordered_fields.size() || j < sorted_extensions.size();) {
          i < ordered_fields.size() || j < sorted_extensions.size();) {
@@ -3505,31 +3568,22 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(
         if (IsFieldStripped(field, options_)) {
         if (IsFieldStripped(field, options_)) {
           continue;
           continue;
         }
         }
+        re.Flush();
         if (field->options().weak()) {
         if (field->options().weak()) {
-          if (last_weak_field == nullptr ||
-              last_weak_field->number() < field->number()) {
-            last_weak_field = field;
-          }
+          largest_weak_field.ReplaceIfLarger(field);
           PrintFieldComment(format, field);
           PrintFieldComment(format, field);
         } else {
         } else {
-          if (last_weak_field != nullptr) {
-            e.Emit(last_weak_field);
-            last_weak_field = nullptr;
-          }
+          e.EmitIfNotNull(largest_weak_field.Release());
           e.Emit(field);
           e.Emit(field);
         }
         }
       } else {
       } else {
-        if (last_weak_field != nullptr) {
-          e.Emit(last_weak_field);
-          last_weak_field = nullptr;
-        }
+        e.EmitIfNotNull(largest_weak_field.Release());
         e.Flush();
         e.Flush();
-        GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]);
+        re.AddToRange(sorted_extensions[j++]);
       }
       }
     }
     }
-    if (last_weak_field != nullptr) {
-      e.Emit(last_weak_field);
-    }
+    re.Flush();
+    e.EmitIfNotNull(largest_weak_field.Release());
   }
   }
 
 
   std::map<std::string, std::string> vars;
   std::map<std::string, std::string> vars;
@@ -3644,9 +3698,9 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBodyShuffled(
   format("}\n");
   format("}\n");
 }
 }
 
 
-std::vector<uint32> MessageGenerator::RequiredFieldsBitMask() const {
+std::vector<uint32_t> MessageGenerator::RequiredFieldsBitMask() const {
   const int array_size = HasBitsSize();
   const int array_size = HasBitsSize();
-  std::vector<uint32> masks(array_size, 0);
+  std::vector<uint32_t> masks(array_size, 0);
 
 
   for (auto field : FieldRange(descriptor_)) {
   for (auto field : FieldRange(descriptor_)) {
     if (!field->is_required()) {
     if (!field->is_required()) {
@@ -3654,7 +3708,8 @@ std::vector<uint32> MessageGenerator::RequiredFieldsBitMask() const {
     }
     }
 
 
     const int has_bit_index = has_bit_indices_[field->index()];
     const int has_bit_index = has_bit_indices_[field->index()];
-    masks[has_bit_index / 32] |= static_cast<uint32>(1) << (has_bit_index % 32);
+    masks[has_bit_index / 32] |= static_cast<uint32_t>(1)
+                                 << (has_bit_index % 32);
   }
   }
   return masks;
   return masks;
 }
 }
@@ -3735,7 +3790,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) {
   // present then the fast path executes; otherwise the slow path executes.
   // present then the fast path executes; otherwise the slow path executes.
   if (num_required_fields_ > 1) {
   if (num_required_fields_ > 1) {
     // The fast path works if all required fields are present.
     // The fast path works if all required fields are present.
-    const std::vector<uint32> masks_for_has_bits = RequiredFieldsBitMask();
+    const std::vector<uint32_t> masks_for_has_bits = RequiredFieldsBitMask();
     format("if ($1$) {  // All required fields are present.\n",
     format("if ($1$) {  // All required fields are present.\n",
            ConditionalToCheckBitmasks(masks_for_has_bits));
            ConditionalToCheckBitmasks(masks_for_has_bits));
     format.Indent();
     format.Indent();
@@ -3791,7 +3846,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* printer) {
 
 
     if (have_outer_if) {
     if (have_outer_if) {
       // Emit an if() that will let us skip the whole chunk if none are set.
       // Emit an if() that will let us skip the whole chunk if none are set.
-      uint32 chunk_mask = GenChunkMask(chunk, has_bit_indices_);
+      uint32_t chunk_mask = GenChunkMask(chunk, has_bit_indices_);
       std::string chunk_mask_str =
       std::string chunk_mask_str =
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
           StrCat(strings::Hex(chunk_mask, strings::ZERO_PAD_8));
 
 

+ 3 - 1
src/google/protobuf/compiler/cpp/cpp_message.h

@@ -35,9 +35,11 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
 #define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
 #define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
 
 
+#include <cstdint>
 #include <memory>
 #include <memory>
 #include <set>
 #include <set>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/cpp/cpp_field.h>
 #include <google/protobuf/compiler/cpp/cpp_field.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_message_layout_helper.h>
 #include <google/protobuf/compiler/cpp/cpp_message_layout_helper.h>
@@ -179,7 +181,7 @@ class MessageGenerator {
   int HasByteIndex(const FieldDescriptor* a) const;
   int HasByteIndex(const FieldDescriptor* a) const;
   int HasWordIndex(const FieldDescriptor* a) const;
   int HasWordIndex(const FieldDescriptor* a) const;
   bool SameHasByte(const FieldDescriptor* a, const FieldDescriptor* b) const;
   bool SameHasByte(const FieldDescriptor* a, const FieldDescriptor* b) const;
-  std::vector<uint32> RequiredFieldsBitMask() const;
+  std::vector<uint32_t> RequiredFieldsBitMask() const;
 
 
   const Descriptor* descriptor_;
   const Descriptor* descriptor_;
   int index_in_file_messages_;
   int index_in_file_messages_;

+ 5 - 0
src/google/protobuf/compiler/cpp/cpp_names.h

@@ -40,6 +40,7 @@ namespace protobuf {
 
 
 class Descriptor;
 class Descriptor;
 class EnumDescriptor;
 class EnumDescriptor;
+class EnumValueDescriptor;
 class FieldDescriptor;
 class FieldDescriptor;
 
 
 namespace compiler {
 namespace compiler {
@@ -72,6 +73,10 @@ std::string QualifiedExtensionName(const FieldDescriptor* d);
 // anyway, so normally this just returns field->name().
 // anyway, so normally this just returns field->name().
 std::string FieldName(const FieldDescriptor* field);
 std::string FieldName(const FieldDescriptor* field);
 
 
+// Get the (unqualified) name that should be used for this enum value in C++
+// code.
+std::string EnumValueName(const EnumValueDescriptor* enum_value);
+
 // Strips ".proto" or ".protodevel" from the end of a filename.
 // Strips ".proto" or ".protodevel" from the end of a filename.
 PROTOC_EXPORT std::string StripProto(const std::string& filename);
 PROTOC_EXPORT std::string StripProto(const std::string& filename);
 
 

+ 18 - 14
src/google/protobuf/compiler/cpp/cpp_primitive_field.cc

@@ -33,6 +33,7 @@
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
+
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/wire_format.h>
@@ -301,7 +302,7 @@ void RepeatedPrimitiveFieldGenerator::GeneratePrivateMembers(
     io::Printer* printer) const {
     io::Printer* printer) const {
   Formatter format(printer, variables_);
   Formatter format(printer, variables_);
   format("::$proto_ns$::RepeatedField< $type$ > $name$_;\n");
   format("::$proto_ns$::RepeatedField< $type$ > $name$_;\n");
-  if (descriptor_->is_packed() &&
+  if (descriptor_->is_packed() && FixedSize(descriptor_->type()) == -1 &&
       HasGeneratedMethods(descriptor_->file(), options_)) {
       HasGeneratedMethods(descriptor_->file(), options_)) {
     format("mutable std::atomic<int> _$name$_cached_byte_size_;\n");
     format("mutable std::atomic<int> _$name$_cached_byte_size_;\n");
   }
   }
@@ -409,13 +410,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray(
     io::Printer* printer) const {
     io::Printer* printer) const {
   Formatter format(printer, variables_);
   Formatter format(printer, variables_);
   if (descriptor_->is_packed()) {
   if (descriptor_->is_packed()) {
-    if (FixedSize(descriptor_->type()) > 0) {
-      format(
-          "if (this->_internal_$name$_size() > 0) {\n"
-          "  target = stream->WriteFixedPacked($number$, _internal_$name$(), "
-          "target);\n"
-          "}\n");
-    } else {
+    if (FixedSize(descriptor_->type()) == -1) {
       format(
       format(
           "{\n"
           "{\n"
           "  int byte_size = "
           "  int byte_size = "
@@ -425,6 +420,12 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializeWithCachedSizesToArray(
           "        $number$, _internal_$name$(), byte_size, target);\n"
           "        $number$, _internal_$name$(), byte_size, target);\n"
           "  }\n"
           "  }\n"
           "}\n");
           "}\n");
+    } else {
+      format(
+          "if (this->_internal_$name$_size() > 0) {\n"
+          "  target = stream->WriteFixedPacked($number$, _internal_$name$(), "
+          "target);\n"
+          "}\n");
     }
     }
   } else {
   } else {
     format(
     format(
@@ -460,11 +461,14 @@ void RepeatedPrimitiveFieldGenerator::GenerateByteSize(
         "  total_size += $tag_size$ +\n"
         "  total_size += $tag_size$ +\n"
         "    ::$proto_ns$::internal::WireFormatLite::Int32Size(\n"
         "    ::$proto_ns$::internal::WireFormatLite::Int32Size(\n"
         "        static_cast<$int32$>(data_size));\n"
         "        static_cast<$int32$>(data_size));\n"
-        "}\n"
-        "int cached_size = ::$proto_ns$::internal::ToCachedSize(data_size);\n"
-        "_$name$_cached_byte_size_.store(cached_size,\n"
-        "                                std::memory_order_relaxed);\n"
-        "total_size += data_size;\n");
+        "}\n");
+    if (FixedSize(descriptor_->type()) == -1) {
+      format(
+          "int cached_size = ::$proto_ns$::internal::ToCachedSize(data_size);\n"
+          "_$name$_cached_byte_size_.store(cached_size,\n"
+          "                                std::memory_order_relaxed);\n");
+    }
+    format("total_size += data_size;\n");
   } else {
   } else {
     format(
     format(
         "total_size += $tag_size$ *\n"
         "total_size += $tag_size$ *\n"
@@ -480,7 +484,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateConstinitInitializer(
     io::Printer* printer) const {
     io::Printer* printer) const {
   Formatter format(printer, variables_);
   Formatter format(printer, variables_);
   format("$name$_()");
   format("$name$_()");
-  if (descriptor_->is_packed() &&
+  if (descriptor_->is_packed() && FixedSize(descriptor_->type()) == -1 &&
       HasGeneratedMethods(descriptor_->file(), options_)) {
       HasGeneratedMethods(descriptor_->file(), options_)) {
     format("\n, _$name$_cached_byte_size_()");
     format("\n, _$name$_cached_byte_size_()");
   }
   }

+ 29 - 23
src/google/protobuf/compiler/cpp/cpp_unittest.inc

@@ -44,6 +44,8 @@
 // correctly and produces the interfaces we expect, which is why this test
 // correctly and produces the interfaces we expect, which is why this test
 // is written this way.
 // is written this way.
 
 
+#include <cstdint>
+#include <limits>
 #include <memory>
 #include <memory>
 #include <vector>
 #include <vector>
 
 
@@ -203,10 +205,13 @@ TEST(GENERATED_MESSAGE_TEST_NAME, Trigraph) {
 TEST(GENERATED_MESSAGE_TEST_NAME, ExtremeSmallIntegerDefault) {
 TEST(GENERATED_MESSAGE_TEST_NAME, ExtremeSmallIntegerDefault) {
   const UNITTEST::TestExtremeDefaultValues& extreme_default =
   const UNITTEST::TestExtremeDefaultValues& extreme_default =
       UNITTEST::TestExtremeDefaultValues::default_instance();
       UNITTEST::TestExtremeDefaultValues::default_instance();
-  EXPECT_EQ(~0x7fffffff, kint32min);
-  EXPECT_EQ(PROTOBUF_LONGLONG(~0x7fffffffffffffff), kint64min);
-  EXPECT_EQ(kint32min, extreme_default.really_small_int32());
-  EXPECT_EQ(kint64min, extreme_default.really_small_int64());
+  EXPECT_EQ(~0x7fffffff, std::numeric_limits<int32_t>::min());
+  EXPECT_EQ(PROTOBUF_LONGLONG(~0x7fffffffffffffff),
+            std::numeric_limits<int64_t>::min());
+  EXPECT_EQ(std::numeric_limits<int32_t>::min(),
+            extreme_default.really_small_int32());
+  EXPECT_EQ(std::numeric_limits<int64_t>::min(),
+            extreme_default.really_small_int64());
 }
 }
 
 
 TEST(GENERATED_MESSAGE_TEST_NAME, Accessors) {
 TEST(GENERATED_MESSAGE_TEST_NAME, Accessors) {
@@ -369,7 +374,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ClearOneField) {
   UNITTEST::TestAllTypes message;
   UNITTEST::TestAllTypes message;
 
 
   TestUtil::SetAllFields(&message);
   TestUtil::SetAllFields(&message);
-  int64 original_value = message.optional_int64();
+  int64_t original_value = message.optional_int64();
 
 
   // Clear the field and make sure it shows up as cleared.
   // Clear the field and make sure it shows up as cleared.
   message.clear_optional_int64();
   message.clear_optional_int64();
@@ -498,7 +503,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ADLSwap) {
 
 
   // Note the address of one of the repeated fields, to verify it was swapped
   // Note the address of one of the repeated fields, to verify it was swapped
   // rather than copied.
   // rather than copied.
-  const int32* addr = &message1.repeated_int32().Get(0);
+  const int32_t* addr = &message1.repeated_int32().Get(0);
 
 
   using std::swap;
   using std::swap;
   swap(message1, message2);
   swap(message1, message2);
@@ -648,7 +653,7 @@ TEST(GENERATED_MESSAGE_TEST_NAME, NonEmptyMergeFrom) {
 
 
   // This tests concatenating.
   // This tests concatenating.
   message2.add_repeated_int32(message1.repeated_int32(1));
   message2.add_repeated_int32(message1.repeated_int32(1));
-  int32 i = message1.repeated_int32(0);
+  int32_t i = message1.repeated_int32(0);
   message1.clear_repeated_int32();
   message1.clear_repeated_int32();
   message1.add_repeated_int32(i);
   message1.add_repeated_int32(i);
 
 
@@ -665,8 +670,8 @@ TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToArray) {
   TestUtil::SetAllFields(&message1);
   TestUtil::SetAllFields(&message1);
   int size = message1.ByteSizeLong();
   int size = message1.ByteSizeLong();
   data.resize(size);
   data.resize(size);
-  uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-  uint8* end = message1.SerializeWithCachedSizesToArray(start);
+  uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+  uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
   EXPECT_EQ(size, end - start);
   EXPECT_EQ(size, end - start);
   EXPECT_TRUE(message2.ParseFromString(data));
   EXPECT_TRUE(message2.ParseFromString(data));
   TestUtil::ExpectAllFieldsSet(message2);
   TestUtil::ExpectAllFieldsSet(message2);
@@ -679,8 +684,9 @@ TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToArray) {
   TestUtil::SetPackedFields(&packed_message1);
   TestUtil::SetPackedFields(&packed_message1);
   int packed_size = packed_message1.ByteSizeLong();
   int packed_size = packed_message1.ByteSizeLong();
   packed_data.resize(packed_size);
   packed_data.resize(packed_size);
-  uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&packed_data));
-  uint8* end = packed_message1.SerializeWithCachedSizesToArray(start);
+  uint8_t* start =
+      reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&packed_data));
+  uint8_t* end = packed_message1.SerializeWithCachedSizesToArray(start);
   EXPECT_EQ(packed_size, end - start);
   EXPECT_EQ(packed_size, end - start);
   EXPECT_TRUE(packed_message2.ParseFromString(packed_data));
   EXPECT_TRUE(packed_message2.ParseFromString(packed_data));
   TestUtil::ExpectPackedFieldsSet(packed_message2);
   TestUtil::ExpectPackedFieldsSet(packed_message2);
@@ -1846,8 +1852,8 @@ std::string data;
 message1.set_foo_int(123);
 message1.set_foo_int(123);
 int size = message1.ByteSizeLong();
 int size = message1.ByteSizeLong();
 data.resize(size);
 data.resize(size);
-uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-uint8* end = message1.SerializeWithCachedSizesToArray(start);
+uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
 EXPECT_EQ(size, end - start);
 EXPECT_EQ(size, end - start);
 EXPECT_TRUE(message2.ParseFromString(data));
 EXPECT_TRUE(message2.ParseFromString(data));
 EXPECT_EQ(message2.foo_int(), 123);
 EXPECT_EQ(message2.foo_int(), 123);
@@ -1860,8 +1866,8 @@ EXPECT_EQ(message2.foo_int(), 123);
     message1.set_foo_string("foo");
     message1.set_foo_string("foo");
     int size = message1.ByteSizeLong();
     int size = message1.ByteSizeLong();
     data.resize(size);
     data.resize(size);
-    uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-    uint8* end = message1.SerializeWithCachedSizesToArray(start);
+    uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+    uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
     EXPECT_EQ(size, end - start);
     EXPECT_EQ(size, end - start);
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_EQ(message2.foo_string(), "foo");
     EXPECT_EQ(message2.foo_string(), "foo");
@@ -1875,8 +1881,8 @@ EXPECT_EQ(message2.foo_int(), 123);
     message1.set_foo_bytes("qux");
     message1.set_foo_bytes("qux");
     int size = message1.ByteSizeLong();
     int size = message1.ByteSizeLong();
     data.resize(size);
     data.resize(size);
-    uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-    uint8* end = message1.SerializeWithCachedSizesToArray(start);
+    uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+    uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
     EXPECT_EQ(size, end - start);
     EXPECT_EQ(size, end - start);
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_EQ(message2.foo_bytes(), "qux");
     EXPECT_EQ(message2.foo_bytes(), "qux");
@@ -1889,8 +1895,8 @@ EXPECT_EQ(message2.foo_int(), 123);
     message1.set_foo_enum(UNITTEST::TestOneof2::FOO);
     message1.set_foo_enum(UNITTEST::TestOneof2::FOO);
     int size = message1.ByteSizeLong();
     int size = message1.ByteSizeLong();
     data.resize(size);
     data.resize(size);
-    uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-    uint8* end = message1.SerializeWithCachedSizesToArray(start);
+    uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+    uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
     EXPECT_EQ(size, end - start);
     EXPECT_EQ(size, end - start);
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO);
     EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO);
@@ -1903,8 +1909,8 @@ EXPECT_EQ(message2.foo_int(), 123);
     message1.mutable_foo_message()->set_qux_int(234);
     message1.mutable_foo_message()->set_qux_int(234);
     int size = message1.ByteSizeLong();
     int size = message1.ByteSizeLong();
     data.resize(size);
     data.resize(size);
-    uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-    uint8* end = message1.SerializeWithCachedSizesToArray(start);
+    uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+    uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
     EXPECT_EQ(size, end - start);
     EXPECT_EQ(size, end - start);
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_EQ(message2.foo_message().qux_int(), 234);
     EXPECT_EQ(message2.foo_message().qux_int(), 234);
@@ -1917,8 +1923,8 @@ EXPECT_EQ(message2.foo_int(), 123);
     message1.mutable_foogroup()->set_a(345);
     message1.mutable_foogroup()->set_a(345);
     int size = message1.ByteSizeLong();
     int size = message1.ByteSizeLong();
     data.resize(size);
     data.resize(size);
-    uint8* start = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&data));
-    uint8* end = message1.SerializeWithCachedSizesToArray(start);
+    uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data));
+    uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
     EXPECT_EQ(size, end - start);
     EXPECT_EQ(size, end - start);
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_TRUE(message2.ParseFromString(data));
     EXPECT_EQ(message2.foogroup().a(), 345);
     EXPECT_EQ(message2.foogroup().a(), 345);

+ 4 - 2
src/google/protobuf/compiler/java/java_enum_field.cc

@@ -32,6 +32,9 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_enum_field.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -39,7 +42,6 @@
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
-#include <google/protobuf/compiler/java/java_enum_field.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
@@ -67,7 +69,7 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
   (*variables)["default_number"] =
   (*variables)["default_number"] =
       StrCat(descriptor->default_value_enum()->number());
       StrCat(descriptor->default_value_enum()->number());
   (*variables)["tag"] = StrCat(
   (*variables)["tag"] = StrCat(
-      static_cast<int32>(internal::WireFormat::MakeTag(descriptor)));
+      static_cast<int32_t>(internal::WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
   // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported

+ 7 - 5
src/google/protobuf/compiler/java/java_enum_field_lite.cc

@@ -32,6 +32,9 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_enum_field_lite.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -39,7 +42,6 @@
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
-#include <google/protobuf/compiler/java/java_enum_field_lite.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
@@ -74,7 +76,7 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
   (*variables)["default_number"] =
   (*variables)["default_number"] =
       StrCat(descriptor->default_value_enum()->number());
       StrCat(descriptor->default_value_enum()->number());
   (*variables)["tag"] = StrCat(
   (*variables)["tag"] = StrCat(
-      static_cast<int32>(internal::WireFormat::MakeTag(descriptor)));
+      static_cast<int32_t>(internal::WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
   // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
@@ -281,7 +283,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
 }
 }
 
 
 void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -377,7 +379,7 @@ void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -632,7 +634,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
 void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);

+ 5 - 3
src/google/protobuf/compiler/java/java_enum_field_lite.h

@@ -35,8 +35,10 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_FIELD_LITE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_FIELD_LITE_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/java/java_field.h>
 #include <google/protobuf/compiler/java/java_field.h>
 
 
 namespace google {
 namespace google {
@@ -70,7 +72,7 @@ class ImmutableEnumFieldLiteGenerator : public ImmutableFieldLiteGenerator {
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 
@@ -95,7 +97,7 @@ class ImmutableEnumOneofFieldLiteGenerator
   void GenerateMembers(io::Printer* printer) const;
   void GenerateMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
  private:
  private:
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableEnumOneofFieldLiteGenerator);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableEnumOneofFieldLiteGenerator);
@@ -115,7 +117,7 @@ class RepeatedImmutableEnumFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 

+ 2 - 1
src/google/protobuf/compiler/java/java_field.h

@@ -35,6 +35,7 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
@@ -104,7 +105,7 @@ class ImmutableFieldLiteGenerator {
   virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
   virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
   virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
   virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
   virtual void GenerateFieldInfo(io::Printer* printer,
   virtual void GenerateFieldInfo(io::Printer* printer,
-                                 std::vector<uint16>* output) const = 0;
+                                 std::vector<uint16_t>* output) const = 0;
 
 
   virtual std::string GetBoxedType() const = 0;
   virtual std::string GetBoxedType() const = 0;
 
 

+ 9 - 8
src/google/protobuf/compiler/java/java_helpers.cc

@@ -35,6 +35,7 @@
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <limits>
 #include <limits>
 #include <unordered_set>
 #include <unordered_set>
 #include <vector>
 #include <vector>
@@ -500,11 +501,11 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
       return StrCat(field->default_value_int32());
       return StrCat(field->default_value_int32());
     case FieldDescriptor::CPPTYPE_UINT32:
     case FieldDescriptor::CPPTYPE_UINT32:
       // Need to print as a signed int since Java has no unsigned.
       // Need to print as a signed int since Java has no unsigned.
-      return StrCat(static_cast<int32>(field->default_value_uint32()));
+      return StrCat(static_cast<int32_t>(field->default_value_uint32()));
     case FieldDescriptor::CPPTYPE_INT64:
     case FieldDescriptor::CPPTYPE_INT64:
       return StrCat(field->default_value_int64()) + "L";
       return StrCat(field->default_value_int64()) + "L";
     case FieldDescriptor::CPPTYPE_UINT64:
     case FieldDescriptor::CPPTYPE_UINT64:
-      return StrCat(static_cast<int64>(field->default_value_uint64())) +
+      return StrCat(static_cast<int64_t>(field->default_value_uint64())) +
              "L";
              "L";
     case FieldDescriptor::CPPTYPE_DOUBLE: {
     case FieldDescriptor::CPPTYPE_DOUBLE: {
       double value = field->default_value_double();
       double value = field->default_value_double();
@@ -896,11 +897,11 @@ bool HasRepeatedFields(const Descriptor* descriptor) {
 //
 //
 // Note that we only use code points in [0x0000, 0xD7FF] and [0xE000, 0xFFFF].
 // Note that we only use code points in [0x0000, 0xD7FF] and [0xE000, 0xFFFF].
 // There will be no surrogate pairs in the encoded character sequence.
 // There will be no surrogate pairs in the encoded character sequence.
-void WriteUInt32ToUtf16CharSequence(uint32 number,
-                                    std::vector<uint16>* output) {
+void WriteUInt32ToUtf16CharSequence(uint32_t number,
+                                    std::vector<uint16_t>* output) {
   // For values in [0x0000, 0xD7FF], only use one char to encode it.
   // For values in [0x0000, 0xD7FF], only use one char to encode it.
   if (number < 0xD800) {
   if (number < 0xD800) {
-    output->push_back(static_cast<uint16>(number));
+    output->push_back(static_cast<uint16_t>(number));
     return;
     return;
   }
   }
   // Encode into multiple chars. All except the last char will be in the range
   // Encode into multiple chars. All except the last char will be in the range
@@ -910,10 +911,10 @@ void WriteUInt32ToUtf16CharSequence(uint32 number,
   // them.
   // them.
   while (number >= 0xD800) {
   while (number >= 0xD800) {
     // [0xE000, 0xFFFF] can represent 13 bits of info.
     // [0xE000, 0xFFFF] can represent 13 bits of info.
-    output->push_back(static_cast<uint16>(0xE000 | (number & 0x1FFF)));
+    output->push_back(static_cast<uint16_t>(0xE000 | (number & 0x1FFF)));
     number >>= 13;
     number >>= 13;
   }
   }
-  output->push_back(static_cast<uint16>(number));
+  output->push_back(static_cast<uint16_t>(number));
 }
 }
 
 
 int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) {
 int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) {
@@ -994,7 +995,7 @@ int GetExperimentalJavaFieldType(const FieldDescriptor* field) {
 }
 }
 
 
 // Escape a UTF-16 character to be embedded in a Java string.
 // Escape a UTF-16 character to be embedded in a Java string.
-void EscapeUtf16ToString(uint16 code, std::string* output) {
+void EscapeUtf16ToString(uint16_t code, std::string* output) {
   if (code == '\t') {
   if (code == '\t') {
     output->append("\\t");
     output->append("\\t");
   } else if (code == '\b') {
   } else if (code == '\b') {

+ 7 - 4
src/google/protobuf/compiler/java/java_helpers.h

@@ -35,7 +35,9 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
 
 
+#include <cstdint>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
@@ -412,15 +414,16 @@ inline std::string GeneratedCodeVersionSuffix() {
   return "V3";
   return "V3";
 }
 }
 
 
-void WriteUInt32ToUtf16CharSequence(uint32 number, std::vector<uint16>* output);
+void WriteUInt32ToUtf16CharSequence(uint32_t number,
+                                    std::vector<uint16_t>* output);
 
 
 inline void WriteIntToUtf16CharSequence(int value,
 inline void WriteIntToUtf16CharSequence(int value,
-                                        std::vector<uint16>* output) {
-  WriteUInt32ToUtf16CharSequence(static_cast<uint32>(value), output);
+                                        std::vector<uint16_t>* output) {
+  WriteUInt32ToUtf16CharSequence(static_cast<uint32_t>(value), output);
 }
 }
 
 
 // Escape a UTF-16 character so it can be embedded in a Java string literal.
 // Escape a UTF-16 character so it can be embedded in a Java string literal.
-void EscapeUtf16ToString(uint16 code, std::string* output);
+void EscapeUtf16ToString(uint16_t code, std::string* output);
 
 
 // Only the lowest two bytes of the return value are used. The lowest byte
 // Only the lowest two bytes of the return value are used. The lowest byte
 // is the integer value of a j/c/g/protobuf/FieldType enum. For the other
 // is the integer value of a j/c/g/protobuf/FieldType enum. For the other

+ 3 - 1
src/google/protobuf/compiler/java/java_map_field_lite.cc

@@ -30,6 +30,8 @@
 
 
 #include <google/protobuf/compiler/java/java_map_field_lite.h>
 #include <google/protobuf/compiler/java/java_map_field_lite.h>
 
 
+#include <cstdint>
+
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
@@ -505,7 +507,7 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void ImmutableMapFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableMapFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);

+ 3 - 1
src/google/protobuf/compiler/java/java_map_field_lite.h

@@ -31,6 +31,8 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__
 
 
+#include <cstdint>
+
 #include <google/protobuf/compiler/java/java_field.h>
 #include <google/protobuf/compiler/java/java_field.h>
 
 
 namespace google {
 namespace google {
@@ -52,7 +54,7 @@ class ImmutableMapFieldLiteGenerator : public ImmutableFieldLiteGenerator {
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 

+ 5 - 4
src/google/protobuf/compiler/java/java_message.cc

@@ -35,6 +35,7 @@
 #include <google/protobuf/compiler/java/java_message.h>
 #include <google/protobuf/compiler/java/java_message.h>
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <vector>
 #include <vector>
@@ -1213,11 +1214,11 @@ void ImmutableMessageGenerator::GenerateParsingConstructor(
 
 
   for (int i = 0; i < descriptor_->field_count(); i++) {
   for (int i = 0; i < descriptor_->field_count(); i++) {
     const FieldDescriptor* field = sorted_fields[i];
     const FieldDescriptor* field = sorted_fields[i];
-    uint32 tag = WireFormatLite::MakeTag(
+    uint32_t tag = WireFormatLite::MakeTag(
         field->number(), WireFormat::WireTypeForFieldType(field->type()));
         field->number(), WireFormat::WireTypeForFieldType(field->type()));
 
 
     printer->Print("case $tag$: {\n", "tag",
     printer->Print("case $tag$: {\n", "tag",
-                   StrCat(static_cast<int32>(tag)));
+                   StrCat(static_cast<int32_t>(tag)));
     printer->Indent();
     printer->Indent();
 
 
     field_generators_.get(field).GenerateParsingCode(printer);
     field_generators_.get(field).GenerateParsingCode(printer);
@@ -1230,10 +1231,10 @@ void ImmutableMessageGenerator::GenerateParsingConstructor(
     if (field->is_packable()) {
     if (field->is_packable()) {
       // To make packed = true wire compatible, we generate parsing code from a
       // To make packed = true wire compatible, we generate parsing code from a
       // packed version of this field regardless of field->options().packed().
       // packed version of this field regardless of field->options().packed().
-      uint32 packed_tag = WireFormatLite::MakeTag(
+      uint32_t packed_tag = WireFormatLite::MakeTag(
           field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
           field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
       printer->Print("case $tag$: {\n", "tag",
       printer->Print("case $tag$: {\n", "tag",
-                     StrCat(static_cast<int32>(packed_tag)));
+                     StrCat(static_cast<int32_t>(packed_tag)));
       printer->Indent();
       printer->Indent();
 
 
       field_generators_.get(field).GenerateParsingCodeFromPacked(printer);
       field_generators_.get(field).GenerateParsingCodeFromPacked(printer);

+ 6 - 4
src/google/protobuf/compiler/java/java_message_field_lite.cc

@@ -32,13 +32,15 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_message_field_lite.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_context.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
-#include <google/protobuf/compiler/java/java_message_field_lite.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/wire_format.h>
@@ -276,7 +278,7 @@ void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
 }
 }
 
 
 void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -366,7 +368,7 @@ void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void ImmutableMessageOneofFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableMessageOneofFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -731,7 +733,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
 }
 }
 
 
 void RepeatedImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
 void RepeatedImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);

+ 5 - 3
src/google/protobuf/compiler/java/java_message_field_lite.h

@@ -35,8 +35,10 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_LITE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_LITE_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/java/java_field.h>
 #include <google/protobuf/compiler/java/java_field.h>
 
 
 namespace google {
 namespace google {
@@ -70,7 +72,7 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator {
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 
@@ -95,7 +97,7 @@ class ImmutableMessageOneofFieldLiteGenerator
   void GenerateMembers(io::Printer* printer) const;
   void GenerateMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
  private:
  private:
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageOneofFieldLiteGenerator);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageOneofFieldLiteGenerator);
@@ -115,7 +117,7 @@ class RepeatedImmutableMessageFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 

+ 3 - 2
src/google/protobuf/compiler/java/java_message_lite.cc

@@ -35,6 +35,7 @@
 #include <google/protobuf/compiler/java/java_message_lite.h>
 #include <google/protobuf/compiler/java/java_message_lite.h>
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 #include <vector>
 #include <vector>
@@ -471,7 +472,7 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
 
 
   // Collect field info into a sequence of UTF-16 chars. It will be embedded
   // Collect field info into a sequence of UTF-16 chars. It will be embedded
   // as a Java string in the generated code.
   // as a Java string in the generated code.
-  std::vector<uint16> chars;
+  std::vector<uint16_t> chars;
 
 
   int flags = 0;
   int flags = 0;
   if (IsProto2(descriptor_->file())) {
   if (IsProto2(descriptor_->file())) {
@@ -553,7 +554,7 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
   printer->Print("java.lang.String info =\n");
   printer->Print("java.lang.String info =\n");
   std::string line;
   std::string line;
   for (size_t i = 0; i < chars.size(); i++) {
   for (size_t i = 0; i < chars.size(); i++) {
-    uint16 code = chars[i];
+    uint16_t code = chars[i];
     EscapeUtf16ToString(code, &line);
     EscapeUtf16ToString(code, &line);
     if (line.size() >= 80) {
     if (line.size() >= 80) {
       printer->Print("    \"$string$\" +\n", "string", line);
       printer->Print("    \"$string$\" +\n", "string", line);

+ 4 - 2
src/google/protobuf/compiler/java/java_primitive_field.cc

@@ -32,6 +32,9 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_primitive_field.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -41,7 +44,6 @@
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
-#include <google/protobuf/compiler/java/java_primitive_field.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
@@ -112,7 +114,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
   (*variables)["capitalized_type"] =
   (*variables)["capitalized_type"] =
       GetCapitalizedType(descriptor, /* immutable = */ true);
       GetCapitalizedType(descriptor, /* immutable = */ true);
   (*variables)["tag"] =
   (*variables)["tag"] =
-      StrCat(static_cast<int32>(WireFormat::MakeTag(descriptor)));
+      StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   if (IsReferenceType(GetJavaType(descriptor))) {
   if (IsReferenceType(GetJavaType(descriptor))) {

+ 5 - 4
src/google/protobuf/compiler/java/java_primitive_field_lite.cc

@@ -34,6 +34,7 @@
 
 
 #include <google/protobuf/compiler/java/java_primitive_field_lite.h>
 #include <google/protobuf/compiler/java/java_primitive_field_lite.h>
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -78,7 +79,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
   (*variables)["capitalized_type"] =
   (*variables)["capitalized_type"] =
       GetCapitalizedType(descriptor, /* immutable = */ true);
       GetCapitalizedType(descriptor, /* immutable = */ true);
   (*variables)["tag"] =
   (*variables)["tag"] =
-      StrCat(static_cast<int32>(WireFormat::MakeTag(descriptor)));
+      StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   (*variables)["required"] = descriptor->is_required() ? "true" : "false";
   (*variables)["required"] = descriptor->is_required() ? "true" : "false";
@@ -304,7 +305,7 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
 
 
 
 
 void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
 void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -385,7 +386,7 @@ void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateFieldInfo(
 void ImmutablePrimitiveOneofFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -615,7 +616,7 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
 }
 }
 
 
 void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
 void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);

+ 5 - 3
src/google/protobuf/compiler/java/java_primitive_field_lite.h

@@ -35,8 +35,10 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_PRIMITIVE_FIELD_LITE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_PRIMITIVE_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_PRIMITIVE_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_PRIMITIVE_FIELD_LITE_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/java/java_field.h>
 #include <google/protobuf/compiler/java/java_field.h>
 
 
 namespace google {
 namespace google {
@@ -70,7 +72,7 @@ class ImmutablePrimitiveFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 
@@ -96,7 +98,7 @@ class ImmutablePrimitiveOneofFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
 
 
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
  private:
  private:
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutablePrimitiveOneofFieldLiteGenerator);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutablePrimitiveOneofFieldLiteGenerator);
@@ -116,7 +118,7 @@ class RepeatedImmutablePrimitiveFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 

+ 4 - 2
src/google/protobuf/compiler/java/java_string_field.cc

@@ -33,6 +33,9 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_string_field.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -42,7 +45,6 @@
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
-#include <google/protobuf/compiler/java/java_string_field.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
@@ -71,7 +73,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
       "= " + ImmutableDefaultValue(descriptor, name_resolver);
       "= " + ImmutableDefaultValue(descriptor, name_resolver);
   (*variables)["capitalized_type"] = "String";
   (*variables)["capitalized_type"] = "String";
   (*variables)["tag"] =
   (*variables)["tag"] =
-      StrCat(static_cast<int32>(WireFormat::MakeTag(descriptor)));
+      StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   (*variables)["null_check"] =
   (*variables)["null_check"] =

+ 7 - 5
src/google/protobuf/compiler/java/java_string_field_lite.cc

@@ -33,6 +33,9 @@
 //  Based on original Protocol Buffers design by
 //  Based on original Protocol Buffers design by
 //  Sanjay Ghemawat, Jeff Dean, and others.
 //  Sanjay Ghemawat, Jeff Dean, and others.
 
 
+#include <google/protobuf/compiler/java/java_string_field_lite.h>
+
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 
 
@@ -42,7 +45,6 @@
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_doc_comment.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_helpers.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
 #include <google/protobuf/compiler/java/java_name_resolver.h>
-#include <google/protobuf/compiler/java/java_string_field_lite.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/io/printer.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/wire_format.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
@@ -72,7 +74,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
       "= " + ImmutableDefaultValue(descriptor, name_resolver);
       "= " + ImmutableDefaultValue(descriptor, name_resolver);
   (*variables)["capitalized_type"] = "java.lang.String";
   (*variables)["capitalized_type"] = "java.lang.String";
   (*variables)["tag"] =
   (*variables)["tag"] =
-      StrCat(static_cast<int32>(WireFormat::MakeTag(descriptor)));
+      StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
   (*variables)["tag_size"] = StrCat(
   (*variables)["tag_size"] = StrCat(
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
   // We use `x.getClass()` as a null check because it generates less bytecode
   // We use `x.getClass()` as a null check because it generates less bytecode
@@ -303,7 +305,7 @@ void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
 }
 }
 
 
 void ImmutableStringFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableStringFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -407,7 +409,7 @@ void ImmutableStringOneofFieldLiteGenerator::GenerateMembers(
 }
 }
 
 
 void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo(
 void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);
@@ -703,7 +705,7 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
 }
 }
 
 
 void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo(
 void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo(
-    io::Printer* printer, std::vector<uint16>* output) const {
+    io::Printer* printer, std::vector<uint16_t>* output) const {
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(descriptor_->number(), output);
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
                               output);
                               output);

+ 5 - 3
src/google/protobuf/compiler/java/java_string_field_lite.h

@@ -36,8 +36,10 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_STRING_FIELD_LITE_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_STRING_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_STRING_FIELD_LITE_H__
 #define GOOGLE_PROTOBUF_COMPILER_JAVA_STRING_FIELD_LITE_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
+
 #include <google/protobuf/compiler/java/java_field.h>
 #include <google/protobuf/compiler/java/java_field.h>
 
 
 namespace google {
 namespace google {
@@ -71,7 +73,7 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator {
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 
@@ -96,7 +98,7 @@ class ImmutableStringOneofFieldLiteGenerator
   void GenerateMembers(io::Printer* printer) const;
   void GenerateMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableStringOneofFieldLiteGenerator);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableStringOneofFieldLiteGenerator);
 };
 };
@@ -115,7 +117,7 @@ class RepeatedImmutableStringFieldLiteGenerator
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateBuilderMembers(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateInitializationCode(io::Printer* printer) const;
   void GenerateFieldInfo(io::Printer* printer,
   void GenerateFieldInfo(io::Printer* printer,
-                         std::vector<uint16>* output) const;
+                         std::vector<uint16_t>* output) const;
 
 
   std::string GetBoxedType() const;
   std::string GetBoxedType() const;
 
 

+ 3 - 2
src/google/protobuf/compiler/mock_code_generator.cc

@@ -34,6 +34,7 @@
 
 
 #include <stdlib.h>
 #include <stdlib.h>
 
 
+#include <cstdint>
 #include <iostream>
 #include <iostream>
 #include <memory>
 #include <memory>
 #include <vector>
 #include <vector>
@@ -88,11 +89,11 @@ MockCodeGenerator::MockCodeGenerator(const std::string& name) : name_(name) {}
 MockCodeGenerator::~MockCodeGenerator() {}
 MockCodeGenerator::~MockCodeGenerator() {}
 
 
 uint64_t MockCodeGenerator::GetSupportedFeatures() const {
 uint64_t MockCodeGenerator::GetSupportedFeatures() const {
-  uint64 all_features = CodeGenerator::FEATURE_PROTO3_OPTIONAL;
+  uint64_t all_features = CodeGenerator::FEATURE_PROTO3_OPTIONAL;
   return all_features & ~suppressed_features_;
   return all_features & ~suppressed_features_;
 }
 }
 
 
-void MockCodeGenerator::SuppressFeatures(uint64 features) {
+void MockCodeGenerator::SuppressFeatures(uint64_t features) {
   suppressed_features_ = features;
   suppressed_features_ = features;
 }
 }
 
 

+ 3 - 2
src/google/protobuf/compiler/mock_code_generator.h

@@ -33,6 +33,7 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
 #define GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
 #define GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
 
 
+#include <cstdint>
 #include <string>
 #include <string>
 
 
 #include <google/protobuf/compiler/code_generator.h>
 #include <google/protobuf/compiler/code_generator.h>
@@ -112,11 +113,11 @@ class MockCodeGenerator : public CodeGenerator {
                 GeneratorContext* context, std::string* error) const override;
                 GeneratorContext* context, std::string* error) const override;
 
 
   uint64_t GetSupportedFeatures() const override;
   uint64_t GetSupportedFeatures() const override;
-  void SuppressFeatures(uint64 features);
+  void SuppressFeatures(uint64_t features);
 
 
  private:
  private:
   std::string name_;
   std::string name_;
-  uint64 suppressed_features_ = 0;
+  uint64_t suppressed_features_ = 0;
 
 
   static std::string GetOutputFileContent(const std::string& generator_name,
   static std::string GetOutputFileContent(const std::string& generator_name,
                                           const std::string& parameter,
                                           const std::string& parameter,

+ 30 - 23
src/google/protobuf/compiler/parser.cc

@@ -38,6 +38,7 @@
 
 
 #include <float.h>
 #include <float.h>
 
 
+#include <cstdint>
 #include <limits>
 #include <limits>
 #include <unordered_map>
 #include <unordered_map>
 #include <unordered_set>
 #include <unordered_set>
@@ -243,8 +244,9 @@ bool Parser::ConsumeIdentifier(std::string* output, const char* error) {
 
 
 bool Parser::ConsumeInteger(int* output, const char* error) {
 bool Parser::ConsumeInteger(int* output, const char* error) {
   if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
   if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
-    uint64 value = 0;
-    if (!io::Tokenizer::ParseInteger(input_->current().text, kint32max,
+    uint64_t value = 0;
+    if (!io::Tokenizer::ParseInteger(input_->current().text,
+                                     std::numeric_limits<int32_t>::max(),
                                      &value)) {
                                      &value)) {
       AddError("Integer out of range.");
       AddError("Integer out of range.");
       // We still return true because we did, in fact, parse an integer.
       // We still return true because we did, in fact, parse an integer.
@@ -260,19 +262,19 @@ bool Parser::ConsumeInteger(int* output, const char* error) {
 
 
 bool Parser::ConsumeSignedInteger(int* output, const char* error) {
 bool Parser::ConsumeSignedInteger(int* output, const char* error) {
   bool is_negative = false;
   bool is_negative = false;
-  uint64 max_value = kint32max;
+  uint64_t max_value = std::numeric_limits<int32_t>::max();
   if (TryConsume("-")) {
   if (TryConsume("-")) {
     is_negative = true;
     is_negative = true;
     max_value += 1;
     max_value += 1;
   }
   }
-  uint64 value = 0;
+  uint64_t value = 0;
   DO(ConsumeInteger64(max_value, &value, error));
   DO(ConsumeInteger64(max_value, &value, error));
   if (is_negative) value *= -1;
   if (is_negative) value *= -1;
   *output = value;
   *output = value;
   return true;
   return true;
 }
 }
 
 
-bool Parser::ConsumeInteger64(uint64 max_value, uint64* output,
+bool Parser::ConsumeInteger64(uint64_t max_value, uint64_t* output,
                               const char* error) {
                               const char* error) {
   if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
   if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     if (!io::Tokenizer::ParseInteger(input_->current().text, max_value,
     if (!io::Tokenizer::ParseInteger(input_->current().text, max_value,
@@ -296,8 +298,9 @@ bool Parser::ConsumeNumber(double* output, const char* error) {
     return true;
     return true;
   } else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
   } else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     // Also accept integers.
     // Also accept integers.
-    uint64 value = 0;
-    if (!io::Tokenizer::ParseInteger(input_->current().text, kuint64max,
+    uint64_t value = 0;
+    if (!io::Tokenizer::ParseInteger(input_->current().text,
+                                     std::numeric_limits<uint64_t>::max(),
                                      &value)) {
                                      &value)) {
       AddError("Integer out of range.");
       AddError("Integer out of range.");
       // We still return true because we did, in fact, parse a number.
       // We still return true because we did, in fact, parse a number.
@@ -829,8 +832,9 @@ bool IsMessageSetWireFormatMessage(const DescriptorProto& message) {
 // tag number can only be determined after all options have been parsed.
 // tag number can only be determined after all options have been parsed.
 void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) {
 void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) {
   const bool is_message_set = IsMessageSetWireFormatMessage(*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
+                                       ? std::numeric_limits<int32_t>::max()
+                                       : FieldDescriptor::kMaxNumber + 1;
   for (int i = 0; i < message->extension_range_size(); ++i) {
   for (int i = 0; i < message->extension_range_size(); ++i) {
     if (message->extension_range(i).end() == kMaxRangeSentinel) {
     if (message->extension_range(i).end() == kMaxRangeSentinel) {
       message->mutable_extension_range(i)->set_end(max_extension_number);
       message->mutable_extension_range(i)->set_end(max_extension_number);
@@ -843,8 +847,9 @@ void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) {
 // tag number can only be determined after all options have been parsed.
 // tag number can only be determined after all options have been parsed.
 void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) {
 void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) {
   const bool is_message_set = IsMessageSetWireFormatMessage(*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
+                                   ? std::numeric_limits<int32_t>::max()
+                                   : FieldDescriptor::kMaxNumber + 1;
   for (int i = 0; i < message->reserved_range_size(); ++i) {
   for (int i = 0; i < message->reserved_range_size(); ++i) {
     if (message->reserved_range(i).end() == kMaxRangeSentinel) {
     if (message->reserved_range(i).end() == kMaxRangeSentinel) {
       message->mutable_reserved_range(i)->set_end(max_field_number);
       message->mutable_reserved_range(i)->set_end(max_field_number);
@@ -1260,11 +1265,11 @@ bool Parser::ParseDefaultAssignment(
     case FieldDescriptorProto::TYPE_SINT64:
     case FieldDescriptorProto::TYPE_SINT64:
     case FieldDescriptorProto::TYPE_SFIXED32:
     case FieldDescriptorProto::TYPE_SFIXED32:
     case FieldDescriptorProto::TYPE_SFIXED64: {
     case FieldDescriptorProto::TYPE_SFIXED64: {
-      uint64 max_value = kint64max;
+      uint64_t max_value = std::numeric_limits<int64_t>::max();
       if (field->type() == FieldDescriptorProto::TYPE_INT32 ||
       if (field->type() == FieldDescriptorProto::TYPE_INT32 ||
           field->type() == FieldDescriptorProto::TYPE_SINT32 ||
           field->type() == FieldDescriptorProto::TYPE_SINT32 ||
           field->type() == FieldDescriptorProto::TYPE_SFIXED32) {
           field->type() == FieldDescriptorProto::TYPE_SFIXED32) {
-        max_value = kint32max;
+        max_value = std::numeric_limits<int32_t>::max();
       }
       }
 
 
       // These types can be negative.
       // These types can be negative.
@@ -1274,7 +1279,7 @@ bool Parser::ParseDefaultAssignment(
         ++max_value;
         ++max_value;
       }
       }
       // Parse the integer to verify that it is not out-of-range.
       // Parse the integer to verify that it is not out-of-range.
-      uint64 value;
+      uint64_t value;
       DO(ConsumeInteger64(max_value, &value,
       DO(ConsumeInteger64(max_value, &value,
                           "Expected integer for field default value."));
                           "Expected integer for field default value."));
       // And stringify it again.
       // And stringify it again.
@@ -1286,10 +1291,10 @@ bool Parser::ParseDefaultAssignment(
     case FieldDescriptorProto::TYPE_UINT64:
     case FieldDescriptorProto::TYPE_UINT64:
     case FieldDescriptorProto::TYPE_FIXED32:
     case FieldDescriptorProto::TYPE_FIXED32:
     case FieldDescriptorProto::TYPE_FIXED64: {
     case FieldDescriptorProto::TYPE_FIXED64: {
-      uint64 max_value = kuint64max;
+      uint64_t max_value = std::numeric_limits<uint64_t>::max();
       if (field->type() == FieldDescriptorProto::TYPE_UINT32 ||
       if (field->type() == FieldDescriptorProto::TYPE_UINT32 ||
           field->type() == FieldDescriptorProto::TYPE_FIXED32) {
           field->type() == FieldDescriptorProto::TYPE_FIXED32) {
-        max_value = kuint32max;
+        max_value = std::numeric_limits<uint32_t>::max();
       }
       }
 
 
       // Numeric, not negative.
       // Numeric, not negative.
@@ -1297,7 +1302,7 @@ bool Parser::ParseDefaultAssignment(
         AddError("Unsigned field can't have negative default value.");
         AddError("Unsigned field can't have negative default value.");
       }
       }
       // Parse the integer to verify that it is not out-of-range.
       // Parse the integer to verify that it is not out-of-range.
-      uint64 value;
+      uint64_t value;
       DO(ConsumeInteger64(max_value, &value,
       DO(ConsumeInteger64(max_value, &value,
                           "Expected integer for field default value."));
                           "Expected integer for field default value."));
       // And stringify it again.
       // And stringify it again.
@@ -1529,15 +1534,17 @@ bool Parser::ParseOption(Message* options,
       }
       }
 
 
       case io::Tokenizer::TYPE_INTEGER: {
       case io::Tokenizer::TYPE_INTEGER: {
-        uint64 value;
-        uint64 max_value =
-            is_negative ? static_cast<uint64>(kint64max) + 1 : kuint64max;
+        uint64_t value;
+        uint64_t max_value =
+            is_negative
+                ? static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1
+                : std::numeric_limits<uint64_t>::max();
         DO(ConsumeInteger64(max_value, &value, "Expected integer."));
         DO(ConsumeInteger64(max_value, &value, "Expected integer."));
         if (is_negative) {
         if (is_negative) {
           value_location.AddPath(
           value_location.AddPath(
               UninterpretedOption::kNegativeIntValueFieldNumber);
               UninterpretedOption::kNegativeIntValueFieldNumber);
           uninterpreted_option->set_negative_int_value(
           uninterpreted_option->set_negative_int_value(
-              static_cast<int64>(-value));
+              static_cast<int64_t>(-value));
         } else {
         } else {
           value_location.AddPath(
           value_location.AddPath(
               UninterpretedOption::kPositiveIntValueFieldNumber);
               UninterpretedOption::kPositiveIntValueFieldNumber);
@@ -2338,8 +2345,8 @@ bool Parser::ParsePackage(FileDescriptorProto* file,
 }
 }
 
 
 bool Parser::ParseImport(RepeatedPtrField<std::string>* dependency,
 bool Parser::ParseImport(RepeatedPtrField<std::string>* dependency,
-                         RepeatedField<int32>* public_dependency,
-                         RepeatedField<int32>* weak_dependency,
+                         RepeatedField<int32_t>* public_dependency,
+                         RepeatedField<int32_t>* weak_dependency,
                          const LocationRecorder& root_location,
                          const LocationRecorder& root_location,
                          const FileDescriptorProto* containing_file) {
                          const FileDescriptorProto* containing_file) {
   LocationRecorder location(root_location,
   LocationRecorder location(root_location,

+ 7 - 3
src/google/protobuf/compiler/parser.h

@@ -37,14 +37,17 @@
 #ifndef GOOGLE_PROTOBUF_COMPILER_PARSER_H__
 #ifndef GOOGLE_PROTOBUF_COMPILER_PARSER_H__
 #define GOOGLE_PROTOBUF_COMPILER_PARSER_H__
 #define GOOGLE_PROTOBUF_COMPILER_PARSER_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 #include <utility>
 #include <utility>
+
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/descriptor.pb.h>
 #include <google/protobuf/io/tokenizer.h>
 #include <google/protobuf/io/tokenizer.h>
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/repeated_field.h>
 #include <google/protobuf/repeated_field.h>
 
 
+// Must be included last.
 #include <google/protobuf/port_def.inc>
 #include <google/protobuf/port_def.inc>
 
 
 namespace google {
 namespace google {
@@ -172,7 +175,8 @@ class PROTOBUF_EXPORT Parser {
   bool ConsumeSignedInteger(int* output, const char* error);
   bool ConsumeSignedInteger(int* output, const char* error);
   // Consume a 64-bit integer and store its value in "output".  If the value
   // Consume a 64-bit integer and store its value in "output".  If the value
   // is greater than max_value, an error will be reported.
   // is greater than max_value, an error will be reported.
-  bool ConsumeInteger64(uint64 max_value, uint64* output, const char* error);
+  bool ConsumeInteger64(uint64_t max_value, uint64_t* output,
+                        const char* error);
   // Consume a number and store its value in "output".  This will accept
   // Consume a number and store its value in "output".  This will accept
   // tokens of either INTEGER or FLOAT type.
   // tokens of either INTEGER or FLOAT type.
   bool ConsumeNumber(double* output, const char* error);
   bool ConsumeNumber(double* output, const char* error);
@@ -323,8 +327,8 @@ class PROTOBUF_EXPORT Parser {
                     const LocationRecorder& root_location,
                     const LocationRecorder& root_location,
                     const FileDescriptorProto* containing_file);
                     const FileDescriptorProto* containing_file);
   bool ParseImport(RepeatedPtrField<std::string>* dependency,
   bool ParseImport(RepeatedPtrField<std::string>* dependency,
-                   RepeatedField<int32>* public_dependency,
-                   RepeatedField<int32>* weak_dependency,
+                   RepeatedField<int32_t>* public_dependency,
+                   RepeatedField<int32_t>* weak_dependency,
                    const LocationRecorder& root_location,
                    const LocationRecorder& root_location,
                    const FileDescriptorProto* containing_file);
                    const FileDescriptorProto* containing_file);
 
 

+ 8 - 8
src/google/protobuf/compiler/plugin.pb.cc

@@ -305,7 +305,6 @@ const char* Version::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::in
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional int32 major = 1;
       // optional int32 major = 1;
       case 1:
       case 1:
@@ -344,7 +343,8 @@ const char* Version::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::in
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -636,7 +636,6 @@ const char* CodeGeneratorRequest::_InternalParse(const char* ptr, ::PROTOBUF_NAM
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated string file_to_generate = 1;
       // repeated string file_to_generate = 1;
       case 1:
       case 1:
@@ -686,7 +685,8 @@ const char* CodeGeneratorRequest::_InternalParse(const char* ptr, ::PROTOBUF_NAM
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1001,7 +1001,6 @@ const char* CodeGeneratorResponse_File::_InternalParse(const char* ptr, ::PROTOB
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -1045,7 +1044,8 @@ const char* CodeGeneratorResponse_File::_InternalParse(const char* ptr, ::PROTOB
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1322,7 +1322,6 @@ const char* CodeGeneratorResponse::_InternalParse(const char* ptr, ::PROTOBUF_NA
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string error = 1;
       // optional string error = 1;
       case 1:
       case 1:
@@ -1357,7 +1356,8 @@ const char* CodeGeneratorResponse::_InternalParse(const char* ptr, ::PROTOBUF_NA
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 19 - 16
src/google/protobuf/compiler/zip_writer.cc

@@ -34,6 +34,9 @@
 // Based on http://www.pkware.com/documents/casestudies/APPNOTE.TXT
 // Based on http://www.pkware.com/documents/casestudies/APPNOTE.TXT
 
 
 #include <google/protobuf/compiler/zip_writer.h>
 #include <google/protobuf/compiler/zip_writer.h>
+
+#include <cstdint>
+
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/io/coded_stream.h>
 
 
 namespace google {
 namespace google {
@@ -42,9 +45,9 @@ namespace compiler {
 
 
 // January 1, 1980 as a DOS date.
 // January 1, 1980 as a DOS date.
 // see https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
 // see https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
-static const uint16 kDosEpoch = 1 << 5 | 1;
+static const uint16_t kDosEpoch = 1 << 5 | 1;
 
 
-static const uint32 kCRC32Table[256] = {
+static const uint32_t kCRC32Table[256] = {
     0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
     0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
     0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
     0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
     0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
     0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
@@ -89,8 +92,8 @@ static const uint32 kCRC32Table[256] = {
     0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
     0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
     0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
     0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
 
 
-static uint32 ComputeCRC32(const std::string& buf) {
-  uint32 x = ~0U;
+static uint32_t ComputeCRC32(const std::string& buf) {
+  uint32_t x = ~0U;
   for (int i = 0; i < buf.size(); ++i) {
   for (int i = 0; i < buf.size(); ++i) {
     unsigned char c = buf[i];
     unsigned char c = buf[i];
     x = kCRC32Table[(x ^ c) & 0xff] ^ (x >> 8);
     x = kCRC32Table[(x ^ c) & 0xff] ^ (x >> 8);
@@ -98,10 +101,10 @@ static uint32 ComputeCRC32(const std::string& buf) {
   return ~x;
   return ~x;
 }
 }
 
 
-static void WriteShort(io::CodedOutputStream* out, uint16 val) {
-  uint8 p[2];
-  p[0] = static_cast<uint8>(val);
-  p[1] = static_cast<uint8>(val >> 8);
+static void WriteShort(io::CodedOutputStream* out, uint16_t val) {
+  uint8_t p[2];
+  p[0] = static_cast<uint8_t>(val);
+  p[1] = static_cast<uint8_t>(val >> 8);
   out->WriteRaw(p, 2);
   out->WriteRaw(p, 2);
 }
 }
 
 
@@ -114,7 +117,7 @@ bool ZipWriter::Write(const std::string& filename,
   FileInfo info;
   FileInfo info;
 
 
   info.name = filename;
   info.name = filename;
-  uint16 filename_size = filename.size();
+  uint16_t filename_size = filename.size();
   info.offset = raw_output_->ByteCount();
   info.offset = raw_output_->ByteCount();
   info.size = contents.size();
   info.size = contents.size();
   info.crc32 = ComputeCRC32(contents);
   info.crc32 = ComputeCRC32(contents);
@@ -141,17 +144,17 @@ bool ZipWriter::Write(const std::string& filename,
 }
 }
 
 
 bool ZipWriter::WriteDirectory() {
 bool ZipWriter::WriteDirectory() {
-  uint16 num_entries = files_.size();
-  uint32 dir_ofs = raw_output_->ByteCount();
+  uint16_t num_entries = files_.size();
+  uint32_t dir_ofs = raw_output_->ByteCount();
 
 
   // write central directory
   // write central directory
   io::CodedOutputStream output(raw_output_);
   io::CodedOutputStream output(raw_output_);
   for (int i = 0; i < num_entries; ++i) {
   for (int i = 0; i < num_entries; ++i) {
     const std::string& filename = files_[i].name;
     const std::string& filename = files_[i].name;
-    uint16 filename_size = filename.size();
-    uint32 crc32 = files_[i].crc32;
-    uint32 size = files_[i].size;
-    uint32 offset = files_[i].offset;
+    uint16_t filename_size = filename.size();
+    uint32_t crc32 = files_[i].crc32;
+    uint32_t size = files_[i].size;
+    uint32_t offset = files_[i].offset;
 
 
     output.WriteLittleEndian32(0x02014b50);  // magic
     output.WriteLittleEndian32(0x02014b50);  // magic
     WriteShort(&output, 10);                 // version made by
     WriteShort(&output, 10);                 // version made by
@@ -172,7 +175,7 @@ bool ZipWriter::WriteDirectory() {
     output.WriteLittleEndian32(offset);      // local header offset
     output.WriteLittleEndian32(offset);      // local header offset
     output.WriteString(filename);            // file name
     output.WriteString(filename);            // file name
   }
   }
-  uint32 dir_len = output.ByteCount();
+  uint32_t dir_len = output.ByteCount();
 
 
   // write end of central directory marker
   // write end of central directory marker
   output.WriteLittleEndian32(0x06054b50);  // magic
   output.WriteLittleEndian32(0x06054b50);  // magic

+ 5 - 3
src/google/protobuf/compiler/zip_writer.h

@@ -30,7 +30,9 @@
 
 
 // Author: kenton@google.com (Kenton Varda)
 // Author: kenton@google.com (Kenton Varda)
 
 
+#include <cstdint>
 #include <vector>
 #include <vector>
+
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/io/zero_copy_stream.h>
 #include <google/protobuf/io/zero_copy_stream.h>
 
 
@@ -49,9 +51,9 @@ class ZipWriter {
  private:
  private:
   struct FileInfo {
   struct FileInfo {
     std::string name;
     std::string name;
-    uint32 offset;
-    uint32 size;
-    uint32 crc32;
+    uint32_t offset;
+    uint32_t size;
+    uint32_t crc32;
   };
   };
 
 
   io::ZeroCopyOutputStream* raw_output_;
   io::ZeroCopyOutputStream* raw_output_;

+ 107 - 106
src/google/protobuf/descriptor.pb.cc

@@ -990,67 +990,68 @@ const char descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto[] PR
   "\347\007 \003(\0132$.google.protobuf.UninterpretedOp"
   "\347\007 \003(\0132$.google.protobuf.UninterpretedOp"
   "tion\":\n\014OptimizeMode\022\t\n\005SPEED\020\001\022\r\n\tCODE_"
   "tion\":\n\014OptimizeMode\022\t\n\005SPEED\020\001\022\r\n\tCODE_"
   "SIZE\020\002\022\020\n\014LITE_RUNTIME\020\003*\t\010\350\007\020\200\200\200\200\002J\004\010&\020"
   "SIZE\020\002\022\020\n\014LITE_RUNTIME\020\003*\t\010\350\007\020\200\200\200\200\002J\004\010&\020"
-  "\'\"\362\001\n\016MessageOptions\022&\n\027message_set_wire"
+  "\'\"\204\002\n\016MessageOptions\022&\n\027message_set_wire"
   "_format\030\001 \001(\010:\005false\022.\n\037no_standard_desc"
   "_format\030\001 \001(\010:\005false\022.\n\037no_standard_desc"
   "riptor_accessor\030\002 \001(\010:\005false\022\031\n\ndeprecat"
   "riptor_accessor\030\002 \001(\010:\005false\022\031\n\ndeprecat"
   "ed\030\003 \001(\010:\005false\022\021\n\tmap_entry\030\007 \001(\010\022C\n\024un"
   "ed\030\003 \001(\010:\005false\022\021\n\tmap_entry\030\007 \001(\010\022C\n\024un"
   "interpreted_option\030\347\007 \003(\0132$.google.proto"
   "interpreted_option\030\347\007 \003(\0132$.google.proto"
-  "buf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002J\004\010\010\020\t"
-  "J\004\010\t\020\n\"\236\003\n\014FieldOptions\022:\n\005ctype\030\001 \001(\0162#"
-  ".google.protobuf.FieldOptions.CType:\006STR"
-  "ING\022\016\n\006packed\030\002 \001(\010\022\?\n\006jstype\030\006 \001(\0162$.go"
-  "ogle.protobuf.FieldOptions.JSType:\tJS_NO"
-  "RMAL\022\023\n\004lazy\030\005 \001(\010:\005false\022\031\n\ndeprecated\030"
-  "\003 \001(\010:\005false\022\023\n\004weak\030\n \001(\010:\005false\022C\n\024uni"
-  "nterpreted_option\030\347\007 \003(\0132$.google.protob"
-  "uf.UninterpretedOption\"/\n\005CType\022\n\n\006STRIN"
-  "G\020\000\022\010\n\004CORD\020\001\022\020\n\014STRING_PIECE\020\002\"5\n\006JSTyp"
-  "e\022\r\n\tJS_NORMAL\020\000\022\r\n\tJS_STRING\020\001\022\r\n\tJS_NU"
-  "MBER\020\002*\t\010\350\007\020\200\200\200\200\002J\004\010\004\020\005\"^\n\014OneofOptions\022"
-  "C\n\024uninterpreted_option\030\347\007 \003(\0132$.google."
-  "protobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002\""
-  "\223\001\n\013EnumOptions\022\023\n\013allow_alias\030\002 \001(\010\022\031\n\n"
-  "deprecated\030\003 \001(\010:\005false\022C\n\024uninterpreted"
-  "_option\030\347\007 \003(\0132$.google.protobuf.Uninter"
-  "pretedOption*\t\010\350\007\020\200\200\200\200\002J\004\010\005\020\006\"}\n\020EnumVal"
-  "ueOptions\022\031\n\ndeprecated\030\001 \001(\010:\005false\022C\n\024"
-  "uninterpreted_option\030\347\007 \003(\0132$.google.pro"
-  "tobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002\"{\n\016"
-  "ServiceOptions\022\031\n\ndeprecated\030! \001(\010:\005fals"
+  "buf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002J\004\010\004\020\005"
+  "J\004\010\005\020\006J\004\010\006\020\007J\004\010\010\020\tJ\004\010\t\020\n\"\236\003\n\014FieldOption"
+  "s\022:\n\005ctype\030\001 \001(\0162#.google.protobuf.Field"
+  "Options.CType:\006STRING\022\016\n\006packed\030\002 \001(\010\022\?\n"
+  "\006jstype\030\006 \001(\0162$.google.protobuf.FieldOpt"
+  "ions.JSType:\tJS_NORMAL\022\023\n\004lazy\030\005 \001(\010:\005fa"
+  "lse\022\031\n\ndeprecated\030\003 \001(\010:\005false\022\023\n\004weak\030\n"
+  " \001(\010:\005false\022C\n\024uninterpreted_option\030\347\007 \003"
+  "(\0132$.google.protobuf.UninterpretedOption"
+  "\"/\n\005CType\022\n\n\006STRING\020\000\022\010\n\004CORD\020\001\022\020\n\014STRIN"
+  "G_PIECE\020\002\"5\n\006JSType\022\r\n\tJS_NORMAL\020\000\022\r\n\tJS"
+  "_STRING\020\001\022\r\n\tJS_NUMBER\020\002*\t\010\350\007\020\200\200\200\200\002J\004\010\004\020"
+  "\005\"^\n\014OneofOptions\022C\n\024uninterpreted_optio"
+  "n\030\347\007 \003(\0132$.google.protobuf.Uninterpreted"
+  "Option*\t\010\350\007\020\200\200\200\200\002\"\223\001\n\013EnumOptions\022\023\n\013all"
+  "ow_alias\030\002 \001(\010\022\031\n\ndeprecated\030\003 \001(\010:\005fals"
   "e\022C\n\024uninterpreted_option\030\347\007 \003(\0132$.googl"
   "e\022C\n\024uninterpreted_option\030\347\007 \003(\0132$.googl"
   "e.protobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200"
   "e.protobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200"
-  "\002\"\255\002\n\rMethodOptions\022\031\n\ndeprecated\030! \001(\010:"
-  "\005false\022_\n\021idempotency_level\030\" \001(\0162/.goog"
-  "le.protobuf.MethodOptions.IdempotencyLev"
-  "el:\023IDEMPOTENCY_UNKNOWN\022C\n\024uninterpreted"
-  "_option\030\347\007 \003(\0132$.google.protobuf.Uninter"
-  "pretedOption\"P\n\020IdempotencyLevel\022\027\n\023IDEM"
-  "POTENCY_UNKNOWN\020\000\022\023\n\017NO_SIDE_EFFECTS\020\001\022\016"
-  "\n\nIDEMPOTENT\020\002*\t\010\350\007\020\200\200\200\200\002\"\236\002\n\023Uninterpre"
-  "tedOption\022;\n\004name\030\002 \003(\0132-.google.protobu"
-  "f.UninterpretedOption.NamePart\022\030\n\020identi"
-  "fier_value\030\003 \001(\t\022\032\n\022positive_int_value\030\004"
-  " \001(\004\022\032\n\022negative_int_value\030\005 \001(\003\022\024\n\014doub"
-  "le_value\030\006 \001(\001\022\024\n\014string_value\030\007 \001(\014\022\027\n\017"
-  "aggregate_value\030\010 \001(\t\0323\n\010NamePart\022\021\n\tnam"
-  "e_part\030\001 \002(\t\022\024\n\014is_extension\030\002 \002(\010\"\325\001\n\016S"
-  "ourceCodeInfo\022:\n\010location\030\001 \003(\0132(.google"
-  ".protobuf.SourceCodeInfo.Location\032\206\001\n\010Lo"
-  "cation\022\020\n\004path\030\001 \003(\005B\002\020\001\022\020\n\004span\030\002 \003(\005B\002"
-  "\020\001\022\030\n\020leading_comments\030\003 \001(\t\022\031\n\021trailing"
-  "_comments\030\004 \001(\t\022!\n\031leading_detached_comm"
-  "ents\030\006 \003(\t\"\247\001\n\021GeneratedCodeInfo\022A\n\nanno"
-  "tation\030\001 \003(\0132-.google.protobuf.Generated"
-  "CodeInfo.Annotation\032O\n\nAnnotation\022\020\n\004pat"
-  "h\030\001 \003(\005B\002\020\001\022\023\n\013source_file\030\002 \001(\t\022\r\n\005begi"
-  "n\030\003 \001(\005\022\013\n\003end\030\004 \001(\005B~\n\023com.google.proto"
-  "bufB\020DescriptorProtosH\001Z-google.golang.o"
-  "rg/protobuf/types/descriptorpb\370\001\001\242\002\003GPB\252"
-  "\002\032Google.Protobuf.Reflection"
+  "\002J\004\010\005\020\006\"}\n\020EnumValueOptions\022\031\n\ndeprecate"
+  "d\030\001 \001(\010:\005false\022C\n\024uninterpreted_option\030\347"
+  "\007 \003(\0132$.google.protobuf.UninterpretedOpt"
+  "ion*\t\010\350\007\020\200\200\200\200\002\"{\n\016ServiceOptions\022\031\n\ndepr"
+  "ecated\030! \001(\010:\005false\022C\n\024uninterpreted_opt"
+  "ion\030\347\007 \003(\0132$.google.protobuf.Uninterpret"
+  "edOption*\t\010\350\007\020\200\200\200\200\002\"\255\002\n\rMethodOptions\022\031\n"
+  "\ndeprecated\030! \001(\010:\005false\022_\n\021idempotency_"
+  "level\030\" \001(\0162/.google.protobuf.MethodOpti"
+  "ons.IdempotencyLevel:\023IDEMPOTENCY_UNKNOW"
+  "N\022C\n\024uninterpreted_option\030\347\007 \003(\0132$.googl"
+  "e.protobuf.UninterpretedOption\"P\n\020Idempo"
+  "tencyLevel\022\027\n\023IDEMPOTENCY_UNKNOWN\020\000\022\023\n\017N"
+  "O_SIDE_EFFECTS\020\001\022\016\n\nIDEMPOTENT\020\002*\t\010\350\007\020\200\200"
+  "\200\200\002\"\236\002\n\023UninterpretedOption\022;\n\004name\030\002 \003("
+  "\0132-.google.protobuf.UninterpretedOption."
+  "NamePart\022\030\n\020identifier_value\030\003 \001(\t\022\032\n\022po"
+  "sitive_int_value\030\004 \001(\004\022\032\n\022negative_int_v"
+  "alue\030\005 \001(\003\022\024\n\014double_value\030\006 \001(\001\022\024\n\014stri"
+  "ng_value\030\007 \001(\014\022\027\n\017aggregate_value\030\010 \001(\t\032"
+  "3\n\010NamePart\022\021\n\tname_part\030\001 \002(\t\022\024\n\014is_ext"
+  "ension\030\002 \002(\010\"\325\001\n\016SourceCodeInfo\022:\n\010locat"
+  "ion\030\001 \003(\0132(.google.protobuf.SourceCodeIn"
+  "fo.Location\032\206\001\n\010Location\022\020\n\004path\030\001 \003(\005B\002"
+  "\020\001\022\020\n\004span\030\002 \003(\005B\002\020\001\022\030\n\020leading_comments"
+  "\030\003 \001(\t\022\031\n\021trailing_comments\030\004 \001(\t\022!\n\031lea"
+  "ding_detached_comments\030\006 \003(\t\"\247\001\n\021Generat"
+  "edCodeInfo\022A\n\nannotation\030\001 \003(\0132-.google."
+  "protobuf.GeneratedCodeInfo.Annotation\032O\n"
+  "\nAnnotation\022\020\n\004path\030\001 \003(\005B\002\020\001\022\023\n\013source_"
+  "file\030\002 \001(\t\022\r\n\005begin\030\003 \001(\005\022\013\n\003end\030\004 \001(\005B~"
+  "\n\023com.google.protobufB\020DescriptorProtosH"
+  "\001Z-google.golang.org/protobuf/types/desc"
+  "riptorpb\370\001\001\242\002\003GPB\252\002\032Google.Protobuf.Refl"
+  "ection"
   ;
   ;
 static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once;
 static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once;
 const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = {
 const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_google_2fprotobuf_2fdescriptor_2eproto = {
-  false, false, 6028, descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", 
+  false, false, 6046, descriptor_table_protodef_google_2fprotobuf_2fdescriptor_2eproto, "google/protobuf/descriptor.proto", 
   &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once, nullptr, 0, 27,
   &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto_once, nullptr, 0, 27,
   schemas, file_default_instances, TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets,
   schemas, file_default_instances, TableStruct_google_2fprotobuf_2fdescriptor_2eproto::offsets,
   file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto, file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto, file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto,
   file_level_metadata_google_2fprotobuf_2fdescriptor_2eproto, file_level_enum_descriptors_google_2fprotobuf_2fdescriptor_2eproto, file_level_service_descriptors_google_2fprotobuf_2fdescriptor_2eproto,
@@ -1289,7 +1290,6 @@ const char* FileDescriptorSet::_InternalParse(const char* ptr, ::PROTOBUF_NAMESP
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.FileDescriptorProto file = 1;
       // repeated .google.protobuf.FileDescriptorProto file = 1;
       case 1:
       case 1:
@@ -1305,7 +1305,8 @@ const char* FileDescriptorSet::_InternalParse(const char* ptr, ::PROTOBUF_NAMESP
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1589,7 +1590,6 @@ const char* FileDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAME
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -1734,7 +1734,8 @@ const char* FileDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAME
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -2177,7 +2178,6 @@ const char* DescriptorProto_ExtensionRange::_InternalParse(const char* ptr, ::PR
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional int32 start = 1;
       // optional int32 start = 1;
       case 1:
       case 1:
@@ -2204,7 +2204,8 @@ const char* DescriptorProto_ExtensionRange::_InternalParse(const char* ptr, ::PR
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -2455,7 +2456,6 @@ const char* DescriptorProto_ReservedRange::_InternalParse(const char* ptr, ::PRO
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional int32 start = 1;
       // optional int32 start = 1;
       case 1:
       case 1:
@@ -2475,7 +2475,8 @@ const char* DescriptorProto_ReservedRange::_InternalParse(const char* ptr, ::PRO
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -2745,7 +2746,6 @@ const char* DescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPAC
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -2867,7 +2867,8 @@ const char* DescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPAC
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -3229,7 +3230,6 @@ const char* ExtensionRangeOptions::_InternalParse(const char* ptr, ::PROTOBUF_NA
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
       // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
       case 999:
       case 999:
@@ -3245,7 +3245,8 @@ const char* ExtensionRangeOptions::_InternalParse(const char* ptr, ::PROTOBUF_NA
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -3564,7 +3565,6 @@ const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -3678,7 +3678,8 @@ const char* FieldDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -4103,7 +4104,6 @@ const char* OneofDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -4125,7 +4125,8 @@ const char* OneofDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAM
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -4359,7 +4360,6 @@ const char* EnumDescriptorProto_EnumReservedRange::_InternalParse(const char* pt
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional int32 start = 1;
       // optional int32 start = 1;
       case 1:
       case 1:
@@ -4379,7 +4379,8 @@ const char* EnumDescriptorProto_EnumReservedRange::_InternalParse(const char* pt
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -4634,7 +4635,6 @@ const char* EnumDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAME
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -4696,7 +4696,8 @@ const char* EnumDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NAME
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -5009,7 +5010,6 @@ const char* EnumValueDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -5039,7 +5039,8 @@ const char* EnumValueDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -5314,7 +5315,6 @@ const char* ServiceDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_N
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -5348,7 +5348,8 @@ const char* ServiceDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_N
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -5657,7 +5658,6 @@ const char* MethodDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NA
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string name = 1;
       // optional string name = 1;
       case 1:
       case 1:
@@ -5717,7 +5717,8 @@ const char* MethodDescriptorProto::_InternalParse(const char* ptr, ::PROTOBUF_NA
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -6200,7 +6201,6 @@ const char* FileOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional string java_package = 1;
       // optional string java_package = 1;
       case 1:
       case 1:
@@ -6410,7 +6410,8 @@ const char* FileOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -7016,7 +7017,6 @@ const char* MessageOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional bool message_set_wire_format = 1 [default = false];
       // optional bool message_set_wire_format = 1 [default = false];
       case 1:
       case 1:
@@ -7064,7 +7064,8 @@ const char* MessageOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -7372,7 +7373,6 @@ const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING];
       // optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING];
       case 1:
       case 1:
@@ -7444,7 +7444,8 @@ const char* FieldOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -7749,7 +7750,6 @@ const char* OneofOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
       // repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
       case 999:
       case 999:
@@ -7765,7 +7765,8 @@ const char* OneofOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_I
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -7986,7 +7987,6 @@ const char* EnumOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional bool allow_alias = 2;
       // optional bool allow_alias = 2;
       case 2:
       case 2:
@@ -8018,7 +8018,8 @@ const char* EnumOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -8273,7 +8274,6 @@ const char* EnumValueOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPA
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional bool deprecated = 1 [default = false];
       // optional bool deprecated = 1 [default = false];
       case 1:
       case 1:
@@ -8297,7 +8297,8 @@ const char* EnumValueOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPA
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -8527,7 +8528,6 @@ const char* ServiceOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional bool deprecated = 33 [default = false];
       // optional bool deprecated = 33 [default = false];
       case 33:
       case 33:
@@ -8551,7 +8551,8 @@ const char* ServiceOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -8794,7 +8795,6 @@ const char* MethodOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // optional bool deprecated = 33 [default = false];
       // optional bool deprecated = 33 [default = false];
       case 33:
       case 33:
@@ -8830,7 +8830,8 @@ const char* MethodOptions::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -9098,7 +9099,6 @@ const char* UninterpretedOption_NamePart::_InternalParse(const char* ptr, ::PROT
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // required string name_part = 1;
       // required string name_part = 1;
       case 1:
       case 1:
@@ -9121,7 +9121,8 @@ const char* UninterpretedOption_NamePart::_InternalParse(const char* ptr, ::PROT
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -9412,7 +9413,6 @@ const char* UninterpretedOption::_InternalParse(const char* ptr, ::PROTOBUF_NAME
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.UninterpretedOption.NamePart name = 2;
       // repeated .google.protobuf.UninterpretedOption.NamePart name = 2;
       case 2:
       case 2:
@@ -9482,7 +9482,8 @@ const char* UninterpretedOption::_InternalParse(const char* ptr, ::PROTOBUF_NAME
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -9822,7 +9823,6 @@ const char* SourceCodeInfo_Location::_InternalParse(const char* ptr, ::PROTOBUF_
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated int32 path = 1 [packed = true];
       // repeated int32 path = 1 [packed = true];
       case 1:
       case 1:
@@ -9884,7 +9884,8 @@ const char* SourceCodeInfo_Location::_InternalParse(const char* ptr, ::PROTOBUF_
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -10169,7 +10170,6 @@ const char* SourceCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.SourceCodeInfo.Location location = 1;
       // repeated .google.protobuf.SourceCodeInfo.Location location = 1;
       case 1:
       case 1:
@@ -10185,7 +10185,8 @@ const char* SourceCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -10400,7 +10401,6 @@ const char* GeneratedCodeInfo_Annotation::_InternalParse(const char* ptr, ::PROT
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated int32 path = 1 [packed = true];
       // repeated int32 path = 1 [packed = true];
       case 1:
       case 1:
@@ -10441,7 +10441,8 @@ const char* GeneratedCodeInfo_Annotation::_InternalParse(const char* ptr, ::PROT
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -10698,7 +10699,6 @@ const char* GeneratedCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESP
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1;
       // repeated .google.protobuf.GeneratedCodeInfo.Annotation annotation = 1;
       case 1:
       case 1:
@@ -10714,7 +10714,8 @@ const char* GeneratedCodeInfo::_InternalParse(const char* ptr, ::PROTOBUF_NAMESP
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 2 - 0
src/google/protobuf/descriptor.proto

@@ -496,6 +496,8 @@ message MessageOptions {
   // this is a formalization for deprecating messages.
   // this is a formalization for deprecating messages.
   optional bool deprecated = 3 [default = false];
   optional bool deprecated = 3 [default = false];
 
 
+  reserved 4, 5, 6;
+
   // Whether the message is an automatically generated map entry type for the
   // Whether the message is an automatically generated map entry type for the
   // maps field.
   // maps field.
   //
   //

+ 2 - 2
src/google/protobuf/duration.pb.cc

@@ -140,7 +140,6 @@ const char* Duration::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::i
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // int64 seconds = 1;
       // int64 seconds = 1;
       case 1:
       case 1:
@@ -158,7 +157,8 @@ const char* Duration::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::i
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 2 - 2
src/google/protobuf/empty.pb.cc

@@ -125,8 +125,8 @@ const char* Empty::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 2 - 2
src/google/protobuf/field_mask.pb.cc

@@ -131,7 +131,6 @@ const char* FieldMask::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated string paths = 1;
       // repeated string paths = 1;
       case 1:
       case 1:
@@ -149,7 +148,8 @@ const char* FieldMask::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 20 - 0
src/google/protobuf/io/coded_stream.cc

@@ -951,6 +951,26 @@ uint8* CodedOutputStream::WriteStringWithSizeToArray(const std::string& str,
   return WriteStringToArray(str, target);
   return WriteStringToArray(str, target);
 }
 }
 
 
+uint8* CodedOutputStream::WriteVarint32ToArrayOutOfLineHelper(uint32 value,
+                                                              uint8* target) {
+  GOOGLE_DCHECK_GE(value, 0x80);
+  target[0] |= static_cast<uint8>(0x80);
+  value >>= 7;
+  target[1] = static_cast<uint8>(value);
+  if (value < 0x80) {
+    return target + 2;
+  }
+  target += 2;
+  do {
+    // Turn on continuation bit in the byte we just wrote.
+    target[-1] |= static_cast<uint8>(0x80);
+    value >>= 7;
+    *target = static_cast<uint8>(value);
+    ++target;
+  } while (value >= 0x80);
+  return target;
+}
+
 }  // namespace io
 }  // namespace io
 }  // namespace protobuf
 }  // namespace protobuf
 }  // namespace google
 }  // namespace google

+ 24 - 7
src/google/protobuf/io/coded_stream.h

@@ -906,23 +906,25 @@ class PROTOBUF_EXPORT EpsCopyOutputStream {
   PROTOBUF_ALWAYS_INLINE static uint8* UnsafeVarint(T value, uint8* ptr) {
   PROTOBUF_ALWAYS_INLINE static uint8* UnsafeVarint(T value, uint8* ptr) {
     static_assert(std::is_unsigned<T>::value,
     static_assert(std::is_unsigned<T>::value,
                   "Varint serialization must be unsigned");
                   "Varint serialization must be unsigned");
+    ptr[0] = static_cast<uint8>(value);
     if (value < 0x80) {
     if (value < 0x80) {
-      ptr[0] = static_cast<uint8>(value);
       return ptr + 1;
       return ptr + 1;
     }
     }
-    ptr[0] = static_cast<uint8>(value | 0x80);
+    // Turn on continuation bit in the byte we just wrote.
+    ptr[0] |= static_cast<uint8>(0x80);
     value >>= 7;
     value >>= 7;
+    ptr[1] = static_cast<uint8>(value);
     if (value < 0x80) {
     if (value < 0x80) {
-      ptr[1] = static_cast<uint8>(value);
       return ptr + 2;
       return ptr + 2;
     }
     }
-    ptr++;
+    ptr += 2;
     do {
     do {
-      *ptr = static_cast<uint8>(value | 0x80);
+      // Turn on continuation bit in the byte we just wrote.
+      ptr[-1] |= static_cast<uint8>(0x80);
       value >>= 7;
       value >>= 7;
+      *ptr = static_cast<uint8>(value);
       ++ptr;
       ++ptr;
-    } while (PROTOBUF_PREDICT_FALSE(value >= 0x80));
-    *ptr++ = static_cast<uint8>(value);
+    } while (value >= 0x80);
     return ptr;
     return ptr;
   }
   }
 
 
@@ -1151,6 +1153,9 @@ class PROTOBUF_EXPORT CodedOutputStream {
   void WriteVarint32(uint32 value);
   void WriteVarint32(uint32 value);
   // Like WriteVarint32()  but writing directly to the target array.
   // Like WriteVarint32()  but writing directly to the target array.
   static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
   static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
+  // Like WriteVarint32()  but writing directly to the target array, and with the
+  // less common-case paths being out of line rather than inlined.
+  static uint8* WriteVarint32ToArrayOutOfLine(uint32 value, uint8* target);
   // Write an unsigned integer with Varint encoding.
   // Write an unsigned integer with Varint encoding.
   void WriteVarint64(uint64 value);
   void WriteVarint64(uint64 value);
   // Like WriteVarint64()  but writing directly to the target array.
   // Like WriteVarint64()  but writing directly to the target array.
@@ -1265,6 +1270,8 @@ class PROTOBUF_EXPORT CodedOutputStream {
   static void SetDefaultSerializationDeterministic() {
   static void SetDefaultSerializationDeterministic() {
     default_serialization_deterministic_.store(true, std::memory_order_relaxed);
     default_serialization_deterministic_.store(true, std::memory_order_relaxed);
   }
   }
+  // REQUIRES: value >= 0x80, and that (value & 7f) has been written to *target.
+  static uint8* WriteVarint32ToArrayOutOfLineHelper(uint32 value, uint8* target);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
 };
 };
 
 
@@ -1595,6 +1602,16 @@ inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
   return EpsCopyOutputStream::UnsafeVarint(value, target);
   return EpsCopyOutputStream::UnsafeVarint(value, target);
 }
 }
 
 
+inline uint8* CodedOutputStream::WriteVarint32ToArrayOutOfLine(uint32 value,
+                                                               uint8* target) {
+  target[0] = static_cast<uint8>(value);
+  if (value < 0x80) {
+    return target + 1;
+  } else {
+    return WriteVarint32ToArrayOutOfLineHelper(value, target);
+  }
+}
+
 inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value,
 inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value,
                                                       uint8* target) {
                                                       uint8* target) {
   return EpsCopyOutputStream::UnsafeVarint(value, target);
   return EpsCopyOutputStream::UnsafeVarint(value, target);

+ 32 - 0
src/google/protobuf/message_unittest.inc

@@ -661,5 +661,37 @@ TEST(MESSAGE_TEST_NAME, MOMIParserEdgeCases) {
 }
 }
 
 
 
 
+TEST(MESSAGE_TEST_NAME, CheckSerializationWhenInterleavedExtensions) {
+  UNITTEST::TestExtensionRangeSerialize in_message;
+
+  in_message.set_foo_one(1);
+  in_message.set_foo_two(2);
+  in_message.set_foo_three(3);
+  in_message.set_foo_four(4);
+
+  in_message.SetExtension(UNITTEST::TestExtensionRangeSerialize::bar_one, 1);
+  in_message.SetExtension(UNITTEST::TestExtensionRangeSerialize::bar_two, 2);
+  in_message.SetExtension(UNITTEST::TestExtensionRangeSerialize::bar_three, 3);
+  in_message.SetExtension(UNITTEST::TestExtensionRangeSerialize::bar_four, 4);
+  in_message.SetExtension(UNITTEST::TestExtensionRangeSerialize::bar_five, 5);
+
+  std::string buffer;
+  in_message.SerializeToString(&buffer);
+
+  UNITTEST::TestExtensionRangeSerialize out_message;
+  out_message.ParseFromString(buffer);
+
+  EXPECT_EQ(1, out_message.foo_one());
+  EXPECT_EQ(2, out_message.foo_two());
+  EXPECT_EQ(3, out_message.foo_three());
+  EXPECT_EQ(4, out_message.foo_four());
+
+  EXPECT_EQ(1, out_message.GetExtension(UNITTEST::TestExtensionRangeSerialize::bar_one));
+  EXPECT_EQ(2, out_message.GetExtension(UNITTEST::TestExtensionRangeSerialize::bar_two));
+  EXPECT_EQ(3, out_message.GetExtension(UNITTEST::TestExtensionRangeSerialize::bar_three));
+  EXPECT_EQ(4, out_message.GetExtension(UNITTEST::TestExtensionRangeSerialize::bar_four));
+  EXPECT_EQ(5, out_message.GetExtension(UNITTEST::TestExtensionRangeSerialize::bar_five));
+}
+
 }  // namespace protobuf
 }  // namespace protobuf
 }  // namespace google
 }  // namespace google

+ 12 - 0
src/google/protobuf/parse_context.cc

@@ -326,6 +326,18 @@ const char* EpsCopyInputStream::InitFrom(io::ZeroCopyInputStream* zcis) {
   return buffer_;
   return buffer_;
 }
 }
 
 
+const char* ParseContext::ReadSizeAndPushLimitAndDepth(const char* ptr,
+                                                       int* old_limit) {
+  int size = ReadSize(&ptr);
+  if (PROTOBUF_PREDICT_FALSE(!ptr)) {
+    *old_limit = 0;  // Make sure this isn't uninitialized even on error return
+    return nullptr;
+  }
+  *old_limit = PushLimit(ptr, size);
+  if (--depth_ < 0) return nullptr;
+  return ptr;
+}
+
 const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
 const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
   return ParseMessage<MessageLite>(msg, ptr);
   return ParseMessage<MessageLite>(msg, ptr);
 }
 }

+ 14 - 6
src/google/protobuf/parse_context.h

@@ -412,6 +412,17 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
   }
   }
 
 
  private:
  private:
+  // Out-of-line routine to save space in ParseContext::ParseMessage<T>
+  //   int old;
+  //   ptr = ReadSizeAndPushLimitAndDepth(ptr, &old)
+  // is equivalent to:
+  //   int size = ReadSize(&ptr);
+  //   if (!ptr) return nullptr;
+  //   int old = PushLimit(ptr, size);
+  //   if (--depth_ < 0) return nullptr;
+  PROTOBUF_MUST_USE_RESULT const char* ReadSizeAndPushLimitAndDepth(
+      const char* ptr, int* old_limit);
+
   // The context keeps an internal stack to keep track of the recursive
   // The context keeps an internal stack to keep track of the recursive
   // part of the parse state.
   // part of the parse state.
   // Current depth of the active parser, depth counts down.
   // Current depth of the active parser, depth counts down.
@@ -639,12 +650,9 @@ inline int32 ReadVarintZigZag32(const char** p) {
 template <typename T>
 template <typename T>
 PROTOBUF_MUST_USE_RESULT const char* ParseContext::ParseMessage(
 PROTOBUF_MUST_USE_RESULT const char* ParseContext::ParseMessage(
     T* msg, const char* ptr) {
     T* msg, const char* ptr) {
-  int size = ReadSize(&ptr);
-  if (!ptr) return nullptr;
-  auto old = PushLimit(ptr, size);
-  if (--depth_ < 0) return nullptr;
-  ptr = msg->_InternalParse(ptr, this);
-  if (PROTOBUF_PREDICT_FALSE(ptr == nullptr)) return nullptr;
+  int old;
+  ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
+  ptr = ptr ? msg->_InternalParse(ptr, this) : nullptr;
   depth_++;
   depth_++;
   if (!PopLimit(old)) return nullptr;
   if (!PopLimit(old)) return nullptr;
   return ptr;
   return ptr;

+ 42 - 9
src/google/protobuf/repeated_field.h

@@ -221,6 +221,10 @@ class RepeatedField final {
   void MergeFrom(const RepeatedField& other);
   void MergeFrom(const RepeatedField& other);
   void CopyFrom(const RepeatedField& other);
   void CopyFrom(const RepeatedField& other);
 
 
+  // Replaces the contents with RepeatedField(begin, end).
+  template <typename Iter>
+  void Assign(Iter begin, Iter end);
+
   // Reserve space to expand the field to at least the given size.  If the
   // Reserve space to expand the field to at least the given size.  If the
   // array is grown, it will always be at least doubled in size.
   // array is grown, it will always be at least doubled in size.
   void Reserve(int new_size);
   void Reserve(int new_size);
@@ -765,8 +769,9 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase {
                                                                   int));
                                                                   int));
 
 
   template <typename TypeHandler>
   template <typename TypeHandler>
-  void MergeFromInnerLoop(void** our_elems, void** other_elems, int length,
-                          int already_allocated);
+  PROTOBUF_NOINLINE void MergeFromInnerLoop(void** our_elems,
+                                            void** other_elems, int length,
+                                            int already_allocated);
 
 
   // Internal helper: extend array space if necessary to contain |extend_amount|
   // Internal helper: extend array space if necessary to contain |extend_amount|
   // more elements, and return a pointer to the element immediately following
   // more elements, and return a pointer to the element immediately following
@@ -953,6 +958,10 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
   Element* Mutable(int index);
   Element* Mutable(int index);
   Element* Add();
   Element* Add();
   void Add(Element&& value);
   void Add(Element&& value);
+  // Append elements in the range [begin, end) after reserving
+  // the appropriate number of elements.
+  template <typename Iter>
+  void Add(Iter begin, Iter end);
 
 
   const Element& operator[](int index) const { return Get(index); }
   const Element& operator[](int index) const { return Get(index); }
   Element& operator[](int index) { return *Mutable(index); }
   Element& operator[](int index) { return *Mutable(index); }
@@ -973,6 +982,10 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
   void MergeFrom(const RepeatedPtrField& other);
   void MergeFrom(const RepeatedPtrField& other);
   void CopyFrom(const RepeatedPtrField& other);
   void CopyFrom(const RepeatedPtrField& other);
 
 
+  // Replaces the contents with RepeatedPtrField(begin, end).
+  template <typename Iter>
+  void Assign(Iter begin, Iter end);
+
   // Reserve space to expand the field to at least the given size.  This only
   // Reserve space to expand the field to at least the given size.  This only
   // resizes the pointer array; it doesn't allocate any objects.  If the
   // resizes the pointer array; it doesn't allocate any objects.  If the
   // array is grown, it will always be at least doubled in size.
   // array is grown, it will always be at least doubled in size.
@@ -1447,6 +1460,13 @@ inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
   MergeFrom(other);
   MergeFrom(other);
 }
 }
 
 
+template <typename Element>
+template <typename Iter>
+inline void RepeatedField<Element>::Assign(Iter begin, Iter end) {
+  Clear();
+  Add(begin, end);
+}
+
 template <typename Element>
 template <typename Element>
 inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
 inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
     const_iterator position) {
     const_iterator position) {
@@ -2122,13 +2142,7 @@ inline RepeatedPtrField<Element>::RepeatedPtrField(
 template <typename Element>
 template <typename Element>
 template <typename Iter, typename>
 template <typename Iter, typename>
 inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, Iter end) {
 inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, Iter end) {
-  int reserve = internal::CalculateReserve(begin, end);
-  if (reserve != -1) {
-    Reserve(reserve);
-  }
-  for (; begin != end; ++begin) {
-    *Add() = *begin;
-  }
+  Add(begin, end);
 }
 }
 
 
 template <typename Element>
 template <typename Element>
@@ -2213,6 +2227,18 @@ inline void RepeatedPtrField<Element>::Add(Element&& value) {
   RepeatedPtrFieldBase::Add<TypeHandler>(std::move(value));
   RepeatedPtrFieldBase::Add<TypeHandler>(std::move(value));
 }
 }
 
 
+template <typename Element>
+template <typename Iter>
+inline void RepeatedPtrField<Element>::Add(Iter begin, Iter end) {
+  int reserve = internal::CalculateReserve(begin, end);
+  if (reserve != -1) {
+    Reserve(size() + reserve);
+  }
+  for (; begin != end; ++begin) {
+    *Add() = *begin;
+  }
+}
+
 template <typename Element>
 template <typename Element>
 inline void RepeatedPtrField<Element>::RemoveLast() {
 inline void RepeatedPtrField<Element>::RemoveLast() {
   RepeatedPtrFieldBase::RemoveLast<TypeHandler>();
   RepeatedPtrFieldBase::RemoveLast<TypeHandler>();
@@ -2319,6 +2345,13 @@ inline void RepeatedPtrField<Element>::CopyFrom(const RepeatedPtrField& other) {
   RepeatedPtrFieldBase::CopyFrom<TypeHandler>(other);
   RepeatedPtrFieldBase::CopyFrom<TypeHandler>(other);
 }
 }
 
 
+template <typename Element>
+template <typename Iter>
+inline void RepeatedPtrField<Element>::Assign(Iter begin, Iter end) {
+  Clear();
+  Add(begin, end);
+}
+
 template <typename Element>
 template <typename Element>
 inline typename RepeatedPtrField<Element>::iterator
 inline typename RepeatedPtrField<Element>::iterator
 RepeatedPtrField<Element>::erase(const_iterator position) {
 RepeatedPtrField<Element>::erase(const_iterator position) {

+ 87 - 0
src/google/protobuf/repeated_field_unittest.cc

@@ -488,6 +488,30 @@ TEST(RepeatedField, AddRange5) {
   ASSERT_EQ(me.Get(1), 2);
   ASSERT_EQ(me.Get(1), 2);
 }
 }
 
 
+TEST(RepeatedField, AddAndAssignRanges) {
+  RepeatedField<int> field;
+
+  int vals[] = {2, 27, 2875, 609250};
+  field.Assign(std::begin(vals), std::end(vals));
+
+  ASSERT_EQ(field.size(), 4);
+  EXPECT_EQ(field.Get(0), 2);
+  EXPECT_EQ(field.Get(1), 27);
+  EXPECT_EQ(field.Get(2), 2875);
+  EXPECT_EQ(field.Get(3), 609250);
+
+  field.Add(std::begin(vals), std::end(vals));
+  ASSERT_EQ(field.size(), 8);
+  EXPECT_EQ(field.Get(0), 2);
+  EXPECT_EQ(field.Get(1), 27);
+  EXPECT_EQ(field.Get(2), 2875);
+  EXPECT_EQ(field.Get(3), 609250);
+  EXPECT_EQ(field.Get(4), 2);
+  EXPECT_EQ(field.Get(5), 27);
+  EXPECT_EQ(field.Get(6), 2875);
+  EXPECT_EQ(field.Get(7), 609250);
+}
+
 TEST(RepeatedField, CopyConstruct) {
 TEST(RepeatedField, CopyConstruct) {
   RepeatedField<int> source;
   RepeatedField<int> source;
   source.Add(1);
   source.Add(1);
@@ -837,6 +861,45 @@ TEST(RepeatedPtrField, ConstInit) {
   EXPECT_TRUE(field.empty());
   EXPECT_TRUE(field.empty());
 }
 }
 
 
+// This helper overload set tests whether X::f can be called with a braced pair,
+// X::f({a, b}) of std::string iterators (specifically, pointers: That call is
+// ambiguous if and only if the call to ValidResolutionPointerRange is not.
+template <typename X>
+auto ValidResolutionPointerRange(const std::string* p)
+    -> decltype(X::f({p, p + 2}), std::true_type{});
+template <typename X>
+std::false_type ValidResolutionPointerRange(void*);
+
+TEST(RepeatedPtrField, UnambiguousConstructor) {
+  struct X {
+    static bool f(std::vector<std::string>) { return false; }
+    static bool f(google::protobuf::RepeatedPtrField<std::string>) { return true; }
+
+    static bool g(std::vector<int>) { return false; }
+    static bool g(google::protobuf::RepeatedPtrField<std::string>) { return true; }
+  };
+
+  // RepeatedPtrField has no initializer-list constructor, and a constructor
+  // from to const char* values is excluded by its constraints.
+  EXPECT_FALSE(X::f({"abc", "xyz"}));
+
+  // Construction from a pair of int* is also not ambiguous.
+  int a[5] = {};
+  EXPECT_FALSE(X::g({a, a + 5}));
+
+  // Construction from string iterators for the unique string overload "g"
+  // works.
+  std::string b[2] = {"abc", "xyz"};
+  // Disabling this for now, this is actually ambiguous with libstdc++.
+  // EXPECT_TRUE(X::g({b, b + 2}));
+
+  // Construction from string iterators for "f" is ambiguous, since both
+  // containers are equally good.
+  //
+  // X::f({b, b + 2});  // error => ValidResolutionPointerRange is unambiguous.
+  EXPECT_FALSE(decltype(ValidResolutionPointerRange<X>(nullptr))::value);
+}
+
 TEST(RepeatedPtrField, Small) {
 TEST(RepeatedPtrField, Small) {
   RepeatedPtrField<std::string> field;
   RepeatedPtrField<std::string> field;
 
 
@@ -899,6 +962,30 @@ TEST(RepeatedPtrField, Large) {
   EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
   EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
 }
 }
 
 
+TEST(RepeatedPtrField, AddAndAssignRanges) {
+  RepeatedPtrField<std::string> field;
+
+  const char* vals[] = {"abc", "x", "yz", "xyzzy"};
+  field.Assign(std::begin(vals), std::end(vals));
+
+  ASSERT_EQ(field.size(), 4);
+  EXPECT_EQ(field.Get(0), "abc");
+  EXPECT_EQ(field.Get(1), "x");
+  EXPECT_EQ(field.Get(2), "yz");
+  EXPECT_EQ(field.Get(3), "xyzzy");
+
+  field.Add(std::begin(vals), std::end(vals));
+  ASSERT_EQ(field.size(), 8);
+  EXPECT_EQ(field.Get(0), "abc");
+  EXPECT_EQ(field.Get(1), "x");
+  EXPECT_EQ(field.Get(2), "yz");
+  EXPECT_EQ(field.Get(3), "xyzzy");
+  EXPECT_EQ(field.Get(4), "abc");
+  EXPECT_EQ(field.Get(5), "x");
+  EXPECT_EQ(field.Get(6), "yz");
+  EXPECT_EQ(field.Get(7), "xyzzy");
+}
+
 TEST(RepeatedPtrField, SwapSmallSmall) {
 TEST(RepeatedPtrField, SwapSmallSmall) {
   RepeatedPtrField<std::string> field1;
   RepeatedPtrField<std::string> field1;
   RepeatedPtrField<std::string> field2;
   RepeatedPtrField<std::string> field2;

+ 2 - 2
src/google/protobuf/source_context.pb.cc

@@ -136,7 +136,6 @@ const char* SourceContext::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string file_name = 1;
       // string file_name = 1;
       case 1:
       case 1:
@@ -149,7 +148,8 @@ const char* SourceContext::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 6 - 6
src/google/protobuf/struct.pb.cc

@@ -241,7 +241,6 @@ const char* Struct::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // map<string, .google.protobuf.Value> fields = 1;
       // map<string, .google.protobuf.Value> fields = 1;
       case 1:
       case 1:
@@ -257,7 +256,8 @@ const char* Struct::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -582,7 +582,6 @@ const char* Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // .google.protobuf.NullValue null_value = 1;
       // .google.protobuf.NullValue null_value = 1;
       case 1:
       case 1:
@@ -631,7 +630,8 @@ const char* Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -911,7 +911,6 @@ const char* ListValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // repeated .google.protobuf.Value values = 1;
       // repeated .google.protobuf.Value values = 1;
       case 1:
       case 1:
@@ -927,7 +926,8 @@ const char* ListValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 2 - 2
src/google/protobuf/timestamp.pb.cc

@@ -140,7 +140,6 @@ const char* Timestamp::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // int64 seconds = 1;
       // int64 seconds = 1;
       case 1:
       case 1:
@@ -158,7 +157,8 @@ const char* Timestamp::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 10 - 10
src/google/protobuf/type.pb.cc

@@ -430,7 +430,6 @@ const char* Type::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -496,7 +495,8 @@ const char* Type::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -815,7 +815,6 @@ const char* Field::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // .google.protobuf.Field.Kind kind = 1;
       // .google.protobuf.Field.Kind kind = 1;
       case 1:
       case 1:
@@ -904,7 +903,8 @@ const char* Field::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inte
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1289,7 +1289,6 @@ const char* Enum::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -1341,7 +1340,8 @@ const char* Enum::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::inter
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1609,7 +1609,6 @@ const char* EnumValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -1641,7 +1640,8 @@ const char* EnumValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1885,7 +1885,6 @@ const char* Option::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string name = 1;
       // string name = 1;
       case 1:
       case 1:
@@ -1905,7 +1904,8 @@ const char* Option::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::int
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }

+ 30 - 0
src/google/protobuf/unittest.proto

@@ -1115,3 +1115,33 @@ enum VeryLargeEnum {
   ENUM_LABEL_99 = 99;
   ENUM_LABEL_99 = 99;
   ENUM_LABEL_100 = 100;
   ENUM_LABEL_100 = 100;
 };
 };
+
+message TestExtensionRangeSerialize {
+  optional int32 foo_one = 1;
+
+  extensions 2 to 2;
+  extensions 3 to 4;
+
+  optional int32 foo_two = 6;
+  optional int32 foo_three = 7;
+
+  extensions 9 to 10;
+
+  optional int32 foo_four = 13;
+
+  extensions 15 to 15;
+  extensions 17 to 17;
+  extensions 19 to 19;
+
+  extend TestExtensionRangeSerialize {
+    optional int32 bar_one = 2;
+    optional int32 bar_two = 4;
+
+    optional int32 bar_three = 10;
+
+    optional int32 bar_four = 15;
+    optional int32 bar_five = 19;
+  }
+}
+
+

+ 21 - 22
src/google/protobuf/util/field_comparator.cc

@@ -48,14 +48,14 @@ namespace util {
 FieldComparator::FieldComparator() {}
 FieldComparator::FieldComparator() {}
 FieldComparator::~FieldComparator() {}
 FieldComparator::~FieldComparator() {}
 
 
-DefaultFieldComparator::DefaultFieldComparator()
+SimpleFieldComparator::SimpleFieldComparator()
     : float_comparison_(EXACT),
     : float_comparison_(EXACT),
       treat_nan_as_equal_(false),
       treat_nan_as_equal_(false),
       has_default_tolerance_(false) {}
       has_default_tolerance_(false) {}
 
 
-DefaultFieldComparator::~DefaultFieldComparator() {}
+SimpleFieldComparator::~SimpleFieldComparator() {}
 
 
-FieldComparator::ComparisonResult DefaultFieldComparator::Compare(
+FieldComparator::ComparisonResult SimpleFieldComparator::SimpleCompare(
     const Message& message_1, const Message& message_2,
     const Message& message_1, const Message& message_2,
     const FieldDescriptor* field, int index_1, int index_2,
     const FieldDescriptor* field, int index_1, int index_2,
     const util::FieldContext* field_context) {
     const util::FieldContext* field_context) {
@@ -127,23 +127,22 @@ FieldComparator::ComparisonResult DefaultFieldComparator::Compare(
   }
   }
 }
 }
 
 
-bool DefaultFieldComparator::Compare(MessageDifferencer* differencer,
-                                     const Message& message1,
-                                     const Message& message2,
-                                     const util::FieldContext* field_context) {
+bool SimpleFieldComparator::CompareWithDifferencer(
+    MessageDifferencer* differencer, const Message& message1,
+    const Message& message2, const util::FieldContext* field_context) {
   return differencer->Compare(message1, message2,
   return differencer->Compare(message1, message2,
                               field_context->parent_fields());
                               field_context->parent_fields());
 }
 }
 
 
-void DefaultFieldComparator::SetDefaultFractionAndMargin(double fraction,
-                                                         double margin) {
+void SimpleFieldComparator::SetDefaultFractionAndMargin(double fraction,
+                                                        double margin) {
   default_tolerance_ = Tolerance(fraction, margin);
   default_tolerance_ = Tolerance(fraction, margin);
   has_default_tolerance_ = true;
   has_default_tolerance_ = true;
 }
 }
 
 
-void DefaultFieldComparator::SetFractionAndMargin(const FieldDescriptor* field,
-                                                  double fraction,
-                                                  double margin) {
+void SimpleFieldComparator::SetFractionAndMargin(const FieldDescriptor* field,
+                                                 double fraction,
+                                                 double margin) {
   GOOGLE_CHECK(FieldDescriptor::CPPTYPE_FLOAT == field->cpp_type() ||
   GOOGLE_CHECK(FieldDescriptor::CPPTYPE_FLOAT == field->cpp_type() ||
         FieldDescriptor::CPPTYPE_DOUBLE == field->cpp_type())
         FieldDescriptor::CPPTYPE_DOUBLE == field->cpp_type())
       << "Field has to be float or double type. Field name is: "
       << "Field has to be float or double type. Field name is: "
@@ -151,25 +150,25 @@ void DefaultFieldComparator::SetFractionAndMargin(const FieldDescriptor* field,
   map_tolerance_[field] = Tolerance(fraction, margin);
   map_tolerance_[field] = Tolerance(fraction, margin);
 }
 }
 
 
-bool DefaultFieldComparator::CompareDouble(const FieldDescriptor& field,
-                                           double value_1, double value_2) {
+bool SimpleFieldComparator::CompareDouble(const FieldDescriptor& field,
+                                          double value_1, double value_2) {
   return CompareDoubleOrFloat(field, value_1, value_2);
   return CompareDoubleOrFloat(field, value_1, value_2);
 }
 }
 
 
-bool DefaultFieldComparator::CompareEnum(const FieldDescriptor& field,
-                                         const EnumValueDescriptor* value_1,
-                                         const EnumValueDescriptor* value_2) {
+bool SimpleFieldComparator::CompareEnum(const FieldDescriptor& field,
+                                        const EnumValueDescriptor* value_1,
+                                        const EnumValueDescriptor* value_2) {
   return value_1->number() == value_2->number();
   return value_1->number() == value_2->number();
 }
 }
 
 
-bool DefaultFieldComparator::CompareFloat(const FieldDescriptor& field,
-                                          float value_1, float value_2) {
+bool SimpleFieldComparator::CompareFloat(const FieldDescriptor& field,
+                                         float value_1, float value_2) {
   return CompareDoubleOrFloat(field, value_1, value_2);
   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) {
+bool SimpleFieldComparator::CompareDoubleOrFloat(const FieldDescriptor& field,
+                                                 T value_1, T value_2) {
   if (value_1 == value_2) {
   if (value_1 == value_2) {
     // Covers +inf and -inf (which are not within margin or fraction of
     // Covers +inf and -inf (which are not within margin or fraction of
     // themselves), and is a shortcut for finite values.
     // themselves), and is a shortcut for finite values.
@@ -201,7 +200,7 @@ bool DefaultFieldComparator::CompareDoubleOrFloat(const FieldDescriptor& field,
   }
   }
 }
 }
 
 
-FieldComparator::ComparisonResult DefaultFieldComparator::ResultFromBoolean(
+FieldComparator::ComparisonResult SimpleFieldComparator::ResultFromBoolean(
     bool boolean_result) const {
     bool boolean_result) const {
   return boolean_result ? FieldComparator::SAME : FieldComparator::DIFFERENT;
   return boolean_result ? FieldComparator::SAME : FieldComparator::DIFFERENT;
 }
 }

+ 48 - 21
src/google/protobuf/util/field_comparator.h

@@ -33,12 +33,12 @@
 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_COMPARATOR_H__
 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_COMPARATOR_H__
 #define GOOGLE_PROTOBUF_UTIL_FIELD_COMPARATOR_H__
 #define GOOGLE_PROTOBUF_UTIL_FIELD_COMPARATOR_H__
 
 
+#include <cstdint>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 
 
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
-
 #include <google/protobuf/port_def.inc>
 #include <google/protobuf/port_def.inc>
 
 
 namespace google {
 namespace google {
@@ -97,7 +97,7 @@ class PROTOBUF_EXPORT FieldComparator {
 // Basic implementation of FieldComparator.  Supports three modes of floating
 // Basic implementation of FieldComparator.  Supports three modes of floating
 // point value comparison: exact, approximate using MathUtil::AlmostEqual
 // point value comparison: exact, approximate using MathUtil::AlmostEqual
 // method, and arbitrarily precise using MathUtil::WithinFractionOrMargin.
 // method, and arbitrarily precise using MathUtil::WithinFractionOrMargin.
-class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
+class PROTOBUF_EXPORT SimpleFieldComparator : public FieldComparator {
  public:
  public:
   enum FloatComparison {
   enum FloatComparison {
     EXACT,        // Floats and doubles are compared exactly.
     EXACT,        // Floats and doubles are compared exactly.
@@ -109,14 +109,9 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
   };
   };
 
 
   // Creates new comparator with float comparison set to EXACT.
   // Creates new comparator with float comparison set to EXACT.
-  DefaultFieldComparator();
-
-  ~DefaultFieldComparator() override;
+  SimpleFieldComparator();
 
 
-  ComparisonResult Compare(const Message& message_1, const Message& message_2,
-                           const FieldDescriptor* field, int index_1,
-                           int index_2,
-                           const util::FieldContext* field_context) override;
+  ~SimpleFieldComparator() override;
 
 
   void set_float_comparison(FloatComparison float_comparison) {
   void set_float_comparison(FloatComparison float_comparison) {
     float_comparison_ = float_comparison;
     float_comparison_ = float_comparison;
@@ -151,12 +146,32 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
   void SetDefaultFractionAndMargin(double fraction, double margin);
   void SetDefaultFractionAndMargin(double fraction, double margin);
 
 
  protected:
  protected:
+  // NOTE: this will go away.
+  ComparisonResult Compare(const Message& message_1, const Message& message_2,
+                           const FieldDescriptor* field, int index_1,
+                           int index_2,
+                           const util::FieldContext* field_context) override {
+    return SimpleCompare(message_1, message_2, field, index_1, index_2,
+                         field_context);
+  }
+
+  // Returns the comparison result for the given field in two messages.
+  //
+  // This function is called directly by DefaultFieldComparator::Compare.
+  // Subclasses can call this function to compare fields they do not need to
+  // handle specially.
+  ComparisonResult SimpleCompare(const Message& message_1,
+                                 const Message& message_2,
+                                 const FieldDescriptor* field, int index_1,
+                                 int index_2,
+                                 const util::FieldContext* field_context);
+
   // Compare using the provided message_differencer. For example, a subclass can
   // Compare using the provided message_differencer. For example, a subclass can
   // use this method to compare some field in a certain way using the same
   // use this method to compare some field in a certain way using the same
   // message_differencer instance and the field context.
   // message_differencer instance and the field context.
-  bool Compare(MessageDifferencer* differencer, const Message& message1,
-               const Message& message2,
-               const util::FieldContext* field_context);
+  bool CompareWithDifferencer(MessageDifferencer* differencer,
+                              const Message& message1, const Message& message2,
+                              const util::FieldContext* field_context);
 
 
  private:
  private:
   // Defines the tolerance for floating point comparison (fraction and margin).
   // Defines the tolerance for floating point comparison (fraction and margin).
@@ -193,13 +208,13 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
   // CompareFloat.
   // CompareFloat.
   bool CompareFloat(const FieldDescriptor& field, float value_1, float value_2);
   bool CompareFloat(const FieldDescriptor& field, float value_1, float value_2);
 
 
-  bool CompareInt32(const FieldDescriptor& /* unused */, int32 value_1,
-                    int32 value_2) {
+  bool CompareInt32(const FieldDescriptor& /* unused */, int32_t value_1,
+                    int32_t value_2) {
     return value_1 == value_2;
     return value_1 == value_2;
   }
   }
 
 
-  bool CompareInt64(const FieldDescriptor& /* unused */, int64 value_1,
-                    int64 value_2) {
+  bool CompareInt64(const FieldDescriptor& /* unused */, int64_t value_1,
+                    int64_t value_2) {
     return value_1 == value_2;
     return value_1 == value_2;
   }
   }
 
 
@@ -208,13 +223,13 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
     return value_1 == value_2;
     return value_1 == value_2;
   }
   }
 
 
-  bool CompareUInt32(const FieldDescriptor& /* unused */, uint32 value_1,
-                     uint32 value_2) {
+  bool CompareUInt32(const FieldDescriptor& /* unused */, uint32_t value_1,
+                     uint32_t value_2) {
     return value_1 == value_2;
     return value_1 == value_2;
   }
   }
 
 
-  bool CompareUInt64(const FieldDescriptor& /* unused */, uint64 value_1,
-                     uint64 value_2) {
+  bool CompareUInt64(const FieldDescriptor& /* unused */, uint64_t value_1,
+                     uint64_t value_2) {
     return value_1 == value_2;
     return value_1 == value_2;
   }
   }
 
 
@@ -249,7 +264,19 @@ class PROTOBUF_EXPORT DefaultFieldComparator : public FieldComparator {
   // those particular fields.
   // those particular fields.
   ToleranceMap map_tolerance_;
   ToleranceMap map_tolerance_;
 
 
-  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultFieldComparator);
+  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleFieldComparator);
+};
+
+// Default field comparison: use the basic implementation of FieldComparator.
+class PROTOBUF_EXPORT DefaultFieldComparator : public SimpleFieldComparator {
+ public:
+  ComparisonResult Compare(const Message& message_1, const Message& message_2,
+                           const FieldDescriptor* field, int index_1,
+                           int index_2,
+                           const util::FieldContext* field_context) override {
+    return SimpleCompare(message_1, message_2, field, index_1, index_2,
+                         field_context);
+  }
 };
 };
 
 
 }  // namespace util
 }  // namespace util

+ 5 - 2
src/google/protobuf/util/field_mask_util.cc

@@ -30,10 +30,13 @@
 
 
 #include <google/protobuf/util/field_mask_util.h>
 #include <google/protobuf/util/field_mask_util.h>
 
 
+#include <cstdint>
+
 #include <google/protobuf/message.h>
 #include <google/protobuf/message.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/map_util.h>
 #include <google/protobuf/stubs/map_util.h>
 
 
+// Must be included last.
 #include <google/protobuf/port_def.inc>
 #include <google/protobuf/port_def.inc>
 
 
 namespace google {
 namespace google {
@@ -552,7 +555,7 @@ void FieldMaskTree::MergeMessage(const Node* node, const Message& source,
 
 
 void FieldMaskTree::AddRequiredFieldPath(Node* node,
 void FieldMaskTree::AddRequiredFieldPath(Node* node,
                                          const Descriptor* descriptor) {
                                          const Descriptor* descriptor) {
-  const int32 field_count = descriptor->field_count();
+  const int32_t field_count = descriptor->field_count();
   for (int index = 0; index < field_count; ++index) {
   for (int index = 0; index < field_count; ++index) {
     const FieldDescriptor* field = descriptor->field(index);
     const FieldDescriptor* field = descriptor->field(index);
     if (field->is_required()) {
     if (field->is_required()) {
@@ -589,7 +592,7 @@ bool FieldMaskTree::TrimMessage(const Node* node, Message* message) {
   GOOGLE_DCHECK(!node->children.empty());
   GOOGLE_DCHECK(!node->children.empty());
   const Reflection* reflection = message->GetReflection();
   const Reflection* reflection = message->GetReflection();
   const Descriptor* descriptor = message->GetDescriptor();
   const Descriptor* descriptor = message->GetDescriptor();
-  const int32 field_count = descriptor->field_count();
+  const int32_t field_count = descriptor->field_count();
   bool modified = false;
   bool modified = false;
   for (int index = 0; index < field_count; ++index) {
   for (int index = 0; index < field_count; ++index) {
     const FieldDescriptor* field = descriptor->field(index);
     const FieldDescriptor* field = descriptor->field(index);

+ 2 - 1
src/google/protobuf/util/field_mask_util.h

@@ -33,6 +33,7 @@
 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
 
 
+#include <cstdint>
 #include <string>
 #include <string>
 
 
 #include <google/protobuf/field_mask.pb.h>
 #include <google/protobuf/field_mask.pb.h>
@@ -58,7 +59,7 @@ class PROTOBUF_EXPORT FieldMaskUtil {
   // Populates the FieldMask with the paths corresponding to the fields with the
   // Populates the FieldMask with the paths corresponding to the fields with the
   // given numbers, after checking that all field numbers are valid.
   // given numbers, after checking that all field numbers are valid.
   template <typename T>
   template <typename T>
-  static void FromFieldNumbers(const std::vector<int64>& field_numbers,
+  static void FromFieldNumbers(const std::vector<int64_t>& field_numbers,
                                FieldMask* out) {
                                FieldMask* out) {
     for (const auto field_number : field_numbers) {
     for (const auto field_number : field_numbers) {
       const FieldDescriptor* field_desc =
       const FieldDescriptor* field_desc =

+ 2 - 1
src/google/protobuf/util/field_mask_util_test.cc

@@ -31,6 +31,7 @@
 #include <google/protobuf/util/field_mask_util.h>
 #include <google/protobuf/util/field_mask_util.h>
 
 
 #include <algorithm>
 #include <algorithm>
+#include <cstdint>
 #include <vector>
 #include <vector>
 
 
 #include <google/protobuf/field_mask.pb.h>
 #include <google/protobuf/field_mask.pb.h>
@@ -164,7 +165,7 @@ TEST(FieldMaskUtilTest, JsonStringFormat) {
 
 
 TEST(FieldMaskUtilTest, FromFieldNumbers) {
 TEST(FieldMaskUtilTest, FromFieldNumbers) {
   FieldMask mask;
   FieldMask mask;
-  std::vector<int64> field_numbers = {
+  std::vector<int64_t> field_numbers = {
       TestAllTypes::kOptionalInt64FieldNumber,
       TestAllTypes::kOptionalInt64FieldNumber,
       TestAllTypes::kOptionalBoolFieldNumber,
       TestAllTypes::kOptionalBoolFieldNumber,
       TestAllTypes::kRepeatedStringFieldNumber,
       TestAllTypes::kRepeatedStringFieldNumber,

+ 2 - 1
src/google/protobuf/util/json_util_test.cc

@@ -30,6 +30,7 @@
 
 
 #include <google/protobuf/util/json_util.h>
 #include <google/protobuf/util/json_util.h>
 
 
+#include <cstdint>
 #include <list>
 #include <list>
 #include <string>
 #include <string>
 
 
@@ -524,7 +525,7 @@ class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream {
  private:
  private:
   std::list<Segment> segments_;
   std::list<Segment> segments_;
   Segment last_segment_;
   Segment last_segment_;
-  int64 byte_count_;
+  int64_t byte_count_;
 };
 };
 
 
 // This test splits the output buffer and also the input data into multiple
 // This test splits the output buffer and also the input data into multiple

+ 30 - 13
src/google/protobuf/util/message_differencer.cc

@@ -36,7 +36,9 @@
 
 
 #include <algorithm>
 #include <algorithm>
 #include <cstddef>
 #include <cstddef>
+#include <cstdint>
 #include <functional>
 #include <functional>
+#include <limits>
 #include <memory>
 #include <memory>
 #include <utility>
 #include <utility>
 
 
@@ -71,7 +73,7 @@ class NumDiffsReporter : public google::protobuf::util::MessageDifferencer::Repo
   NumDiffsReporter() : num_diffs_(0) {}
   NumDiffsReporter() : num_diffs_(0) {}
 
 
   // Returns the total number of diffs.
   // Returns the total number of diffs.
-  int32 GetNumDiffs() const { return num_diffs_; }
+  int32_t GetNumDiffs() const { return num_diffs_; }
   void Reset() { num_diffs_ = 0; }
   void Reset() { num_diffs_ = 0; }
 
 
   // Report that a field has been added into Message2.
   // Report that a field has been added into Message2.
@@ -99,7 +101,7 @@ class NumDiffsReporter : public google::protobuf::util::MessageDifferencer::Repo
   }
   }
 
 
  private:
  private:
-  int32 num_diffs_;
+  int32_t num_diffs_;
 };
 };
 
 
 // When comparing a repeated field as map, MultipleFieldMapKeyComparator can
 // When comparing a repeated field as map, MultipleFieldMapKeyComparator can
@@ -145,7 +147,7 @@ class MessageDifferencer::MultipleFieldsMapKeyComparator
       int path_index) const {
       int path_index) const {
     const FieldDescriptor* field = key_field_path[path_index];
     const FieldDescriptor* field = key_field_path[path_index];
     std::vector<SpecificField> current_parent_fields(parent_fields);
     std::vector<SpecificField> current_parent_fields(parent_fields);
-    if (path_index == static_cast<int64>(key_field_path.size() - 1)) {
+    if (path_index == static_cast<int64_t>(key_field_path.size() - 1)) {
       if (field->is_repeated()) {
       if (field->is_repeated()) {
         if (!message_differencer_->CompareRepeatedField(
         if (!message_differencer_->CompareRepeatedField(
                 message1, message2, field, &current_parent_fields)) {
                 message1, message2, field, &current_parent_fields)) {
@@ -913,6 +915,11 @@ bool MessageDifferencer::CompareMapFieldByMapReflection(
     const Message& message1, const Message& message2,
     const Message& message1, const Message& message2,
     const FieldDescriptor* map_field, std::vector<SpecificField>* parent_fields,
     const FieldDescriptor* map_field, std::vector<SpecificField>* parent_fields,
     DefaultFieldComparator* comparator) {
     DefaultFieldComparator* comparator) {
+  GOOGLE_DCHECK_EQ(nullptr, reporter_);
+  GOOGLE_DCHECK(map_field->is_map());
+  GOOGLE_DCHECK(map_field_key_comparator_.find(map_field) ==
+         map_field_key_comparator_.end());
+  GOOGLE_DCHECK_EQ(repeated_field_comparison_, AS_LIST);
   const Reflection* reflection1 = message1.GetReflection();
   const Reflection* reflection1 = message1.GetReflection();
   const Reflection* reflection2 = message2.GetReflection();
   const Reflection* reflection2 = message2.GetReflection();
   const int count1 = reflection1->MapSize(message1, map_field);
   const int count1 = reflection1->MapSize(message1, map_field);
@@ -924,18 +931,28 @@ bool MessageDifferencer::CompareMapFieldByMapReflection(
   if (count1 > count2) {
   if (count1 > count2) {
     return false;
     return false;
   }
   }
+
+  // First pass: check whether the same keys are present.
+  for (MapIterator it = reflection1->MapBegin(const_cast<Message*>(&message1),
+                                              map_field),
+                   it_end = reflection1->MapEnd(const_cast<Message*>(&message1),
+                                                map_field);
+       it != it_end; ++it) {
+    if (!reflection2->ContainsMapKey(message2, map_field, it.GetKey())) {
+      return false;
+    }
+  }
+
+  // Second pass: compare values for matching keys.
   const FieldDescriptor* val_des = map_field->message_type()->map_value();
   const FieldDescriptor* val_des = map_field->message_type()->map_value();
   switch (val_des->cpp_type()) {
   switch (val_des->cpp_type()) {
 #define HANDLE_TYPE(CPPTYPE, METHOD, COMPAREMETHOD)                           \
 #define HANDLE_TYPE(CPPTYPE, METHOD, COMPAREMETHOD)                           \
   case FieldDescriptor::CPPTYPE_##CPPTYPE: {                                  \
   case FieldDescriptor::CPPTYPE_##CPPTYPE: {                                  \
     for (MapIterator it = reflection1->MapBegin(                              \
     for (MapIterator it = reflection1->MapBegin(                              \
-             const_cast<Message*>(&message1), map_field);                     \
-         it !=                                                                \
-         reflection1->MapEnd(const_cast<Message*>(&message1), map_field);     \
-         ++it) {                                                              \
-      if (!reflection2->ContainsMapKey(message2, map_field, it.GetKey())) {   \
-        return false;                                                         \
-      }                                                                       \
+                         const_cast<Message*>(&message1), map_field),         \
+                     it_end = reflection1->MapEnd(                            \
+                         const_cast<Message*>(&message1), map_field);         \
+         it != it_end; ++it) {                                                \
       MapValueConstRef value2;                                                \
       MapValueConstRef value2;                                                \
       reflection2->LookupMapValue(message2, map_field, it.GetKey(), &value2); \
       reflection2->LookupMapValue(message2, map_field, it.GetKey(), &value2); \
       if (!comparator->Compare##COMPAREMETHOD(*val_des,                       \
       if (!comparator->Compare##COMPAREMETHOD(*val_des,                       \
@@ -1716,9 +1733,9 @@ bool MessageDifferencer::MatchRepeatedFieldIndices(
   Reporter* reporter = reporter_;
   Reporter* reporter = reporter_;
   reporter_ = NULL;
   reporter_ = NULL;
   NumDiffsReporter num_diffs_reporter;
   NumDiffsReporter num_diffs_reporter;
-  std::vector<int32> num_diffs_list1;
+  std::vector<int32_t> num_diffs_list1;
   if (is_treated_as_smart_set) {
   if (is_treated_as_smart_set) {
-    num_diffs_list1.assign(count1, kint32max);
+    num_diffs_list1.assign(count1, std::numeric_limits<int32_t>::max());
   }
   }
 
 
   bool success = true;
   bool success = true;
@@ -1786,7 +1803,7 @@ bool MessageDifferencer::MatchRepeatedFieldIndices(
           } else if (repeated_field->cpp_type() ==
           } else if (repeated_field->cpp_type() ==
                      FieldDescriptor::CPPTYPE_MESSAGE) {
                      FieldDescriptor::CPPTYPE_MESSAGE) {
             // Replace with the one with fewer diffs.
             // Replace with the one with fewer diffs.
-            const int32 num_diffs = num_diffs_reporter.GetNumDiffs();
+            const int32_t num_diffs = num_diffs_reporter.GetNumDiffs();
             if (num_diffs < num_diffs_list1[i]) {
             if (num_diffs < num_diffs_list1[i]) {
               // If j has been already matched to some element, ensure the
               // If j has been already matched to some element, ensure the
               // current num_diffs is smaller.
               // current num_diffs is smaller.

+ 1 - 1
src/google/protobuf/util/message_differencer.h

@@ -708,7 +708,7 @@ class PROTOBUF_EXPORT MessageDifferencer {
   };
   };
 
 
  private:
  private:
-  friend class DefaultFieldComparator;
+  friend class SimpleFieldComparator;
 
 
   // A MapKeyComparator to be used in TreatAsMapUsingKeyComparator.
   // A MapKeyComparator to be used in TreatAsMapUsingKeyComparator.
   // Implementation of this class needs to do field value comparison which
   // Implementation of this class needs to do field value comparison which

+ 5 - 1
src/google/protobuf/util/message_differencer_unittest.cc

@@ -3375,7 +3375,11 @@ TEST_F(ComparisonTest, MapEntryPartialEmptyKeyTest) {
 
 
   util::MessageDifferencer differencer;
   util::MessageDifferencer differencer;
   differencer.set_scope(util::MessageDifferencer::PARTIAL);
   differencer.set_scope(util::MessageDifferencer::PARTIAL);
-  EXPECT_TRUE(differencer.Compare(map1, map2));
+  // TODO(jieluo): Remove the round trip
+  std::string serialized_value;
+  map1.SerializeToString(&serialized_value);
+  map1.ParseFromString(serialized_value);
+  EXPECT_FALSE(differencer.Compare(map1, map2));
 }
 }
 
 
 TEST_F(ComparisonTest, MapEntryMissingEmptyFieldIsOkTest) {
 TEST_F(ComparisonTest, MapEntryMissingEmptyFieldIsOkTest) {

+ 65 - 62
src/google/protobuf/util/time_util.cc

@@ -30,6 +30,8 @@
 
 
 #include <google/protobuf/util/time_util.h>
 #include <google/protobuf/util/time_util.h>
 
 
+#include <cstdint>
+
 #include <google/protobuf/stubs/stringprintf.h>
 #include <google/protobuf/stubs/stringprintf.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/stubs/strutil.h>
 #include <google/protobuf/duration.pb.h>
 #include <google/protobuf/duration.pb.h>
@@ -57,10 +59,10 @@ static const int kSecondsPerMinute = 60;  // Note that we ignore leap seconds.
 static const int kSecondsPerHour = 3600;
 static const int kSecondsPerHour = 3600;
 
 
 template <typename T>
 template <typename T>
-T CreateNormalized(int64 seconds, int64 nanos);
+T CreateNormalized(int64_t seconds, int64_t nanos);
 
 
 template <>
 template <>
-Timestamp CreateNormalized(int64 seconds, int64 nanos) {
+Timestamp CreateNormalized(int64_t seconds, int64_t nanos) {
   // Make sure nanos is in the range.
   // Make sure nanos is in the range.
   if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
   if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
     seconds += nanos / kNanosPerSecond;
     seconds += nanos / kNanosPerSecond;
@@ -75,12 +77,12 @@ Timestamp CreateNormalized(int64 seconds, int64 nanos) {
          seconds <= TimeUtil::kTimestampMaxSeconds);
          seconds <= TimeUtil::kTimestampMaxSeconds);
   Timestamp result;
   Timestamp result;
   result.set_seconds(seconds);
   result.set_seconds(seconds);
-  result.set_nanos(static_cast<int32>(nanos));
+  result.set_nanos(static_cast<int32_t>(nanos));
   return result;
   return result;
 }
 }
 
 
 template <>
 template <>
-Duration CreateNormalized(int64 seconds, int64 nanos) {
+Duration CreateNormalized(int64_t seconds, int64_t nanos) {
   // Make sure nanos is in the range.
   // Make sure nanos is in the range.
   if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
   if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
     seconds += nanos / kNanosPerSecond;
     seconds += nanos / kNanosPerSecond;
@@ -98,13 +100,13 @@ Duration CreateNormalized(int64 seconds, int64 nanos) {
          seconds <= TimeUtil::kDurationMaxSeconds);
          seconds <= TimeUtil::kDurationMaxSeconds);
   Duration result;
   Duration result;
   result.set_seconds(seconds);
   result.set_seconds(seconds);
-  result.set_nanos(static_cast<int32>(nanos));
+  result.set_nanos(static_cast<int32_t>(nanos));
   return result;
   return result;
 }
 }
 
 
 // Format nanoseconds with either 3, 6, or 9 digits depending on the required
 // Format nanoseconds with either 3, 6, or 9 digits depending on the required
 // precision to represent the exact value.
 // precision to represent the exact value.
-std::string FormatNanos(int32 nanos) {
+std::string FormatNanos(int32_t nanos) {
   if (nanos % kNanosPerMillisecond == 0) {
   if (nanos % kNanosPerMillisecond == 0) {
     return StringPrintf("%03d", nanos / kNanosPerMillisecond);
     return StringPrintf("%03d", nanos / kNanosPerMillisecond);
   } else if (nanos % kNanosPerMicrosecond == 0) {
   } else if (nanos % kNanosPerMicrosecond == 0) {
@@ -127,9 +129,9 @@ void CurrentTime(int64* seconds, int32* nanos) {
 }
 }
 
 
 // Truncates the remainder part after division.
 // Truncates the remainder part after division.
-int64 RoundTowardZero(int64 value, int64 divider) {
-  int64 result = value / divider;
-  int64 remainder = value % divider;
+int64_t RoundTowardZero(int64_t value, int64_t divider) {
+  int64_t result = value / divider;
+  int64_t remainder = value % divider;
   // Before C++11, the sign of the remainder is implementation dependent if
   // Before C++11, the sign of the remainder is implementation dependent if
   // any of the operands is negative. Here we try to enforce C++11's "rounded
   // any of the operands is negative. Here we try to enforce C++11's "rounded
   // toward zero" semantics. For example, for (-5) / 2 an implementation may
   // toward zero" semantics. For example, for (-5) / 2 an implementation may
@@ -146,10 +148,10 @@ int64 RoundTowardZero(int64 value, int64 divider) {
 // Actually define these static const integers. Required by C++ standard (but
 // Actually define these static const integers. Required by C++ standard (but
 // some compilers don't like it).
 // some compilers don't like it).
 #ifndef _MSC_VER
 #ifndef _MSC_VER
-const int64 TimeUtil::kTimestampMinSeconds;
-const int64 TimeUtil::kTimestampMaxSeconds;
-const int64 TimeUtil::kDurationMaxSeconds;
-const int64 TimeUtil::kDurationMinSeconds;
+const int64_t TimeUtil::kTimestampMinSeconds;
+const int64_t TimeUtil::kTimestampMaxSeconds;
+const int64_t TimeUtil::kDurationMaxSeconds;
+const int64_t TimeUtil::kDurationMinSeconds;
 #endif  // !_MSC_VER
 #endif  // !_MSC_VER
 
 
 std::string TimeUtil::ToString(const Timestamp& timestamp) {
 std::string TimeUtil::ToString(const Timestamp& timestamp) {
@@ -157,8 +159,8 @@ std::string TimeUtil::ToString(const Timestamp& timestamp) {
 }
 }
 
 
 bool TimeUtil::FromString(const std::string& value, Timestamp* timestamp) {
 bool TimeUtil::FromString(const std::string& value, Timestamp* timestamp) {
-  int64 seconds;
-  int32 nanos;
+  int64_t seconds;
+  int32_t nanos;
   if (!ParseTime(value, &seconds, &nanos)) {
   if (!ParseTime(value, &seconds, &nanos)) {
     return false;
     return false;
   }
   }
@@ -167,8 +169,8 @@ bool TimeUtil::FromString(const std::string& value, Timestamp* timestamp) {
 }
 }
 
 
 Timestamp TimeUtil::GetCurrentTime() {
 Timestamp TimeUtil::GetCurrentTime() {
-  int64 seconds;
-  int32 nanos;
+  int64_t seconds;
+  int32_t nanos;
   CurrentTime(&seconds, &nanos);
   CurrentTime(&seconds, &nanos);
   return CreateNormalized<Timestamp>(seconds, nanos);
   return CreateNormalized<Timestamp>(seconds, nanos);
 }
 }
@@ -177,8 +179,8 @@ Timestamp TimeUtil::GetEpoch() { return Timestamp(); }
 
 
 std::string TimeUtil::ToString(const Duration& duration) {
 std::string TimeUtil::ToString(const Duration& duration) {
   std::string result;
   std::string result;
-  int64 seconds = duration.seconds();
-  int32 nanos = duration.nanos();
+  int64_t seconds = duration.seconds();
+  int32_t nanos = duration.nanos();
   if (seconds < 0 || nanos < 0) {
   if (seconds < 0 || nanos < 0) {
     result += "-";
     result += "-";
     seconds = -seconds;
     seconds = -seconds;
@@ -192,8 +194,8 @@ std::string TimeUtil::ToString(const Duration& duration) {
   return result;
   return result;
 }
 }
 
 
-static int64 Pow(int64 x, int y) {
-  int64 result = 1;
+static int64_t Pow(int64_t x, int y) {
+  int64_t result = 1;
   for (int i = 0; i < y; ++i) {
   for (int i = 0; i < y; ++i) {
     result *= x;
     result *= x;
   }
   }
@@ -218,11 +220,11 @@ bool TimeUtil::FromString(const std::string& value, Duration* duration) {
     nanos_part = value.substr(pos + 1, value.length() - pos - 2);
     nanos_part = value.substr(pos + 1, value.length() - pos - 2);
   }
   }
   char* end;
   char* end;
-  int64 seconds = strto64(seconds_part.c_str(), &end, 10);
+  int64_t seconds = strto64(seconds_part.c_str(), &end, 10);
   if (end != seconds_part.c_str() + seconds_part.length()) {
   if (end != seconds_part.c_str() + seconds_part.length()) {
     return false;
     return false;
   }
   }
-  int64 nanos = strto64(nanos_part.c_str(), &end, 10);
+  int64_t nanos = strto64(nanos_part.c_str(), &end, 10);
   if (end != nanos_part.c_str() + nanos_part.length()) {
   if (end != nanos_part.c_str() + nanos_part.length()) {
     return false;
     return false;
   }
   }
@@ -233,106 +235,106 @@ bool TimeUtil::FromString(const std::string& value, Duration* duration) {
     nanos = -nanos;
     nanos = -nanos;
   }
   }
   duration->set_seconds(seconds);
   duration->set_seconds(seconds);
-  duration->set_nanos(static_cast<int32>(nanos));
+  duration->set_nanos(static_cast<int32_t>(nanos));
   return true;
   return true;
 }
 }
 
 
-Duration TimeUtil::NanosecondsToDuration(int64 nanos) {
+Duration TimeUtil::NanosecondsToDuration(int64_t nanos) {
   return CreateNormalized<Duration>(nanos / kNanosPerSecond,
   return CreateNormalized<Duration>(nanos / kNanosPerSecond,
                                     nanos % kNanosPerSecond);
                                     nanos % kNanosPerSecond);
 }
 }
 
 
-Duration TimeUtil::MicrosecondsToDuration(int64 micros) {
+Duration TimeUtil::MicrosecondsToDuration(int64_t micros) {
   return CreateNormalized<Duration>(
   return CreateNormalized<Duration>(
       micros / kMicrosPerSecond,
       micros / kMicrosPerSecond,
       (micros % kMicrosPerSecond) * kNanosPerMicrosecond);
       (micros % kMicrosPerSecond) * kNanosPerMicrosecond);
 }
 }
 
 
-Duration TimeUtil::MillisecondsToDuration(int64 millis) {
+Duration TimeUtil::MillisecondsToDuration(int64_t millis) {
   return CreateNormalized<Duration>(
   return CreateNormalized<Duration>(
       millis / kMillisPerSecond,
       millis / kMillisPerSecond,
       (millis % kMillisPerSecond) * kNanosPerMillisecond);
       (millis % kMillisPerSecond) * kNanosPerMillisecond);
 }
 }
 
 
-Duration TimeUtil::SecondsToDuration(int64 seconds) {
+Duration TimeUtil::SecondsToDuration(int64_t seconds) {
   return CreateNormalized<Duration>(seconds, 0);
   return CreateNormalized<Duration>(seconds, 0);
 }
 }
 
 
-Duration TimeUtil::MinutesToDuration(int64 minutes) {
+Duration TimeUtil::MinutesToDuration(int64_t minutes) {
   return CreateNormalized<Duration>(minutes * kSecondsPerMinute, 0);
   return CreateNormalized<Duration>(minutes * kSecondsPerMinute, 0);
 }
 }
 
 
-Duration TimeUtil::HoursToDuration(int64 hours) {
+Duration TimeUtil::HoursToDuration(int64_t hours) {
   return CreateNormalized<Duration>(hours * kSecondsPerHour, 0);
   return CreateNormalized<Duration>(hours * kSecondsPerHour, 0);
 }
 }
 
 
-int64 TimeUtil::DurationToNanoseconds(const Duration& duration) {
+int64_t TimeUtil::DurationToNanoseconds(const Duration& duration) {
   return duration.seconds() * kNanosPerSecond + duration.nanos();
   return duration.seconds() * kNanosPerSecond + duration.nanos();
 }
 }
 
 
-int64 TimeUtil::DurationToMicroseconds(const Duration& duration) {
+int64_t TimeUtil::DurationToMicroseconds(const Duration& duration) {
   return duration.seconds() * kMicrosPerSecond +
   return duration.seconds() * kMicrosPerSecond +
          RoundTowardZero(duration.nanos(), kNanosPerMicrosecond);
          RoundTowardZero(duration.nanos(), kNanosPerMicrosecond);
 }
 }
 
 
-int64 TimeUtil::DurationToMilliseconds(const Duration& duration) {
+int64_t TimeUtil::DurationToMilliseconds(const Duration& duration) {
   return duration.seconds() * kMillisPerSecond +
   return duration.seconds() * kMillisPerSecond +
          RoundTowardZero(duration.nanos(), kNanosPerMillisecond);
          RoundTowardZero(duration.nanos(), kNanosPerMillisecond);
 }
 }
 
 
-int64 TimeUtil::DurationToSeconds(const Duration& duration) {
+int64_t TimeUtil::DurationToSeconds(const Duration& duration) {
   return duration.seconds();
   return duration.seconds();
 }
 }
 
 
-int64 TimeUtil::DurationToMinutes(const Duration& duration) {
+int64_t TimeUtil::DurationToMinutes(const Duration& duration) {
   return RoundTowardZero(duration.seconds(), kSecondsPerMinute);
   return RoundTowardZero(duration.seconds(), kSecondsPerMinute);
 }
 }
 
 
-int64 TimeUtil::DurationToHours(const Duration& duration) {
+int64_t TimeUtil::DurationToHours(const Duration& duration) {
   return RoundTowardZero(duration.seconds(), kSecondsPerHour);
   return RoundTowardZero(duration.seconds(), kSecondsPerHour);
 }
 }
 
 
-Timestamp TimeUtil::NanosecondsToTimestamp(int64 nanos) {
+Timestamp TimeUtil::NanosecondsToTimestamp(int64_t nanos) {
   return CreateNormalized<Timestamp>(nanos / kNanosPerSecond,
   return CreateNormalized<Timestamp>(nanos / kNanosPerSecond,
                                      nanos % kNanosPerSecond);
                                      nanos % kNanosPerSecond);
 }
 }
 
 
-Timestamp TimeUtil::MicrosecondsToTimestamp(int64 micros) {
+Timestamp TimeUtil::MicrosecondsToTimestamp(int64_t micros) {
   return CreateNormalized<Timestamp>(
   return CreateNormalized<Timestamp>(
       micros / kMicrosPerSecond,
       micros / kMicrosPerSecond,
       micros % kMicrosPerSecond * kNanosPerMicrosecond);
       micros % kMicrosPerSecond * kNanosPerMicrosecond);
 }
 }
 
 
-Timestamp TimeUtil::MillisecondsToTimestamp(int64 millis) {
+Timestamp TimeUtil::MillisecondsToTimestamp(int64_t millis) {
   return CreateNormalized<Timestamp>(
   return CreateNormalized<Timestamp>(
       millis / kMillisPerSecond,
       millis / kMillisPerSecond,
       millis % kMillisPerSecond * kNanosPerMillisecond);
       millis % kMillisPerSecond * kNanosPerMillisecond);
 }
 }
 
 
-Timestamp TimeUtil::SecondsToTimestamp(int64 seconds) {
+Timestamp TimeUtil::SecondsToTimestamp(int64_t seconds) {
   return CreateNormalized<Timestamp>(seconds, 0);
   return CreateNormalized<Timestamp>(seconds, 0);
 }
 }
 
 
-int64 TimeUtil::TimestampToNanoseconds(const Timestamp& timestamp) {
+int64_t TimeUtil::TimestampToNanoseconds(const Timestamp& timestamp) {
   return timestamp.seconds() * kNanosPerSecond + timestamp.nanos();
   return timestamp.seconds() * kNanosPerSecond + timestamp.nanos();
 }
 }
 
 
-int64 TimeUtil::TimestampToMicroseconds(const Timestamp& timestamp) {
+int64_t TimeUtil::TimestampToMicroseconds(const Timestamp& timestamp) {
   return timestamp.seconds() * kMicrosPerSecond +
   return timestamp.seconds() * kMicrosPerSecond +
          RoundTowardZero(timestamp.nanos(), kNanosPerMicrosecond);
          RoundTowardZero(timestamp.nanos(), kNanosPerMicrosecond);
 }
 }
 
 
-int64 TimeUtil::TimestampToMilliseconds(const Timestamp& timestamp) {
+int64_t TimeUtil::TimestampToMilliseconds(const Timestamp& timestamp) {
   return timestamp.seconds() * kMillisPerSecond +
   return timestamp.seconds() * kMillisPerSecond +
          RoundTowardZero(timestamp.nanos(), kNanosPerMillisecond);
          RoundTowardZero(timestamp.nanos(), kNanosPerMillisecond);
 }
 }
 
 
-int64 TimeUtil::TimestampToSeconds(const Timestamp& timestamp) {
+int64_t TimeUtil::TimestampToSeconds(const Timestamp& timestamp) {
   return timestamp.seconds();
   return timestamp.seconds();
 }
 }
 
 
 Timestamp TimeUtil::TimeTToTimestamp(time_t value) {
 Timestamp TimeUtil::TimeTToTimestamp(time_t value) {
-  return CreateNormalized<Timestamp>(static_cast<int64>(value), 0);
+  return CreateNormalized<Timestamp>(static_cast<int64_t>(value), 0);
 }
 }
 
 
 time_t TimeUtil::TimestampToTimeT(const Timestamp& value) {
 time_t TimeUtil::TimestampToTimeT(const Timestamp& value) {
@@ -382,19 +384,20 @@ using ::PROTOBUF_NAMESPACE_ID::util::kNanosPerSecond;
 void ToUint128(const Duration& value, uint128* result, bool* negative) {
 void ToUint128(const Duration& value, uint128* result, bool* negative) {
   if (value.seconds() < 0 || value.nanos() < 0) {
   if (value.seconds() < 0 || value.nanos() < 0) {
     *negative = true;
     *negative = true;
-    *result = static_cast<uint64>(-value.seconds());
-    *result = *result * kNanosPerSecond + static_cast<uint32>(-value.nanos());
+    *result = static_cast<uint64_t>(-value.seconds());
+    *result = *result * kNanosPerSecond + static_cast<uint32_t>(-value.nanos());
   } else {
   } else {
     *negative = false;
     *negative = false;
-    *result = static_cast<uint64>(value.seconds());
-    *result = *result * kNanosPerSecond + static_cast<uint32>(value.nanos());
+    *result = static_cast<uint64_t>(value.seconds());
+    *result = *result * kNanosPerSecond + static_cast<uint32_t>(value.nanos());
   }
   }
 }
 }
 
 
 void ToDuration(const uint128& value, bool negative, Duration* duration) {
 void ToDuration(const uint128& value, bool negative, Duration* duration) {
-  int64 seconds =
-      static_cast<int64>(Uint128Low64(value / kNanosPerSecond));
-  int32 nanos = static_cast<int32>(Uint128Low64(value % kNanosPerSecond));
+  int64_t seconds =
+      static_cast<int64_t>(Uint128Low64(value / kNanosPerSecond));
+  int32_t nanos =
+      static_cast<int32_t>(Uint128Low64(value % kNanosPerSecond));
   if (negative) {
   if (negative) {
     seconds = -seconds;
     seconds = -seconds;
     nanos = -nanos;
     nanos = -nanos;
@@ -416,15 +419,15 @@ Duration& operator-=(Duration& d1, const Duration& d2) {  // NOLINT
   return d1;
   return d1;
 }
 }
 
 
-Duration& operator*=(Duration& d, int64 r) {  // NOLINT
+Duration& operator*=(Duration& d, int64_t r) {  // NOLINT
   bool negative;
   bool negative;
   uint128 value;
   uint128 value;
   ToUint128(d, &value, &negative);
   ToUint128(d, &value, &negative);
   if (r > 0) {
   if (r > 0) {
-    value *= static_cast<uint64>(r);
+    value *= static_cast<uint64_t>(r);
   } else {
   } else {
     negative = !negative;
     negative = !negative;
-    value *= static_cast<uint64>(-r);
+    value *= static_cast<uint64_t>(-r);
   }
   }
   ToDuration(value, negative, &d);
   ToDuration(value, negative, &d);
   return d;
   return d;
@@ -432,8 +435,8 @@ Duration& operator*=(Duration& d, int64 r) {  // NOLINT
 
 
 Duration& operator*=(Duration& d, double r) {  // NOLINT
 Duration& operator*=(Duration& d, double r) {  // NOLINT
   double result = (d.seconds() * 1.0 + 1.0 * d.nanos() / kNanosPerSecond) * r;
   double result = (d.seconds() * 1.0 + 1.0 * d.nanos() / kNanosPerSecond) * r;
-  int64 seconds = static_cast<int64>(result);
-  int32 nanos = static_cast<int32>((result - seconds) * kNanosPerSecond);
+  int64_t seconds = static_cast<int64_t>(result);
+  int32_t nanos = static_cast<int32_t>((result - seconds) * kNanosPerSecond);
   // Note that we normalize here not just because nanos can have a different
   // Note that we normalize here not just because nanos can have a different
   // sign from seconds but also that nanos can be any arbitrary value when
   // sign from seconds but also that nanos can be any arbitrary value when
   // overflow happens (i.e., the result is a much larger value than what
   // overflow happens (i.e., the result is a much larger value than what
@@ -442,15 +445,15 @@ Duration& operator*=(Duration& d, double r) {  // NOLINT
   return d;
   return d;
 }
 }
 
 
-Duration& operator/=(Duration& d, int64 r) {  // NOLINT
+Duration& operator/=(Duration& d, int64_t r) {  // NOLINT
   bool negative;
   bool negative;
   uint128 value;
   uint128 value;
   ToUint128(d, &value, &negative);
   ToUint128(d, &value, &negative);
   if (r > 0) {
   if (r > 0) {
-    value /= static_cast<uint64>(r);
+    value /= static_cast<uint64_t>(r);
   } else {
   } else {
     negative = !negative;
     negative = !negative;
-    value /= static_cast<uint64>(-r);
+    value /= static_cast<uint64_t>(-r);
   }
   }
   ToDuration(value, negative, &d);
   ToDuration(value, negative, &d);
   return d;
   return d;
@@ -476,12 +479,12 @@ Duration& operator%=(Duration& d1, const Duration& d2) {  // NOLINT
   return d1;
   return d1;
 }
 }
 
 
-int64 operator/(const Duration& d1, const Duration& d2) {
+int64_t operator/(const Duration& d1, const Duration& d2) {
   bool negative1, negative2;
   bool negative1, negative2;
   uint128 value1, value2;
   uint128 value1, value2;
   ToUint128(d1, &value1, &negative1);
   ToUint128(d1, &value1, &negative1);
   ToUint128(d2, &value2, &negative2);
   ToUint128(d2, &value2, &negative2);
-  int64 result = Uint128Low64(value1 / value2);
+  int64_t result = Uint128Low64(value1 / value2);
   if (negative1 != negative2) {
   if (negative1 != negative2) {
     result = -result;
     result = -result;
   }
   }

+ 30 - 29
src/google/protobuf/util/time_util.h

@@ -33,6 +33,7 @@
 #ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
 #ifndef GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
 #define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
 #define GOOGLE_PROTOBUF_UTIL_TIME_UTIL_H__
 
 
+#include <cstdint>
 #include <ctime>
 #include <ctime>
 #include <ostream>
 #include <ostream>
 #include <string>
 #include <string>
@@ -67,11 +68,11 @@ class PROTOBUF_EXPORT TimeUtil {
   // The min/max Timestamp/Duration values we support.
   // The min/max Timestamp/Duration values we support.
   //
   //
   // For "0001-01-01T00:00:00Z".
   // For "0001-01-01T00:00:00Z".
-  static const int64 kTimestampMinSeconds = -62135596800LL;
+  static const int64_t kTimestampMinSeconds = -62135596800LL;
   // For "9999-12-31T23:59:59.999999999Z".
   // For "9999-12-31T23:59:59.999999999Z".
-  static const int64 kTimestampMaxSeconds = 253402300799LL;
-  static const int64 kDurationMinSeconds = -315576000000LL;
-  static const int64 kDurationMaxSeconds = 315576000000LL;
+  static const int64_t kTimestampMaxSeconds = 253402300799LL;
+  static const int64_t kDurationMinSeconds = -315576000000LL;
+  static const int64_t kDurationMaxSeconds = 315576000000LL;
 
 
   // Converts Timestamp to/from RFC 3339 date string format.
   // Converts Timestamp to/from RFC 3339 date string format.
   // Generated output will always be Z-normalized and uses 3, 6 or 9
   // Generated output will always be Z-normalized and uses 3, 6 or 9
@@ -110,12 +111,12 @@ class PROTOBUF_EXPORT TimeUtil {
 
 
   // Converts between Duration and integer types. The behavior is undefined if
   // Converts between Duration and integer types. The behavior is undefined if
   // the input value is not in the valid range of Duration.
   // the input value is not in the valid range of Duration.
-  static Duration NanosecondsToDuration(int64 nanos);
-  static Duration MicrosecondsToDuration(int64 micros);
-  static Duration MillisecondsToDuration(int64 millis);
-  static Duration SecondsToDuration(int64 seconds);
-  static Duration MinutesToDuration(int64 minutes);
-  static Duration HoursToDuration(int64 hours);
+  static Duration NanosecondsToDuration(int64_t nanos);
+  static Duration MicrosecondsToDuration(int64_t micros);
+  static Duration MillisecondsToDuration(int64_t millis);
+  static Duration SecondsToDuration(int64_t seconds);
+  static Duration MinutesToDuration(int64_t minutes);
+  static Duration HoursToDuration(int64_t hours);
   // Result will be truncated towards zero. For example, "-1.5s" will be
   // Result will be truncated towards zero. For example, "-1.5s" will be
   // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
   // truncated to "-1s", and "1.5s" to "1s" when converting to seconds.
   // It's undefined behavior if the input duration is not valid or the result
   // It's undefined behavior if the input duration is not valid or the result
@@ -123,28 +124,28 @@ class PROTOBUF_EXPORT TimeUtil {
   // valid range of Duration, or have an invalid nanos value (i.e., larger
   // valid range of Duration, or have an invalid nanos value (i.e., larger
   // than 999999999, less than -999999999, or have a different sign from the
   // than 999999999, less than -999999999, or have a different sign from the
   // seconds part).
   // seconds part).
-  static int64 DurationToNanoseconds(const Duration& duration);
-  static int64 DurationToMicroseconds(const Duration& duration);
-  static int64 DurationToMilliseconds(const Duration& duration);
-  static int64 DurationToSeconds(const Duration& duration);
-  static int64 DurationToMinutes(const Duration& duration);
-  static int64 DurationToHours(const Duration& duration);
+  static int64_t DurationToNanoseconds(const Duration& duration);
+  static int64_t DurationToMicroseconds(const Duration& duration);
+  static int64_t DurationToMilliseconds(const Duration& duration);
+  static int64_t DurationToSeconds(const Duration& duration);
+  static int64_t DurationToMinutes(const Duration& duration);
+  static int64_t DurationToHours(const Duration& duration);
   // Creates Timestamp from integer types. The integer value indicates the
   // Creates Timestamp from integer types. The integer value indicates the
   // time elapsed from Epoch time. The behavior is undefined if the input
   // time elapsed from Epoch time. The behavior is undefined if the input
   // value is not in the valid range of Timestamp.
   // value is not in the valid range of Timestamp.
-  static Timestamp NanosecondsToTimestamp(int64 nanos);
-  static Timestamp MicrosecondsToTimestamp(int64 micros);
-  static Timestamp MillisecondsToTimestamp(int64 millis);
-  static Timestamp SecondsToTimestamp(int64 seconds);
+  static Timestamp NanosecondsToTimestamp(int64_t nanos);
+  static Timestamp MicrosecondsToTimestamp(int64_t micros);
+  static Timestamp MillisecondsToTimestamp(int64_t millis);
+  static Timestamp SecondsToTimestamp(int64_t seconds);
   // Result will be truncated down to the nearest integer value. For example,
   // Result will be truncated down to the nearest integer value. For example,
   // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
   // with "1969-12-31T23:59:59.9Z", TimestampToMilliseconds() returns -100
   // and TimestampToSeconds() returns -1. It's undefined behavior if the input
   // and TimestampToSeconds() returns -1. It's undefined behavior if the input
   // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
   // Timestamp is not valid (i.e., its seconds part or nanos part does not fall
   // in the valid range) or the return value doesn't fit into int64.
   // in the valid range) or the return value doesn't fit into int64.
-  static int64 TimestampToNanoseconds(const Timestamp& timestamp);
-  static int64 TimestampToMicroseconds(const Timestamp& timestamp);
-  static int64 TimestampToMilliseconds(const Timestamp& timestamp);
-  static int64 TimestampToSeconds(const Timestamp& timestamp);
+  static int64_t TimestampToNanoseconds(const Timestamp& timestamp);
+  static int64_t TimestampToMicroseconds(const Timestamp& timestamp);
+  static int64_t TimestampToMilliseconds(const Timestamp& timestamp);
+  static int64_t TimestampToSeconds(const Timestamp& timestamp);
 
 
   // Conversion to/from other time/date types. Note that these types may
   // Conversion to/from other time/date types. Note that these types may
   // have a different precision and time range from Timestamp/Duration.
   // have a different precision and time range from Timestamp/Duration.
@@ -176,19 +177,19 @@ PROTOBUF_EXPORT Duration& operator+=(Duration& d1,
                                      const Duration& d2);  // NOLINT
                                      const Duration& d2);  // NOLINT
 PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
 PROTOBUF_EXPORT Duration& operator-=(Duration& d1,
                                      const Duration& d2);     // NOLINT
                                      const Duration& d2);     // NOLINT
-PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64 r);   // NOLINT
+PROTOBUF_EXPORT Duration& operator*=(Duration& d, int64_t r);  // NOLINT
 PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);  // NOLINT
 PROTOBUF_EXPORT Duration& operator*=(Duration& d, double r);  // NOLINT
-PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64 r);   // NOLINT
+PROTOBUF_EXPORT Duration& operator/=(Duration& d, int64_t r);  // NOLINT
 PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);  // NOLINT
 PROTOBUF_EXPORT Duration& operator/=(Duration& d, double r);  // NOLINT
 // Overload for other integer types.
 // Overload for other integer types.
 template <typename T>
 template <typename T>
 Duration& operator*=(Duration& d, T r) {  // NOLINT
 Duration& operator*=(Duration& d, T r) {  // NOLINT
-  int64 x = r;
+  int64_t x = r;
   return d *= x;
   return d *= x;
 }
 }
 template <typename T>
 template <typename T>
 Duration& operator/=(Duration& d, T r) {  // NOLINT
 Duration& operator/=(Duration& d, T r) {  // NOLINT
-  int64 x = r;
+  int64_t x = r;
   return d /= x;
   return d /= x;
 }
 }
 PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
 PROTOBUF_EXPORT Duration& operator%=(Duration& d1,
@@ -243,7 +244,7 @@ template <typename T>
 inline Duration operator/(Duration d, T r) {
 inline Duration operator/(Duration d, T r) {
   return d /= r;
   return d /= r;
 }
 }
-PROTOBUF_EXPORT int64 operator/(const Duration& d1, const Duration& d2);
+PROTOBUF_EXPORT int64_t operator/(const Duration& d1, const Duration& d2);
 
 
 inline Duration operator%(const Duration& d1, const Duration& d2) {
 inline Duration operator%(const Duration& d1, const Duration& d2) {
   Duration result = d1;
   Duration result = d1;

+ 13 - 11
src/google/protobuf/util/time_util_test.cc

@@ -30,6 +30,7 @@
 
 
 #include <google/protobuf/util/time_util.h>
 #include <google/protobuf/util/time_util.h>
 
 
+#include <cstdint>
 #include <ctime>
 #include <ctime>
 
 
 #include <google/protobuf/duration.pb.h>
 #include <google/protobuf/duration.pb.h>
@@ -119,10 +120,10 @@ TEST(TimeUtilTest, DurationStringFormat) {
   // Duration must support range from -315,576,000,000s to +315576000000s
   // Duration must support range from -315,576,000,000s to +315576000000s
   // which includes negative values.
   // which includes negative values.
   EXPECT_TRUE(TimeUtil::FromString("315576000000.999999999s", &d));
   EXPECT_TRUE(TimeUtil::FromString("315576000000.999999999s", &d));
-  EXPECT_EQ(int64{315576000000}, d.seconds());
+  EXPECT_EQ(int64_t{315576000000}, d.seconds());
   EXPECT_EQ(999999999, d.nanos());
   EXPECT_EQ(999999999, d.nanos());
   EXPECT_TRUE(TimeUtil::FromString("-315576000000.999999999s", &d));
   EXPECT_TRUE(TimeUtil::FromString("-315576000000.999999999s", &d));
-  EXPECT_EQ(int64{-315576000000}, d.seconds());
+  EXPECT_EQ(int64_t{-315576000000}, d.seconds());
   EXPECT_EQ(-999999999, d.nanos());
   EXPECT_EQ(-999999999, d.nanos());
 }
 }
 
 
@@ -277,23 +278,24 @@ TEST(TimeUtilTest, DurationOperators) {
   EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b * 0.5));
   EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b * 0.5));
   // Multiplication should not overflow if the result fits into the supported
   // Multiplication should not overflow if the result fits into the supported
   // range of Duration (intermediate result may be larger than int64).
   // range of Duration (intermediate result may be larger than int64).
-  EXPECT_EQ("315575999684.424s",
-            TimeUtil::ToString((one_second - one_nano) * int64{315576000000}));
-  EXPECT_EQ("-315575999684.424s",
-            TimeUtil::ToString((one_nano - one_second) * int64{315576000000}));
+  EXPECT_EQ("315575999684.424s", TimeUtil::ToString((one_second - one_nano) *
+                                                    int64_t{315576000000}));
+  EXPECT_EQ("-315575999684.424s", TimeUtil::ToString((one_nano - one_second) *
+                                                     int64_t{315576000000}));
   EXPECT_EQ("-315575999684.424s", TimeUtil::ToString((one_second - one_nano) *
   EXPECT_EQ("-315575999684.424s", TimeUtil::ToString((one_second - one_nano) *
-                                                     (int64{-315576000000})));
+                                                     (int64_t{-315576000000})));
 
 
   // Test / and %
   // Test / and %
   EXPECT_EQ("0.999999999s", TimeUtil::ToString(a / 2));
   EXPECT_EQ("0.999999999s", TimeUtil::ToString(a / 2));
   EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b / 2));
   EXPECT_EQ("-0.999999999s", TimeUtil::ToString(b / 2));
-  Duration large = TimeUtil::SecondsToDuration(int64{315576000000}) - one_nano;
+  Duration large =
+      TimeUtil::SecondsToDuration(int64_t{315576000000}) - one_nano;
   // We have to handle division with values beyond 64 bits.
   // We have to handle division with values beyond 64 bits.
-  EXPECT_EQ("0.999999999s", TimeUtil::ToString(large / int64{315576000000}));
+  EXPECT_EQ("0.999999999s", TimeUtil::ToString(large / int64_t{315576000000}));
   EXPECT_EQ("-0.999999999s",
   EXPECT_EQ("-0.999999999s",
-            TimeUtil::ToString((-large) / int64{315576000000}));
+            TimeUtil::ToString((-large) / int64_t{315576000000}));
   EXPECT_EQ("-0.999999999s",
   EXPECT_EQ("-0.999999999s",
-            TimeUtil::ToString(large / (int64{-315576000000})));
+            TimeUtil::ToString(large / (int64_t{-315576000000})));
   Duration large2 = large + one_nano;
   Duration large2 = large + one_nano;
   EXPECT_EQ(large, large % large2);
   EXPECT_EQ(large, large % large2);
   EXPECT_EQ(-large, (-large) % large2);
   EXPECT_EQ(-large, (-large) % large2);

+ 3 - 2
src/google/protobuf/util/type_resolver_util_test.cc

@@ -30,6 +30,7 @@
 
 
 #include <google/protobuf/util/type_resolver_util.h>
 #include <google/protobuf/util/type_resolver_util.h>
 
 
+#include <cstdint>
 #include <limits>
 #include <limits>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
@@ -135,12 +136,12 @@ class DescriptorPoolTypeResolverTest : public testing::Test {
   }
   }
 
 
   bool HasInt32Option(const RepeatedPtrField<Option>& options,
   bool HasInt32Option(const RepeatedPtrField<Option>& options,
-                      const std::string& name, int32 value) {
+                      const std::string& name, int32_t value) {
     return HasOption<Int32Value>(options, name, value);
     return HasOption<Int32Value>(options, name, value);
   }
   }
 
 
   bool HasUInt64Option(const RepeatedPtrField<Option>& options,
   bool HasUInt64Option(const RepeatedPtrField<Option>& options,
-                       const std::string& name, uint64 value) {
+                       const std::string& name, uint64_t value) {
     return HasOption<UInt64Value>(options, name, value);
     return HasOption<UInt64Value>(options, name, value);
   }
   }
 
 

+ 1 - 1
src/google/protobuf/wire_format_lite.h

@@ -1692,7 +1692,7 @@ inline uint8* WireFormatLite::InternalWriteMessage(
     int field_number, const MessageType& value, uint8* target,
     int field_number, const MessageType& value, uint8* target,
     io::EpsCopyOutputStream* stream) {
     io::EpsCopyOutputStream* stream) {
   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
-  target = io::CodedOutputStream::WriteVarint32ToArray(
+  target = io::CodedOutputStream::WriteVarint32ToArrayOutOfLine(
       static_cast<uint32>(value.GetCachedSize()), target);
       static_cast<uint32>(value.GetCachedSize()), target);
   return value._InternalSerialize(target, stream);
   return value._InternalSerialize(target, stream);
 }
 }

+ 18 - 18
src/google/protobuf/wrappers.pb.cc

@@ -297,7 +297,6 @@ const char* DoubleValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // double value = 1;
       // double value = 1;
       case 1:
       case 1:
@@ -308,7 +307,8 @@ const char* DoubleValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -485,7 +485,6 @@ const char* FloatValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // float value = 1;
       // float value = 1;
       case 1:
       case 1:
@@ -496,7 +495,8 @@ const char* FloatValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -673,7 +673,6 @@ const char* Int64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // int64 value = 1;
       // int64 value = 1;
       case 1:
       case 1:
@@ -684,7 +683,8 @@ const char* Int64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -863,7 +863,6 @@ const char* UInt64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // uint64 value = 1;
       // uint64 value = 1;
       case 1:
       case 1:
@@ -874,7 +873,8 @@ const char* UInt64Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1053,7 +1053,6 @@ const char* Int32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // int32 value = 1;
       // int32 value = 1;
       case 1:
       case 1:
@@ -1064,7 +1063,8 @@ const char* Int32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1243,7 +1243,6 @@ const char* UInt32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // uint32 value = 1;
       // uint32 value = 1;
       case 1:
       case 1:
@@ -1254,7 +1253,8 @@ const char* UInt32Value::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1433,7 +1433,6 @@ const char* BoolValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // bool value = 1;
       // bool value = 1;
       case 1:
       case 1:
@@ -1444,7 +1443,8 @@ const char* BoolValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1626,7 +1626,6 @@ const char* StringValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // string value = 1;
       // string value = 1;
       case 1:
       case 1:
@@ -1639,7 +1638,8 @@ const char* StringValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }
@@ -1827,7 +1827,6 @@ const char* BytesValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
   while (!ctx->Done(&ptr)) {
   while (!ctx->Done(&ptr)) {
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ::PROTOBUF_NAMESPACE_ID::uint32 tag;
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
     ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag);
-    CHK_(ptr);
     switch (tag >> 3) {
     switch (tag >> 3) {
       // bytes value = 1;
       // bytes value = 1;
       case 1:
       case 1:
@@ -1839,7 +1838,8 @@ const char* BytesValue::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID:
         continue;
         continue;
       default: {
       default: {
       handle_unusual:
       handle_unusual:
-        if ((tag & 7) == 4 || tag == 0) {
+        if ((tag == 0) || ((tag & 7) == 4)) {
+          CHK_(ptr);
           ctx->SetLastTag(tag);
           ctx->SetLastTag(tag);
           goto success;
           goto success;
         }
         }