Fixes for non-C89 code.

pull/13171/head
Joshua Haberman 5 years ago
parent 6c2d732082
commit 7f5fe52dfa
  1. 1
      BUILD
  2. 33
      tests/pb/test_varint.c
  3. 10
      upb/def.c
  4. 19
      upb/json/printer.c
  5. 20
      upb/pb/varint.int.h
  6. 4
      upb/reflection.c
  7. 2
      upb/table.c

@ -32,6 +32,7 @@ CPPOPTS = [
COPTS = CPPOPTS + [
# copybara:strip_for_google3_begin
"-pedantic",
"-Werror=pedantic",
"-Wstrict-prototypes",
# copybara:strip_end
]

@ -53,6 +53,13 @@ static void test_varint_for_num(upb_decoderet (*decoder)(const char*),
ASSERT(upb_zzenc_64(upb_zzdec_64(num)) == num);
}
/* Making up for the lack of 64-bit constants in C89. */
static uint64_t make_u64(uint32_t high, uint32_t low) {
uint64_t ret = high;
ret = (ret << 32) | low;
return ret;
}
static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
#define TEST(bytes, expected_val) {\
size_t n = sizeof(bytes) - 1; /* for NULL */ \
@ -74,18 +81,20 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
upb_decoderet r = decoder(twelvebyte_buf);
ASSERT(r.p == NULL);
TEST("\x00", 0ULL);
TEST("\x01", 1ULL);
TEST("\x81\x14", 0xa01ULL);
TEST("\x81\x03", 0x181ULL);
TEST("\x81\x83\x07", 0x1c181ULL);
TEST("\x81\x83\x87\x0f", 0x1e1c181ULL);
TEST("\x81\x83\x87\x8f\x1f", 0x1f1e1c181ULL);
TEST("\x81\x83\x87\x8f\x9f\x3f", 0x1f9f1e1c181ULL);
TEST("\x81\x83\x87\x8f\x9f\xbf\x7f", 0x1fdf9f1e1c181ULL);
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x01", 0x3fdf9f1e1c181ULL);
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x03", 0x303fdf9f1e1c181ULL);
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x83\x07", 0x8303fdf9f1e1c181ULL);
TEST("\x00", 0UL);
TEST("\x01", 1UL);
TEST("\x81\x14", 0xa01UL);
TEST("\x81\x03", 0x181UL);
TEST("\x81\x83\x07", 0x1c181UL);
TEST("\x81\x83\x87\x0f", 0x1e1c181UL);
TEST("\x81\x83\x87\x8f\x1f", make_u64(0x1, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\x3f", make_u64(0x1f9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\x7f", make_u64(0x1fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x01", make_u64(0x3fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x03",
make_u64(0x303fdf9, 0xf1e1c181UL));
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x83\x07",
make_u64(0x8303fdf9, 0xf1e1c181UL));
#undef TEST
for (num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) {

@ -869,9 +869,9 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
// Map entries aren't actually stored, they are only used during parsing.
// For parsing, it helps a lot if all map entry messages have the same
// layout.
/* Map entries aren't actually stored, they are only used during parsing.
* For parsing, it helps a lot if all map entry messages have the same
* layout. */
return sizeof(upb_strview);
} else if (upb_fielddef_isseq(f)) {
return sizeof(void*);
@ -1188,7 +1188,7 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len,
}
case UPB_TYPE_INT64: {
/* XXX: Need to write our own strtoll, since it's not available in c89. */
long long val = strtol(str, &end, 0);
int64_t val = strtol(str, &end, 0);
CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end);
f->defaultval.sint = val;
break;
@ -1201,7 +1201,7 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len,
}
case UPB_TYPE_UINT64: {
/* XXX: Need to write our own strtoull, since it's not available in c89. */
unsigned long long val = strtoul(str, &end, 0);
uint64_t val = strtoul(str, &end, 0);
CHK(val <= UINT64_MAX && errno != ERANGE && !*end);
f->defaultval.uint = val;
break;

@ -6,6 +6,7 @@
#include "upb/json/printer.h"
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
@ -208,28 +209,26 @@ static size_t fmt_bool(bool val, char* buf, size_t length) {
return n;
}
static size_t fmt_int64_as_number(long long val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%lld", val);
static size_t fmt_int64_as_number(int64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%" PRId64, val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_uint64_as_number(
unsigned long long val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%llu", val);
static size_t fmt_uint64_as_number(uint64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "%" PRIu64, val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_int64_as_string(long long val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%lld\"", val);
static size_t fmt_int64_as_string(int64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%" PRId64 "\"", val);
CHKLENGTH(n > 0 && n < length);
return n;
}
static size_t fmt_uint64_as_string(
unsigned long long val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%llu\"", val);
static size_t fmt_uint64_as_string(uint64_t val, char* buf, size_t length) {
size_t n = _upb_snprintf(buf, length, "\"%" PRIu64 "\"", val);
CHKLENGTH(n > 0 && n < length);
return n;
}

@ -26,16 +26,16 @@ extern "C" {
* descriptor type (upb_descriptortype_t). */
extern const uint8_t upb_pb_native_wire_types[];
UPB_INLINE uint64_t byteswap64(uint64_t val)
{
return ((((val) & 0xff00000000000000ull) >> 56)
| (((val) & 0x00ff000000000000ull) >> 40)
| (((val) & 0x0000ff0000000000ull) >> 24)
| (((val) & 0x000000ff00000000ull) >> 8)
| (((val) & 0x00000000ff000000ull) << 8)
| (((val) & 0x0000000000ff0000ull) << 24)
| (((val) & 0x000000000000ff00ull) << 40)
| (((val) & 0x00000000000000ffull) << 56));
UPB_INLINE uint64_t byteswap64(uint64_t val) {
uint64_t byte = 0xff;
return (val & (byte << 56) >> 56)
| (val & (byte << 48) >> 40)
| (val & (byte << 40) >> 24)
| (val & (byte << 32) >> 8)
| (val & (byte << 24) << 8)
| (val & (byte << 16) << 24)
| (val & (byte << 8) << 40)
| (val & (byte << 0) << 56);
}
/* Zig-zag encoding/decoding **************************************************/

@ -148,18 +148,18 @@ size_t upb_array_size(const upb_array *arr) {
}
upb_msgval upb_array_get(const upb_array *arr, size_t i) {
UPB_ASSERT(i < arr->len);
upb_msgval ret;
const char* data = _upb_array_constptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->len);
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
UPB_ASSERT(i < arr->len);
char* data = _upb_array_ptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->len);
memcpy(data + (i << lg2), &val, 1 << lg2);
}

@ -371,9 +371,9 @@ bool upb_strtable_done(const upb_strtable_iter *i) {
}
upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
UPB_ASSERT(!upb_strtable_done(i));
upb_strview key;
uint32_t len;
UPB_ASSERT(!upb_strtable_done(i));
key.data = upb_tabstr(str_tabent(i)->key, &len);
key.size = len;
return key;

Loading…
Cancel
Save