diff options
author | Haru <haru@dotalux.com> | 2013-12-14 15:14:23 +0100 |
---|---|---|
committer | Haru <haru@dotalux.com> | 2013-12-17 01:02:33 +0100 |
commit | a23d072a66d2569ba13921522be3c82ae9aad576 (patch) | |
tree | 7e71523316ba55f2b08477331ecab561e53700d0 /src | |
parent | 853c5a3141d1f431af95aed55d991334a2b995f6 (diff) | |
download | hercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.gz hercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.bz2 hercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.xz hercules-a23d072a66d2569ba13921522be3c82ae9aad576.zip |
Added support for non-aborting assertions
- Added Assert_ret, Assert_retv, Assert_retb, Assert_retr, working
similarly to the corresponding nullpo_ functions.
- Moved Assert-related macros to nullpo.h, since they share some
functions.
Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/common/cbasetypes.h | 19 | ||||
-rw-r--r-- | src/common/nullpo.c | 18 | ||||
-rw-r--r-- | src/common/nullpo.h | 75 |
3 files changed, 71 insertions, 41 deletions
diff --git a/src/common/cbasetypes.h b/src/common/cbasetypes.h index 6de2ace01..d00f49864 100644 --- a/src/common/cbasetypes.h +++ b/src/common/cbasetypes.h @@ -353,23 +353,6 @@ typedef char bool; #endif ////////////////////////////////////////////////////////////////////////// -// Assert - -#if ! defined(Assert) -#if defined(RELEASE) -#define Assert(EX) -#else -// extern "C" { -#include <assert.h> -// } -#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW) -#include <crtdbg.h> -#endif -#define Assert(EX) assert(EX) -#endif -#endif /* ! defined(Assert) */ - -////////////////////////////////////////////////////////////////////////// // Has to be unsigned to avoid problems in some systems // Problems arise when these functions expect an argument in the range [0,256[ and are fed a signed char. #include <ctype.h> @@ -405,7 +388,7 @@ typedef char bool; ////////////////////////////////////////////////////////////////////////// -// Use the preprocessor to 'stringify' stuff (concert to a string). +// Use the preprocessor to 'stringify' stuff (convert to a string). // example: // #define TESTE blabla // QUOTE(TESTE) -> "TESTE" diff --git a/src/common/nullpo.c b/src/common/nullpo.c index f0fc2c7ab..20180dd3b 100644 --- a/src/common/nullpo.c +++ b/src/common/nullpo.c @@ -4,20 +4,26 @@ #include <stdio.h> #include <stdarg.h> #include <string.h> -#include "nullpo.h" +#include "../common/nullpo.h" #include "../common/showmsg.h" /** - * Reports NULL pointer information + * Reports failed assertions or NULL pointers + * + * @param file Source file where the error was detected + * @param line Line + * @param func Function + * @param targetname Name of the checked symbol + * @param title Message title to display (i.e. failed assertion or nullpo info) */ -void nullpo_info(const char *file, int line, const char *func, const char *targetname) { +void assert_report(const char *file, int line, const char *func, const char *targetname, const char *title) { if (file == NULL) file = "??"; if (func == NULL || *func == '\0') func = "unknown"; - ShowMessage("--- nullpo info --------------------------------------------\n"); - ShowMessage("%s:%d: target '%s' in func `%s'\n", file, line, targetname, func); - ShowMessage("--- end nullpo info ----------------------------------------\n"); + ShowError("--- %s --------------------------------------------\n", title); + ShowError("%s:%d: '%s' in function `%s'\n", file, line, targetname, func); + ShowError("--- end %s ----------------------------------------\n", title); } diff --git a/src/common/nullpo.h b/src/common/nullpo.h index 5400acaa1..ae39b1151 100644 --- a/src/common/nullpo.h +++ b/src/common/nullpo.h @@ -1,19 +1,37 @@ // Copyright (c) Athena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder -#ifndef _NULLPO_H_ -#define _NULLPO_H_ - +#ifndef COMMON_NULLPO_H +#define COMMON_NULLPO_H #include "../common/cbasetypes.h" -#define NLP_MARK __FILE__, __LINE__, __func__ - // enabled by default on debug builds #if defined(DEBUG) && !defined(NULLPO_CHECK) #define NULLPO_CHECK #endif +// Skip assert checks on release builds +#if !defined(RELEASE) && !defined(ASSERT_CHECK) +#define ASSERT_CHECK +#endif + +/** Assert */ + +#if defined(ASSERT_CHECK) +// extern "C" { +#include <assert.h> +// } +#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW) +#include <crtdbg.h> +#endif // !DEFCPP && WIN && !MINGW +#define Assert(EX) assert(EX) +#define Assert_chk(EX) ( (EX) ? false : (assert_report(__FILE__, __LINE__, __func__, #EX, "failed assertion"), true) ) +#else // ! ASSERT_CHECK +#define Assert(EX) (EX) +#define Assert_chk(EX) ((EX), false) +#endif // ASSERT_CHECK + #if defined(NULLPO_CHECK) /** * Reports NULL pointer information if the passed pointer is NULL @@ -21,7 +39,7 @@ * @param t pointer to check * @return true if the passed pointer is NULL, false otherwise */ -#define nullpo_chk(t) ( (t) != NULL ? false : (nullpo_info(NLP_MARK, #t), true) ) +#define nullpo_chk(t) ( (t) != NULL ? false : (assert_report(__FILE__, __LINE__, __func__, #t, "nullpo info"), true) ) #else // ! NULLPO_CHECK #define nullpo_chk(t) ((t), false) #endif // NULLPO_CHECK @@ -46,6 +64,14 @@ do { if (nullpo_chk(t)) return(0); } while(0) /** + * Returns 0 if the given assertion fails. + * + * @param t statement to check + */ +#define Assert_ret(t) \ + do { if (Assert_chk(t)) return(0); } while(0) + +/** * Returns void if a NULL pointer is found. * * @param t pointer to check @@ -54,6 +80,14 @@ do { if (nullpo_chk(t)) return; } while(0) /** + * Returns void if the given assertion fails. + * + * @param t statement to check + */ +#define Assert_retv(t) \ + do { if (Assert_chk(t)) return; } while(0) + +/** * Returns the given value if a NULL pointer is found. * * @param ret value to return @@ -63,7 +97,16 @@ do { if (nullpo_chk(t)) return(ret); } while(0) /** - * Breaks fromt he current loop/switch if a NULL pointer is found. + * Returns the given value if the given assertion fails. + * + * @param ret value to return + * @param t statement to check + */ +#define Assert_retr(ret, t) \ + do { if (Assert_chk(t)) return(ret); } while(0) + +/** + * Breaks from the current loop/switch if a NULL pointer is found. * * @param t pointer to check */ @@ -71,16 +114,14 @@ if (nullpo_chk(t)) break; else (void)0 /** - * Reports NULL pointer information - * - * @param file Source file where the error was detected - * @param line Line - * @param func Function - * @param targetname Name of the checked symbol + * Breaks from the current loop/switch if the given assertion fails. * - * It is possible to use the NLP_MARK macro that expands to: - * __FILE__, __LINE__, __func__ + * @param t statement to check */ -void nullpo_info(const char *file, int line, const char *func, const char *targetname); +#define Assert_retb(t) \ + if (Assert_chk(t)) break; else (void)0 + + +void assert_report(const char *file, int line, const char *func, const char *targetname, const char *title); -#endif /* _NULLPO_H_ */ +#endif /* COMMON_NULLPO_H */ |