Drop C89/C90 support and MSVC prior to Visual Studio 2015.

upb previously attempted to support C89 and pre-2015 versions
of Visual Studio. This was to support older compilers with
limited C99 support (particularly MSVC). But as of last August,
even gRPC has dropped support for MSVC prior to 2015
c87276d058

Therefore it seems safe for upb to no longer attempt C89 support
(we were already not truly C89 compliant, with our use of "bool").
We now explicitly require C99 or greater and MSVC 2015 or greater.

This cleaned up port_def.inc a fair bit. I took the chance to
also remove some obsolete macros.
pull/13171/head
Joshua Haberman 4 years ago
parent 0756999ab6
commit 8f3ee80d46
  1. 1
      BUILD
  2. 2
      cmake/BUILD
  3. 1
      cmake/CMakeLists.txt
  4. 1
      cmake/make_cmakelists.py
  5. 2
      cmake/upb/json/parser.c
  6. 2
      tests/pb/test_decoder.cc
  7. 2
      upb/json/parser.rl
  8. 30
      upb/json/printer.c
  9. 9
      upb/json_decode.c
  10. 10
      upb/json_encode.c
  11. 4
      upb/pb/textprinter.c
  12. 4
      upb/pb/varint.int.h
  13. 75
      upb/port_def.inc
  14. 7
      upb/port_undef.inc
  15. 2
      upb/text_encode.c
  16. 4
      upb/upb.c

@ -250,6 +250,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 #################################################################

@ -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 $< $@",
)

@ -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 (!!).

@ -166,6 +166,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

@ -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;

@ -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)

@ -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;

@ -7,7 +7,9 @@
#include <ctype.h>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@ -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--) {

@ -5,6 +5,7 @@
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
@ -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");
}

@ -4,14 +4,16 @@
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#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\"");

@ -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). */

@ -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;
}

@ -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 <stdint.h>
#include <stddef.h>
@ -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 <stdio.h>
#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 <stdarg.h>
#include <stdio.h>
#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 <math.h>
#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

@ -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

@ -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)) {

@ -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';
}

Loading…
Cancel
Save