From a23d072a66d2569ba13921522be3c82ae9aad576 Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 14 Dec 2013 15:14:23 +0100 Subject: 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 --- src/common/cbasetypes.h | 19 +------------ src/common/nullpo.c | 18 ++++++++---- src/common/nullpo.h | 75 ++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 71 insertions(+), 41 deletions(-) (limited to 'src/common') 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 @@ -352,23 +352,6 @@ typedef char bool; #define PATHSEP_STR "/" #endif -////////////////////////////////////////////////////////////////////////// -// Assert - -#if ! defined(Assert) -#if defined(RELEASE) -#define Assert(EX) -#else -// extern "C" { -#include -// } -#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW) -#include -#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. @@ -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 #include #include -#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 +// } +#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW) +#include +#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 @@ -45,6 +63,14 @@ #define nullpo_ret(t) \ 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. * @@ -53,6 +79,14 @@ #define nullpo_retv(t) \ 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. * @@ -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 */ -- cgit v1.2.3-60-g2f50