Added required field checking into the encoder.

pull/13171/head
Joshua Haberman 3 years ago
parent 0d87ddc7d7
commit a0374b3b08
  1. 162
      cmake/google/protobuf/descriptor.upb.h
  2. 13
      upb/decode.c
  3. 9
      upb/encode.c
  4. 3
      upb/encode.h
  5. 12
      upb/msg_internal.h
  6. 37
      upb/msg_test.cc
  7. 6
      upbc/protoc-gen-upb.cc

@ -188,6 +188,12 @@ UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_
UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize_ex(const google_protobuf_FileDescriptorSet *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_FileDescriptorSet_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_FileDescriptorSet_has_file(const google_protobuf_FileDescriptorSet *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(0, 0)); }
UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
@ -232,6 +238,12 @@ UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorPr
UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize_ex(const google_protobuf_FileDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_FileDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -412,6 +424,12 @@ UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_pars
UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_DescriptorProto_serialize_ex(const google_protobuf_DescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_DescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -582,6 +600,12 @@ UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_Descr
UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize_ex(const google_protobuf_DescriptorProto_ExtensionRange *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 4), int32_t);
@ -644,6 +668,12 @@ UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_Descri
UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize_ex(const google_protobuf_DescriptorProto_ReservedRange *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 4), int32_t);
@ -689,6 +719,12 @@ UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRange
UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize_ex(const google_protobuf_ExtensionRangeOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_ExtensionRangeOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_ExtensionRangeOptions_has_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(0, 0)); }
UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
@ -733,6 +769,12 @@ UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptor
UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize_ex(const google_protobuf_FieldDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_FieldDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(24, 24), upb_strview);
@ -859,6 +901,12 @@ UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptor
UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize_ex(const google_protobuf_OneofDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_OneofDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -913,6 +961,12 @@ UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorPr
UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize_ex(const google_protobuf_EnumDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_EnumDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -1008,6 +1062,12 @@ UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobu
UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize_ex(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 4), int32_t);
@ -1053,6 +1113,12 @@ UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDe
UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize_ex(const google_protobuf_EnumValueDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_EnumValueDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(8, 8), upb_strview);
@ -1115,6 +1181,12 @@ UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescri
UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize_ex(const google_protobuf_ServiceDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_ServiceDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -1184,6 +1256,12 @@ UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescript
UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize_ex(const google_protobuf_MethodDescriptorProto *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_MethodDescriptorProto_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -1270,6 +1348,12 @@ UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parse_ex(con
UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_FileOptions_serialize_ex(const google_protobuf_FileOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_FileOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(20, 24), upb_strview);
@ -1474,6 +1558,12 @@ UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parse_
UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_MessageOptions_serialize_ex(const google_protobuf_MessageOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_MessageOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(1, 1), bool);
@ -1550,6 +1640,12 @@ UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parse_ex(c
UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_FieldOptions_serialize_ex(const google_protobuf_FieldOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_FieldOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE int32_t google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 4), int32_t);
@ -1642,6 +1738,12 @@ UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parse_ex(c
UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_OneofOptions_serialize_ex(const google_protobuf_OneofOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_OneofOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_OneofOptions_has_uninterpreted_option(const google_protobuf_OneofOptions *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(0, 0)); }
UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
@ -1686,6 +1788,12 @@ UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parse_ex(con
UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_EnumOptions_serialize_ex(const google_protobuf_EnumOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_EnumOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(1, 1), bool);
@ -1746,6 +1854,12 @@ UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_pa
UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_EnumValueOptions_serialize_ex(const google_protobuf_EnumValueOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_EnumValueOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(1, 1), bool);
@ -1798,6 +1912,12 @@ UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parse_
UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_ServiceOptions_serialize_ex(const google_protobuf_ServiceOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_ServiceOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(1, 1), bool);
@ -1850,6 +1970,12 @@ UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parse_ex
UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_MethodOptions_serialize_ex(const google_protobuf_MethodOptions *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_MethodOptions_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(8, 8), bool);
@ -1910,6 +2036,12 @@ UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOpt
UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_UninterpretedOption_serialize_ex(const google_protobuf_UninterpretedOption *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_UninterpretedOption_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_UninterpretedOption_has_name(const google_protobuf_UninterpretedOption *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(56, 80)); }
UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); }
UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_hasbit(msg, 1); }
@ -2002,6 +2134,12 @@ UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_Uninter
UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize_ex(const google_protobuf_UninterpretedOption_NamePart *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) {
return *UPB_PTR_AT(msg, UPB_SIZE(4, 8), upb_strview);
@ -2047,6 +2185,12 @@ UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parse_
UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize_ex(const google_protobuf_SourceCodeInfo *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_SourceCodeInfo_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_SourceCodeInfo_has_location(const google_protobuf_SourceCodeInfo *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(0, 0)); }
UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
@ -2091,6 +2235,12 @@ UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeIn
UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize_ex(const google_protobuf_SourceCodeInfo_Location *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_SourceCodeInfo_Location_msginit, options, arena, len);
}
UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_hasbit(msg, 1); }
@ -2169,6 +2319,12 @@ UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_
UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize_ex(const google_protobuf_GeneratedCodeInfo *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_GeneratedCodeInfo_msginit, options, arena, len);
}
UPB_INLINE bool google_protobuf_GeneratedCodeInfo_has_annotation(const google_protobuf_GeneratedCodeInfo *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(0, 0)); }
UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
@ -2213,6 +2369,12 @@ UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_Generat
UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len);
}
UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize_ex(const google_protobuf_GeneratedCodeInfo_Annotation *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, options, arena, len);
}
UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); }
UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_hasbit(msg, 1); }
UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) {

@ -690,17 +690,6 @@ static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
return ptr;
}
// Computes a bitmask in which the |n| lowest bits are set, except that we
// skip the lowest bit (because upb never uses hasbit 0).
//
// Sample output:
// decode_requiredmask(1) => 0b10 (0x2)
// decode_requiredmask(5) => 0b111110 (0x3e)
uint64_t decode_requiredmask(int n) {
assert(0 < n && n < 63);
return ((1 << n) - 1) << 1;
}
UPB_NOINLINE
const char *decode_checkrequired(upb_decstate *d, const char *ptr,
const upb_msg *msg, const upb_msglayout *l) {
@ -711,7 +700,7 @@ const char *decode_checkrequired(upb_decstate *d, const char *ptr,
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = _upb_be_swap64(msg_head);
if (decode_requiredmask(l->required_count) & ~msg_head) {
if (upb_msglayout_requiredmask(l) & ~msg_head) {
d->missing_required = true;
}
return ptr;

@ -511,6 +511,15 @@ static void encode_message(upb_encstate *e, const upb_msg *msg,
const upb_msglayout *m, size_t *size) {
size_t pre_len = e->limit - e->ptr;
if (e->options & UPB_ENCODE_CHECKREQUIRED && m->required_count) {
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = _upb_be_swap64(msg_head);
if (upb_msglayout_requiredmask(m) & ~msg_head) {
encode_err(e);
}
}
if ((e->options & UPB_ENCODE_SKIPUNKNOWN) == 0) {
size_t unknown_size;
const char *unknown = upb_msg_getunknown(msg, &unknown_size);

@ -52,6 +52,9 @@ enum {
/* When set, unknown fields are not printed. */
UPB_ENCODE_SKIPUNKNOWN = 2,
/* When set, the encode will fail if any required fields are missing. */
UPB_ENCODE_CHECKREQUIRED = 4,
};
#define UPB_ENCODE_MAXDEPTH(depth) ((depth) << 16)

@ -198,6 +198,18 @@ typedef struct {
int ext_count;
} upb_msglayout_file;
// Computes a bitmask in which the |n| lowest bits are set, except that we
// skip the lowest bit (because upb never uses hasbit 0).
//
// Sample output:
// requiredmask(1) => 0b10 (0x2)
// requiredmask(5) => 0b111110 (0x3e)
UPB_INLINE uint64_t upb_msglayout_requiredmask(const upb_msglayout *l) {
int n = l->required_count;
assert(0 < n && n < 63);
return ((1 << n) - 1) << 1;
}
/** upb_extreg ****************************************************************/
/* Adds the given extension info for message type |l| and field number |num|

@ -233,7 +233,7 @@ TEST(MessageTest, TestBadUTF8) {
serialized.data(), serialized.size(), arena.ptr()));
}
TEST(MessageTest, RequiredFieldsTopLevelMessage) {
TEST(MessageTest, DecodeRequiredFieldsTopLevelMessage) {
upb::Arena arena;
upb_test_TestRequiredFields *test_msg;
upb_test_EmptyMessage *empty_msg;
@ -291,7 +291,7 @@ TEST(MessageTest, RequiredFieldsTopLevelMessage) {
kUpb_DecodeOption_CheckRequired, arena.ptr()));
}
TEST(MessageTest, RequiredFieldsSubMessage) {
TEST(MessageTest, DecodeRequiredFieldsSubMessage) {
upb::Arena arena;
upb_test_TestRequiredFields *test_msg =
upb_test_TestRequiredFields_new(arena.ptr());
@ -326,3 +326,36 @@ TEST(MessageTest, RequiredFieldsSubMessage) {
serialized, size, NULL,
kUpb_DecodeOption_CheckRequired, arena.ptr()));
}
TEST(MessageTest, EncodeRequiredFields) {
upb::Arena arena;
upb_test_TestRequiredFields *test_msg =
upb_test_TestRequiredFields_new(arena.ptr());
// Succeeds, we didn't ask for required field checking.
size_t size;
char* serialized =
upb_test_TestRequiredFields_serialize_ex(test_msg, 0, arena.ptr(), &size);
ASSERT_TRUE(serialized != nullptr);
EXPECT_EQ(size, 0);
// Fails, we asked for required field checking but the required field is
// missing.
serialized = upb_test_TestRequiredFields_serialize_ex(
test_msg, UPB_ENCODE_CHECKREQUIRED, arena.ptr(), &size);
ASSERT_TRUE(serialized == nullptr);
// Fails, some required fields are present but not others.
upb_test_TestRequiredFields_set_required_int32(test_msg, 1);
serialized = upb_test_TestRequiredFields_serialize_ex(
test_msg, UPB_ENCODE_CHECKREQUIRED, arena.ptr(), &size);
ASSERT_TRUE(serialized == nullptr);
// Succeeds, all required fields are set.
upb_test_EmptyMessage *empty_msg = upb_test_EmptyMessage_new(arena.ptr());
upb_test_TestRequiredFields_set_required_int64(test_msg, 2);
upb_test_TestRequiredFields_set_required_message(test_msg, empty_msg);
serialized = upb_test_TestRequiredFields_serialize_ex(
test_msg, UPB_ENCODE_CHECKREQUIRED, arena.ptr(), &size);
ASSERT_TRUE(serialized != nullptr);
}

@ -437,6 +437,12 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
UPB_INLINE char *$0_serialize(const $0 *msg, upb_arena *arena, size_t *len) {
return upb_encode(msg, &$1, arena, len);
}
UPB_INLINE char *$0_serialize_ex(const $0 *msg,
int options,
upb_arena *arena,
size_t *len) {
return upb_encode_ex(msg, &$1, options, arena, len);
}
)cc",
MessageName(message), MessageInit(message));
}

Loading…
Cancel
Save