Improved *_FAIL_INDEX error macros to print the index/size

This commit is contained in:
Marcelo Fernandez 2017-10-17 11:37:40 -03:00
parent 6e960c7d6b
commit a97d7d948b
3 changed files with 30 additions and 22 deletions

View File

@ -97,3 +97,10 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
_err_error_exists = false; _err_error_exists = false;
} }
} }
void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, bool fatal) {
String fstr(fatal ? "FATAL: " : "");
String err(fstr + "Index" + p_index_str + "=" + itos(p_index) + " out of size (" + p_size_str + "=" + itos(p_size) + ")");
_err_print_error(p_function, p_file, p_line, err.utf8().get_data());
}

View File

@ -76,6 +76,7 @@ void add_error_handler(ErrorHandlerList *p_handler);
void remove_error_handler(ErrorHandlerList *p_handler); void remove_error_handler(ErrorHandlerList *p_handler);
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR); void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, bool fatal = false);
#ifndef _STR #ifndef _STR
#define _STR(m_x) #m_x #define _STR(m_x) #m_x
@ -132,7 +133,7 @@ extern bool _err_error_exists;
#define ERR_FAIL_INDEX(m_index, m_size) \ #define ERR_FAIL_INDEX(m_index, m_size) \
do { \ do { \
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
return; \ return; \
} else \ } else \
_err_error_exists = false; \ _err_error_exists = false; \
@ -146,7 +147,7 @@ extern bool _err_error_exists;
#define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \ #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
do { \ do { \
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
return m_retval; \ return m_retval; \
} else \ } else \
_err_error_exists = false; \ _err_error_exists = false; \
@ -158,7 +159,7 @@ extern bool _err_error_exists;
#define CRASH_BAD_INDEX(m_index, m_size) \ #define CRASH_BAD_INDEX(m_index, m_size) \
do { \ do { \
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), true); \
GENERATE_TRAP \ GENERATE_TRAP \
} \ } \
} while (0); // (*) } while (0); // (*)

View File

@ -98,11 +98,11 @@ T *_nullptr() {
#undef OK #undef OK
#endif #endif
#include "int_types.h"
#include "error_list.h" #include "error_list.h"
#include "error_macros.h" #include "error_macros.h"
#include "int_types.h"
/** Generic ABS function, for math uses please use Math::abs */ /** Generic ABS function, for math uses please use Math::abs */
#ifndef ABS #ifndef ABS