re-enable -Wunreachable-code under clang

whitelist the valid cases showing up

If an earlier version of clang is showing more cases (particularly
if they don't make sense), the re-enabling of the warning in
sys/unix/hints/include/compiler.2020 can be made clang-version
specific instead. I had no way to test earlier versions.
This commit is contained in:
nhmall
2021-10-22 12:27:10 -04:00
parent 0617410ff3
commit b44b5e35a8
8 changed files with 50 additions and 26 deletions

View File

@@ -50,6 +50,7 @@
#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL _Pragma("clang diagnostic pop")
#define RESTORE_WARNING_UNREACHABLE_CODE _Pragma("clang diagnostic pop")
#define RESTORE_WARNINGS _Pragma("clang diagnostic pop")
#define STDC_Pragma_AVAILABLE
@@ -64,6 +65,7 @@
#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL _Pragma("GCC diagnostic pop")
#define RESTORE_WARNING_UNREACHABLE_CODE _Pragma("GCC diagnostic pop")
#define RESTORE_WARNINGS _Pragma("GCC diagnostic pop")
#define STDC_Pragma_AVAILABLE
@@ -80,6 +82,7 @@
_Pragma("warning( disable : 4127 )")
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT _Pragma("warning( pop )")
#define RESTORE_WARNING_FORMAT_NONLITERAL _Pragma("warning( pop )")
#define RESTORE_WARNING_UNREACHABLE_CODE _Pragma("warning( pop )")
#define RESTORE_WARNINGS _Pragma("warning( pop )")
#define STDC_Pragma_AVAILABLE
#else /* Visual Studio prior to 2019 below */
@@ -94,6 +97,7 @@
__pragma(warning(disable:4127))
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT __pragma(warning(pop))
#define RESTORE_WARNING_FORMAT_NONLITERAL __pragma(warning(pop))
#define RESTORE_WARNING_UNREACHABLE_CODE __pragma(warning(pop))
#define RESTORE_WARNINGS __pragma(warning(pop))
#define STDC_Pragma_AVAILABLE
#endif /* vs2019 or vs2017 */
@@ -112,6 +116,7 @@
#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL
#define RESTORE_WARNING_UNREACHABLE_CODE
#define RESTORE_WARNINGS
#endif

View File

@@ -530,11 +530,14 @@ coord_desc(int x, int y, char *outbuf, char cmode)
/* for normal map sizes, force a fixed-width formatting so that
/m, /M, /o, and /O output lines up cleanly; map sizes bigger
than Nx999 or 999xM will still work, but not line up like normal
when displayed in a column setting */
when displayed in a column setting.
The (100) is placed in brackets below to mark the [: "03"] as
explicit compile-time dead code for clang */
if (!*screen_fmt)
Sprintf(screen_fmt, "[%%%sd,%%%sd]",
(ROWNO - 1 + 2 < 100) ? "02" : "03",
(COLNO - 1 < 100) ? "02" : "03");
(ROWNO - 1 + 2 < (100)) ? "02" : "03",
(COLNO - 1 < (100)) ? "02" : "03");
/* map line 0 is screen row 2;
map column 0 isn't used, map column 1 is screen column 1 */
Sprintf(outbuf, screen_fmt, y + 2, x);

View File

