From 7f5fe52dfa70cc2c71db2a7039bf8ec25037b670 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 10 Dec 2019 22:21:45 -0800 Subject: [PATCH] Fixes for non-C89 code. --- BUILD | 1 + tests/pb/test_varint.c | 33 +++++++++++++++++++++------------ upb/def.c | 10 +++++----- upb/json/printer.c | 19 +++++++++---------- upb/pb/varint.int.h | 20 ++++++++++---------- upb/reflection.c | 4 ++-- upb/table.c | 2 +- 7 files changed, 49 insertions(+), 40 deletions(-) diff --git a/BUILD b/BUILD index c12aae8425..7c1707d1ab 100644 --- a/BUILD +++ b/BUILD @@ -32,6 +32,7 @@ CPPOPTS = [ COPTS = CPPOPTS + [ # copybara:strip_for_google3_begin "-pedantic", + "-Werror=pedantic", "-Wstrict-prototypes", # copybara:strip_end ] diff --git a/tests/pb/test_varint.c b/tests/pb/test_varint.c index 95a04ab9b8..cdae9dba47 100644 --- a/tests/pb/test_varint.c +++ b/tests/pb/test_varint.c @@ -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) { diff --git a/upb/def.c b/upb/def.c index a4a2200e3a..12bb104bed 100644 --- a/upb/def.c +++ b/upb/def.c @@ -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; diff --git a/upb/json/printer.c b/upb/json/printer.c index 38f817d4d4..a3cb9bdb01 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -6,6 +6,7 @@ #include "upb/json/printer.h" #include +#include #include #include #include @@ -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; } diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index ff1ca66149..293067a5cb 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -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 **************************************************/ diff --git a/upb/reflection.c b/upb/reflection.c index f1e20e59d4..e0a6299bb2 100644 --- a/upb/reflection.c +++ b/upb/reflection.c @@ -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); } diff --git a/upb/table.c b/upb/table.c index 63389c0144..5b48bb7e50 100644 --- a/upb/table.c +++ b/upb/table.c @@ -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;