diff --git a/BUILD b/BUILD index 6c9e9a1615..8d5761b80d 100644 --- a/BUILD +++ b/BUILD @@ -50,6 +50,7 @@ cc_library( srcs = [ "upb/port.c", ], + copts = UPB_DEFAULT_COPTS, textual_hdrs = [ "upb/port_def.inc", "upb/port_undef.inc", @@ -93,7 +94,7 @@ cc_library( "upb/msg.h", "upb/upb.int.h", ], - copts = ["-std=gnu99"], + copts = UPB_DEFAULT_COPTS, deps = [ ":port", ":table", @@ -281,6 +282,7 @@ genrule( outs = ["upb/json/parser.c"], cmd = "$(location @ragel//:ragelc) -C -o upb/json/parser.c $< && mv upb/json/parser.c $@", tools = ["@ragel//:ragelc"], + visibility = ["//cmake:__pkg__"], ) # Amalgamation ################################################################# @@ -313,6 +315,7 @@ cc_library( name = "amalgamation", srcs = ["upb.c"], hdrs = ["upb.h"], + copts = UPB_DEFAULT_COPTS, ) upb_amalgamation( @@ -338,6 +341,7 @@ cc_library( name = "php_amalgamation", srcs = ["php-upb.c"], hdrs = ["php-upb.h"], + copts = UPB_DEFAULT_COPTS, ) upb_amalgamation( @@ -362,6 +366,7 @@ cc_library( name = "ruby_amalgamation", srcs = ["ruby-upb.c"], hdrs = ["ruby-upb.h"], + copts = UPB_DEFAULT_COPTS, ) exports_files( diff --git a/bazel/build_defs.bzl b/bazel/build_defs.bzl index d721d0060c..a199243947 100644 --- a/bazel/build_defs.bzl +++ b/bazel/build_defs.bzl @@ -19,6 +19,8 @@ UPB_DEFAULT_COPTS = select({ "//conditions:default": [ # copybara:strip_for_google3_begin "-std=c99", + "-pedantic", + "-Werror=pedantic", "-Wstrict-prototypes", # copybara:strip_end ], diff --git a/cmake/BUILD b/cmake/BUILD index 76601b0e0f..d59475d909 100644 --- a/cmake/BUILD +++ b/cmake/BUILD @@ -37,7 +37,7 @@ genrule( genrule( name = "copy_json_ragel", - srcs = ["upb/json/parser.c"], + srcs = ["//:upb/json/parser.c"], outs = ["generated-in/upb/json/parser.c"], cmd = "cp $< $@", ) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 8199fe39c1..fcdce9936a 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -12,6 +12,7 @@ cmake_minimum_required (VERSION 3.0) cmake_policy(SET CMP0048 NEW) project(upb) +set(CMAKE_C_STANDARD 99) # Prevent CMake from setting -rdynamic on Linux (!!). diff --git a/cmake/google/protobuf/descriptor.upb.c b/cmake/google/protobuf/descriptor.upb.c index 7225c146ff..339fafac09 100644 --- a/cmake/google/protobuf/descriptor.upb.c +++ b/cmake/google/protobuf/descriptor.upb.c @@ -24,8 +24,6 @@ const upb_msglayout google_protobuf_FileDescriptorSet_msginit = { &google_protobuf_FileDescriptorSet_submsgs[0], &google_protobuf_FileDescriptorSet__fields[0], UPB_SIZE(8, 8), 1, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = { @@ -56,8 +54,6 @@ const upb_msglayout google_protobuf_FileDescriptorProto_msginit = { &google_protobuf_FileDescriptorProto_submsgs[0], &google_protobuf_FileDescriptorProto__fields[0], UPB_SIZE(64, 128), 12, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[7] = { @@ -87,8 +83,6 @@ const upb_msglayout google_protobuf_DescriptorProto_msginit = { &google_protobuf_DescriptorProto_submsgs[0], &google_protobuf_DescriptorProto__fields[0], UPB_SIZE(48, 96), 10, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { @@ -105,8 +99,6 @@ const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = { &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0], &google_protobuf_DescriptorProto_ExtensionRange__fields[0], UPB_SIZE(16, 24), 3, false, 255, - { - }, }; static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = { @@ -118,8 +110,6 @@ const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = { NULL, &google_protobuf_DescriptorProto_ReservedRange__fields[0], UPB_SIZE(16, 16), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = { @@ -134,8 +124,6 @@ const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = { &google_protobuf_ExtensionRangeOptions_submsgs[0], &google_protobuf_ExtensionRangeOptions__fields[0], UPB_SIZE(8, 8), 1, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = { @@ -160,8 +148,6 @@ const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = { &google_protobuf_FieldDescriptorProto_submsgs[0], &google_protobuf_FieldDescriptorProto__fields[0], UPB_SIZE(72, 112), 11, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = { @@ -177,8 +163,6 @@ const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = { &google_protobuf_OneofDescriptorProto_submsgs[0], &google_protobuf_OneofDescriptorProto__fields[0], UPB_SIZE(16, 32), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = { @@ -199,8 +183,6 @@ const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = { &google_protobuf_EnumDescriptorProto_submsgs[0], &google_protobuf_EnumDescriptorProto__fields[0], UPB_SIZE(32, 64), 5, false, 255, - { - }, }; static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = { @@ -212,8 +194,6 @@ const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msgini NULL, &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0], UPB_SIZE(16, 16), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = { @@ -230,8 +210,6 @@ const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = { &google_protobuf_EnumValueDescriptorProto_submsgs[0], &google_protobuf_EnumValueDescriptorProto__fields[0], UPB_SIZE(24, 32), 3, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = { @@ -249,8 +227,6 @@ const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = { &google_protobuf_ServiceDescriptorProto_submsgs[0], &google_protobuf_ServiceDescriptorProto__fields[0], UPB_SIZE(24, 48), 3, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = { @@ -270,8 +246,6 @@ const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = { &google_protobuf_MethodDescriptorProto_submsgs[0], &google_protobuf_MethodDescriptorProto__fields[0], UPB_SIZE(32, 64), 6, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = { @@ -306,8 +280,6 @@ const upb_msglayout google_protobuf_FileOptions_msginit = { &google_protobuf_FileOptions_submsgs[0], &google_protobuf_FileOptions__fields[0], UPB_SIZE(104, 192), 21, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = { @@ -326,8 +298,6 @@ const upb_msglayout google_protobuf_MessageOptions_msginit = { &google_protobuf_MessageOptions_submsgs[0], &google_protobuf_MessageOptions__fields[0], UPB_SIZE(16, 16), 5, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = { @@ -348,8 +318,6 @@ const upb_msglayout google_protobuf_FieldOptions_msginit = { &google_protobuf_FieldOptions_submsgs[0], &google_protobuf_FieldOptions__fields[0], UPB_SIZE(24, 24), 7, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = { @@ -364,8 +332,6 @@ const upb_msglayout google_protobuf_OneofOptions_msginit = { &google_protobuf_OneofOptions_submsgs[0], &google_protobuf_OneofOptions__fields[0], UPB_SIZE(8, 8), 1, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = { @@ -382,8 +348,6 @@ const upb_msglayout google_protobuf_EnumOptions_msginit = { &google_protobuf_EnumOptions_submsgs[0], &google_protobuf_EnumOptions__fields[0], UPB_SIZE(8, 16), 3, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = { @@ -399,8 +363,6 @@ const upb_msglayout google_protobuf_EnumValueOptions_msginit = { &google_protobuf_EnumValueOptions_submsgs[0], &google_protobuf_EnumValueOptions__fields[0], UPB_SIZE(8, 16), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = { @@ -416,8 +378,6 @@ const upb_msglayout google_protobuf_ServiceOptions_msginit = { &google_protobuf_ServiceOptions_submsgs[0], &google_protobuf_ServiceOptions__fields[0], UPB_SIZE(8, 16), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = { @@ -434,8 +394,6 @@ const upb_msglayout google_protobuf_MethodOptions_msginit = { &google_protobuf_MethodOptions_submsgs[0], &google_protobuf_MethodOptions__fields[0], UPB_SIZE(16, 24), 3, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = { @@ -456,8 +414,6 @@ const upb_msglayout google_protobuf_UninterpretedOption_msginit = { &google_protobuf_UninterpretedOption_submsgs[0], &google_protobuf_UninterpretedOption__fields[0], UPB_SIZE(64, 96), 7, false, 255, - { - }, }; static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = { @@ -469,8 +425,6 @@ const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = { NULL, &google_protobuf_UninterpretedOption_NamePart__fields[0], UPB_SIZE(16, 32), 2, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = { @@ -485,8 +439,6 @@ const upb_msglayout google_protobuf_SourceCodeInfo_msginit = { &google_protobuf_SourceCodeInfo_submsgs[0], &google_protobuf_SourceCodeInfo__fields[0], UPB_SIZE(8, 8), 1, false, 255, - { - }, }; static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = { @@ -501,8 +453,6 @@ const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = { NULL, &google_protobuf_SourceCodeInfo_Location__fields[0], UPB_SIZE(32, 64), 5, false, 255, - { - }, }; static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = { @@ -517,8 +467,6 @@ const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = { &google_protobuf_GeneratedCodeInfo_submsgs[0], &google_protobuf_GeneratedCodeInfo__fields[0], UPB_SIZE(8, 8), 1, false, 255, - { - }, }; static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { @@ -532,8 +480,6 @@ const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = { NULL, &google_protobuf_GeneratedCodeInfo_Annotation__fields[0], UPB_SIZE(24, 48), 4, false, 255, - { - }, }; #include "upb/port_undef.inc" diff --git a/cmake/make_cmakelists.py b/cmake/make_cmakelists.py index 1dafbbda5b..d63740439b 100755 --- a/cmake/make_cmakelists.py +++ b/cmake/make_cmakelists.py @@ -172,6 +172,7 @@ class WorkspaceFileFunctions(object): def workspace(self, **kwargs): self.converter.prelude += "project(%s)\n" % (kwargs["name"]) + self.converter.prelude += "set(CMAKE_C_STANDARD 99)\n" def http_archive(self, **kwargs): pass diff --git a/cmake/upb/json/parser.c b/cmake/upb/json/parser.c index 7cdb4de83b..75cacd4c1d 100644 --- a/cmake/upb/json/parser.c +++ b/cmake/upb/json/parser.c @@ -953,7 +953,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_fieldtype_t type = upb_fielddef_type(p->top->f); double val; double dummy; - double inf = UPB_INFINITY; + double inf = INFINITY; errno = 0; diff --git a/examples/bazel/BUILD b/examples/bazel/BUILD index 550115b21d..97119252e2 100644 --- a/examples/bazel/BUILD +++ b/examples/bazel/BUILD @@ -16,5 +16,6 @@ upb_proto_library( cc_binary( name = "test_binary", srcs = ["test_binary.c"], + copts = ["-std=c99"], deps = [":foo_upbproto"], ) diff --git a/tests/BUILD b/tests/BUILD index 998023e96d..ad12fce14a 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -50,6 +50,7 @@ upb_proto_library( cc_test( name = "test_generated_code", srcs = ["test_generated_code.c"], + copts = UPB_DEFAULT_COPTS, deps = [ ":empty_upbdefs_proto", ":test_messages_proto3_proto_upb", diff --git a/tests/bindings/lua/BUILD b/tests/bindings/lua/BUILD index ba4918d415..8834feb8ff 100644 --- a/tests/bindings/lua/BUILD +++ b/tests/bindings/lua/BUILD @@ -2,12 +2,17 @@ load( "//upb/bindings/lua:lua_proto_library.bzl", "lua_proto_library", ) +load( + "//bazel:build_defs.bzl", + "UPB_DEFAULT_COPTS", +) licenses(["notice"]) cc_test( name = "test_lua", srcs = ["main.c"], + copts = UPB_DEFAULT_COPTS, data = [ "test_upb.lua", ":descriptor_proto_lua", diff --git a/tests/bindings/lua/main.c b/tests/bindings/lua/main.c index f3714b5126..6e48ccaf24 100644 --- a/tests/bindings/lua/main.c +++ b/tests/bindings/lua/main.c @@ -34,7 +34,7 @@ const char *init = "upb/bindings/lua/?.lua" "'"; -int main() { +int main(int argc, char **argv) { int ret = 0; L = luaL_newstate(); luaL_openlibs(L); diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 32eec748e7..e8b3a63ed5 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -113,7 +113,7 @@ using std::string; void vappendf(string* str, const char *format, va_list args) { va_list copy; - _upb_va_copy(copy, args); + va_copy(copy, args); int count = vsnprintf(NULL, 0, format, args); if (count >= 0) diff --git a/tests/test_generated_code.c b/tests/test_generated_code.c index 98f224372e..ff7266f9d7 100644 --- a/tests/test_generated_code.c +++ b/tests/test_generated_code.c @@ -24,7 +24,7 @@ const int32_t test_int32_2 = -20; const int32_t test_int32_3 = 30; const int32_t test_int32_4 = -40; -static void test_scalars() { +static void test_scalars(void) { upb_arena *arena = upb_arena_new(); protobuf_test_messages_proto3_TestAllTypesProto3 *msg = protobuf_test_messages_proto3_TestAllTypesProto3_new(arena); @@ -117,7 +117,7 @@ static void check_string_map_one_entry( ASSERT(!const_ent); } -static void test_string_double_map() { +static void test_string_double_map(void) { upb_arena *arena = upb_arena_new(); upb_strview serialized; upb_test_MapTest *msg = upb_test_MapTest_new(arena); @@ -141,7 +141,7 @@ static void test_string_double_map() { upb_arena_free(arena); } -static void test_string_map() { +static void test_string_map(void) { upb_arena *arena = upb_arena_new(); protobuf_test_messages_proto3_TestAllTypesProto3 *msg = protobuf_test_messages_proto3_TestAllTypesProto3_new(arena); @@ -259,7 +259,7 @@ static void check_int32_map_one_entry( ASSERT(!const_ent); } -static void test_int32_map() { +static void test_int32_map(void) { upb_arena *arena = upb_arena_new(); protobuf_test_messages_proto3_TestAllTypesProto3 *msg = protobuf_test_messages_proto3_TestAllTypesProto3_new(arena); @@ -328,7 +328,7 @@ static void test_int32_map() { upb_arena_free(arena); } -void test_repeated() { +void test_repeated(void) { upb_arena *arena = upb_arena_new(); protobuf_test_messages_proto3_TestAllTypesProto3 *msg = protobuf_test_messages_proto3_TestAllTypesProto3_new(arena); @@ -347,7 +347,7 @@ void test_repeated() { upb_arena_free(arena); } -void test_null_decode_buf() { +void test_null_decode_buf(void) { upb_arena *arena = upb_arena_new(); protobuf_test_messages_proto3_TestAllTypesProto3 *msg = protobuf_test_messages_proto3_TestAllTypesProto3_parse(NULL, 0, arena); @@ -359,7 +359,7 @@ void test_null_decode_buf() { upb_arena_free(arena); } -void test_status_truncation() { +void test_status_truncation(void) { int i, j; upb_status status; upb_status status2; diff --git a/upb/json/parser.rl b/upb/json/parser.rl index d7dcc54a8b..ed7eead408 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -951,7 +951,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_fieldtype_t type = upb_fielddef_type(p->top->f); double val; double dummy; - double inf = UPB_INFINITY; + double inf = INFINITY; errno = 0; diff --git a/upb/json/printer.c b/upb/json/printer.c index b6d53a9216..2f0c8fb6c4 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -7,7 +7,9 @@ #include #include +#include #include +#include #include #include @@ -139,7 +141,7 @@ static void putstring(upb_json_printer *p, const char *buf, size_t len) { char escape_buf[8]; if (!escape) { unsigned char byte = (unsigned char)c; - _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte); + snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte); escape = escape_buf; } @@ -178,53 +180,53 @@ const char neginf[] = "\"-Infinity\""; const char inf[] = "\"Infinity\""; static size_t fmt_double(double val, char* buf, size_t length) { - if (val == UPB_INFINITY) { + if (val == INFINITY) { CHKLENGTH(length >= strlen(inf)); strcpy(buf, inf); return strlen(inf); - } else if (val == -UPB_INFINITY) { + } else if (val == -INFINITY) { CHKLENGTH(length >= strlen(neginf)); strcpy(buf, neginf); return strlen(neginf); } else { - size_t n = _upb_snprintf(buf, length, "%.17g", val); + size_t n = snprintf(buf, length, "%.17g", val); CHKLENGTH(n > 0 && n < length); return n; } } static size_t fmt_float(float val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%.8g", val); + size_t n = snprintf(buf, length, "%.8g", val); CHKLENGTH(n > 0 && n < length); return n; } static size_t fmt_bool(bool val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false")); + size_t n = snprintf(buf, length, "%s", (val ? "true" : "false")); CHKLENGTH(n > 0 && n < length); return n; } static size_t fmt_int64_as_number(int64_t val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%" PRId64, val); + size_t n = snprintf(buf, length, "%" PRId64, val); CHKLENGTH(n > 0 && n < length); return n; } static size_t fmt_uint64_as_number(uint64_t val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%" PRIu64, val); + size_t n = snprintf(buf, length, "%" PRIu64, val); CHKLENGTH(n > 0 && n < length); return n; } static size_t fmt_int64_as_string(int64_t val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "\"%" PRId64 "\"", val); + size_t n = snprintf(buf, length, "\"%" PRId64 "\"", val); CHKLENGTH(n > 0 && n < length); return n; } static size_t fmt_uint64_as_string(uint64_t val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "\"%" PRIu64 "\"", val); + size_t n = snprintf(buf, length, "\"%" PRIu64 "\"", val); CHKLENGTH(n > 0 && n < length); return n; } @@ -870,12 +872,12 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data, return false; } - _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds); + snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds); base_len = strlen(buffer); if (p->nanos != 0) { char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3]; - _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", p->nanos / 1000000000.0); /* Remove trailing 0. */ for (i = UPB_DURATION_MAX_NANO_LEN + 2; @@ -949,8 +951,8 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data, "%Y-%m-%dT%H:%M:%S", gmtime(&time)); if (p->nanos != 0) { char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3]; - _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", - p->nanos / 1000000000.0); + snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f", + p->nanos / 1000000000.0); /* Remove trailing 0. */ for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2; nanos_buffer[i] == '0'; i--) { diff --git a/upb/json_decode.c b/upb/json_decode.c index 1b8cac8ff3..fb53c3de35 100644 --- a/upb/json_decode.c +++ b/upb/json_decode.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -747,11 +748,11 @@ static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) { case JD_STRING: str = jsondec_string(d); if (jsondec_streql(str, "NaN")) { - val.double_val = UPB_NAN; + val.double_val = NAN; } else if (jsondec_streql(str, "Infinity")) { - val.double_val = UPB_INFINITY; + val.double_val = INFINITY; } else if (jsondec_streql(str, "-Infinity")) { - val.double_val = -UPB_INFINITY; + val.double_val = -INFINITY; } else { val.double_val = strtod(str.data, NULL); } @@ -761,7 +762,7 @@ static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) { } if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) { - if (val.double_val != UPB_INFINITY && val.double_val != -UPB_INFINITY && + if (val.double_val != INFINITY && val.double_val != -INFINITY && (val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) { jsondec_err(d, "Float out of range"); } diff --git a/upb/json_encode.c b/upb/json_encode.c index 587772d12e..628d1c194a 100644 --- a/upb/json_encode.c +++ b/upb/json_encode.c @@ -4,14 +4,16 @@ #include #include #include +#include +#include #include #include #include -#include #include "upb/decode.h" #include "upb/reflection.h" +/* Must be last. */ #include "upb/port_def.inc" typedef struct { @@ -76,7 +78,7 @@ static void jsonenc_printf(jsonenc *e, const char *fmt, ...) { va_list args; va_start(args, fmt); - n = _upb_vsnprintf(e->ptr, have, fmt, args); + n = vsnprintf(e->ptr, have, fmt, args); va_end(args); if (UPB_LIKELY(have > n)) { @@ -268,9 +270,9 @@ static void jsonenc_string(jsonenc *e, upb_strview str) { } static void jsonenc_double(jsonenc *e, const char *fmt, double val) { - if (val == UPB_INFINITY) { + if (val == INFINITY) { jsonenc_putstr(e, "\"Infinity\""); - } else if (val == -UPB_INFINITY) { + } else if (val == -INFINITY) { jsonenc_putstr(e, "\"-Infinity\""); } else if (val != val) { jsonenc_putstr(e, "\"NaN\""); diff --git a/upb/msg.h b/upb/msg.h index 4f4185ffde..587e895db9 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -67,13 +67,9 @@ typedef struct upb_msglayout { uint16_t field_count; bool extendable; uint8_t table_mask; -#if __STDC_VERSION__ >= 199901L /* To constant-initialize the tables of variable length, we need a flexible * array member, and we need to compile in C99 mode. */ _upb_fasttable_entry fasttable[]; -#else - _upb_fasttable_entry fasttable[1]; -#endif } upb_msglayout; /** upb_msg *******************************************************************/ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index 1d4acb1c07..1331268a06 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -105,8 +105,8 @@ bool putf(upb_textprinter *p, const char *fmt, ...) { va_start(args, fmt); /* Run once to get the length of the string. */ - _upb_va_copy(args_copy, args); - len = _upb_vsnprintf(NULL, 0, fmt, args_copy); + va_copy(args_copy, args); + len = vsnprintf(NULL, 0, fmt, args_copy); va_end(args_copy); /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */ diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index 8ab0d9f141..9b98a81c76 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -150,9 +150,7 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) { uint64_t ret = 0; UPB_ASSERT(bytes <= 5); memcpy(&ret, buf, bytes); -#ifdef UPB_BIG_ENDIAN - ret = byteswap64(ret); -#endif + ret = _upb_be_swap64(ret); UPB_ASSERT(ret <= 0xffffffffffU); return ret; } diff --git a/upb/port_def.inc b/upb/port_def.inc index 2dafc3d99e..0d25c90038 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -20,6 +20,15 @@ * * This file is private and must not be included by users! */ + +#if !(__STDC_VERSION__ >= 199901L || __cplusplus >= 201103L) +#error upb requires C99 or C++11 +#endif + +#if (defined(_MSC_VER) && _MSC_VER < 1900) +#error upb requires MSVC >= 2015. +#endif + #include #include @@ -68,12 +77,6 @@ #define UPB_UNLIKELY(x) (x) #endif -/* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler - * doesn't provide these preprocessor symbols. */ -#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -#define UPB_BIG_ENDIAN -#endif - /* Macros for function attributes on compilers that support them. */ #ifdef __GNUC__ #define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) @@ -89,49 +92,6 @@ #define UPB_NORETURN #endif -#if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L -/* C99/C++11 versions. */ -#include -#define _upb_snprintf snprintf -#define _upb_vsnprintf vsnprintf -#define _upb_va_copy(a, b) va_copy(a, b) -#elif defined(_MSC_VER) -/* Microsoft C/C++ versions. */ -#include -#include -#if _MSC_VER < 1900 -int msvc_snprintf(char* s, size_t n, const char* format, ...); -int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg); -#define UPB_MSVC_VSNPRINTF -#define _upb_snprintf msvc_snprintf -#define _upb_vsnprintf msvc_vsnprintf -#else -#define _upb_snprintf snprintf -#define _upb_vsnprintf vsnprintf -#endif -#define _upb_va_copy(a, b) va_copy(a, b) -#elif defined __GNUC__ -/* A few hacky workarounds for functions not in C89. - * For internal use only! - * TODO(haberman): fix these by including our own implementations, or finding - * another workaround. - */ -#define _upb_snprintf __builtin_snprintf -#define _upb_vsnprintf __builtin_vsnprintf -#define _upb_va_copy(a, b) __va_copy(a, b) -#else -#error Need implementations of [v]snprintf and va_copy -#endif - -#ifdef __cplusplus -#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \ - (defined(_MSC_VER) && _MSC_VER >= 1900) -/* C++11 is present */ -#else -#error upb requires C++11 for C++ support -#endif -#endif - #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -159,29 +119,12 @@ int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg); #define UPB_ASSERT(expr) assert(expr) #endif -/* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only - * exist in debug mode. This turns into regular assert. */ -#define UPB_ASSERT_DEBUGVAR(expr) assert(expr) - #if defined(__GNUC__) || defined(__clang__) #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0) #else #define UPB_UNREACHABLE() do { assert(0); } while(0) #endif -/* UPB_INFINITY representing floating-point positive infinity. */ -#include -#ifdef INFINITY -#define UPB_INFINITY INFINITY -#else -#define UPB_INFINITY (1.0 / 0.0) -#endif -#ifdef NAN -#define UPB_NAN NAN -#else -#define UPB_NAN (0.0 / 0.0) -#endif - #if defined(__SANITIZE_ADDRESS__) #define UPB_ASAN 1 #ifdef __cplusplus diff --git a/upb/port_undef.inc b/upb/port_undef.inc index 5a11ca6397..b7be52cc98 100644 --- a/upb/port_undef.inc +++ b/upb/port_undef.inc @@ -18,14 +18,7 @@ #undef UPB_UNUSED #undef UPB_ASSUME #undef UPB_ASSERT -#undef UPB_ASSERT_DEBUGVAR #undef UPB_UNREACHABLE -#undef UPB_INFINITY -#undef UPB_NAN -#undef UPB_MSVC_VSNPRINTF -#undef _upb_snprintf -#undef _upb_vsnprintf -#undef _upb_va_copy #undef UPB_POISON_MEMORY_REGION #undef UPB_UNPOISON_MEMORY_REGION #undef UPB_ASAN diff --git a/upb/text_encode.c b/upb/text_encode.c index 7c99228131..3e9630d8cd 100644 --- a/upb/text_encode.c +++ b/upb/text_encode.c @@ -43,7 +43,7 @@ static void txtenc_printf(txtenc *e, const char *fmt, ...) { va_list args; va_start(args, fmt); - n = _upb_vsnprintf(e->ptr, have, fmt, args); + n = vsnprintf(e->ptr, have, fmt, args); va_end(args); if (UPB_LIKELY(have > n)) { diff --git a/upb/upb.c b/upb/upb.c index e3acbda0c5..8e6ad77142 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -40,7 +40,7 @@ void upb_status_seterrf(upb_status *status, const char *fmt, ...) { void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) { if (!status) return; status->ok = false; - _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args); + vsnprintf(status->msg, sizeof(status->msg), fmt, args); status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0'; } @@ -49,7 +49,7 @@ void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) { if (!status) return; status->ok = false; len = strlen(status->msg); - _upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args); + vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args); status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0'; } diff --git a/upbc/generator.cc b/upbc/generator.cc index ff59f2ce30..642498adfd 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -1036,13 +1036,14 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output, "false", // TODO: extendable table_mask ); - output(" {\n"); - for (const auto& ent : table) { - output(" {0x$1, &$0},\n", ent.first, - absl::StrCat(absl::Hex(ent.second, absl::kZeroPad16))); + if (!table.empty()) { + output(" {\n"); + for (const auto& ent : table) { + output(" {0x$1, &$0},\n", ent.first, + absl::StrCat(absl::Hex(ent.second, absl::kZeroPad16))); + } + output(" },\n"); } - output(" },\n"); - output("};\n\n"); }