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 DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT #define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL _Pragma("clang diagnostic pop") #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 RESTORE_WARNINGS _Pragma("clang diagnostic pop")
#define STDC_Pragma_AVAILABLE #define STDC_Pragma_AVAILABLE
@@ -64,6 +65,7 @@
#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT #define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT #define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL _Pragma("GCC diagnostic pop") #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 RESTORE_WARNINGS _Pragma("GCC diagnostic pop")
#define STDC_Pragma_AVAILABLE #define STDC_Pragma_AVAILABLE
@@ -80,6 +82,7 @@
_Pragma("warning( disable : 4127 )") _Pragma("warning( disable : 4127 )")
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT _Pragma("warning( pop )") #define RESTORE_WARNING_CONDEXPR_IS_CONSTANT _Pragma("warning( pop )")
#define RESTORE_WARNING_FORMAT_NONLITERAL _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 RESTORE_WARNINGS _Pragma("warning( pop )")
#define STDC_Pragma_AVAILABLE #define STDC_Pragma_AVAILABLE
#else /* Visual Studio prior to 2019 below */ #else /* Visual Studio prior to 2019 below */
@@ -94,6 +97,7 @@
__pragma(warning(disable:4127)) __pragma(warning(disable:4127))
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT __pragma(warning(pop)) #define RESTORE_WARNING_CONDEXPR_IS_CONSTANT __pragma(warning(pop))
#define RESTORE_WARNING_FORMAT_NONLITERAL __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 RESTORE_WARNINGS __pragma(warning(pop))
#define STDC_Pragma_AVAILABLE #define STDC_Pragma_AVAILABLE
#endif /* vs2019 or vs2017 */ #endif /* vs2019 or vs2017 */
@@ -112,6 +116,7 @@
#define DISABLE_WARNING_CONDEXPR_IS_CONSTANT #define DISABLE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_CONDEXPR_IS_CONSTANT #define RESTORE_WARNING_CONDEXPR_IS_CONSTANT
#define RESTORE_WARNING_FORMAT_NONLITERAL #define RESTORE_WARNING_FORMAT_NONLITERAL
#define RESTORE_WARNING_UNREACHABLE_CODE
#define RESTORE_WARNINGS #define RESTORE_WARNINGS
#endif #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 /* for normal map sizes, force a fixed-width formatting so that
/m, /M, /o, and /O output lines up cleanly; map sizes bigger /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 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) if (!*screen_fmt)
Sprintf(screen_fmt, "[%%%sd,%%%sd]", Sprintf(screen_fmt, "[%%%sd,%%%sd]",
(ROWNO - 1 + 2 < 100) ? "02" : "03", (ROWNO - 1 + 2 < (100)) ? "02" : "03",
(COLNO - 1 < 100) ? "02" : "03"); (COLNO - 1 < (100)) ? "02" : "03");
/* map line 0 is screen row 2; /* map line 0 is screen row 2;
map column 0 isn't used, map column 1 is screen column 1 */ map column 0 isn't used, map column 1 is screen column 1 */
Sprintf(outbuf, screen_fmt, y + 2, x); Sprintf(outbuf, screen_fmt, y + 2, x);

View File

@@ -923,7 +923,10 @@ set_savefile_name(boolean regularize_it)
overflow = 2; overflow = 2;
} }
#ifdef SAVE_EXTENSION #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)) { if (strlen(g.SAVEF) + strlen(SAVE_EXTENSION) < (SAVESIZE - 1)) {
Strcat(g.SAVEF, SAVE_EXTENSION); Strcat(g.SAVEF, SAVE_EXTENSION);
#ifdef MSDOS #ifdef MSDOS

View File

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

View File

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

View File

@@ -1570,6 +1570,8 @@ create_subroom(
return TRUE; return TRUE;
} }
DISABLE_WARNING_UNREACHABLE_CODE
/* /*
* Create a new door in a room. * Create a new door in a room.
* It's placed on a wall (north, south, east or west). * It's placed on a wall (north, south, east or west).
@@ -1659,6 +1661,7 @@ create_door(room_door* dd, struct mkroom* broom)
default: default:
x = y = 0; x = y = 0;
panic("create_door: No wall for door!"); panic("create_door: No wall for door!");
/*UNREACHABLE_CODE*/
goto outdirloop; goto outdirloop;
} }
outdirloop: outdirloop:
@@ -1675,6 +1678,8 @@ create_door(room_door* dd, struct mkroom* broom)
levl[x][y].doormask = dd->mask; levl[x][y].doormask = dd->mask;
} }
RESTORE_WARNING_UNREACHABLE_CODE
/* /*
* Create a secret door in croom on any one of the specified walls. * 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+=-pedantic
CFLAGS+=-Wmissing-declarations CFLAGS+=-Wmissing-declarations
CFLAGS+=-Wformat-nonliteral CFLAGS+=-Wformat-nonliteral
# As of LLVM build 2336.1.00, this gives dozens of spurious messages, so CFLAGS+=-Wunreachable-code
# # leave it out by default.
# #CFLAGS+=-Wunreachable-code
# #
# #
# the following are not allowed in C++ # the following are not allowed in C++
CFLAGS+=-Wimplicit CFLAGS+=-Wimplicit

View File

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