Merge pull request #375 from haberman/cleanups

A few minor cleanups
pull/13171/head
Joshua Haberman 4 years ago committed by GitHub
commit 97e2aeb7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      BUILD
  2. 11
      upb/def.c
  3. 35
      upb/reflection.c
  4. 8
      upb/table.c
  5. 2
      upb/table.int.h

17
BUILD

@ -11,15 +11,7 @@ load(
"upb_proto_reflection_library", "upb_proto_reflection_library",
) )
# copybara:strip_for_google3_begin licenses(["notice"])
load(
"@rules_proto//proto:defs.bzl",
"proto_library",
)
# copybara:strip_end
licenses(["notice"]) # BSD (Google-authored w/ possible external contributions)
exports_files([ exports_files([
"LICENSE", "LICENSE",
@ -212,6 +204,8 @@ cc_library(
# Amalgamation ################################################################# # Amalgamation #################################################################
# copybara:strip_for_google3_begin
py_binary( py_binary(
name = "amalgamate", name = "amalgamate",
srcs = ["tools/amalgamate.py"], srcs = ["tools/amalgamate.py"],
@ -314,9 +308,6 @@ exports_files(
filegroup( filegroup(
name = "cmake_files", name = "cmake_files",
srcs = glob([ srcs = glob([
"upb/json/parser.c",
"CMakeLists.txt",
"generated_for_cmake/**/*",
"google/**/*", "google/**/*",
"upbc/**/*", "upbc/**/*",
"upb/**/*", "upb/**/*",
@ -325,3 +316,5 @@ filegroup(
]), ]),
visibility = ["//cmake:__pkg__"], visibility = ["//cmake:__pkg__"],
) )
# copybara:strip_end

@ -815,7 +815,6 @@ void upb_symtab_free(upb_symtab *s) {
upb_symtab *upb_symtab_new(void) { upb_symtab *upb_symtab_new(void) {
upb_symtab *s = upb_gmalloc(sizeof(*s)); upb_symtab *s = upb_gmalloc(sizeof(*s));
upb_alloc *alloc;
if (!s) { if (!s) {
return NULL; return NULL;
@ -823,7 +822,6 @@ upb_symtab *upb_symtab_new(void) {
s->arena = upb_arena_new(); s->arena = upb_arena_new();
s->bytes_loaded = 0; s->bytes_loaded = 0;
alloc = upb_arena_alloc(s->arena);
if (!upb_strtable_init(&s->syms, 32, s->arena) || if (!upb_strtable_init(&s->syms, 32, s->arena) ||
!upb_strtable_init(&s->files, 4, s->arena)) { !upb_strtable_init(&s->files, 4, s->arena)) {
@ -1401,8 +1399,7 @@ static void parse_default(symtab_addctx *ctx, const char *str, size_t len,
break; break;
} }
case UPB_TYPE_INT64: { case UPB_TYPE_INT64: {
/* XXX: Need to write our own strtoll, since it's not available in c89. */ long long val = strtoll(str, &end, 0);
int64_t val = strtol(str, &end, 0);
if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) { if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) {
goto invalid; goto invalid;
} }
@ -1418,8 +1415,7 @@ static void parse_default(symtab_addctx *ctx, const char *str, size_t len,
break; break;
} }
case UPB_TYPE_UINT64: { case UPB_TYPE_UINT64: {
/* XXX: Need to write our own strtoull, since it's not available in c89. */ unsigned long long val = strtoull(str, &end, 0);
uint64_t val = strtoul(str, &end, 0);
if (val > UINT64_MAX || errno == ERANGE || *end) { if (val > UINT64_MAX || errno == ERANGE || *end) {
goto invalid; goto invalid;
} }
@ -1435,8 +1431,7 @@ static void parse_default(symtab_addctx *ctx, const char *str, size_t len,
break; break;
} }
case UPB_TYPE_FLOAT: { case UPB_TYPE_FLOAT: {
/* XXX: Need to write our own strtof, since it's not available in c89. */ float val = strtof(str, &end);
float val = strtod(str, &end);
if (errno == ERANGE || *end) { if (errno == ERANGE || *end) {
goto invalid; goto invalid;
} }

@ -113,40 +113,7 @@ upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) { if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
return _upb_msg_getraw(msg, f); return _upb_msg_getraw(msg, f);
} else { } else {
/* TODO(haberman): change upb_fielddef to not require this switch(). */ return upb_fielddef_default(f);
upb_msgval val = {0};
switch (upb_fielddef_type(f)) {
case UPB_TYPE_INT32:
case UPB_TYPE_ENUM:
val.int32_val = upb_fielddef_defaultint32(f);
break;
case UPB_TYPE_INT64:
val.int64_val = upb_fielddef_defaultint64(f);
break;
case UPB_TYPE_UINT32:
val.uint32_val = upb_fielddef_defaultuint32(f);
break;
case UPB_TYPE_UINT64:
val.uint64_val = upb_fielddef_defaultuint64(f);
break;
case UPB_TYPE_FLOAT:
val.float_val = upb_fielddef_defaultfloat(f);
break;
case UPB_TYPE_DOUBLE:
val.double_val = upb_fielddef_defaultdouble(f);
break;
case UPB_TYPE_BOOL:
val.bool_val = upb_fielddef_defaultbool(f);
break;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
val.str_val.data = upb_fielddef_defaultstr(f, &val.str_val.size);
break;
case UPB_TYPE_MESSAGE:
val.msg_val = NULL;
break;
}
return val;
} }
} }

@ -25,7 +25,7 @@ static const double MAX_LOAD = 0.85;
* cache effects). The lower this is, the more memory we'll use. */ * cache effects). The lower this is, the more memory we'll use. */
static const double MIN_DENSITY = 0.1; static const double MIN_DENSITY = 0.1;
bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; } static bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
static upb_value _upb_value_val(uint64_t val) { static upb_value _upb_value_val(uint64_t val) {
upb_value ret; upb_value ret;
@ -33,7 +33,7 @@ static upb_value _upb_value_val(uint64_t val) {
return ret; return ret;
} }
int log2ceil(uint64_t v) { static int log2ceil(uint64_t v) {
int ret = 0; int ret = 0;
bool pow2 = is_pow2(v); bool pow2 = is_pow2(v);
while (v >>= 1) ret++; while (v >>= 1) ret++;
@ -41,10 +41,6 @@ int log2ceil(uint64_t v) {
return UPB_MIN(UPB_MAXARRSIZE, ret); return UPB_MIN(UPB_MAXARRSIZE, ret);
} }
char *upb_strdup(const char *s, upb_arena *a) {
return upb_strdup2(s, strlen(s), a);
}
char *upb_strdup2(const char *s, size_t len, upb_arena *a) { char *upb_strdup2(const char *s, size_t len, upb_arena *a) {
size_t n; size_t n;
char *p; char *p;

@ -37,8 +37,6 @@ typedef struct {
uint64_t val; uint64_t val;
} upb_value; } upb_value;
/* Like strdup(), which isn't always available since it's not ANSI C. */
char *upb_strdup(const char *s, upb_arena *a);
/* Variant that works with a length-delimited rather than NULL-delimited string, /* Variant that works with a length-delimited rather than NULL-delimited string,
* as supported by strtable. */ * as supported by strtable. */
char *upb_strdup2(const char *s, size_t len, upb_arena *a); char *upb_strdup2(const char *s, size_t len, upb_arena *a);

Loading…
Cancel
Save