From f179e23e5b9f502cce9a2b9fa6a918d1a689758b Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Mon, 29 Jul 2019 18:12:55 -0700 Subject: Support Visual Studio --- upb/port_def.inc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'upb') diff --git a/upb/port_def.inc b/upb/port_def.inc index 5220c29..05c4ebc 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -75,8 +75,13 @@ /* Windows versions */ #include #include -#define _upb_snprintf _snprintf_s -#define _upb_vsnprintf _vsnprintf_s +#if defined(_MSC_VER) && _MSC_VER < 1900 +#define _upb_snprintf _snprintf +#define _upb_vsnprintf _vsnprintf +#else +#define _upb_snprintf snprintf +#define _upb_vsnprintf vsnprintf +#endif #define _upb_va_copy(a, b) va_copy(a, b) #else #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L -- cgit v1.2.3 From 7a1e6aa84ba857d2e3cdc6e5fd50e3bda210c164 Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Tue, 30 Jul 2019 15:43:50 -0700 Subject: Support MSVC prior to 2015 --- BUILD | 1 + CMakeLists.txt | 1 + generated_for_cmake/upb/json/parser.c | 2 +- upb/json/parser.rl | 2 +- upb/json/printer.c | 4 ++-- upb/port.c | 27 +++++++++++++++++++++++++++ upb/port_def.inc | 15 +++++++++++++-- upb/port_undef.inc | 2 ++ 8 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 upb/port.c (limited to 'upb') diff --git a/BUILD b/BUILD index 8c20d70..c55e224 100644 --- a/BUILD +++ b/BUILD @@ -62,6 +62,7 @@ cc_library( "upb/generated_util.h", "upb/msg.c", "upb/msg.h", + "upb/port.c", "upb/port_def.inc", "upb/port_undef.inc", "upb/table.c", diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c45df..836c5ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_library(upb upb/generated_util.h upb/msg.c upb/msg.h + upb/port.c upb/port_def.inc upb/port_undef.inc upb/table.c diff --git a/generated_for_cmake/upb/json/parser.c b/generated_for_cmake/upb/json/parser.c index a1bd0d2..8af0916 100644 --- a/generated_for_cmake/upb/json/parser.c +++ b/generated_for_cmake/upb/json/parser.c @@ -949,7 +949,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 = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ + double inf = UPB_INFINITY; errno = 0; diff --git a/upb/json/parser.rl b/upb/json/parser.rl index fd3704c..f80ed18 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -947,7 +947,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 = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */ + double inf = UPB_INFINITY; errno = 0; diff --git a/upb/json/printer.c b/upb/json/printer.c index 90705a7..38f817d 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -181,11 +181,11 @@ const char neginf[] = "\"-Infinity\""; const char inf[] = "\"Infinity\""; static size_t fmt_double(double val, char* buf, size_t length) { - if (val == (1.0 / 0.0)) { + if (val == UPB_INFINITY) { CHKLENGTH(length >= strlen(inf)); strcpy(buf, inf); return strlen(inf); - } else if (val == (-1.0 / 0.0)) { + } else if (val == -UPB_INFINITY) { CHKLENGTH(length >= strlen(neginf)); strcpy(buf, neginf); return strlen(neginf); diff --git a/upb/port.c b/upb/port.c new file mode 100644 index 0000000..023f7dc --- /dev/null +++ b/upb/port.c @@ -0,0 +1,27 @@ + +#include "upb/upb.h" +#include "upb/port_def.inc" + +#ifdef UPB_MSVC_VSNPRINTF +/* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and + * vsnprintf. To support them, missing functions are manually implemented + * using the existing secure functions. */ +int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) { + if (!s) { + return _vscprintf(format, arg); + } + int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg); + if (ret < 0) { + ret = _vscprintf(format, arg); + } + return ret; +} + +int msvc_snprintf(char* s, size_t n, const char* format, ...) { + va_list arg; + va_start(arg, format); + int ret = msvc_vsnprintf(s, n, format, arg); + va_end(arg); + return ret; +} +#endif diff --git a/upb/port_def.inc b/upb/port_def.inc index 05c4ebc..0650920 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -76,8 +76,11 @@ #include #include #if defined(_MSC_VER) && _MSC_VER < 1900 -#define _upb_snprintf _snprintf -#define _upb_vsnprintf _vsnprintf +int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg); +int msvc_snprintf(char* s, size_t n, const char* format, ...); +#define UPB_MSVC_VSNPRINTF +#define _upb_snprintf msvc_snprintf +#define _upb_vsnprintf msvc_vsnprintf #else #define _upb_snprintf snprintf #define _upb_vsnprintf vsnprintf @@ -135,3 +138,11 @@ #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 diff --git a/upb/port_undef.inc b/upb/port_undef.inc index bfd2a08..103180b 100644 --- a/upb/port_undef.inc +++ b/upb/port_undef.inc @@ -14,6 +14,8 @@ #undef UPB_ASSERT #undef UPB_ASSERT_DEBUGVAR #undef UPB_UNREACHABLE +#undef UPB_INFINITY +#undef UPB_MSVC_VSNPRINTF #undef _upb_snprintf #undef _upb_vsnprintf #undef _upb_va_copy -- cgit v1.2.3