From 3221665f5a98cd21aea5db62c0dd53260ce8020e Mon Sep 17 00:00:00 2001 From: nhmall Date: Sun, 19 Jan 2025 20:51:46 -0500 Subject: [PATCH] updates to tradstdc.h Define a macro NH_C to provide a shorter & simpler way to test for which C standard the build is being carried out under (c99 or c23). NH_C > 202300L Being compiled under C23 or greater NH_C > 199900L Being compiled under C99 or greater NH_C > 198900L Being compiled under C89 or greater, or C std could not be determined. While NetHack only requires c99, we've been taking advantage of some c23 features (attributes), if they are available, to allow the use of ATTRNORETURN/NORETURN and FALLTHROUGH on compilers other than gcc. Also add some comment documentation to tradstdc.h about NetHack's use of c99. The sys/unix/Makefile.top change overcomes a warning in the Makefile-generated nhlua.h. That warning arises under some compilers that rely on attribute [[noreturn]] ahead of a declaration (NetHack macro ATTRNORETURN), rather than the trailing gcc __attribute((noreturn)) (NetHack macro NORETURN). The sed command is modified to include ATTRNORETURN at the start of the declaration in addition to the NORETURN at the end of the declaration, in the generated file. That's the same combination that's used for the declaration of other functions that don't return. --- include/tradstdc.h | 145 ++++++++++++++++++++++++++++++++---------- sys/unix/Makefile.top | 5 +- 2 files changed, 116 insertions(+), 34 deletions(-) diff --git a/include/tradstdc.h b/include/tradstdc.h index b607d4a99..ddbf040fb 100644 --- a/include/tradstdc.h +++ b/include/tradstdc.h @@ -317,6 +317,89 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ #undef signed #endif +/* + * Language + * Standard + * + * NetHack 3.7 range + * / + * / + * C2y X NetHack 3.6 and earlier range + * C23 X / + * C17 X X + * C11 X X + * C99 X X + * C89 X + * + * + * The NetHack 3.7 source code currently makes use of the following + * C99 (and above) language features: + * + * commas at the end of enumerator lists + * variable declarations in for loop initializers + * mixing declarations and code + * variable declaration in for loop + * variadic macros + * 'long long' + * + * The NetHack 3.7 source code adheres to the following greater-than C99 + * language restrictions: + * + * Removal of K&R function definitions + * Removal of implicit int + */ + +/* + * Provide a shorthand way of checking for a certain C standard + * in the NetHack header files by always setting NH_C to one + * of three possible values (as of January 2025): + * + * NH_C > 202300L Being compiled under C23 or greater + * NH_C > 199900L Being compiled under C99 or greater + * NH_C > 198900L Being compiled under C89 or greater, + * or C std could not be determined. + */ +#if defined(__STDC_VERSION__) || defined(__cplusplus) +#if (__STDC_VERSION__ >= 202000L) || defined(__cplusplus) +#define NH_C 202300L +#else +#define NH_C 199900L +#endif /* C23 or C99 */ +#else /* __STDC_VERSION not defined */ +#define NH_C 198900L +#endif /* __STDC_VERSION not defined */ +#ifndef NH_C +#define NH_C 198900L +#endif + +/* NH_C is now defined to 198900L or 199900L or 202300L */ + +#if NH_C >= 202300L +/* Give first priority to standard */ +#ifndef __has_c_attribute +#define __has_c_attribute(x) 0 +#endif +/* + * noreturn + */ +#ifndef ATTRNORETURN +#define ATTRNORETURN [[noreturn]] +/* #warning [[noreturn]] from C23 */ +#endif /* ATTRNORETURN not defined */ +/* + * fallthrough + */ +#if __has_c_attribute(fallthrough) +/* Standard attribute is available, use it. */ +#define FALLTHROUGH [[fallthrough]] +/* #warning [[fallthrough]] from C23 */ +#endif /* __has_c_attribute(fallthrough) */ +#endif /* NH_C >= 202300L */ + +/* + * Compiler-specific + */ + #ifdef __clang__ /* clang's gcc emulation is sufficient for nethack's usage */ #ifndef __GNUC__ @@ -325,26 +408,10 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ #endif /* - * Give first priority to standard - */ -#if defined(__STDC_VERSION__) || defined(__cplusplus) -#if (__STDC_VERSION__ > 202300L) || defined(__cplusplus) -#ifndef ATTRNORETURN -#define ATTRNORETURN [[noreturn]] -#endif -#ifndef __has_c_attribute -#define __has_c_attribute(x) 0 -#endif /* __has_c_attribute */ -#if __has_c_attribute(fallthrough) -/* Standard attribute is available, use it. */ -#define FALLTHROUGH [[fallthrough]] -#endif /* __has_c_attribute(fallthrough) */ -#endif /* __STDC_VERSION__ gt 202300L || __cplusplus */ -#endif /* __STDC_VERSION || __cplusplus */ - -/* - * Allow gcc2 to check parameters of printf-like calls with -Wformat; - * append this to a prototype declaration (see pline() in extern.h). + * gcc (and also clang which masquerades as__GNUC__==5 due to #define above) + * + * Allow gcc2 and above to check parameters of printf-like calls with + * -Wformat; append this to a prototype declaration (see pline() in extern.h). */ #ifdef __GNUC__ #if (__GNUC__ >= 2) && !defined(USE_OLDARGS) @@ -358,15 +425,10 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ #ifndef ATTRNORETURN #ifndef NORETURN #define NORETURN __attribute__((noreturn)) -#endif -#endif -#if (!defined(__linux__) && !defined(MACOS)) || defined(GCC_URWARN) -/* disable gcc's __attribute__((__warn_unused_result__)) since explicitly - discarding the result by casting to (void) is not accepted as a 'use' */ -#define __warn_unused_result__ /*empty*/ -#define warn_unused_result /*empty*/ -#endif -#endif +/* #warning NORETURN __attribute__((noreturn)) from __GNUC__ >= 3 */ +#endif /* NORETURN */ +#endif /* ATTRNORETURN */ +#endif /* __GNUC__ >= 3 */ #if __GNUC__ >= 5 #ifndef NONNULLS_DEFINED #define DO_DEFINE_NONNULLS @@ -374,13 +436,24 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ /* #pragma message is available */ #define NH_PRAGMA_MESSAGE 1 #endif /* __GNUC__ greater than or equal to 5 */ -#endif /* __GNUC__ */ +#if (!defined(__linux__) && !defined(MACOS)) || defined(GCC_URWARN) +/* disable gcc's __attribute__((__warn_unused_result__)) since explicitly + discarding the result by casting to (void) is not accepted as a 'use' */ +#define __warn_unused_result__ /*empty*/ +#define warn_unused_result /*empty*/ +#endif /* GCC_URWARN || !__linux || !MACOS */ +#endif /* __GNUC__ || clang masquerading as __GNUC__==5 */ +/* + * clang-specific + * + */ #if defined(__clang__) #ifndef FALLTHROUGH #if defined(__clang_major__) #if __clang_major__ >= 9 #define FALLTHROUGH __attribute__((fallthrough)) +/* #warning FALLTHROUGH __attribute__((fallthrough)) from clang */ #endif /* __clang_major__ greater than or equal to 9 */ #endif /* __clang_major__ is defined */ #endif /* FALLTHROUGH */ @@ -389,6 +462,9 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ #endif #endif /* __clang__ */ +/* + * NONNULL args + */ #if defined(DO_DEFINE_NONNULLS) && !defined(NONNULLS_DEFINED) #define NONNULL __attribute__((returns_nonnull)) #define NONNULLPTRS __attribute__((nonnull)) @@ -411,15 +487,20 @@ typedef genericptr genericptr_t; /* (void *) or (char *) */ #define NONNULLARG45 __attribute__((nonnull (4, 5))) /* do_screen_descri... */ #define NONNULLS_DEFINED #undef DO_DEFINE_NONNULLS -#endif /* __clang__ && !NONNULLS_DEFINED */ +#endif /* DO_DEFINE_NONNULLS && !NONNULLS_DEFINED */ +/* + * Microsoft compiler + */ #ifdef _MSC_VER #ifndef ATTRNORETURN #define ATTRNORETURN __declspec(noreturn) +/* #warning ATTRNORETURN __declspec(noreturn) from _MSC_VER */ #endif /* #pragma message is available */ #define NH_PRAGMA_MESSAGE 1 -#endif +#endif /* _MSC_VER */ + /* Fallback implementations */ #ifndef PRINTF_F diff --git a/sys/unix/Makefile.top b/sys/unix/Makefile.top index 45cd7330a..3d7b54727 100644 --- a/sys/unix/Makefile.top +++ b/sys/unix/Makefile.top @@ -150,8 +150,9 @@ $(TOPLUALIB): $(LUATOP)/liblua.a include/nhlua.h: $(TOPLUALIB) echo '/* nhlua.h - generated by top Makefile */' > $@ @echo '#include "../$(LUAHEADERS)/lua.h"' >> $@ - @sed -e '/(lua_error)/!d' -e '/(lua_error)/s/;/ NORETURN;/1' \ - < $(LUAHEADERS)/lua.h >> $@ + @sed -e '/(lua_error)/!d' \ + -e '/(lua_error)/s/LUA_API/ATTRNORETURN LUA_API/1' \ + -e '/(lua_error)/s/;/ NORETURN;/1' < $(LUAHEADERS)/lua.h >> $@ @echo '#include "../$(LUAHEADERS)/lualib.h"' >> $@ @echo '#include "../$(LUAHEADERS)/lauxlib.h"' >> $@ @echo '/*nhlua.h*/' >> $@