From 2f3da35c6893d2a44e8281ae209f00d54eefa2f2 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 12 Jul 2019 18:40:34 -0700 Subject: [PATCH] Tweaks to nhassert implementation. Change to warnings on MSC build. --- include/extern.h | 2 +- include/global.h | 2 +- include/ntconf.h | 12 ++++++++++++ src/pline.c | 15 +++++++++++++-- sys/winnt/winnt.c | 17 +++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/include/extern.h b/include/extern.h index 5812366f2..abea20a68 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1932,7 +1932,7 @@ E void VDECL(verbalize, (const char *, ...)) PRINTF_F(1, 2); E void VDECL(raw_printf, (const char *, ...)) PRINTF_F(1, 2); E void VDECL(impossible, (const char *, ...)) PRINTF_F(1, 2); E void VDECL(config_error_add, (const char *, ...)) PRINTF_F(1, 2); -E void FDECL(nhassert_failed, (const char *, const char *, int)); +E void FDECL(nhassert_failed, (const char *, int)); /* ### polyself.c ### */ diff --git a/include/global.h b/include/global.h index ada34523c..1bcc2ae5d 100644 --- a/include/global.h +++ b/include/global.h @@ -378,7 +378,7 @@ struct savefile_info { /* Supply nhassert macro if not supplied by port */ #ifndef nhassert #define nhassert(expression) (void)((!!(expression)) || \ - (nhassert_failed(#expression, __FILE__, __LINE__), 0)) + (nhassert_failed(__FILE__, __LINE__), 0)) #endif #endif /* GLOBAL_H */ diff --git a/include/ntconf.h b/include/ntconf.h index 62c269bab..0c76ed718 100644 --- a/include/ntconf.h +++ b/include/ntconf.h @@ -134,6 +134,8 @@ extern void NDECL(getlock); #ifndef HAS_STDINT_H #define HAS_STDINT_H /* force include of stdint.h in integer.h */ #endif +/* Turn on some additional warnings */ +#pragma warning(3:4389) #endif /* _MSC_VER */ /* The following is needed for prototypes of certain functions */ @@ -270,4 +272,14 @@ extern int FDECL(alternative_palette, (char *)); #define nethack_enter(argc, argv) nethack_enter_winnt() extern void FDECL(nethack_exit, (int)) NORETURN; extern boolean FDECL(file_exists, (const char *)); + +/* Override the default version of nhassert. The default version is unable + * to generate a string form of the expression due to the need to be + * compatible with compilers which do not support macro stringization (i.e. + * #x to turn x into its string form). + */ +extern void FDECL(nt_assert_failed, (const char *, const char *, int)); +#define nhassert(expression) (void)((!!(expression)) || \ + (nt_assert_failed(#expression, __FILE__, __LINE__), 0)) + #endif /* NTCONF_H */ diff --git a/src/pline.c b/src/pline.c index 535b75e95..91e825963 100644 --- a/src/pline.c +++ b/src/pline.c @@ -583,9 +583,20 @@ VA_DECL(const char *, str) } /* nhassert_failed is called when an nhassert's condition is false */ -void nhassert_failed(const char * exp, const char * file, int line) +void +nhassert_failed(filepath, line) + const char * filepath; + int line; { - impossible("NHASSERT(%s) in '%s' at line %d", exp, file, line); + const char * filename; + + /* attempt to get filename from path. TODO: we really need a port provided + * function to return a filename from a path */ + filename = strrchr(filepath, '/'); + filename = (filename == NULL ? strrchr(filepath, '\\') : filename); + filename = (filename == NULL ? filepath : filename + 1); + + impossible("nhassert failed in file '%s' at line %d", filename, line); } /*pline.c*/ diff --git a/sys/winnt/winnt.c b/sys/winnt/winnt.c index 0e19e1f76..8d513c59e 100644 --- a/sys/winnt/winnt.c +++ b/sys/winnt/winnt.c @@ -708,6 +708,23 @@ sys_random_seed(VOID_ARGS) } return ourseed; } + +/* nt_assert_failed is called when an nhassert's condition is false */ +void +nt_assert_failed(expression, filepath, line) + const char * expression; + const char * filepath; + int line; +{ + const char * filename; + + /* get file name from path */ + filename = strrchr(filepath, '\\'); + filename = (filename == NULL ? filepath : filename + 1); + impossible("nhassert(%s) failed in file '%s' at line %d", + expression, filename, line); +} + #endif /* WIN32 */ /*winnt.c*/