error_macros.hpp 38 KB

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