diff --git a/include/warnings.h b/include/warnings.h index 28da4d108..d0971893c 100644 --- a/include/warnings.h +++ b/include/warnings.h @@ -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 diff --git a/src/do_name.c b/src/do_name.c index 818fa41b8..029938c84 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -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); diff --git a/src/files.c b/src/files.c index 615f7a661..fe33836e1 100644 --- a/src/files.c +++ b/src/files.c @@ -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 diff --git a/src/mthrowu.c b/src/mthrowu.c index 89bf4f350..3644ebf0d 100644 --- a/src/mthrowu.c +++ b/src/mthrowu.c @@ -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]; diff --git a/src/nhlua.c b/src/nhlua.c index db4deb6c4..4f1787a5f 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -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 diff --git a/src/sp_lev.c b/src/sp_lev.c index d02e74a1f..4117305c8 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -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. */ diff --git a/sys/unix/hints/include/compiler.2020 b/sys/unix/hints/include/compiler.2020 index 4ba1e9b31..044b7faf2 100755 --- a/sys/unix/hints/include/compiler.2020 +++ b/sys/unix/hints/include/compiler.2020 @@ -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 diff --git a/util/panic.c b/util/panic.c index d598e0378..d15bec88c 100644 --- a/util/panic.c +++ b/util/panic.c @@ -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