]> granicus.if.org Git - nethack/commitdiff
suppress a particular warning for an individual function; useful for non-gcc
authornhmall <nhmall@nethack.org>
Mon, 1 Feb 2021 17:54:19 +0000 (12:54 -0500)
committernhmall <nhmall@nethack.org>
Mon, 1 Feb 2021 17:54:19 +0000 (12:54 -0500)
Microsoft and other non-GNU compilers don't recognize gcc tricks
like  /*NOTREACHED*/ to suppress individual warnings. clang recognizes most
of them because it tries to be gcc-compatible. Because of that, a lot of
potentially useful warnings have had to be completely suppressed in the
past in all source files when using the non-gcc compatible compilers.

Now that the code is C99, take advantage of a way to suppress warnings for
individual functions, a big step up from suppressing the warnings
altogether.

Unfortunately, it does require a bit of ugliness caused by the
insertion of some macros in a few spots, but I'm not aware of
a cleaner alternative that still allows warnings to be enabled
in general, while suppressing a warning for known white-listed
instances.

Prior to the warning-tiggering function, place whichever one of
the following is needed to suppress the warning being encountered:

DISABLE_WARNING_UNREACHABLE_CODE
DISABLE_WARNING_CONDEXPR_IS_CONSTANT

After the warning-triggering function, place this:

RESTORE_WARNINGS

Under the hood, the compiler-appropriate warning-disabling
mechanics involve the use of C99 _Pragma, which can be used
in macros.

For unrecognized or inappropriate compilers, or if
DISABLE_WARNING_PRAGMAS is defined, the macros expand
to nothing.

12 files changed:
include/global.h
include/warnings.h [new file with mode: 0644]
src/cmd.c
src/do_name.c
src/muse.c
src/nhlua.c
src/wizard.c
sys/share/uudecode.c
sys/unix/Makefile.src
util/makedefs.c
win/share/tile2bmp.c
win/share/tilemap.c

index 787159fb44b5843ef3e7bf16fd921612214b2750..2163e7659b862ff3de733316a51085d4d2405adb 100644 (file)
@@ -165,6 +165,8 @@ extern struct cross_target_s cross_target;
 #include "ntconf.h"
 #endif
 
+#include "warnings.h"
+
 /* amiconf.h needs to be the last nested #include of config.h because
    'make depend' will turn it into a comment, hiding anything after it */
 #ifdef AMIGA
diff --git a/include/warnings.h b/include/warnings.h
new file mode 100644 (file)
index 0000000..fdbe99a
--- /dev/null
@@ -0,0 +1,74 @@
+/* NetHack 3.7 warnings.h      $NHDT-Date: 1596498562 2020/08/03 23:49:22 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.24 $ */
+/* Copyright (c) Michael Allison, 2021. */
+
+#ifndef WARNINGS_H
+#define WARNINGS_H
+
+/*
+ * If ENABLE_WARNING_PRAGMAS is defined, the checks for various
+ * compilers is activated.
+ *
+ * If a suitable compiler is found, STDC_Pragma_AVAILABLE will be defined.
+ * When STDC_Pragma_AVAILABLE is not defined, these are defined as no-ops:
+ *     DISABLE_WARNING_UNREACHABLE_CODE
+ *     DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+ *       ...
+ *     RESTORE_WARNINGS
+ *
+ */
+
+#if !defined(DISABLE_WARNING_PRAGMAS)
+#if defined(__STDC_VERSION__)
+#if __STDC_VERSION__ >= 199901L
+#define ACTIVATE_WARNING_PRAGMAS
+#endif /* __STDC_VERSION >= 199901L */
+#endif /* __STDC_VERSION */
+#if defined(_MSC_VER)
+#ifndef ACTIVATE_WARNING_PRAGMAS
+#define ACTIVATE_WARNING_PRAGMAS
+#endif
+#endif
+
+#ifdef ACTIVATE_WARNING_PRAGMAS
+#if defined(__clang__)
+#define DISABLE_WARNING_UNREACHABLE_CODE \
+                           _Pragma("clang diagnostic push") \
+                           _Pragma("clang diagnostic ignored \"-Wunreachable-code\"")
+#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+#define RESTORE_WARNINGS _Pragma("clang diagnostic pop")
+#define STDC_Pragma_AVAILABLE
+
+#elif defined(__GNUC__)
+/* unlike in clang, -Wunreachable-code does not function in later versions of gcc */
+#define DISABLE_WARNING_UNREACHABLE_CODE \
+                           _Pragma("GCC diagnostic push") \
+                           _Pragma("GCC diagnostic ignored \"-Wunreachable-code\"")
+#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+#define RESTORE_WARNINGS _Pragma("GCC diagnostic pop")
+#define STDC_Pragma_AVAILABLE
+
+#elif defined(_MSC_VER)
+#define DISABLE_WARNING_UNREACHABLE_CODE \
+                           _Pragma("warning( push )") \
+                           _Pragma("warning( disable : 4702 )")
+#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT \
+                           _Pragma("warning( push )") \
+                           _Pragma("warning( disable : 4127 )")
+#define RESTORE_WARNINGS _Pragma("warning( pop )")
+#define STDC_Pragma_AVAILABLE
+
+#endif /* various compiler detections */
+#endif /* ACTIVATE_WARNING_PRAGMAS */
+#else  /* DISABLE_WARNING_PRAGMAS */
+#if defined(STDC_Pragma_AVAILABLE)
+#undef STDC_Pragma_AVAILABLE
+#endif
+#endif /* DISABLE_WARNING_PRAGMAS */
+
+#if !defined(STDC_Pragma_AVAILABLE)
+#define DISABLE_WARNING_UNREACHABLE_CODE
+#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+#deifne RESTORE_WARNINGS
+#endif
+
+#endif /* WARNINGS_H */
index b5a640658165dcf483363b71e58e758538217013..60b8574b9680b534183330a5edae9a787e7a2309 100644 (file)
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -1497,6 +1497,8 @@ wiz_levltyp_legend(void)
     return;
 }
 
+DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+
 /* #wizsmell command - test usmellmon(). */
 static int
 wiz_smell(void)
@@ -1537,6 +1539,8 @@ wiz_smell(void)
     return 0;
 }
 
+RESTORE_WARNINGS
+
 #define DEFAULT_TIMEOUT_INCR 30
 
 /* #wizinstrinsic command to set some intrinsics for testing */
index d7e2597c9a34acf71a09c9106307ef5470216a98..c78a5e1b2fe010490bf3af71702770b2667bc937 100644 (file)
@@ -335,6 +335,8 @@ gloc_filter_done(void)
     }
 }
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 static boolean
 gather_locs_interesting(int x, int y, int gloc)
 {
@@ -402,6 +404,8 @@ gather_locs_interesting(int x, int y, int gloc)
     return FALSE;
 }
 
+RESTORE_WARNINGS
+
 /* gather locations for monsters or objects shown on the map */
 static void
 gather_locs(coord **arr_p, int *cnt_p, int gloc)
index a968b590534115bc905dd406639e9359b29febfc..f2008ff6df180cfef31d1f85f7a740ebd1f0c072 100644 (file)
@@ -2027,6 +2027,8 @@ mloot_container(
     return res;
 }
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 use_misc(struct monst* mtmp)
 {
@@ -2240,6 +2242,8 @@ use_misc(struct monst* mtmp)
     return 0;
 }
 
+RESTORE_WARNINGS
+
 static void
 you_aggravate(struct monst* mtmp)
 {
index 43f9f403384f21ddcb5a2f3895c3089a4f28ccc3..8ca4d873af2de32a93e72779a88e6a8779fec3d0 100644 (file)
@@ -289,6 +289,8 @@ nhl_deltrap(lua_State *L)
     return 0;
 }
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 /* local loc = getmap(x,y) */
 static int
 nhl_getmap(lua_State *L)
@@ -380,6 +382,8 @@ nhl_getmap(lua_State *L)
     return 1;
 }
 
+RESTORE_WARNINGS
+
 /* pline("It hits!") */
 static int
 nhl_pline(lua_State *L)
@@ -1123,6 +1127,8 @@ load_lua(const char *name)
     return ret;
 }
 
+DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+
 const char *
 get_lua_version(void)
 {
@@ -1154,3 +1160,8 @@ get_lua_version(void)
     }
     return (const char *) g.lua_ver;
 }
+
+RESTORE_WARNINGS
+
+
+
index 8e16337a4b1dcf1d589040ed81f51944767887cd..59e0499a4a83abb889410d05f216825c73a16eac 100644 (file)
@@ -341,6 +341,8 @@ choose_stairs(xchar *sx, xchar *sy)
     }
 }
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 tactics(struct monst *mtmp)
 {
@@ -432,6 +434,8 @@ tactics(struct monst *mtmp)
     return 0;
 }
 
