Merge branch 'arrayapi' into defcleanup

pull/13171/head
Joshua Haberman 6 years ago
commit 9ae5a3af5b
  1. 416
      google/protobuf/descriptor.upb.h
  2. 22
      tests/conformance_upb.c
  3. 2
      tests/pb/test_encoder.cc
  4. 4
      upb/bindings/lua/def.c
  5. 2
      upb/bindings/lua/upb/pb.c
  6. 14
      upb/decode.c
  7. 2
      upb/decode.h
  8. 38
      upb/def.c
  9. 2
      upb/def.h
  10. 6
      upb/encode.c
  11. 2
      upb/msg.c
  12. 26
      upb/msg.h
  13. 2
      upb/msgfactory.c
  14. 12
      upbc/generator.cc

File diff suppressed because it is too large Load Diff

@ -39,7 +39,7 @@ void CheckedWrite(int fd, const void *buf, size_t len) {
}
}
bool stringview_eql(upb_stringview view, const char *str) {
bool strview_eql(upb_strview view, const char *str) {
return view.size == strlen(str) && memcmp(view.data, str, view.size) == 0;
}
@ -50,11 +50,11 @@ void DoTest(
const conformance_ConformanceRequest* request,
conformance_ConformanceResponse *response,
upb_arena *arena) {
if (!stringview_eql(conformance_ConformanceRequest_message_type(request),
proto3_msg)) {
if (!strview_eql(conformance_ConformanceRequest_message_type(request),
proto3_msg)) {
static const char msg[] = "Only proto3 for now.";
conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg)));
response, upb_strview_make(msg, sizeof(msg)));
return;
}
@ -62,14 +62,14 @@ void DoTest(
switch (conformance_ConformanceRequest_payload_case(request)) {
case conformance_ConformanceRequest_payload_protobuf_payload: {
upb_stringview payload = conformance_ConformanceRequest_protobuf_payload(request);
upb_strview payload = conformance_ConformanceRequest_protobuf_payload(request);
test_message = protobuf_test_messages_proto3_TestAllTypesProto3_parsenew(
payload, arena);
if (!test_message) {
static const char msg[] = "Parse error";
conformance_ConformanceResponse_set_parse_error(
response, upb_stringview_make(msg, sizeof(msg)));
response, upb_strview_make(msg, sizeof(msg)));
return;
}
break;
@ -78,7 +78,7 @@ void DoTest(
case conformance_ConformanceRequest_payload_json_payload: {
static const char msg[] = "JSON support not yet implemented.";
conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg)));
response, upb_strview_make(msg, sizeof(msg)));
return;
}
@ -105,18 +105,18 @@ void DoTest(
if (!serialized) {
static const char msg[] = "Error serializing.";
conformance_ConformanceResponse_set_serialize_error(
response, upb_stringview_make(msg, sizeof(msg)));
response, upb_strview_make(msg, sizeof(msg)));
return;
}
conformance_ConformanceResponse_set_protobuf_payload(
response, upb_stringview_make(serialized, serialized_len));
response, upb_strview_make(serialized, serialized_len));
break;
}
case conformance_JSON: {
static const char msg[] = "JSON support not yet implemented.";
conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg)));
response, upb_strview_make(msg, sizeof(msg)));
break;
}
@ -155,7 +155,7 @@ bool DoTestIo() {
}
request = conformance_ConformanceRequest_parsenew(
upb_stringview_make(serialized_input, input_size), arena);
upb_strview_make(serialized_input, input_size), arena);
response = conformance_ConformanceResponse_new(arena);
if (request) {

@ -24,7 +24,7 @@ void test_pb_roundtrip() {
upb::Arena arena;
google_protobuf_FileDescriptorSet *set =
google_protobuf_FileDescriptorSet_parsenew(
upb_stringview_make(input.c_str(), input.size()), arena.ptr());
upb_strview_make(input.c_str(), input.size()), arena.ptr());
ASSERT(set);
size_t n;
const google_protobuf_FileDescriptorProto *const *files =

@ -639,8 +639,8 @@ static int lupb_symtab_add(lua_State *L) {
lupb_arena_new(L);
arena = lupb_arena_check(L, -1);
set = google_protobuf_FileDescriptorSet_parsenew(
upb_stringview_make(str, len), arena);
set = google_protobuf_FileDescriptorSet_parsenew(upb_strview_make(str, len),
arena);
if (!set) {
luaL_argerror(L, 2, "failed to parse descriptor");

@ -16,7 +16,7 @@ static int lupb_pb_decode(lua_State *L) {
const upb_msglayout *layout;
upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout);
const char *pb = lua_tolstring(L, 2, &len);
upb_stringview buf = upb_stringview_make(pb, len);
upb_strview buf = upb_strview_make(pb, len);
upb_decode(buf, msg, layout);
/* TODO(haberman): check for error. */

@ -113,14 +113,14 @@ static int64_t upb_zzdecode_64(uint64_t n) {
}
static bool upb_decode_string(const char **ptr, const char *limit,
upb_stringview *val) {
upb_strview *val) {
uint32_t len;
CHK(upb_decode_varint32(ptr, limit, &len) &&
len < INT32_MAX &&
limit - *ptr >= (int32_t)len);
*val = upb_stringview_make(*ptr, len);
*val = upb_strview_make(*ptr, len);
*ptr += len;
return true;
}
@ -151,7 +151,7 @@ static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame,
return upb_decode_64bit(&d->ptr, frame->limit, &val);
}
case UPB_WIRE_TYPE_DELIMITED: {
upb_stringview val;
upb_strview val;
return upb_decode_string(&d->ptr, frame->limit, &val);
}
case UPB_WIRE_TYPE_START_GROUP:
@ -375,7 +375,7 @@ static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame,
return true;
}
static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data,
int elem_size) {
int elements = data.size / elem_size;
void *field_mem;
@ -390,7 +390,7 @@ static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
const char *field_start,
const upb_msglayout_field *field,
upb_stringview val) {
upb_strview val) {
upb_array *arr = upb_getorcreatearr(frame, field);
#define VARINT_CASE(ctype, decode) { \
@ -471,7 +471,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame,
const char *field_start,
const upb_msglayout_field *field) {
upb_stringview val;
upb_strview val;
CHK(upb_decode_string(&d->ptr, frame->limit, &val));
@ -585,7 +585,7 @@ static bool upb_decode_message(upb_decstate *d, const char *limit,
return true;
}
bool upb_decode(upb_stringview buf, void *msg, const upb_msglayout *l) {
bool upb_decode(upb_strview buf, void *msg, const upb_msglayout *l) {
upb_decstate state;
state.ptr = buf.data;

@ -11,7 +11,7 @@
extern "C" {
#endif
bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l);
bool upb_decode(upb_strview buf, upb_msg *msg, const upb_msglayout *l);
#ifdef __cplusplus
} /* extern "C" */

@ -143,7 +143,7 @@ static bool upb_isalphanum(char c) {
return upb_isletter(c) || upb_isbetween(c, '0', '9');
}
static bool upb_isident(upb_stringview name, bool full, upb_status *s) {
static bool upb_isident(upb_strview name, bool full, upb_status *s) {
const char *str = name.data;
size_t len = name.size;
bool start = true;
@ -933,7 +933,7 @@ typedef struct {
upb_status *status; /* Record errors here. */
} symtab_addctx;
static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) {
static char* strviewdup(const symtab_addctx *ctx, upb_strview view) {
if (view.size == 0) {
return NULL;
}
@ -944,12 +944,12 @@ static bool streql2(const char *a, size_t n, const char *b) {
return n == strlen(b) && memcmp(a, b, n) == 0;
}
static bool streql_view(upb_stringview view, const char *b) {
static bool streql_view(upb_strview view, const char *b) {
return streql2(view.data, view.size, b);
}
static const char *makefullname(const symtab_addctx *ctx, const char *prefix,
upb_stringview name) {
upb_strview name) {
if (prefix) {
/* ret = prefix + '.' + name; */
size_t n = strlen(prefix);
@ -981,7 +981,7 @@ static bool symtab_add(const symtab_addctx *ctx, const char *name,
/* Given a symbol and the base symbol inside which it is defined, find the
* symbol's definition in t. */
static bool resolvename(const upb_strtable *t, const upb_fielddef *f,
const char *base, upb_stringview sym,
const char *base, upb_strview sym,
upb_deftype_t type, upb_status *status,
const void **def) {
if(sym.size == 0) return NULL;
@ -1013,7 +1013,7 @@ static bool resolvename(const upb_strtable *t, const upb_fielddef *f,
}
const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f,
const char *base, upb_stringview sym,
const char *base, upb_strview sym,
upb_deftype_t type) {
const void *ret;
if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) &&
@ -1030,7 +1030,7 @@ static bool create_oneofdef(
const symtab_addctx *ctx, upb_msgdef *m,
const google_protobuf_OneofDescriptorProto *oneof_proto) {
upb_oneofdef *o;
upb_stringview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
upb_value v;
o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
@ -1153,7 +1153,7 @@ static bool create_fielddef(
upb_alloc *alloc = ctx->alloc;
upb_fielddef *f;
const google_protobuf_FieldOptions *options;
upb_stringview name;
upb_strview name;
const char *full_name;
const char *shortname;
uint32_t field_number;
@ -1273,7 +1273,7 @@ static bool create_enumdef(
const google_protobuf_EnumDescriptorProto *enum_proto) {
upb_enumdef *e;
const google_protobuf_EnumValueDescriptorProto *const *values;
upb_stringview name;
upb_strview name;
size_t i, n;
name = google_protobuf_EnumDescriptorProto_name(enum_proto);
@ -1300,7 +1300,7 @@ static bool create_enumdef(
for (i = 0; i < n; i++) {
const google_protobuf_EnumValueDescriptorProto *value = values[i];
upb_stringview name = google_protobuf_EnumValueDescriptorProto_name(value);
upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value);
char *name2 = strviewdup(ctx, name);
int32_t num = google_protobuf_EnumValueDescriptorProto_number(value);
upb_value v = upb_value_int32(num);
@ -1339,7 +1339,7 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix,
const google_protobuf_EnumDescriptorProto *const *enums;
const google_protobuf_DescriptorProto *const *msgs;
size_t i, n;
upb_stringview name;
upb_strview name;
name = google_protobuf_DescriptorProto_name(msg_proto);
CHK(upb_isident(name, false, ctx->status));
@ -1437,7 +1437,7 @@ static void count_types_in_file(
static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
upb_fielddef *f) {
upb_stringview name;
upb_strview name;
const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
if (f->is_extension_) {
@ -1473,7 +1473,7 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
/* Have to delay resolving of the default value until now because of the enum
* case, since enum defaults are specified with a label. */
if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
upb_stringview defaultval =
upb_strview defaultval =
google_protobuf_FieldDescriptorProto_default_value(field_proto);
if (f->file->syntax == UPB_SYNTAX_PROTO3) {
@ -1492,9 +1492,9 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
if (!parse_default(ctx, defaultval.data, defaultval.size, f)) {
upb_status_seterrf(ctx->status,
"couldn't parse default '" UPB_STRINGVIEW_FORMAT
"couldn't parse default '" UPB_STRVIEW_FORMAT
"' for field (%s)",
UPB_STRINGVIEW_ARGS(defaultval), f->full_name);
UPB_STRVIEW_ARGS(defaultval), f->full_name);
return false;
}
} else {
@ -1512,7 +1512,7 @@ static bool build_filedef(
const google_protobuf_DescriptorProto *const *msgs;
const google_protobuf_EnumDescriptorProto *const *enums;
const google_protobuf_FieldDescriptorProto *const *exts;
const upb_stringview* strs;
const upb_strview* strs;
size_t i, n;
decl_counts counts = {0};
@ -1542,7 +1542,7 @@ static bool build_filedef(
file->phpnamespace = NULL;
if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
upb_stringview package =
upb_strview package =
google_protobuf_FileDescriptorProto_package(file_proto);
CHK(upb_isident(package, true, ctx->status));
file->package = strviewdup(ctx, package);
@ -1551,7 +1551,7 @@ static bool build_filedef(
}
if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
upb_stringview syntax =
upb_strview syntax =
google_protobuf_FileDescriptorProto_syntax(file_proto);
if (streql_view(syntax, "proto2")) {
@ -1581,7 +1581,7 @@ static bool build_filedef(
CHK_OOM(n == 0 || file->deps);
for (i = 0; i < n; i++) {
upb_stringview dep_name = strs[i];
upb_strview dep_name = strs[i];
upb_value v;
if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
dep_name.size, &v)) {

@ -796,7 +796,7 @@ bool upb_symtab_addfile(upb_symtab *s,
typedef struct upb_def_init {
struct upb_def_init **deps;
const char *filename;
upb_stringview descriptor;
upb_strview descriptor;
} upb_def_init;
bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init);

@ -208,8 +208,8 @@ do { ; } while(0)
VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_stringview *start = arr->data;
upb_stringview *ptr = start + arr->len;
upb_strview *start = arr->data;
upb_strview *ptr = start + arr->len;
do {
ptr--;
CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
@ -293,7 +293,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_stringview view = *(upb_stringview*)field_mem;
upb_strview view = *(upb_strview*)field_mem;
if (skip_zero_value && view.size == 0) {
return true;
}

@ -53,7 +53,7 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
return sizeof(void*);
case UPB_TYPE_BYTES:
case UPB_TYPE_STRING:
return sizeof(upb_stringview);
return sizeof(upb_strview);
}
UPB_UNREACHABLE();
}

@ -79,32 +79,32 @@ typedef struct upb_msglayout {
bool extendable;
} upb_msglayout;
/** upb_stringview ************************************************************/
/** upb_strview ************************************************************/
typedef struct {
const char *data;
size_t size;
} upb_stringview;
} upb_strview;
UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) {
upb_stringview ret;
UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
upb_strview ret;
ret.data = data;
ret.size = size;
return ret;
}
UPB_INLINE upb_stringview upb_stringview_makez(const char *data) {
return upb_stringview_make(data, strlen(data));
UPB_INLINE upb_strview upb_strview_makez(const char *data) {
return upb_strview_make(data, strlen(data));
}
UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) {
UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) {
return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
}
#define UPB_STRINGVIEW_FORMAT "%.*s"
#define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
#define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len}
#define UPB_STRVIEW_FORMAT "%.*s"
#define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
/** upb_msgval ****************************************************************/
@ -123,7 +123,7 @@ typedef union {
const upb_msg* msg;
const upb_array* arr;
const void* ptr;
upb_stringview str;
upb_strview str;
} upb_msgval;
#define ACCESSORS(name, membername, ctype) \
@ -150,12 +150,12 @@ ACCESSORS(map, map, const upb_map*)
ACCESSORS(msg, msg, const upb_msg*)
ACCESSORS(ptr, ptr, const void*)
ACCESSORS(arr, arr, const upb_array*)
ACCESSORS(str, str, upb_stringview)
ACCESSORS(str, str, upb_strview)
#undef ACCESSORS
UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
return upb_msgval_str(upb_stringview_make(data, size));
return upb_msgval_str(upb_strview_make(data, size));
}
/** upb_msg *******************************************************************/

@ -33,7 +33,7 @@ static size_t upb_msgval_sizeof2(upb_fieldtype_t type) {
return sizeof(void*);
case UPB_TYPE_BYTES:
case UPB_TYPE_STRING:
return sizeof(upb_stringview);
return sizeof(upb_strview);
}
UPB_UNREACHABLE();
}

@ -225,7 +225,7 @@ std::string CTypeInternal(const protobuf::FieldDescriptor* field,
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
return "uint64_t";
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return "upb_stringview";
return "upb_strview";
default:
fprintf(stderr, "Unexpected type");
abort();
@ -265,7 +265,7 @@ std::string FieldDefault(const protobuf::FieldDescriptor* field) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return "NULL";
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return absl::Substitute("upb_stringview_make(\"$0\", strlen(\"$0\"))",
return absl::Substitute("upb_strview_make(\"$0\", strlen(\"$0\"))",
absl::CEscape(field->default_value_string()));
case protobuf::FieldDescriptor::CPPTYPE_INT32:
return absl::StrCat(field->default_value_int32());
@ -338,7 +338,7 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
"UPB_INLINE $0 *$0_new(upb_arena *arena) {\n"
" return ($0 *)upb_msg_new(&$1, arena);\n"
"}\n"
"UPB_INLINE $0 *$0_parsenew(upb_stringview buf, upb_arena *arena) {\n"
"UPB_INLINE $0 *$0_parsenew(upb_strview buf, upb_arena *arena) {\n"
" $0 *ret = $0_new(arena);\n"
" return (ret && upb_decode(buf, ret, &$1)) ? ret : NULL;\n"
"}\n"
@ -413,13 +413,13 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
for (auto field : FieldNumberOrder(message)) {
if (field->is_repeated()) {
output(
"UPB_INLINE $0* $1_$2_mutable($1 *msg, size_t *len) {\n"
"UPB_INLINE $0* $1_mutable_$2($1 *msg, size_t *len) {\n"
" return ($0*)_upb_array_mutable_accessor(msg, $3, len);\n"
"}\n",
CType(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)));
output(
"UPB_INLINE $0* $1_$2_resize($1 *msg, size_t len, "
"UPB_INLINE $0* $1_resize_$2($1 *msg, size_t len, "
"upb_arena *arena) {\n"
" return ($0*)_upb_array_resize_accessor(msg, $3, len, $4, $5, "
"arena);\n"
@ -748,7 +748,7 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
output("upb_def_init $0 = {\n", DefInitSymbol(file));
output(" deps,\n");
output(" \"$0\",\n", file->name());
output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size());
output(" UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size());
output("};\n");
}

Loading…
Cancel
Save