From 93e2a408817a8b00a922aae42e32ccf9ee7a9e25 Mon Sep 17 00:00:00 2001 From: Charlie Savage Date: Mon, 18 May 2020 09:43:09 -0700 Subject: [PATCH] MSVC 2019 Fixes (#285) * resolvename is declared to return a bool value, but instead can return NULL. MSVC 2019 does not like that an throws a compile error. Fixed by returning false instead of NULL. * When compiling with MSVC 2019, the UPB_ASSUME macro expands out to: do {} if (false && (ok)) That isn't valid C code. Fixed by adding an elif for MSVC that uses __assume(0), which is similar to gcc's __builtin_unreachable according to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0627r0.pdf. --- upb/def.c | 2 +- upb/port_def.inc | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/upb/def.c b/upb/def.c index d8f4cf92cb..a477e2458d 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1191,7 +1191,7 @@ static bool resolvename(const upb_strtable *t, const upb_fielddef *f, const char *base, upb_strview sym, upb_deftype_t type, upb_status *status, const void **def) { - if(sym.size == 0) return NULL; + if(sym.size == 0) return false; if(sym.data[0] == '.') { /* Symbols starting with '.' are absolute, so we do a single lookup. * Slice to omit the leading '.' */ diff --git a/upb/port_def.inc b/upb/port_def.inc index 73747d791d..61675d37c0 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -138,6 +138,8 @@ int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg); #ifdef NDEBUG #ifdef __GNUC__ #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable() +#elif defined _MSC_VER +#define UPB_ASSUME(expr) if (!(expr)) __assume(0) #else #define UPB_ASSUME(expr) do {} if (false && (expr)) #endif