You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_util.h 4.1 kB

Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * $Id: json_util.h,v 1.4 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. /**
  12. * @file
  13. * @brief Miscllaneous utility functions and macros.
  14. */
  15. #ifndef _json_util_h_
  16. #define _json_util_h_
  17. #include "json_object.h"
  18. #ifndef json_min
  19. #define json_min(a, b) ((a) < (b) ? (a) : (b))
  20. #endif
  21. #ifndef json_max
  22. #define json_max(a, b) ((a) > (b) ? (a) : (b))
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #define JSON_FILE_BUF_SIZE 4096
  28. /* utility functions */
  29. /**
  30. * Read the full contents of the given file, then convert it to a
  31. * json_object using json_tokener_parse().
  32. *
  33. * Returns NULL on failure. See json_util_get_last_err() for details.
  34. */
  35. JSON_EXPORT struct json_object *json_object_from_file(const char *filename);
  36. /**
  37. * Create a JSON object from already opened file descriptor.
  38. *
  39. * This function can be helpful, when you opened the file already,
  40. * e.g. when you have a temp file.
  41. * Note, that the fd must be readable at the actual position, i.e.
  42. * use lseek(fd, 0, SEEK_SET) before.
  43. *
  44. * The depth argument specifies the maximum object depth to pass to
  45. * json_tokener_new_ex(). When depth == -1, JSON_TOKENER_DEFAULT_DEPTH
  46. * is used instead.
  47. *
  48. * Returns NULL on failure. See json_util_get_last_err() for details.
  49. */
  50. JSON_EXPORT struct json_object *json_object_from_fd_ex(int fd, int depth);
  51. /**
  52. * Create a JSON object from an already opened file descriptor, using
  53. * the default maximum object depth. (JSON_TOKENER_DEFAULT_DEPTH)
  54. *
  55. * See json_object_from_fd_ex() for details.
  56. */
  57. JSON_EXPORT struct json_object *json_object_from_fd(int fd);
  58. /**
  59. * Equivalent to:
  60. * json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  61. *
  62. * Returns -1 if something fails. See json_util_get_last_err() for details.
  63. */
  64. JSON_EXPORT int json_object_to_file(const char *filename, struct json_object *obj);
  65. /**
  66. * Open and truncate the given file, creating it if necessary, then
  67. * convert the json_object to a string and write it to the file.
  68. *
  69. * Returns -1 if something fails. See json_util_get_last_err() for details.
  70. */
  71. JSON_EXPORT int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
  72. /**
  73. * Convert the json_object to a string and write it to the file descriptor.
  74. * Handles partial writes and will keep writing until done, or an error
  75. * occurs.
  76. *
  77. * @param fd an open, writable file descriptor to write to
  78. * @param obj the object to serializer and write
  79. * @param flags flags to pass to json_object_to_json_string_ext()
  80. * @return -1 if something fails. See json_util_get_last_err() for details.
  81. */
  82. JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags);
  83. /**
  84. * Return the last error from various json-c functions, including:
  85. * json_object_to_file{,_ext}, json_object_to_fd() or
  86. * json_object_from_{file,fd}, or NULL if there is none.
  87. */
  88. JSON_EXPORT const char *json_util_get_last_err(void);
  89. JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
  90. JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
  91. JSON_EXPORT int json_parse_double(const char *buf, double *retval);
  92. /**
  93. * Return a string describing the type of the object.
  94. * e.g. "int", or "object", etc...
  95. */
  96. JSON_EXPORT const char *json_type_to_name(enum json_type o_type);
  97. #ifndef JSON_NORETURN
  98. #if defined(_MSC_VER)
  99. #define JSON_NORETURN __declspec(noreturn)
  100. #else
  101. /* 'cold' attribute is for optimization, telling the computer this code
  102. * path is unlikely.
  103. */
  104. #define JSON_NORETURN __attribute__((noreturn, cold))
  105. #endif
  106. #endif
  107. /**
  108. * Abort and optionally print a message on standard error.
  109. * This should be used rather than assert() for unconditional abortion
  110. * (in particular for code paths which are never supposed to be run).
  111. * */
  112. JSON_NORETURN JSON_EXPORT void json_abort(const char *message);
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif