error_macros.hpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /**************************************************************************/
  2. /* error_macros.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_ERROR_MACROS_HPP
  31. #define GODOT_ERROR_MACROS_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <atomic>
  34. namespace godot {
  35. class String;
  36. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_editor_notify = false, 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_editor_notify = false, 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_editor_notify = false, bool p_is_warning = false);
  39. 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_editor_notify = false, bool p_is_warning = false);
  40. 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_editor_notify = false, 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_editor_notify = false, 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 p_editor_notify = false, bool p_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 p_editor_notify = false, bool p_fatal = false);
  44. void _err_flush_stdout();
  45. } // namespace godot
  46. #ifdef __GNUC__
  47. #define FUNCTION_STR __FUNCTION__
  48. #else
  49. #define FUNCTION_STR __FUNCTION__
  50. #endif
  51. #ifdef _MSC_VER
  52. /**
  53. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  54. */
  55. #define GENERATE_TRAP() __debugbreak()
  56. #else
  57. /**
  58. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  59. */
  60. #define GENERATE_TRAP() __builtin_trap()
  61. #endif
  62. /**
  63. * Error macros.
  64. * WARNING: These macros work in the opposite way to assert().
  65. *
  66. * Unlike exceptions and asserts, these macros try to maintain consistency and stability.
  67. * In most cases, bugs and/or invalid data are not fatal. They should never allow a perfectly
  68. * running application to fail or crash.
  69. * Always try to return processable data, so the engine can keep running well.
  70. * Use the _MSG versions to print a meaningful message to help with debugging.
  71. *
  72. * The `((void)0)` no-op statement is used as a trick to force us to put a semicolon after
  73. * those macros, making them look like proper statements.
  74. * The if wrappers are used to ensure that the macro replacement does not trigger unexpected
  75. * issues when expanded e.g. after an `if (cond) ERR_FAIL();` without braces.
  76. */
  77. // Index out of bounds error macros.
  78. // These macros should be used instead of `ERR_FAIL_COND` for bounds checking.
  79. // Integer index out of bounds error macros.
  80. /**
  81. * Try using `ERR_FAIL_INDEX_MSG`.
  82. * Only use this macro if there is no sensible error message.
  83. *
  84. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  85. * If not, the current function returns.
  86. */
  87. #define ERR_FAIL_INDEX(m_index, m_size) \
  88. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  89. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  90. return; \
  91. } else \
  92. ((void)0)
  93. /**
  94. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  95. * If not, prints `m_msg` and the current function returns.
  96. */
  97. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  98. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  99. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  100. return; \
  101. } else \
  102. ((void)0)
  103. /**
  104. * Same as `ERR_FAIL_INDEX_MSG` but also notifies the editor.
  105. */
  106. #define ERR_FAIL_INDEX_EDMSG(m_index, m_size, m_msg) \
  107. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  108. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  109. return; \
  110. } else \
  111. ((void)0)
  112. /**
  113. * Try using `ERR_FAIL_INDEX_V_MSG`.
  114. * Only use this macro if there is no sensible error message.
  115. *
  116. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  117. * If not, the current function returns `m_retval`.
  118. */
  119. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  120. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  121. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  122. return m_retval; \
  123. } else \
  124. ((void)0)
  125. /**
  126. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  127. * If not, prints `m_msg` and the current function returns `m_retval`.
  128. */
  129. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  130. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  131. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  132. return m_retval; \
  133. } else \
  134. ((void)0)
  135. /**
  136. * Same as `ERR_FAIL_INDEX_V_MSG` but also notifies the editor.
  137. */
  138. #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  139. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  140. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  141. return m_retval; \
  142. } else \
  143. ((void)0)
  144. /**
  145. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  146. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  147. * there is no sensible error message.
  148. *
  149. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  150. * If not, the application crashes.
  151. */
  152. #define CRASH_BAD_INDEX(m_index, m_size) \
  153. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  154. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  155. ::godot::_err_flush_stdout(); \
  156. GENERATE_TRAP(); \
  157. } else \
  158. ((void)0)
  159. /**
  160. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  161. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  162. *
  163. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  164. * If not, prints `m_msg` and the application crashes.
  165. */
  166. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  167. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  168. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  169. ::godot::_err_flush_stdout(); \
  170. GENERATE_TRAP(); \
  171. } else \
  172. ((void)0)
  173. // Unsigned integer index out of bounds error macros.
  174. /**
  175. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  176. * Only use this macro if there is no sensible error message.
  177. *
  178. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  179. * If not, the current function returns.
  180. */
  181. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  182. if (unlikely((m_index) >= (m_size))) { \
  183. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  184. return; \
  185. } else \
  186. ((void)0)
  187. /**
  188. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  189. * If not, prints `m_msg` and the current function returns.
  190. */
  191. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  192. if (unlikely((m_index) >= (m_size))) { \
  193. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  194. return; \
  195. } else \
  196. ((void)0)
  197. /**
  198. * Same as `ERR_FAIL_UNSIGNED_INDEX_MSG` but also notifies the editor.
  199. */
  200. #define ERR_FAIL_UNSIGNED_INDEX_EDMSG(m_index, m_size, m_msg) \
  201. if (unlikely((m_index) >= (m_size))) { \
  202. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  203. return; \
  204. } else \
  205. ((void)0)
  206. /**
  207. * Try using `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  208. * Only use this macro if there is no sensible error message.
  209. *
  210. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  211. * If not, the current function returns `m_retval`.
  212. */
  213. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  214. if (unlikely((m_index) >= (m_size))) { \
  215. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  216. return m_retval; \
  217. } else \
  218. ((void)0)
  219. /**
  220. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  221. * If not, prints `m_msg` and the current function returns `m_retval`.
  222. */
  223. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  224. if (unlikely((m_index) >= (m_size))) { \
  225. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  226. return m_retval; \
  227. } else \
  228. ((void)0)
  229. /**
  230. * Same as `ERR_FAIL_UNSIGNED_INDEX_V_EDMSG` but also notifies the editor.
  231. */
  232. #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  233. if (unlikely((m_index) >= (m_size))) { \
  234. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  235. return m_retval; \
  236. } else \
  237. ((void)0)
  238. /**
  239. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  240. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  241. * there is no sensible error message.
  242. *
  243. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  244. * If not, the application crashes.
  245. */
  246. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  247. if (unlikely((m_index) >= (m_size))) { \
  248. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  249. ::godot::_err_flush_stdout(); \
  250. GENERATE_TRAP(); \
  251. } else \
  252. ((void)0)
  253. /**
  254. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  255. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  256. *
  257. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  258. * If not, prints `m_msg` and the application crashes.
  259. */
  260. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  261. if (unlikely((m_index) >= (m_size))) { \
  262. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  263. ::godot::_err_flush_stdout(); \
  264. GENERATE_TRAP(); \
  265. } else \
  266. ((void)0)
  267. // Null reference error macros.
  268. /**
  269. * Try using `ERR_FAIL_NULL_MSG`.
  270. * Only use this macro if there is no sensible error message.
  271. *
  272. * Ensures a pointer `m_param` is not null.
  273. * If it is null, the current function returns.
  274. */
  275. #define ERR_FAIL_NULL(m_param) \
  276. if (unlikely(m_param == nullptr)) { \
  277. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  278. return; \
  279. } else \
  280. ((void)0)
  281. /**
  282. * Ensures a pointer `m_param` is not null.
  283. * If it is null, prints `m_msg` and the current function returns.
  284. */
  285. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  286. if (unlikely(m_param == nullptr)) { \
  287. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  288. return; \
  289. } else \
  290. ((void)0)
  291. /**
  292. * Same as `ERR_FAIL_NULL_MSG` but also notifies the editor.
  293. */
  294. #define ERR_FAIL_NULL_EDMSG(m_param, m_msg) \
  295. if (unlikely(m_param == nullptr)) { \
  296. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  297. return; \
  298. } else \
  299. ((void)0)
  300. /**
  301. * Try using `ERR_FAIL_NULL_V_MSG`.
  302. * Only use this macro if there is no sensible error message.
  303. *
  304. * Ensures a pointer `m_param` is not null.
  305. * If it is null, the current function returns `m_retval`.
  306. */
  307. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  308. if (unlikely(m_param == nullptr)) { \
  309. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  310. return m_retval; \
  311. } else \
  312. ((void)0)
  313. /**
  314. * Ensures a pointer `m_param` is not null.
  315. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  316. */
  317. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  318. if (unlikely(m_param == nullptr)) { \
  319. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  320. return m_retval; \
  321. } else \
  322. ((void)0)
  323. /**
  324. * Same as `ERR_FAIL_NULL_V_MSG` but also notifies the editor.
  325. */
  326. #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \
  327. if (unlikely(m_param == nullptr)) { \
  328. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  329. return m_retval; \
  330. } else \
  331. ((void)0)
  332. /**
  333. * Try using `ERR_FAIL_COND_MSG`.
  334. * Only use this macro if there is no sensible error message.
  335. * If checking for null use ERR_FAIL_NULL_MSG instead.
  336. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  337. *
  338. * Ensures `m_cond` is false.
  339. * If `m_cond` is true, the current function returns.
  340. */
  341. #define ERR_FAIL_COND(m_cond) \
  342. if (unlikely(m_cond)) { \
  343. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  344. return; \
  345. } else \
  346. ((void)0)
  347. /**
  348. * Ensures `m_cond` is false.
  349. * If `m_cond` is true, prints `m_msg` and the current function returns.
  350. *
  351. * If checking for null use ERR_FAIL_NULL_MSG instead.
  352. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  353. */
  354. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  355. if (unlikely(m_cond)) { \
  356. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  357. return; \
  358. } else \
  359. ((void)0)
  360. /**
  361. * Same as `ERR_FAIL_COND_MSG` but also notifies the editor.
  362. */
  363. #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \
  364. if (unlikely(m_cond)) { \
  365. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg, true); \
  366. return; \
  367. } else \
  368. ((void)0)
  369. /**
  370. * Try using `ERR_FAIL_COND_V_MSG`.
  371. * Only use this macro if there is no sensible error message.
  372. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  373. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  374. *
  375. * Ensures `m_cond` is false.
  376. * If `m_cond` is true, the current function returns `m_retval`.
  377. */
  378. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  379. if (unlikely(m_cond)) { \
  380. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  381. return m_retval; \
  382. } else \
  383. ((void)0)
  384. /**
  385. * Ensures `m_cond` is false.
  386. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  387. *
  388. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  389. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  390. */
  391. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  392. if (unlikely(m_cond)) { \
  393. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg); \
  394. return m_retval; \
  395. } else \
  396. ((void)0)
  397. /**
  398. * Same as `ERR_FAIL_COND_V_MSG` but also notifies the editor.
  399. */
  400. #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \
  401. if (unlikely(m_cond)) { \
  402. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg, true); \
  403. return m_retval; \
  404. } else \
  405. ((void)0)
  406. /**
  407. * Try using `ERR_CONTINUE_MSG`.
  408. * Only use this macro if there is no sensible error message.
  409. *
  410. * Ensures `m_cond` is false.
  411. * If `m_cond` is true, the current loop continues.
  412. */
  413. #define ERR_CONTINUE(m_cond) \
  414. if (unlikely(m_cond)) { \
  415. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  416. continue; \
  417. } else \
  418. ((void)0)
  419. /**
  420. * Ensures `m_cond` is false.
  421. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  422. */
  423. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  424. if (unlikely(m_cond)) { \
  425. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  426. continue; \
  427. } else \
  428. ((void)0)
  429. /**
  430. * Same as `ERR_CONTINUE_MSG` but also notifies the editor.
  431. */
  432. #define ERR_CONTINUE_EDMSG(m_cond, m_msg) \
  433. if (unlikely(m_cond)) { \
  434. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg, true); \
  435. continue; \
  436. } else \
  437. ((void)0)
  438. /**
  439. * Try using `ERR_BREAK_MSG`.
  440. * Only use this macro if there is no sensible error message.
  441. *
  442. * Ensures `m_cond` is false.
  443. * If `m_cond` is true, the current loop breaks.
  444. */
  445. #define ERR_BREAK(m_cond) \
  446. if (unlikely(m_cond)) { \
  447. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  448. break; \
  449. } else \
  450. ((void)0)
  451. /**
  452. * Ensures `m_cond` is false.
  453. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  454. */
  455. #define ERR_BREAK_MSG(m_cond, m_msg) \
  456. if (unlikely(m_cond)) { \
  457. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  458. break; \
  459. } else \
  460. ((void)0)
  461. /**
  462. * Same as `ERR_BREAK_MSG` but also notifies the editor.
  463. */
  464. #define ERR_BREAK_EDMSG(m_cond, m_msg) \
  465. if (unlikely(m_cond)) { \
  466. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg, true); \
  467. break; \
  468. } else \
  469. ((void)0)
  470. /**
  471. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  472. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  473. * there is no sensible error message.
  474. *
  475. * Ensures `m_cond` is false.
  476. * If `m_cond` is true, the application crashes.
  477. */
  478. #define CRASH_COND(m_cond) \
  479. if (unlikely(m_cond)) { \
  480. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  481. ::godot::_err_flush_stdout(); \
  482. GENERATE_TRAP(); \
  483. } else \
  484. ((void)0)
  485. /**
  486. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  487. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  488. *
  489. * Ensures `m_cond` is false.
  490. * If `m_cond` is true, prints `m_msg` and the application crashes.
  491. */
  492. #define CRASH_COND_MSG(m_cond, m_msg) \
  493. if (unlikely(m_cond)) { \
  494. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  495. ::godot::_err_flush_stdout(); \
  496. GENERATE_TRAP(); \
  497. } else \
  498. ((void)0)
  499. // Generic error macros.
  500. /**
  501. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  502. * Only use this macro if more complex error detection or recovery is required, and
  503. * there is no sensible error message.
  504. *
  505. * The current function returns.
  506. */
  507. #define ERR_FAIL() \
  508. if (true) { \
  509. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  510. return; \
  511. } else \
  512. ((void)0)
  513. /**
  514. * Try using `ERR_FAIL_COND_MSG`.
  515. * Only use this macro if more complex error detection or recovery is required.
  516. *
  517. * Prints `m_msg`, and the current function returns.
  518. */
  519. #define ERR_FAIL_MSG(m_msg) \
  520. if (true) { \
  521. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg); \
  522. return; \
  523. } else \
  524. ((void)0)
  525. /**
  526. * Same as `ERR_FAIL_MSG` but also notifies the editor.
  527. */
  528. #define ERR_FAIL_EDMSG(m_msg) \
  529. if (true) { \
  530. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg, true); \
  531. return; \
  532. } else \
  533. ((void)0)
  534. /**
  535. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  536. * Only use this macro if more complex error detection or recovery is required, and
  537. * there is no sensible error message.
  538. *
  539. * The current function returns `m_retval`.
  540. */
  541. #define ERR_FAIL_V(m_retval) \
  542. if (true) { \
  543. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  544. return m_retval; \
  545. } else \
  546. ((void)0)
  547. /**
  548. * Try using `ERR_FAIL_COND_V_MSG`.
  549. * Only use this macro if more complex error detection or recovery is required.
  550. *
  551. * Prints `m_msg`, and the current function returns `m_retval`.
  552. */
  553. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  554. if (true) { \
  555. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg); \
  556. return m_retval; \
  557. } else \
  558. ((void)0)
  559. /**
  560. * Same as `ERR_FAIL_V_MSG` but also notifies the editor.
  561. */
  562. #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \
  563. if (true) { \
  564. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg, true); \
  565. return m_retval; \
  566. } else \
  567. ((void)0)
  568. /**
  569. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or `ERR_BREAK_MSG`.
  570. * Only use this macro at the start of a function that has not been implemented yet, or
  571. * if more complex error detection or recovery is required.
  572. *
  573. * Prints `m_msg`.
  574. */
  575. #define ERR_PRINT(m_msg) \
  576. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  577. /**
  578. * Same as `ERR_PRINT` but also notifies the editor.
  579. */
  580. #define ERR_PRINT_ED(m_msg) \
  581. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true)
  582. /**
  583. * Prints `m_msg` once during the application lifetime.
  584. */
  585. #define ERR_PRINT_ONCE(m_msg) \
  586. if (true) { \
  587. static bool first_print = true; \
  588. if (first_print) { \
  589. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  590. first_print = false; \
  591. } \
  592. } else \
  593. ((void)0)
  594. /**
  595. * Same as `ERR_PRINT_ONCE` but also notifies the editor.
  596. */
  597. #define ERR_PRINT_ONCE_ED(m_msg) \
  598. if (true) { \
  599. static bool first_print = true; \
  600. if (first_print) { \
  601. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  602. first_print = false; \
  603. } \
  604. } else \
  605. ((void)0)
  606. // Print warning message macros.
  607. /**
  608. * Prints `m_msg`.
  609. *
  610. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  611. */
  612. #define WARN_PRINT(m_msg) \
  613. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, true)
  614. /**
  615. * Same as `WARN_PRINT` but also notifies the editor.
  616. */
  617. #define WARN_PRINT_ED(m_msg) \
  618. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, true)
  619. /**
  620. * Prints `m_msg` once during the application lifetime.
  621. *
  622. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  623. */
  624. #define WARN_PRINT_ONCE(m_msg) \
  625. if (true) { \
  626. static bool first_print = true; \
  627. if (first_print) { \
  628. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, true); \
  629. first_print = false; \
  630. } \
  631. } else \
  632. ((void)0)
  633. /**
  634. * Same as `WARN_PRINT_ONCE` but also notifies the editor.
  635. */
  636. #define WARN_PRINT_ONCE_ED(m_msg) \
  637. if (true) { \
  638. static bool first_print = true; \
  639. if (first_print) { \
  640. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, true); \
  641. first_print = false; \
  642. } \
  643. } else \
  644. ((void)0)
  645. // Print deprecated warning message macros.
  646. /**
  647. * Warns that the current function is deprecated.
  648. */
  649. #define WARN_DEPRECATED \
  650. if (true) { \
  651. static std::atomic<bool> warning_shown; \
  652. if (!warning_shown.load()) { \
  653. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", false, true); \
  654. warning_shown.store(true); \
  655. } \
  656. } else \
  657. ((void)0)
  658. /**
  659. * Warns that the current function is deprecated and prints `m_msg`.
  660. */
  661. #define WARN_DEPRECATED_MSG(m_msg) \
  662. if (true) { \
  663. static std::atomic<bool> warning_shown; \
  664. if (!warning_shown.load()) { \
  665. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", m_msg, false, true); \
  666. warning_shown.store(true); \
  667. } \
  668. } else \
  669. ((void)0)
  670. /**
  671. * Do not use.
  672. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  673. *
  674. * The application crashes.
  675. */
  676. #define CRASH_NOW() \
  677. if (true) { \
  678. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  679. ::godot::_err_flush_stdout(); \
  680. GENERATE_TRAP(); \
  681. } else \
  682. ((void)0)
  683. /**
  684. * Only use if the application should never reach this point.
  685. *
  686. * Prints `m_msg`, and then the application crashes.
  687. */
  688. #define CRASH_NOW_MSG(m_msg) \
  689. if (true) { \
  690. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", m_msg); \
  691. ::godot::_err_flush_stdout(); \
  692. GENERATE_TRAP(); \
  693. } else \
  694. ((void)0)
  695. /**
  696. * This should be a 'free' assert for program flow and should not be needed in any releases,
  697. * only used in dev builds.
  698. */
  699. #ifdef DEBUG_ENABLED
  700. #define DEV_ASSERT(m_cond) \
  701. if (unlikely(!(m_cond))) { \
  702. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  703. ::godot::_err_flush_stdout(); \
  704. GENERATE_TRAP(); \
  705. } else \
  706. ((void)0)
  707. #else
  708. #define DEV_ASSERT(m_cond)
  709. #endif
  710. /**
  711. * Gives an error message when a method bind is invalid (likely the hash changed).
  712. * Avoids crashing the application in this case. It's not free, so it's debug only.
  713. */
  714. #ifdef DEBUG_ENABLED
  715. #define CHECK_METHOD_BIND_RET(m_mb, m_ret) \
  716. if (unlikely(!m_mb)) { \
  717. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  718. return m_ret; \
  719. } else \
  720. ((void)0)
  721. #define CHECK_METHOD_BIND(m_mb) \
  722. if (unlikely(!m_mb)) { \
  723. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  724. return; \
  725. } else \
  726. ((void)0)
  727. #else
  728. #define CHECK_METHOD_BIND_RET(m_mb, m_ret)
  729. #define CHECK_METHOD_BIND(m_mb)
  730. #endif
  731. #endif // GODOT_ERROR_MACROS_HPP