diff --git a/BUILD b/BUILD index 202154c9d0..bcecd387af 100644 --- a/BUILD +++ b/BUILD @@ -28,6 +28,8 @@ exports_files([ CPPOPTS = [ # copybara:strip_for_google3_begin + "-Wextra", + # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang) "-Werror", "-Wno-long-long", # copybara:strip_end @@ -164,6 +166,10 @@ cc_library( hdrs = [ "upb/text_encode.h", ], + copts = select({ + ":windows": [], + "//conditions:default": COPTS, + }), visibility = ["//visibility:public"], deps = [ ":port", @@ -181,6 +187,10 @@ cc_library( "upb/json_decode.h", "upb/json_encode.h", ], + copts = select({ + ":windows": [], + "//conditions:default": COPTS, + }), deps = [ ":port", ":reflection", diff --git a/generated_for_cmake/upb/json/parser.c b/generated_for_cmake/upb/json/parser.c index 34571880f4..dd4fd82f2f 100644 --- a/generated_for_cmake/upb/json/parser.c +++ b/generated_for_cmake/upb/json/parser.c @@ -977,6 +977,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint32(p->top->sink, parser_getsel(p), (int32_t)val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT32: { unsigned long val = strtoul(buf, &end, 0); @@ -988,6 +989,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint32(p->top->sink, parser_getsel(p), (uint32_t)val); return true; } + UPB_UNREACHABLE(); } /* XXX: We can't handle [u]int64 properly on 32-bit machines because * strto[u]ll isn't in C89. */ @@ -999,6 +1001,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT64: { unsigned long val = strtoul(p->accumulated, &end, 0); @@ -1010,6 +1013,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } default: break; @@ -2575,11 +2579,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2780 "upb/json/parser.rl" +#line 2784 "upb/json/parser.rl" -#line 2583 "upb/json/parser.c" +#line 2587 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2834,7 +2838,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2783 "upb/json/parser.rl" +#line 2787 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2857,7 +2861,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2861 "upb/json/parser.c" +#line 2865 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2932,147 +2936,147 @@ _match: switch ( *_acts++ ) { case 1: -#line 2588 "upb/json/parser.rl" +#line 2592 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2590 "upb/json/parser.rl" +#line 2594 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2594 "upb/json/parser.rl" +#line 2598 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2595 "upb/json/parser.rl" +#line 2599 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2601 "upb/json/parser.rl" +#line 2605 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2602 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2603 "upb/json/parser.rl" +#line 2607 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2609 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2615 "upb/json/parser.rl" +#line 2619 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2620 "upb/json/parser.rl" +#line 2624 "upb/json/parser.rl" { start_year(parser, p); } break; case 11: -#line 2621 "upb/json/parser.rl" +#line 2625 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 2625 "upb/json/parser.rl" +#line 2629 "upb/json/parser.rl" { start_month(parser, p); } break; case 13: -#line 2626 "upb/json/parser.rl" +#line 2630 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 2630 "upb/json/parser.rl" +#line 2634 "upb/json/parser.rl" { start_day(parser, p); } break; case 15: -#line 2631 "upb/json/parser.rl" +#line 2635 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 2635 "upb/json/parser.rl" +#line 2639 "upb/json/parser.rl" { start_hour(parser, p); } break; case 17: -#line 2636 "upb/json/parser.rl" +#line 2640 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 2640 "upb/json/parser.rl" +#line 2644 "upb/json/parser.rl" { start_minute(parser, p); } break; case 19: -#line 2641 "upb/json/parser.rl" +#line 2645 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 2645 "upb/json/parser.rl" +#line 2649 "upb/json/parser.rl" { start_second(parser, p); } break; case 21: -#line 2646 "upb/json/parser.rl" +#line 2650 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 2651 "upb/json/parser.rl" +#line 2655 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 23: -#line 2652 "upb/json/parser.rl" +#line 2656 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 2654 "upb/json/parser.rl" +#line 2658 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2659 "upb/json/parser.rl" +#line 2663 "upb/json/parser.rl" { start_timestamp_base(parser); } break; case 26: -#line 2661 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 27: -#line 2662 "upb/json/parser.rl" +#line 2666 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 28: -#line 2664 "upb/json/parser.rl" +#line 2668 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 29: -#line 2665 "upb/json/parser.rl" +#line 2669 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 30: -#line 2667 "upb/json/parser.rl" +#line 2671 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 31: -#line 2672 "upb/json/parser.rl" +#line 2676 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 32: -#line 2673 "upb/json/parser.rl" +#line 2677 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 33: -#line 2678 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 34: -#line 2679 "upb/json/parser.rl" +#line 2683 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 35: -#line 2685 "upb/json/parser.rl" +#line 2689 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 36: -#line 2690 "upb/json/parser.rl" +#line 2694 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3086,11 +3090,11 @@ _match: } break; case 37: -#line 2703 "upb/json/parser.rl" +#line 2707 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 38: -#line 2708 "upb/json/parser.rl" +#line 2712 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3100,11 +3104,11 @@ _match: } break; case 39: -#line 2715 "upb/json/parser.rl" +#line 2719 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 40: -#line 2718 "upb/json/parser.rl" +#line 2722 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3114,7 +3118,7 @@ _match: } break; case 41: -#line 2729 "upb/json/parser.rl" +#line 2733 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3124,7 +3128,7 @@ _match: } break; case 42: -#line 2738 "upb/json/parser.rl" +#line 2742 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3134,54 +3138,54 @@ _match: } break; case 43: -#line 2750 "upb/json/parser.rl" +#line 2754 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 44: -#line 2754 "upb/json/parser.rl" +#line 2758 "upb/json/parser.rl" { end_array(parser); } break; case 45: -#line 2759 "upb/json/parser.rl" +#line 2763 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 46: -#line 2760 "upb/json/parser.rl" +#line 2764 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 47: -#line 2762 "upb/json/parser.rl" +#line 2766 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 48: -#line 2763 "upb/json/parser.rl" +#line 2767 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 49: -#line 2765 "upb/json/parser.rl" +#line 2769 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2767 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2769 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 52: -#line 2771 "upb/json/parser.rl" +#line 2775 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 53: -#line 2772 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 54: -#line 2777 "upb/json/parser.rl" +#line 2781 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3185 "upb/json/parser.c" +#line 3189 "upb/json/parser.c" } } @@ -3198,32 +3202,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2586 "upb/json/parser.rl" +#line 2590 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 46: -#line 2760 "upb/json/parser.rl" +#line 2764 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 49: -#line 2765 "upb/json/parser.rl" +#line 2769 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2767 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2769 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 53: -#line 2772 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3227 "upb/json/parser.c" +#line 3231 "upb/json/parser.c" } } } @@ -3231,7 +3235,7 @@ goto _again;} } _out: {} } -#line 2805 "upb/json/parser.rl" +#line 2809 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3274,13 +3278,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3278 "upb/json/parser.c" +#line 3282 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2847 "upb/json/parser.rl" +#line 2851 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/kokoro/ubuntu/build.sh b/kokoro/ubuntu/build.sh index 97678ea286..3888d15f6a 100644 --- a/kokoro/ubuntu/build.sh +++ b/kokoro/ubuntu/build.sh @@ -1,12 +1,18 @@ #!/bin/bash +set -ex + # Install the latest version of Bazel. -use_bazel.sh latest +if [ -x "$(command -v use_bazel.sh)" ]; then + use_bazel.sh latest +fi # Verify/query CMake echo PATH=$PATH ls -l `which cmake` cmake --version +echo CC=${CC:-cc} +${CC:-cc} --version # Log the bazel path and version. which bazel diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index 4a04adf8e4..fe9f5224e5 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -138,7 +138,7 @@ void serialize_json(const upb_msg *msg, const upb_msgdef *m, const ctx *c) { upb_status_clear(&status); len = upb_json_encode(msg, m, c->symtab, opts, NULL, 0, &status); - if (len == -1) { + if (len == (size_t)-1) { const char *inerr = upb_status_errmsg(&status); size_t len = strlen(inerr); char *err = upb_arena_malloc(c->arena, len + 1); diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index e5f0103584..18ab07fe50 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -149,6 +149,7 @@ static TestCase kTestSkipUnknown[] = { static TestCase kTestFailure[] = { { TEST("{\"optionalEnum\":\"UNKNOWN_ENUM_VALUE\"}"), + EXPECT("{}"), /* Actually we expect error, this is checked later. */ }, TEST_SENTINEL }; @@ -242,7 +243,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, false); } } @@ -257,7 +258,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, true); } } @@ -274,7 +275,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, false); } } @@ -319,7 +320,7 @@ void test_json_failure() { test_case->input != NULL; test_case++) { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_parse_failure(test_case->input, serialize_handlers, - parser_method, i); + parser_method, (int)i); } } } diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index ef40f8f3e2..14fd72abbc 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -509,8 +509,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, } bool ok = env->Start() && - parse(env, decoder, i) && - parse(env, decoder, j - i) && + parse(env, decoder, (int)i) && + parse(env, decoder, (int)(j - i)) && parse(env, decoder, -1) && env->End(); @@ -1108,7 +1108,7 @@ void test_valid() { run_decoder(buf, &textbuf); } -void empty_callback(const void *closure, upb::Handlers* h_ptr) {} +void empty_callback(const void* /* closure */, upb::Handlers* /* h_ptr */) {} void test_emptyhandlers(upb::SymbolTable* symtab) { // Create an empty handlers to make sure that the decoder can handle empty @@ -1176,7 +1176,7 @@ extern "C" { int run_tests(int argc, char *argv[]) { if (argc > 1) - filter_hash = strtol(argv[1], NULL, 16); + filter_hash = (uint32_t)strtol(argv[1], NULL, 16); for (int i = 0; i < MAX_NESTING; i++) { closures[i] = i; } diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 55cc077c88..4ca81e1c35 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -681,6 +681,7 @@ void DoNothingEndMessageHandler(C* closure, upb_status *status) { void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) { upb::HandlersPtr h(h_ptr); + UPB_UNUSED(closure); upb::MessageDefPtr md(h.message_def()); ASSERT(md); @@ -797,6 +798,7 @@ void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) { void RegisterMismatchedTypes2(const void* closure, upb::Handlers* h_ptr) { upb::HandlersPtr h(h_ptr); + UPB_UNUSED(closure); upb::MessageDefPtr md(h.message_def()); ASSERT(md); @@ -941,7 +943,7 @@ void TestArena() { extern "C" { -int run_tests(int argc, char *argv[]) { +int run_tests() { TestHandler(); TestHandler(); TestHandler(); diff --git a/tests/test_util.h b/tests/test_util.h index 485410082d..418a9d8899 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -102,7 +102,7 @@ class VerboseParserEnvironment { bool ParseBuffer(int bytes) { if (bytes < 0) { - bytes = len_ - ofs_; + bytes = (int)(len_ - ofs_); } ASSERT((size_t)bytes <= (len_ - ofs_)); @@ -130,7 +130,7 @@ class VerboseParserEnvironment { (unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes)); } - int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle); + int parsed = (int)sink_.PutBuffer(subc_, buf2, bytes, &global_handle); free(buf2); if (verbose_) { @@ -156,7 +156,7 @@ class VerboseParserEnvironment { } if (parsed > bytes && skip_until_ >= 0) { - skip_until_ = ofs_ + parsed; + skip_until_ = (int)(ofs_ + parsed); } ofs_ += UPB_MIN(parsed, bytes); diff --git a/upb/bindings/lua/upbc.cc b/upb/bindings/lua/upbc.cc index e7563bc0ba..c3988f6519 100644 --- a/upb/bindings/lua/upbc.cc +++ b/upb/bindings/lua/upbc.cc @@ -73,9 +73,9 @@ static void PrintString(int max_cols, absl::string_view* str, bool LuaGenerator::Generate( const protobuf::FileDescriptor* file, - const std::string& parameter, + const std::string& /* parameter */, protoc::GeneratorContext* context, - std::string* error) const { + std::string* /* error */) const { std::string filename = Filename(file); protobuf::io::ZeroCopyOutputStream* out = context->Open(filename); protobuf::io::Printer printer(out, '$'); diff --git a/upb/decode.c b/upb/decode.c index 7ee199f51a..cecd68a8e7 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -155,7 +155,7 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg, UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); } -static bool decode_reserve(upb_decstate *d, upb_array *arr, int elem) { +static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) { bool need_realloc = arr->size - arr->len < elem; if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) { decode_err(d); @@ -324,7 +324,7 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr, /* Fixed packed. */ int lg2 = op - OP_FIXPCK_LG2(0); int mask = (1 << lg2) - 1; - int count = val.str_val.size >> lg2; + size_t count = val.str_val.size >> lg2; if ((val.str_val.size & mask) != 0) { decode_err(d); /* Length isn't a round multiple of elem size. */ } @@ -408,7 +408,7 @@ static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg, /* Set presence if necessary. */ if (field->presence < 0) { /* Oneof case */ - int32_t *oneof_case = _upb_oneofcase_field(msg, field); + uint32_t *oneof_case = _upb_oneofcase_field(msg, field); if (op == OP_SUBMSG && *oneof_case != field->number) { memset(mem, 0, sizeof(void*)); } diff --git a/upb/def.c b/upb/def.c index 20b2be9969..10f6b94bf1 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1909,11 +1909,11 @@ static bool build_filedef( } /* Now that all names are in the table, build layouts and resolve refs. */ - for (i = 0; i < file->ext_count; i++) { + for (i = 0; i < (size_t)file->ext_count; i++) { CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i])); } - for (i = 0; i < file->msg_count; i++) { + for (i = 0; i < (size_t)file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; int j; for (j = 0; j < m->field_count; j++) { @@ -1922,7 +1922,7 @@ static bool build_filedef( } if (!ctx->layouts) { - for (i = 0; i < file->msg_count; i++) { + for (i = 0; i < (size_t)file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; make_layout(ctx->symtab, m); } @@ -1931,8 +1931,7 @@ static bool build_filedef( return true; } -static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, - upb_status *status) { +static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx) { const upb_filedef *file = ctx->file; upb_alloc *alloc = upb_arena_alloc(s->arena); upb_strtable_iter iter; @@ -2075,10 +2074,8 @@ static const upb_filedef *_upb_symtab_addfile( ctx.layouts = layouts; ctx.status = status; - ok = file && - upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && - build_filedef(&ctx, file, file_proto) && - upb_symtab_addtotabs(s, &ctx, status); + ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && + build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx); upb_arena_free(tmparena); return ok ? file : NULL; diff --git a/upb/handlers.c b/upb/handlers.c index 5cf7ccc074..b90b32c71f 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -152,28 +152,6 @@ const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f, return ret; } -/* Checks whether the START* handler specified by f & type is missing even - * though it is required to convert the established type of an outer frame - * ("closure_type") into the established type of an inner frame (represented in - * the return closure type of this handler's attr. */ -bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, - upb_status *status) { - const void *closure_type; - const upb_handlerattr *attr; - const void *return_closure_type; - - upb_selector_t sel = handlers_getsel(h, f, type); - if (h->table[sel].func) return true; - closure_type = effective_closure_type(h, f, type); - attr = &h->table[sel].attr; - return_closure_type = attr->return_closure_type; - if (closure_type && return_closure_type && - closure_type != return_closure_type) { - return false; - } - return true; -} - static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *cache, upb_arena *arena) { diff --git a/upb/json/parser.rl b/upb/json/parser.rl index d5ffb65425..f1ea076281 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -975,6 +975,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint32(p->top->sink, parser_getsel(p), (int32_t)val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT32: { unsigned long val = strtoul(buf, &end, 0); @@ -986,6 +987,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint32(p->top->sink, parser_getsel(p), (uint32_t)val); return true; } + UPB_UNREACHABLE(); } /* XXX: We can't handle [u]int64 properly on 32-bit machines because * strto[u]ll isn't in C89. */ @@ -997,6 +999,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT64: { unsigned long val = strtoul(p->accumulated, &end, 0); @@ -1008,6 +1011,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } default: break; diff --git a/upb/json_decode.c b/upb/json_decode.c index dd8e58254f..301f16a145 100644 --- a/upb/json_decode.c +++ b/upb/json_decode.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -83,8 +84,9 @@ static bool jsondec_tryparsech(jsondec *d, char ch) { } static void jsondec_parselit(jsondec *d, const char *lit) { + size_t avail = d->end - d->ptr; size_t len = strlen(lit); - if (d->end - d->ptr < len || memcmp(d->ptr, lit, len) != 0) { + if (avail < len || memcmp(d->ptr, lit, len) != 0) { jsondec_errf(d, "Expected: '%s'", lit); } d->ptr += len; @@ -677,7 +679,7 @@ static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) { if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) { jsondec_err(d, "Integer out of range."); } - val.int32_val = val.int64_val; + val.int32_val = (int32_t)val.int64_val; } return val; @@ -713,7 +715,7 @@ static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) { if (val.uint64_val > UINT32_MAX) { jsondec_err(d, "Integer out of range."); } - val.uint32_val = val.uint64_val; + val.uint32_val = (uint32_t)val.uint64_val; } return val; @@ -960,15 +962,17 @@ static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits, const char *end = p + digits; size_t after_len = after ? strlen(after) : 0; - assert(digits <= 9); /* int can't overflow. */ + UPB_ASSERT(digits <= 9); /* int can't overflow. */ if (jsondec_buftouint64(d, p, end, &val) != end || (after_len && memcmp(end, after, after_len) != 0)) { jsondec_err(d, "Malformed timestamp"); } + UPB_ASSERT(val < INT_MAX); + *ptr = end + after_len; - return val; + return (int)val; } static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { @@ -977,7 +981,7 @@ static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { if (p != end && *p == '.') { const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos); - int digits = nano_end - p - 1; + int digits = (int)(nano_end - p - 1); int exp_lg10 = 9 - digits; if (digits > 9) { jsondec_err(d, "Too many digits for partial seconds"); @@ -986,14 +990,16 @@ static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { *ptr = nano_end; } - return nanos; + UPB_ASSERT(nanos < INT_MAX); + + return (int)nanos; } /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */ int jsondec_epochdays(int y, int m, int d) { const uint32_t year_base = 4800; /* Before min year, multiple of 400. */ const uint32_t m_adj = m - 3; /* March-based month. */ - const uint32_t carry = m_adj > m ? 1 : 0; + const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0; const uint32_t adjust = carry ? 12 : 0; const uint32_t y_adj = y + year_base - carry; const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048; @@ -1038,7 +1044,7 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) { switch (*ptr++) { case '-': neg = true; - /* Fallthrough intended. */ + /* fallthrough */ case '+': if ((end - ptr) != 5) goto malformed; ofs = jsondec_tsdigits(d, &ptr, 2, ":00"); diff --git a/upb/json_encode.c b/upb/json_encode.c index 4bd9b599d9..21f661f8f6 100644 --- a/upb/json_encode.c +++ b/upb/json_encode.c @@ -117,7 +117,7 @@ static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg, * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for * Processing Calendar Dates," Communications of the Association of * Computing Machines, vol. 11 (1968), p. 657. */ - L = (seconds / 86400) + 68569 + 2440588; + L = (int)(seconds / 86400) + 68569 + 2440588; N = 4 * L / 146097; L = L - (146097 * N + 3) / 4; I = 4000 * (L + 1) / 1461001; diff --git a/upb/msg.h b/upb/msg.h index 66f04a0e80..1ded2cf3e2 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -40,10 +40,10 @@ enum { typedef struct { uint32_t number; uint16_t offset; - int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */ + int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */ uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */ uint8_t descriptortype; - uint8_t label; + uint8_t label; /* google.protobuf.Label or _UPB_LABEL_* above. */ } upb_msglayout_field; typedef struct upb_msglayout { @@ -131,12 +131,12 @@ UPB_INLINE void _upb_clearhas_field(const upb_msg *msg, /** Oneof case access *********************************************************/ -UPB_INLINE int32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) { - return PTR_AT(msg, case_ofs, int32_t); +UPB_INLINE uint32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) { + return PTR_AT(msg, case_ofs, uint32_t); } -UPB_INLINE int32_t _upb_getoneofcase(const void *msg, size_t case_ofs) { - return *PTR_AT(msg, case_ofs, int32_t); +UPB_INLINE uint32_t _upb_getoneofcase(const void *msg, size_t case_ofs) { + return *PTR_AT(msg, case_ofs, uint32_t); } UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) { @@ -144,13 +144,13 @@ UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) { return ~(int64_t)f->presence; } -UPB_INLINE int32_t *_upb_oneofcase_field(upb_msg *msg, - const upb_msglayout_field *f) { +UPB_INLINE uint32_t *_upb_oneofcase_field(upb_msg *msg, + const upb_msglayout_field *f) { return _upb_oneofcase(msg, _upb_oneofcase_ofs(f)); } -UPB_INLINE int32_t _upb_getoneofcase_field(const upb_msg *msg, - const upb_msglayout_field *f) { +UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_msg *msg, + const upb_msglayout_field *f) { return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f)); } @@ -162,6 +162,10 @@ UPB_INLINE bool _upb_isrepeated(const upb_msglayout_field *field) { return (field->label & 3) == UPB_LABEL_REPEATED; } +UPB_INLINE bool _upb_repeated_or_map(const upb_msglayout_field *field) { + return field->label >= UPB_LABEL_REPEATED; +} + /** upb_array *****************************************************************/ /* Our internal representation for repeated fields. */ diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 709db49e96..8c4d8bf5d5 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -158,7 +158,9 @@ class upb::pb::DecoderPtr { * Setting the limit will fail if the parser is currently suspended at a depth * greater than this, or if memory allocation of the stack fails. */ size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); } - bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); } + bool set_max_nesting(size_t max) { + return upb_pbdecoder_setmaxnesting(ptr(), max); + } void Reset() { upb_pbdecoder_reset(ptr()); } diff --git a/upb/reflection.c b/upb/reflection.c index 057da629cc..487241b2b5 100644 --- a/upb/reflection.c +++ b/upb/reflection.c @@ -7,27 +7,30 @@ #include "upb/port_def.inc" -static char field_size[] = { - 0,/* 0 */ - 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */ - 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */ - 8, /* UPB_DESCRIPTOR_TYPE_INT64 */ - 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */ - 4, /* UPB_DESCRIPTOR_TYPE_INT32 */ - 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */ - 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */ - 1, /* UPB_DESCRIPTOR_TYPE_BOOL */ - sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */ - sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */ - sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */ - sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */ - 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */ - 4, /* UPB_DESCRIPTOR_TYPE_ENUM */ - 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */ - 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */ - 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */ - 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */ -}; +static size_t get_field_size(const upb_msglayout_field *f) { + static unsigned char sizes[] = { + 0,/* 0 */ + 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */ + 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */ + 8, /* UPB_DESCRIPTOR_TYPE_INT64 */ + 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */ + 4, /* UPB_DESCRIPTOR_TYPE_INT32 */ + 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */ + 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */ + 1, /* UPB_DESCRIPTOR_TYPE_BOOL */ + sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */ + sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */ + sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */ + sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */ + 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */ + 4, /* UPB_DESCRIPTOR_TYPE_ENUM */ + 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */ + 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */ + 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */ + 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */ + }; + return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype]; +} /* Strings/bytes are special-cased in maps. */ static char _upb_fieldtype_to_mapsize[12] = { @@ -59,9 +62,7 @@ static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) { const upb_msglayout_field *field = upb_fielddef_layout(f); const char *mem = UPB_PTR_AT(msg, field->offset, char); upb_msgval val = {0}; - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; - memcpy(&val, mem, size); + memcpy(&val, mem, get_field_size(field)); return val; } @@ -175,9 +176,8 @@ void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val, upb_arena *a) { const upb_msglayout_field *field = upb_fielddef_layout(f); char *mem = UPB_PTR_AT(msg, field->offset, char); - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; - memcpy(mem, &val, size); + UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */ + memcpy(mem, &val, get_field_size(field)); if (field->presence > 0) { _upb_sethas_field(msg, field); } else if (in_oneof(field)) { @@ -188,18 +188,16 @@ void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val, void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) { const upb_msglayout_field *field = upb_fielddef_layout(f); char *mem = UPB_PTR_AT(msg, field->offset, char); - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; if (field->presence > 0) { _upb_clearhas_field(msg, field); } else if (in_oneof(field)) { - int32_t *oneof_case = _upb_oneofcase_field(msg, field); + uint32_t *oneof_case = _upb_oneofcase_field(msg, field); if (*oneof_case != field->number) return; *oneof_case = 0; } - memset(mem, 0, size); + memset(mem, 0, get_field_size(field)); } void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) { @@ -212,6 +210,7 @@ bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m, size_t i = *iter; const upb_msgval zero = {0}; const upb_fielddef *f; + UPB_UNUSED(ext_pool); while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) { upb_msgval val = _upb_msg_getraw(msg, f); diff --git a/upb/table.c b/upb/table.c index 084b6a2bbf..e89f72c698 100644 --- a/upb/table.c +++ b/upb/table.c @@ -281,6 +281,7 @@ static bool streql(upb_tabkey k1, lookupkey_t k2) { } bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) { + UPB_UNUSED(ctype); /* TODO(haberman): rm */ return init(&t->t, 2, a); } @@ -473,6 +474,7 @@ bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2, } bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) { + UPB_UNUSED(ctype); /* TODO(haberman): rm */ return upb_inttable_sizedinit(t, 0, 4, a); } @@ -786,8 +788,8 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { int32_t sr; switch(align) { - case 1: t |= data[2] << 16; - case 2: t |= data[1] << 8; + case 1: t |= data[2] << 16; /* fallthrough */ + case 2: t |= data[1] << 8; /* fallthrough */ case 3: t |= data[0]; } @@ -825,9 +827,9 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { uint32_t k; switch(align) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; + case 3: d |= data[2] << 16; /* fallthrough */ + case 2: d |= data[1] << 8; /* fallthrough */ + case 1: d |= data[0]; /* fallthrough */ } k = (t >> sr) | (d << sl); @@ -840,15 +842,15 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { * Handle tail bytes */ switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; h *= m; + case 3: h ^= data[2] << 16; /* fallthrough */ + case 2: h ^= data[1] << 8; /* fallthrough */ + case 1: h ^= data[0]; h *= m; /* fallthrough */ }; } else { switch(len) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; + case 3: d |= data[2] << 16; /* fallthrough */ + case 2: d |= data[1] << 8; /* fallthrough */ + case 1: d |= data[0]; /* fallthrough */ case 0: h ^= (t >> sr) | (d << sl); h *= m; } } @@ -872,8 +874,8 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { * Handle tail bytes */ switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; + case 3: h ^= data[2] << 16; /* fallthrough */ + case 2: h ^= data[1] << 8; /* fallthrough */ case 1: h ^= data[0]; h *= m; }; diff --git a/upb/text_encode.c b/upb/text_encode.c index 3ebd9cff74..7c99228131 100644 --- a/upb/text_encode.c +++ b/upb/text_encode.c @@ -263,7 +263,7 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, tag = (uint32_t)tag_64; if ((tag & 7) == UPB_WIRE_TYPE_END_GROUP) { - CHK((tag >> 3) == groupnum); + CHK((tag >> 3) == (uint32_t)groupnum); return ptr; } @@ -295,10 +295,11 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, } case UPB_WIRE_TYPE_DELIMITED: { uint64_t len; + size_t avail = end - ptr; char *start = e->ptr; size_t start_overflow = e->overflow; CHK(ptr = txtenc_parsevarint(ptr, end, &len)); - CHK(end - ptr >= len); + CHK(avail >= len); /* Speculatively try to parse as message. */ txtenc_putstr(e, "{"); @@ -310,10 +311,12 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, txtenc_putstr(e, "}"); } else { /* Didn't work out, print as raw bytes. */ + upb_strview str; e->indent_depth--; e->ptr = start; e->overflow = start_overflow; - upb_strview str = {ptr, len}; + str.data = ptr; + str.size = len; txtenc_string(e, str, true); } ptr += len; diff --git a/upb/upb.c b/upb/upb.c index 84852f49a6..c2d406950d 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -110,10 +110,10 @@ static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) { mem_block *block = ptr; block->next = a->freelist; - block->size = size; + block->size = (uint32_t)size; block->cleanups = 0; a->freelist = block; - a->last_size = size; + a->last_size = block->size; if (!a->freelist_tail) a->freelist_tail = block; a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char); diff --git a/upb/upb.h b/upb/upb.h index 19ee059b8f..c3e1c5f3d5 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -271,7 +271,7 @@ typedef enum { UPB_DTYPE_SINT64 = 18 } upb_descriptortype_t; -#define UPB_MAP_BEGIN -1 +#define UPB_MAP_BEGIN ((size_t)-1) #include "upb/port_undef.inc" diff --git a/upbc/generator.cc b/upbc/generator.cc index af39cacd12..d5c27fb2af 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -43,7 +43,7 @@ static std::string DefSourceFilename(std::string proto_filename) { class Output { public: Output(protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {} - ~Output() { stream_->BackUp(size_); } + ~Output() { stream_->BackUp((int)size_); } template void operator()(absl::string_view format, const Arg&... arg) { @@ -922,9 +922,9 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { } bool Generator::Generate(const protobuf::FileDescriptor* file, - const std::string& parameter, + const std::string& /* parameter */, protoc::GeneratorContext* context, - std::string* error) const { + std::string* /* error */) const { Output h_output(context->Open(HeaderFilename(file->name()))); WriteHeader(file, h_output);