error_macros.hpp 35 KB

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