expand support for noreturn declarations

Although gcc specifies support for declaring a function as
noreturn after the function name and parameters, other compilers
do so via an attribute at the start of the declaration. Add some
macro support for the attribute-at-the-beginning method:
  o MS Visual Studio compiler
  o Upcoming C23 standard (untested at this point)
This commit is contained in:
nhmall
2022-11-24 00:51:42 -05:00
parent ddf1dfde29
commit 4b04b1e6ac
13 changed files with 173 additions and 19 deletions

View File

@@ -264,6 +264,8 @@ Fread(genericptr_t ptr, int size, int nitems, dlb *stream)
}
#endif
DISABLE_WARNING_UNREACHABLE_CODE
static xint16
dname_to_dnum(const char *s)
{
@@ -278,6 +280,8 @@ dname_to_dnum(const char *s)
return (xint16) 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
s_level *
find_level(const char *s)
{
@@ -317,6 +321,8 @@ find_branch(const char *s, /* dungeon name */
return i;
}
DISABLE_WARNING_UNREACHABLE_CODE
/*
* Find the "parent" by searching the prototype branch list for the branch
* listing, then figuring out to which dungeon it belongs.
@@ -342,6 +348,8 @@ parent_dnum(const char *s, /* dungeon name */
return (xint16) 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/*
* Return a starting point and number of successive positions a level
* or dungeon entrance can occupy.
@@ -592,6 +600,8 @@ possible_places(int idx, /* prototype index */
return count;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* Pick the nth TRUE entry in the given boolean array. */
static xint16
pick_level(boolean *map, /* an array MAXLEVEL+1 in size */
@@ -602,9 +612,12 @@ pick_level(boolean *map, /* an array MAXLEVEL+1 in size */
if (map[i] && !nth--)
return i;
panic("pick_level: ran out of valid levels");
/*NOTREACHED*/
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
#ifdef DDEBUG
static void indent(int);
@@ -1271,6 +1284,8 @@ maxledgerno(void)
+ g.dungeons[g.n_dgns - 1].num_dunlevs);
}
DISABLE_WARNING_UNREACHABLE_CODE
/* return the dungeon that this ledgerno exists in */
xint16
ledger_to_dnum(xint16 ledgerno)
@@ -1289,6 +1304,8 @@ ledger_to_dnum(xint16 ledgerno)
return (xint16) 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* return the level of the dungeon this ledgerno exists in */
xint16
ledger_to_dlev(xint16 ledgerno)

View File

@@ -596,7 +596,7 @@ fixup_death(int how)
DISABLE_WARNING_FORMAT_NONLITERAL
/*VARARGS1*/
void
ATTRNORETURN void
panic VA_DECL(const char *, str)
{
VA_START(str);
@@ -1780,7 +1780,7 @@ container_contents(
}
/* should be called with either EXIT_SUCCESS or EXIT_FAILURE */
void
ATTRNORETURN void
nh_terminate(int status)
{
g.program_state.in_moveloop = 0; /* won't be returning to normal play */

View File

@@ -765,6 +765,8 @@ same_race(struct permonst* pm1, struct permonst* pm2)
return FALSE;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* return an index into the mons array */
int
monsndx(struct permonst* ptr)
@@ -775,11 +777,14 @@ monsndx(struct permonst* ptr)
if (i < LOW_PM || i >= NUMMONS) {
panic("monsndx - could not index monster (%s)",
fmt_ptr((genericptr_t) ptr));
/*NOTREACHED*/
return NON_PM; /* will not get here */
}
return i;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* for handling alternate spellings */
struct alt_spl {
const char *name;

View File

@@ -158,6 +158,8 @@ nhl_obj_u_giveobj(lua_State *L)
return 0;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* Get a table of object class data. */
/* local odata = obj.class(otbl.otyp); */
/* local odata = obj.class(obj.new("rock")); */
@@ -171,6 +173,7 @@ l_obj_objects_to_table(lua_State *L)
if (argc != 1) {
nhl_error(L, "l_obj_objects_to_table: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -230,6 +233,8 @@ l_obj_objects_to_table(lua_State *L)
return 1;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* Create a lua table representation of the object, unpacking all the
object fields.
local o = obj.new("rock");
@@ -331,6 +336,8 @@ l_obj_to_table(lua_State *L)
return 1;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* create a new object via wishing routine */
/* local o = obj.new("rock"); */
static int
@@ -348,6 +355,7 @@ l_obj_new_readobjnam(lua_State *L)
return 1;
} else
nhl_error(L, "l_obj_new_readobjname: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -370,6 +378,7 @@ l_obj_at(lua_State *L)
return 1;
} else
nhl_error(L, "l_obj_at: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -401,6 +410,8 @@ l_obj_placeobj(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* Get the next object in the object chain */
/* local o = obj.at(x, y);
local o2 = o:next(true);
@@ -453,6 +464,8 @@ l_obj_isnull(lua_State *L)
return 1;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* does object have a timer of certain type? */
/* local hastimer = o:has_timer("rot-organic"); */
static int
@@ -476,6 +489,7 @@ l_obj_timer_has(lua_State *L)
return 0;
}
/* peek at an object timer. return the turn when timer triggers.
returns 0 if no such timer attached to the object. */
/* local timeout = o:peek_timer("hatch-egg"); */
@@ -497,6 +511,7 @@ l_obj_timer_peek(lua_State *L)
}
} else
nhl_error(L, "l_obj_timer_peek: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -533,6 +548,8 @@ l_obj_timer_stop(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* start an object timer. */
/* o:start_timer("hatch-egg", 10); */
static int

View File

@@ -145,6 +145,8 @@ l_selection_clone(lua_State *L)
return 1;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* selection.set(sel, x, y); */
/* selection.set(sel, x, y, value); */
/* local sel = selection.set(); */
@@ -207,6 +209,7 @@ l_selection_getpoint(lua_State *L)
lua_remove(L, 1); /* sel */
if (!nhl_get_xy_params(L, &ix, &iy)) {
nhl_error(L, "l_selection_getpoint: Incorrect params");
/*NOTREACHED*/
return 0;
}
x = (coordxy) ix;
@@ -224,6 +227,8 @@ l_selection_getpoint(lua_State *L)
return 1;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* local s = selection.negate(sel); */
/* local s = selection.negate(); */
/* local s = sel:negate(); */

View File

@@ -168,7 +168,7 @@ l_nhcore_call(int callidx)
DISABLE_WARNING_UNREACHABLE_CODE
void
ATTRNORETURN void
nhl_error(lua_State *L, const char *msg)
{
lua_Debug ar;
@@ -188,7 +188,6 @@ nhl_error(lua_State *L, const char *msg)
#endif
(void) lua_error(L);
/*NOTREACHED*/
/* UNREACHABLE_CODE */
}
RESTORE_WARNING_UNREACHABLE_CODE
@@ -209,6 +208,8 @@ lcheck_param_table(lua_State *L)
luaL_checktype(L, 1, LUA_TTABLE);
}
DISABLE_WARNING_UNREACHABLE_CODE
schar
get_table_mapchr(lua_State *L, const char *name)
{
@@ -257,6 +258,8 @@ nhl_get_timertype(lua_State *L, int idx)
return ret;
}
RESTORE_WARNING_UNREACHABLE_CODE
void
nhl_add_table_entry_int(lua_State *L, const char *name, lua_Integer value)
{
@@ -375,6 +378,8 @@ splev_typ2chr(schar typ)
return 'x';
}
DISABLE_WARNING_UNREACHABLE_CODE
/* local t = nh.gettrap(x,y); */
/* local t = nh.gettrap({ x = 10, y = 10 }); */
static int
@@ -385,6 +390,7 @@ nhl_gettrap(lua_State *L)
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "Incorrect arguments");
/*NOTREACHED*/
return 0;
}
x = (coordxy) lx;
@@ -436,6 +442,7 @@ nhl_deltrap(lua_State *L)
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "Incorrect arguments");
/*NOTREACHED*/
return 0;
}
x = (coordxy) lx;
@@ -451,6 +458,8 @@ nhl_deltrap(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* get parameters (XX,YY) or ({ x = XX, y = YY }) or ({ XX, YY }),
and set the x and y values.
return TRUE if there are such params in the stack.
@@ -574,8 +583,6 @@ nhl_getmap(lua_State *L)
}
}
RESTORE_WARNING_CONDEXPR_IS_CONSTANT
/* impossible("Error!") */
static int
nhl_impossible(lua_State *L)
@@ -664,6 +671,7 @@ nhl_getlin(lua_State *L)
}
nhl_error(L, "Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -688,6 +696,7 @@ nhl_menu(lua_State *L)
if (argc < 2 || argc > 4) {
nhl_error(L, "Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -874,6 +883,8 @@ nhl_level_difficulty(lua_State *L)
return 1;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* get mandatory integer value from table */
int
get_table_int(lua_State *L, const char *name)
@@ -1150,6 +1161,8 @@ nhl_debug_flags(lua_State *L)
return 0;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* does location at x,y have timer? */
/* local has_melttimer = nh.has_timer_at(x,y, "melt-ice"); */
/* local has_melttimer = nh.has_timer_at({x=4,y=7}, "melt-ice"); */
@@ -1165,6 +1178,7 @@ nhl_timer_has_at(lua_State *L)
lua_pop(L, 1); /* remove timertype */
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "nhl_timer_has_at: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -1194,6 +1208,7 @@ nhl_timer_peek_at(lua_State *L)
lua_pop(L, 1); /* remove timertype */
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "nhl_timer_peek_at: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -1220,6 +1235,7 @@ nhl_timer_stop_at(lua_State *L)
lua_pop(L, 1); /* remove timertype */
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "nhl_timer_stop_at: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -1245,6 +1261,7 @@ nhl_timer_start_at(lua_State *L)
lua_pop(L, 2); /* remove when and timertype */
if (!nhl_get_xy_params(L, &lx, &ly)) {
nhl_error(L, "nhl_timer_start_at: Wrong args");
/*NOTREACHED*/
return 0;
}
@@ -1262,6 +1279,8 @@ nhl_timer_start_at(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
static const struct luaL_Reg nhl_functions[] = {
{"test", nhl_test},
@@ -1357,6 +1376,8 @@ nhl_push_anything(lua_State *L, int anytype, void *src)
return 1;
}
DISABLE_WARNING_UNREACHABLE_CODE
static int
nhl_meta_u_index(lua_State *L)
{
@@ -1417,6 +1438,7 @@ nhl_meta_u_index(lua_State *L)
}
nhl_error(L, "Unknown u table index");
/*NOTREACHED*/
return 0;
}
@@ -1424,9 +1446,12 @@ static int
nhl_meta_u_newindex(lua_State *L)
{
nhl_error(L, "Cannot set u table values");
/*NOTREACHED*/
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
static int
nhl_u_clear_inventory(lua_State *L UNUSED)
{
@@ -1790,7 +1815,7 @@ get_lua_version(void)
return (const char *) g.lua_ver;
}
RESTORE_WARNINGS
RESTORE_WARNING_CONDEXPR_IS_CONSTANT
/***
*** SANDBOX / HARDENING CODE
@@ -2297,6 +2322,8 @@ nhl_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
return re_alloc(ptr, nsize);
}
DISABLE_WARNING_UNREACHABLE_CODE
static int
nhl_panic(lua_State *L)
{
@@ -2305,9 +2332,12 @@ nhl_panic(lua_State *L)
if (msg == NULL)
msg = "error object is not a string";
panic("unprotected error in call to Lua API (%s)\n", msg);
/*NOTREACHED*/
return 0; /* return to Lua to abort */
}
RESTORE_WARNING_UNREACHABLE_CODE
/* called when lua issues a warning message; the text of the message
is passed to us in pieces across multiple function calls */
static void

View File

@@ -2988,6 +2988,8 @@ l_push_mkroom_table(lua_State *L, struct mkroom *tmpr)
nhl_add_table_entry_str(L, "type", get_mkroom_name(tmpr->rtype));
}
DISABLE_WARNING_UNREACHABLE_CODE
/* message("What a strange feeling!"); */
int
lspo_message(lua_State *L)
@@ -3000,6 +3002,7 @@ lspo_message(lua_State *L)
if (argc < 1) {
nhl_error(L, "Wrong parameters");
/*NOTREACHED*/
return 0;
}
@@ -3024,6 +3027,8 @@ lspo_message(lua_State *L)
return 0; /* number of results */
}
RESTORE_WARNING_UNREACHABLE_CODE
static int
get_table_align(lua_State *L)
{
@@ -3295,6 +3300,8 @@ lspo_monster(lua_State *L)
return 0;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* the hash key 'name' is an integer or "random",
or if not existent, also return rndval */
static lua_Integer
@@ -3321,6 +3328,7 @@ get_table_int_or_random(lua_State *L, const char *name, int rndval)
else
Strcat(buf, "<Null>");
nhl_error(L, buf);
/*NOTREACHED*/
lua_pop(L, 1);
return 0;
}
@@ -3329,6 +3337,8 @@ get_table_int_or_random(lua_State *L, const char *name, int rndval)
return ret;
}
RESTORE_WARNING_UNREACHABLE_CODE
static int
get_table_buc(lua_State *L)
{
@@ -4300,6 +4310,8 @@ lspo_trap(lua_State *L)
return 0;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* gold(500, 3,5); */
/* gold(500, {5, 6}); */
/* gold({ amount = 500, x = 2, y = 5 });*/
@@ -4333,6 +4345,7 @@ lspo_gold(lua_State *L)
x = gx, y = gy;
} else {
nhl_error(L, "Wrong parameters");
/*NOTREACHED*/
return 0;
}
@@ -4349,6 +4362,8 @@ lspo_gold(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* corridor({ srcroom=1, srcdoor=2, srcwall="north", destroom=2, destdoor=1, destwall="west" });*/
int
lspo_corridor(lua_State *L)
@@ -5312,6 +5327,8 @@ cvt_to_relcoord(coordxy *x, coordxy *y)
}
}
DISABLE_WARNING_UNREACHABLE_CODE
/* convert map-relative coordinate to absolute.
local ax,ay = nh.abscoord(rx, ry);
local pt = nh.abscoord({ x = 10, y = 5 });
@@ -5339,6 +5356,7 @@ nhl_abs_coord(lua_State *L)
return 1;
} else {
nhl_error(L, "nhl_abs_coord: Wrong args");
/*NOTREACHED*/
return 0;
}
}
@@ -5417,6 +5435,8 @@ lspo_feature(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/*
* [lit_state: 1 On, 0 Off, -1 random, -2 leave as-is]
* terrain({ x=NN, y=NN, typ=MAPCHAR, lit=lit_state });
@@ -5701,6 +5721,8 @@ ensure_way_out(void)
selection_free(ov, TRUE);
}
DISABLE_WARNING_UNREACHABLE_CODE
static lua_Integer
get_table_intarray_entry(lua_State *L, int tableidx, int entrynum)
{
@@ -5746,6 +5768,7 @@ get_table_region(
lua_pop(L, 1);
if (arrlen != 4) {
nhl_error(L, "Not a region");
/*NOTREACHED*/
lua_pop(L, 1);
return 0;
}
@@ -5784,6 +5807,7 @@ get_coord(lua_State *L, int i, lua_Integer *x, lua_Integer *y)
ret = TRUE;
} else {
nhl_error(L, "Not a coordinate");
/*NOTREACHED*/
return FALSE;
}
} else {
@@ -5792,6 +5816,7 @@ get_coord(lua_State *L, int i, lua_Integer *x, lua_Integer *y)
lua_pop(L, 1);
if (arrlen != 2) {
nhl_error(L, "Not a coordinate");
/*NOTREACHED*/
return FALSE;
}
@@ -5807,6 +5832,8 @@ get_coord(lua_State *L, int i, lua_Integer *x, lua_Integer *y)
return ret;
}
RESTORE_WARNING_UNREACHABLE_CODE
static void
levregion_add(lev_region* lregion)
{
@@ -5944,6 +5971,8 @@ add_doors_to_room(struct mkroom *croom)
maybe_add_door(x, y, croom);
}
DISABLE_WARNING_UNREACHABLE_CODE
/* region(selection, lit); */
/* region({ x1=NN, y1=NN, x2=NN, y2=NN, lit=BOOL, type=ROOMTYPE, joined=BOOL,
irregular=BOOL, filled=NN [ , contents = FUNCTION ] }); */
@@ -6005,6 +6034,7 @@ lspo_region(lua_State *L)
return 0;
} else {
nhl_error(L, "Wrong parameters");
/*NOTREACHED*/
return 0;
}
@@ -6087,6 +6117,8 @@ lspo_region(lua_State *L)
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
/* drawbridge({ dir="east", state="closed", x=05,y=08 }); */
/* drawbridge({ dir="east", state="closed", coord={05,08} }); */
int
@@ -6427,6 +6459,8 @@ lspo_finalize_level(lua_State *L UNUSED)
return 0;
}
DISABLE_WARNING_UNREACHABLE_CODE
/* map({ x = 10, y = 10, map = [[...]] }); */
/* map({ coord = {10, 10}, map = [[...]] }); */
/* map({ halign = "center", valign = "center", map = [[...]] }); */
@@ -6489,6 +6523,7 @@ TODO: g.coder->croom needs to be updated
if (!mf) {
nhl_error(L, "Map data error");
/*NOTREACHED*/
return 0;
}
@@ -6682,6 +6717,8 @@ TODO: g.coder->croom needs to be updated
return 0;
}
RESTORE_WARNING_UNREACHABLE_CODE
struct selectionvar *
selection_from_mkroom(struct mkroom *croom)
{

View File

@@ -2316,6 +2316,8 @@ write_timer(NHFILE* nhfp, timer_element* timer)
}
}
DISABLE_WARNING_UNREACHABLE_CODE
/*
* Return TRUE if the object will stay on the level when the level is
* saved.
@@ -2336,6 +2338,7 @@ obj_is_local(struct obj* obj)
return mon_is_local(obj->ocarry);
}
panic("obj_is_local");
/*NOTREACHED*/
return FALSE;
}
@@ -2376,9 +2379,12 @@ timer_is_local(timer_element* timer)
return mon_is_local(timer->arg.a_monst);
}
panic("timer_is_local");
/*NOTREACHED*/
return FALSE;
}
RESTORE_WARNING_UNREACHABLE_CODE
/*
* Part of the save routine. Count up the number of timers that would
* be written. If write_it is true, actually write the timer.