suppress a particular warning for an individual function; useful for non-gcc

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.
This commit is contained in:
nhmall
2021-02-01 12:54:19 -05:00
parent 7445435a36
commit a6631e3bb0
12 changed files with 125 additions and 4 deletions

View File

@@ -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 */

View 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)

View 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)
{

View 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

View 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)