error_macros.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ERROR_MACROS_H
  31. #define ERROR_MACROS_H
  32. #include "core/typedefs.h"
  33. /**
  34. * Error macros. Unlike exceptions and asserts, these macros try to maintain consistency and stability
  35. * inside the code. It is recommended to always return processable data, so in case of an error, the
  36. * engine can stay working well.
  37. * In most cases, bugs and/or invalid data are not fatal and should never allow a perfectly running application
  38. * to fail or crash.
  39. */
  40. /**
  41. * Pointer to the error macro priting function. Reassign to any function to have errors printed
  42. */
  43. /** Function used by the error macros */
  44. // function, file, line, error, explanation
  45. enum ErrorHandlerType {
  46. ERR_HANDLER_ERROR,
  47. ERR_HANDLER_WARNING,
  48. ERR_HANDLER_SCRIPT,
  49. ERR_HANDLER_SHADER,
  50. };
  51. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
  52. void _err_set_last_error(const char *p_err);
  53. void _err_clear_last_error();
  54. struct ErrorHandlerList {
  55. ErrorHandlerFunc errfunc;
  56. void *userdata;
  57. ErrorHandlerList *next;
  58. ErrorHandlerList() {
  59. errfunc = 0;
  60. next = 0;
  61. userdata = 0;
  62. }
  63. };
  64. void add_error_handler(ErrorHandlerList *p_handler);
  65. void remove_error_handler(ErrorHandlerList *p_handler);
  66. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  67. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, bool fatal = false);
  68. #ifndef _STR
  69. #define _STR(m_x) #m_x
  70. #define _MKSTR(m_x) _STR(m_x)
  71. #endif
  72. #define _FNL __FILE__ ":"
  73. /** An index has failed if m_index<0 or m_index >=m_size, the function exists */
  74. extern bool _err_error_exists;
  75. #ifdef DEBUG_ENABLED
  76. /** Print a warning string.
  77. */
  78. #define ERR_EXPLAINC(m_reason) \
  79. { \
  80. _err_set_last_error(m_reason); \
  81. _err_error_exists = true; \
  82. }
  83. #define ERR_EXPLAIN(m_string) \
  84. { \
  85. _err_set_last_error(String(m_string).utf8().get_data()); \
  86. _err_error_exists = true; \
  87. }
  88. #else
  89. #define ERR_EXPLAIN(m_text)
  90. #define ERR_EXPLAINC(m_text)
  91. #endif
  92. #ifdef __GNUC__
  93. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  94. #define FUNCTION_STR __FUNCTION__
  95. #else
  96. #define FUNCTION_STR __FUNCTION__
  97. #endif
  98. // Don't use this directly; instead, use any of the CRASH_* macros
  99. #ifdef _MSC_VER
  100. #define GENERATE_TRAP \
  101. __debugbreak(); \
  102. /* Avoid warning about control paths */ \
  103. for (;;) { \
  104. }
  105. #else
  106. #define GENERATE_TRAP __builtin_trap();
  107. #endif
  108. // (*): See https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for
  109. #define ERR_FAIL_INDEX(m_index, m_size) \
  110. do { \
  111. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  112. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  113. return; \
  114. } else \
  115. _err_error_exists = false; \
  116. } while (0); // (*)
  117. /** An index has failed if m_index<0 or m_index >=m_size, the function exists.
  118. * This function returns an error value, if returning Error, please select the most
  119. * appropriate error condition from error_macros.h
  120. */
  121. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  122. do { \
  123. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  124. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  125. return m_retval; \
  126. } else \
  127. _err_error_exists = false; \
  128. } while (0); // (*)
  129. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  130. * We'll return a null reference and try to keep running.
  131. */
  132. #define CRASH_BAD_INDEX(m_index, m_size) \
  133. do { \
  134. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  135. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), true); \
  136. GENERATE_TRAP \
  137. } \
  138. } while (0); // (*)
  139. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  140. * the function will exit.
  141. */
  142. #define ERR_FAIL_NULL(m_param) \
  143. { \
  144. if (unlikely(!m_param)) { \
  145. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
  146. return; \
  147. } else \
  148. _err_error_exists = false; \
  149. }
  150. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  151. { \
  152. if (unlikely(!m_param)) { \
  153. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
  154. return m_retval; \
  155. } else \
  156. _err_error_exists = false; \
  157. }
  158. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  159. * the function will exit.
  160. */
  161. #define ERR_FAIL_COND(m_cond) \
  162. { \
  163. if (unlikely(m_cond)) { \
  164. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true."); \
  165. return; \
  166. } else \
  167. _err_error_exists = false; \
  168. }
  169. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  170. */
  171. #define CRASH_COND(m_cond) \
  172. { \
  173. if (unlikely(m_cond)) { \
  174. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition ' " _STR(m_cond) " ' is true."); \
  175. GENERATE_TRAP \
  176. } \
  177. }
  178. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  179. * the function will exit.
  180. * This function returns an error value, if returning Error, please select the most
  181. * appropriate error condition from error_macros.h
  182. */
  183. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  184. { \
  185. if (unlikely(m_cond)) { \
  186. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. returned: " _STR(m_retval)); \
  187. return m_retval; \
  188. } else \
  189. _err_error_exists = false; \
  190. }
  191. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  192. * the loop will skip to the next iteration.
  193. */
  194. #define ERR_CONTINUE(m_cond) \
  195. { \
  196. if (unlikely(m_cond)) { \
  197. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Continuing..:"); \
  198. continue; \
  199. } else \
  200. _err_error_exists = false; \
  201. }
  202. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  203. * the loop will break
  204. */
  205. #define ERR_BREAK(m_cond) \
  206. { \
  207. if (unlikely(m_cond)) { \
  208. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  209. break; \
  210. } else \
  211. _err_error_exists = false; \
  212. }
  213. /** Print an error string and return
  214. */
  215. #define ERR_FAIL() \
  216. { \
  217. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed."); \
  218. _err_error_exists = false; \
  219. return; \
  220. }
  221. /** Print an error string and return with value
  222. */
  223. #define ERR_FAIL_V(m_value) \
  224. { \
  225. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed, returning: " __STR(m_value)); \
  226. _err_error_exists = false; \
  227. return m_value; \
  228. }
  229. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  230. */
  231. #define CRASH_NOW() \
  232. { \
  233. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/Function Failed."); \
  234. GENERATE_TRAP \
  235. }
  236. /** Print an error string.
  237. */
  238. #define ERR_PRINT(m_string) \
  239. { \
  240. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  241. _err_error_exists = false; \
  242. }
  243. #define ERR_PRINTS(m_string) \
  244. { \
  245. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data()); \
  246. _err_error_exists = false; \
  247. }
  248. /** Print a warning string.
  249. */
  250. #define WARN_PRINT(m_string) \
  251. { \
  252. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  253. _err_error_exists = false; \
  254. }
  255. #define WARN_PRINTS(m_string) \
  256. { \
  257. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data(), ERR_HANDLER_WARNING); \
  258. _err_error_exists = false; \
  259. }
  260. #define WARN_DEPRECATED \
  261. { \
  262. static volatile bool warning_shown = false; \
  263. if (!warning_shown) { \
  264. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future", ERR_HANDLER_WARNING); \
  265. _err_error_exists = false; \
  266. warning_shown = true; \
  267. } \
  268. }
  269. #endif