+RESTORE_WARNINGS
+
 /* are there any monsters mon could aggravate? */
 boolean
 has_aggravatables(struct monst *mon)
index 12b3d9339f2299fc85a31086444fc19928d8d906..4c3d8cc3cf9cadd2412e53f243770c58be789923 100644 (file)
@@ -81,12 +81,16 @@ static char sccsid[] = "@(#)uudecode.c      5.5 (Berkeley) 7/6/88";
 #include <stdlib.h>
 #endif
 
+#include "warnings.h"
+
 static void decode(FILE *, FILE *);
 static void outdec(char *, FILE *, int);
 
 /* single-character decode */
 #define DEC(c) (((c) - ' ') & 077)
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 main(int argc, char **argv)
 {
@@ -173,6 +177,8 @@ main(int argc, char **argv)
     return 0;
 }
 
+RESTORE_WARNINGS
+
 /*
  * copy from in to out, decoding as you go along.
  */
index 0bfb6421fed27f2e88ee35e170e3dcca8a23d1a6..415b404c20be48efb1c7d200cd4e551ace09fe43 100644 (file)
@@ -521,7 +521,7 @@ CSOURCES = $(HACKCSRC) $(SYSCSRC) $(WINCSRC) $(CHAINSRC) $(GENCSRC)
 HACKINCL = align.h artifact.h artilist.h attrib.h botl.h \
        color.h config.h config1.h context.h coord.h decl.h \
        display.h dlb.h dungeon.h engrave.h extern.h flag.h fnamesiz.h \
-       func_tab.h global.h hack.h lint.h mextra.h mfndpos.h \
+       func_tab.h global.h warnings.h hack.h lint.h mextra.h mfndpos.h \
        micro.h mkroom.h \
        monattk.h mondata.h monflag.h monst.h monsym.h obj.h objclass.h \
        optlist.h patchlevel.h pcconf.h permonst.h prop.h rect.h \
@@ -790,9 +790,9 @@ depend: ../sys/unix/depend.awk \
 
 # config.h timestamp
 $(CONFIG_H): ../include/config.h ../include/config1.h ../include/patchlevel.h \
-               ../include/tradstdc.h ../include/global.h ../include/coord.h \
-               ../include/vmsconf.h ../include/system.h ../include/nhlua.h \
-               ../include/unixconf.h ../include/pcconf.h ../include/micro.h \
+               ../include/tradstdc.h ../include/global.h ../include/warnings.h \
+               ../include/coord.h ../include/vmsconf.h ../include/system.h \
+               ../include/nhlua.h ../include/unixconf.h ../include/pcconf.h \
                ../include/ntconf.h ../include/fnamesiz.h
        touch $(CONFIG_H)
 # hack.h timestamp
index bc63790d6ef00322bdc1103757d6648c627064b5..ed17b202bb4224a51e6a900b22ce4afcde5266a6 100644 (file)
@@ -213,6 +213,8 @@ main(void)
 
 #else /* ! MAC */
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 main(int argc, char *argv[])
 {
@@ -246,6 +248,8 @@ main(int argc, char *argv[])
 }
 #endif
 
+RESTORE_WARNINGS
+
 void
 do_makedefs(char *options)
 {
index e29d8f7a7af5c8fde103d437e2d8268072a1a8b8..73edbce026b714faf0d97569d70fa0f7cbba02dd 100644 (file)
@@ -183,6 +183,8 @@ int yoffset, xoffset;
 char bmpname[128];
 FILE *fp;
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 main(int argc, char *argv[])
 {
@@ -268,6 +270,8 @@ main(int argc, char *argv[])
     return 0;
 }
 
+RESTORE_WARNINGS
+
 static void
 build_bmfh(BITMAPFILEHEADER* pbmfh)
 {
index 604f92448c18e968d1edbc663e9a72269493351b..4899042e3ddc4dd3b7663ac7d840a83853c4259a 100644 (file)
@@ -644,6 +644,8 @@ extern void monst_globals_init(void);
 extern void objects_globals_init(void);
 #endif
 
+DISABLE_WARNING_UNREACHABLE_CODE
+
 int
 main(int argc UNUSED, char *argv[] UNUSED)
 {
@@ -694,6 +696,8 @@ main(int argc UNUSED, char *argv[] UNUSED)
     return 0;
 }
 
+RESTORE_WARNINGS
+
 #endif /* TILETEXT */
 
 struct {