@@ -923,7 +923,10 @@ set_savefile_name(boolean regularize_it)
overflow = 2;
}
#ifdef SAVE_EXTENSION
if (strlen(SAVE_EXTENSION) > 0 && !overflow) {
/* (0) is placed in brackets below so that the [&& !overflow] is
explicit dead code (the ">" comparison is detected as always
FALSE at compile-time). Done to appease clang's -Wunreachable-code */
if (strlen(SAVE_EXTENSION) > (0) && !overflow) {
if (strlen(g.SAVEF) + strlen(SAVE_EXTENSION) < (SAVESIZE - 1)) {
Strcat(g.SAVEF, SAVE_EXTENSION);
#ifdef MSDOS

View File

@@ -579,10 +579,9 @@ m_throw(
potionhit(&g.youmonst, singleobj, POTHIT_MONST_THROW);
break;
}
oldumort = u.umortality;
switch (singleobj->otyp) {
int dam, hitv;
case EGG:
if (!touch_petrifies(&mons[singleobj->corpsenm])) {
impossible("monster throwing egg type %d",
@@ -596,24 +595,28 @@ m_throw(
hitu = thitu(8, 0, &singleobj, (char *) 0);
break;
default:
dam = dmgval(singleobj, &g.youmonst);
hitv = 3 - distmin(u.ux, u.uy, mon->mx, mon->my);
if (hitv < -4)
hitv = -4;
if (is_elf(mon->data)
&& objects[singleobj->otyp].oc_skill == P_BOW) {
hitv++;
if (MON_WEP(mon) && MON_WEP(mon)->otyp == ELVEN_BOW)
{
int dam, hitv;
dam = dmgval(singleobj, &g.youmonst);
hitv = 3 - distmin(u.ux, u.uy, mon->mx, mon->my);
if (hitv < -4)
hitv = -4;
if (is_elf(mon->data)
&& objects[singleobj->otyp].oc_skill == P_BOW) {
hitv++;
if (singleobj->otyp == ELVEN_ARROW)
dam++;
if (MON_WEP(mon) && MON_WEP(mon)->otyp == ELVEN_BOW)
hitv++;
if (singleobj->otyp == ELVEN_ARROW)
dam++;
}
if (bigmonst(g.youmonst.data))
hitv++;
hitv += 8 + singleobj->spe;
if (dam < 1)
dam = 1;
hitu = thitu(hitv, dam, &singleobj, (char *) 0);
}
if (bigmonst(g.youmonst.data))
hitv++;
hitv += 8 + singleobj->spe;
if (dam < 1)
dam = 1;
hitu = thitu(hitv, dam, &singleobj, (char *) 0);
}
if (hitu && singleobj->opoisoned && is_poisonable(singleobj)) {
char onmbuf[BUFSZ], knmbuf[BUFSZ];

View File

@@ -116,6 +116,8 @@ l_nhcore_call(int callidx)
}
}
DISABLE_WARNING_UNREACHABLE_CODE
void
nhl_error(lua_State *L, const char *msg)
{
@@ -136,8 +138,11 @@ nhl_error(lua_State *L, const char *msg)
#endif
(void) lua_error(L);
/*NOTREACHED*/
/* UNREACHABLE_CODE */
}
RESTORE_WARNING_UNREACHABLE_CODE
/* Check that parameters are nothing but single table,
or if no parameters given, put empty table there */
void

View File

@@ -1570,6 +1570,8 @@ create_subroom(
return TRUE;
}
DISABLE_WARNING_UNREACHABLE_CODE
/*
* Create a new door in a room.
* It's placed on a wall (north, south, east or west).
@@ -1659,6 +1661,7 @@ create_door(room_door* dd, struct mkroom* broom)
default:
x = y = 0;
panic("create_door: No wall for door!");
/*UNREACHABLE_CODE*/
goto outdirloop;
}
outdirloop:
@@ -1675,6 +1678,8 @@ create_door(room_door* dd, struct mkroom* broom)
levl[x][y].doormask = dd->mask;
}
RESTORE_WARNING_UNREACHABLE_CODE
/*
* Create a secret door in croom on any one of the specified walls.
*/

View File

@@ -32,10 +32,7 @@ CFLAGS+=-Wall -Wextra -Wno-missing-field-initializers \
CFLAGS+=-pedantic
CFLAGS+=-Wmissing-declarations
CFLAGS+=-Wformat-nonliteral
# As of LLVM build 2336.1.00, this gives dozens of spurious messages, so
# # leave it out by default.
# #CFLAGS+=-Wunreachable-code
# #
CFLAGS+=-Wunreachable-code
#
# the following are not allowed in C++
CFLAGS+=-Wimplicit

View File

@@ -23,6 +23,7 @@ boolean panicking;
void panic(const char *, ...);
DISABLE_WARNING_FORMAT_NONLITERAL
DISABLE_WARNING_UNREACHABLE_CODE
void panic
VA_DECL(const char *, str)
@@ -45,9 +46,11 @@ VA_DECL(const char *, str)
abort(); /* generate core dump */
#endif
VA_END();
/* UNREACHABLE_CODE */
exit(EXIT_FAILURE); /* redundant */
}
RESTORE_WARNING_UNREACHABLE_CODE
RESTORE_WARNING_FORMAT_NONLITERAL
#ifdef ALLOCA_HACK