error_macros.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*************************************************************************/
  2. /* error_macros.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 GODOT_CPP_ERROR_MACROS_HPP
  31. #define GODOT_CPP_ERROR_MACROS_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <godot_cpp/variant/string.hpp>
  34. #include <cstdint>
  35. namespace godot {
  36. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_is_warning = false);
  37. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, bool p_is_warning = false);
  38. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_is_warning = false);
  39. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, bool p_is_warning = false);
  40. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, bool p_is_warning = false);
  41. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, bool p_is_warning = false);
  42. 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, const char *p_message = "", bool fatal = false);
  43. 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, const String &p_message, bool fatal = false);
  44. // Used to strip debug messages in release mode
  45. #ifdef DEBUG_ENABLED
  46. #define DEBUG_STR(m_msg) m_msg
  47. #else
  48. #define DEBUG_STR(m_msg) ""
  49. #endif
  50. #ifdef __GNUC__
  51. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  52. #define FUNCTION_STR __FUNCTION__
  53. #else
  54. #define FUNCTION_STR __FUNCTION__
  55. #endif
  56. #ifdef _MSC_VER
  57. /**
  58. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  59. */
  60. #define GENERATE_TRAP() __debugbreak()
  61. #else
  62. /**
  63. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  64. */
  65. #define GENERATE_TRAP() __builtin_trap()
  66. #endif
  67. #define ERR_FAIL_INDEX(m_index, m_size) \
  68. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  69. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  70. return; \
  71. } else \
  72. ((void)0)
  73. /**
  74. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  75. * If not, prints `m_msg` and the current function returns.
  76. */
  77. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  78. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  79. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  80. return; \
  81. } else \
  82. ((void)0)
  83. /**
  84. * Try using `ERR_FAIL_INDEX_V_MSG`.
  85. * Only use this macro if there is no sensible error message.
  86. *
  87. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  88. * If not, the current function returns `m_retval`.
  89. */
  90. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  91. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  92. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  93. return m_retval; \
  94. } else \
  95. ((void)0)
  96. /**
  97. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  98. * If not, prints `m_msg` and the current function returns `m_retval`.
  99. */
  100. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  101. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  102. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  103. return m_retval; \
  104. } else \
  105. ((void)0)
  106. /**
  107. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  108. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  109. * there is no sensible error message.
  110. *
  111. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  112. * If not, the application crashes.
  113. */
  114. #define CRASH_BAD_INDEX(m_index, m_size) \
  115. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  116. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  117. GENERATE_TRAP(); \
  118. } else \
  119. ((void)0)
  120. /**
  121. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  122. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  123. *
  124. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  125. * If not, prints `m_msg` and the application crashes.
  126. */
  127. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  128. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  129. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg), true); \
  130. GENERATE_TRAP(); \
  131. } else \
  132. ((void)0)
  133. // Unsigned integer index out of bounds error macros.
  134. /**
  135. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  136. * Only use this macro if there is no sensible error message.
  137. *
  138. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  139. * If not, the current function returns.
  140. */
  141. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  142. if (unlikely((m_index) >= (m_size))) { \
  143. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  144. return; \
  145. } else \
  146. ((void)0)
  147. /**
  148. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  149. * If not, prints `m_msg` and the current function returns.
  150. */
  151. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  152. if (unlikely((m_index) >= (m_size))) { \
  153. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  154. return; \
  155. } else \
  156. ((void)0)
  157. /**
  158. * Try using `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  159. * Only use this macro if there is no sensible error message.
  160. *
  161. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  162. * If not, the current function returns `m_retval`.
  163. */
  164. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  165. if (unlikely((m_index) >= (m_size))) { \
  166. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  167. return m_retval; \
  168. } else \
  169. ((void)0)
  170. /**
  171. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  172. * If not, prints `m_msg` and the current function returns `m_retval`.
  173. */
  174. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  175. if (unlikely((m_index) >= (m_size))) { \
  176. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  177. return m_retval; \
  178. } else \
  179. ((void)0)
  180. /**
  181. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  182. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  183. * there is no sensible error message.
  184. *
  185. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  186. * If not, the application crashes.
  187. */
  188. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  189. if (unlikely((m_index) >= (m_size))) { \
  190. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  191. GENERATE_TRAP(); \
  192. } else \
  193. ((void)0)
  194. /**
  195. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  196. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  197. *
  198. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  199. * If not, prints `m_msg` and the application crashes.
  200. */
  201. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  202. if (unlikely((m_index) >= (m_size))) { \
  203. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg), true); \
  204. GENERATE_TRAP(); \
  205. } else \
  206. ((void)0)
  207. // Null reference error macros.
  208. /**
  209. * Try using `ERR_FAIL_NULL_MSG`.
  210. * Only use this macro if there is no sensible error message.
  211. *
  212. * Ensures a pointer `m_param` is not null.
  213. * If it is null, the current function returns.
  214. */
  215. #define ERR_FAIL_NULL(m_param) \
  216. if (unlikely(m_param == nullptr)) { \
  217. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  218. return; \
  219. } else \
  220. ((void)0)
  221. /**
  222. * Ensures a pointer `m_param` is not null.
  223. * If it is null, prints `m_msg` and the current function returns.
  224. */
  225. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  226. if (unlikely(m_param == nullptr)) { \
  227. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  228. return; \
  229. } else \
  230. ((void)0)
  231. /**
  232. * Try using `ERR_FAIL_NULL_V_MSG`.
  233. * Only use this macro if there is no sensible error message.
  234. *
  235. * Ensures a pointer `m_param` is not null.
  236. * If it is null, the current function returns `m_retval`.
  237. */
  238. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  239. if (unlikely(m_param == nullptr)) { \
  240. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  241. return m_retval; \
  242. } else \
  243. ((void)0)
  244. /**
  245. * Ensures a pointer `m_param` is not null.
  246. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  247. */
  248. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  249. if (unlikely(m_param == nullptr)) { \
  250. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  251. return m_retval; \
  252. } else \
  253. ((void)0)
  254. /**
  255. * Try using `ERR_FAIL_COND_MSG`.
  256. * Only use this macro if there is no sensible error message.
  257. * If checking for null use ERR_FAIL_NULL_MSG instead.
  258. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  259. *
  260. * Ensures `m_cond` is false.
  261. * If `m_cond` is true, the current function returns.
  262. */
  263. #define ERR_FAIL_COND(m_cond) \
  264. if (unlikely(m_cond)) { \
  265. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  266. return; \
  267. } else \
  268. ((void)0)
  269. /**
  270. * Ensures `m_cond` is false.
  271. * If `m_cond` is true, prints `m_msg` and the current function returns.
  272. *
  273. * If checking for null use ERR_FAIL_NULL_MSG instead.
  274. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  275. */
  276. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  277. if (unlikely(m_cond)) { \
  278. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  279. return; \
  280. } else \
  281. ((void)0)
  282. /**
  283. * Try using `ERR_FAIL_COND_V_MSG`.
  284. * Only use this macro if there is no sensible error message.
  285. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  286. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  287. *
  288. * Ensures `m_cond` is false.
  289. * If `m_cond` is true, the current function returns `m_retval`.
  290. */
  291. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  292. if (unlikely(m_cond)) { \
  293. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  294. return m_retval; \
  295. } else \
  296. ((void)0)
  297. /**
  298. * Ensures `m_cond` is false.
  299. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  300. *
  301. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  302. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  303. */
  304. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  305. if (unlikely(m_cond)) { \
  306. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), DEBUG_STR(m_msg)); \
  307. return m_retval; \
  308. } else \
  309. ((void)0)
  310. /**
  311. * Try using `ERR_CONTINUE_MSG`.
  312. * Only use this macro if there is no sensible error message.
  313. *
  314. * Ensures `m_cond` is false.
  315. * If `m_cond` is true, the current loop continues.
  316. */
  317. #define ERR_CONTINUE(m_cond) \
  318. if (unlikely(m_cond)) { \
  319. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  320. continue; \
  321. } else \
  322. ((void)0)
  323. /**
  324. * Ensures `m_cond` is false.
  325. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  326. */
  327. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  328. if (unlikely(m_cond)) { \
  329. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", DEBUG_STR(m_msg)); \
  330. continue; \
  331. } else \
  332. ((void)0)
  333. /**
  334. * Try using `ERR_BREAK_MSG`.
  335. * Only use this macro if there is no sensible error message.
  336. *
  337. * Ensures `m_cond` is false.
  338. * If `m_cond` is true, the current loop breaks.
  339. */
  340. #define ERR_BREAK(m_cond) \
  341. if (unlikely(m_cond)) { \
  342. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  343. break; \
  344. } else \
  345. ((void)0)
  346. /**
  347. * Ensures `m_cond` is false.
  348. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  349. */
  350. #define ERR_BREAK_MSG(m_cond, m_msg) \
  351. if (unlikely(m_cond)) { \
  352. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", DEBUG_STR(m_msg)); \
  353. break; \
  354. } else \
  355. ((void)0)
  356. /**
  357. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  358. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  359. * there is no sensible error message.
  360. *
  361. * Ensures `m_cond` is false.
  362. * If `m_cond` is true, the application crashes.
  363. */
  364. #define CRASH_COND(m_cond) \
  365. if (unlikely(m_cond)) { \
  366. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  367. GENERATE_TRAP(); \
  368. } else \
  369. ((void)0)
  370. /**
  371. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  372. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  373. *
  374. * Ensures `m_cond` is false.
  375. * If `m_cond` is true, prints `m_msg` and the application crashes.
  376. */
  377. #define CRASH_COND_MSG(m_cond, m_msg) \
  378. if (unlikely(m_cond)) { \
  379. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  380. GENERATE_TRAP(); \
  381. } else \
  382. ((void)0)
  383. // Generic error macros.
  384. /**
  385. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  386. * Only use this macro if more complex error detection or recovery is required, and
  387. * there is no sensible error message.
  388. *
  389. * The current function returns.
  390. */
  391. #define ERR_FAIL() \
  392. if (true) { \
  393. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  394. return; \
  395. } else \
  396. ((void)0)
  397. /**
  398. * Try using `ERR_FAIL_COND_MSG`.
  399. * Only use this macro if more complex error detection or recovery is required.
  400. *
  401. * Prints `m_msg`, and the current function returns.
  402. */
  403. #define ERR_FAIL_MSG(m_msg) \
  404. if (true) { \
  405. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", DEBUG_STR(m_msg)); \
  406. return; \
  407. } else \
  408. ((void)0)
  409. /**
  410. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  411. * Only use this macro if more complex error detection or recovery is required, and
  412. * there is no sensible error message.
  413. *
  414. * The current function returns `m_retval`.
  415. */
  416. #define ERR_FAIL_V(m_retval) \
  417. if (true) { \
  418. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  419. return m_retval; \
  420. } else \
  421. ((void)0)
  422. /**
  423. * Try using `ERR_FAIL_COND_V_MSG`.
  424. * Only use this macro if more complex error detection or recovery is required.
  425. *
  426. * Prints `m_msg`, and the current function returns `m_retval`.
  427. */
  428. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  429. if (true) { \
  430. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), DEBUG_STR(m_msg)); \
  431. return m_retval; \
  432. } else \
  433. ((void)0)
  434. /**
  435. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG.
  436. * Only use this macro at the start of a function that has not been implemented yet, or
  437. * if more complex error detection or recovery is required.
  438. *
  439. * Prints `m_msg`.
  440. */
  441. #define ERR_PRINT(m_msg) \
  442. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  443. /**
  444. * Prints `m_msg` once during the application lifetime.
  445. */
  446. #define ERR_PRINT_ONCE(m_msg) \
  447. if (true) { \
  448. static bool first_print = true; \
  449. if (first_print) { \
  450. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  451. first_print = false; \
  452. } \
  453. } else \
  454. ((void)0)
  455. // Print warning message macros.
  456. /**
  457. * Prints `m_msg`.
  458. *
  459. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  460. */
  461. #define WARN_PRINT(m_msg) \
  462. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true)
  463. /**
  464. * Prints `m_msg` once during the application lifetime.
  465. *
  466. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  467. */
  468. #define WARN_PRINT_ONCE(m_msg) \
  469. if (true) { \
  470. static bool first_print = true; \
  471. if (first_print) { \
  472. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  473. first_print = false; \
  474. } \
  475. } else \
  476. ((void)0)
  477. // Print deprecated warning message macros.
  478. /**
  479. * Warns that the current function is deprecated.
  480. */
  481. #define WARN_DEPRECATED \
  482. if (true) { \
  483. static SafeFlag warning_shown; \
  484. if (!warning_shown.is_set()) { \
  485. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", true); \
  486. warning_shown.set(); \
  487. } \
  488. } else \
  489. ((void)0)
  490. /**
  491. * Warns that the current function is deprecated and prints `m_msg`.
  492. */
  493. #define WARN_DEPRECATED_MSG(m_msg) \
  494. if (true) { \
  495. static SafeFlag warning_shown; \
  496. if (!warning_shown.is_set()) { \
  497. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", DEBUG_STR(m_msg), true); \
  498. warning_shown.set(); \
  499. } \
  500. } else \
  501. ((void)0)
  502. /**
  503. * Do not use.
  504. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  505. *
  506. * The application crashes.
  507. */
  508. #define CRASH_NOW() \
  509. if (true) { \
  510. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  511. GENERATE_TRAP(); \
  512. } else \
  513. ((void)0)
  514. /**
  515. * Only use if the application should never reach this point.
  516. *
  517. * Prints `m_msg`, and then the application crashes.
  518. */
  519. #define CRASH_NOW_MSG(m_msg) \
  520. if (true) { \
  521. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", DEBUG_STR(m_msg)); \
  522. GENERATE_TRAP(); \
  523. } else \
  524. ((void)0)
  525. } // namespace godot
  526. /**
  527. * Gives an error message when a method bind is invalid (likely the hash changed).
  528. * Avoids crashing the application in this case. It's not free, so it's debug only.
  529. */
  530. #ifdef DEBUG_ENABLED
  531. #define CHECK_METHOD_BIND_RET(m_mb, m_ret) \
  532. if (unlikely(!m_mb)) { \
  533. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  534. return m_ret; \
  535. } else \
  536. ((void)0)
  537. #define CHECK_METHOD_BIND(m_mb) \
  538. if (unlikely(!m_mb)) { \
  539. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  540. return; \
  541. } else \
  542. ((void)0)
  543. #else
  544. #define CHECK_METHOD_BIND_RET(m_mb, m_ret)
  545. #define CHECK_METHOD_BIND(m_mb)
  546. #endif
  547. #endif // ! GODOT_CPP_ERROR_MACROS_HPP