From 28e90769f74e55ea7c8f75146d5b33e0aa777da8 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Thu, 3 Jun 2021 16:55:08 +0200 Subject: [PATCH] Codechange: use "[[maybe_unused]]" instead of a wide variety of other ways we had While at it, replace OTTD_ASSERT with WITH_ASSERT, as this is always set if assert() is valid. No matter if NDEBUG is set or not. --- src/3rdparty/squirrel/squirrel/sqcompiler.cpp | 3 +-- src/3rdparty/squirrel/squirrel/sqvm.cpp | 6 +----- src/autoreplace_cmd.cpp | 6 ++---- src/core/pool_func.hpp | 8 ++++---- src/core/pool_type.hpp | 8 ++++---- src/engine.cpp | 3 +-- src/gfxinit.cpp | 3 +-- src/industry_cmd.cpp | 3 +-- src/misc/hashtable.hpp | 3 +-- src/pathfinder/npf/npf.cpp | 3 +-- src/saveload/saveload.cpp | 8 +------- src/script/squirrel.cpp | 3 +-- src/spritecache.cpp | 4 +--- src/stdafx.h | 5 ----- src/tgp.cpp | 4 +--- src/town_cmd.cpp | 18 ++++++++++-------- src/townname.cpp | 4 +--- src/tunnelbridge_cmd.cpp | 3 +-- src/vehicle.cpp | 4 +--- src/vehiclelist.cpp | 3 +-- src/widget.cpp | 8 ++------ 21 files changed, 37 insertions(+), 73 deletions(-) diff --git a/src/3rdparty/squirrel/squirrel/sqcompiler.cpp b/src/3rdparty/squirrel/squirrel/sqcompiler.cpp index 00e5df04f4..a80e08639b 100644 --- a/src/3rdparty/squirrel/squirrel/sqcompiler.cpp +++ b/src/3rdparty/squirrel/squirrel/sqcompiler.cpp @@ -835,8 +835,7 @@ public: nkeys++; SQInteger val = _fs->PopTarget(); SQInteger key = _fs->PopTarget(); - SQInteger attrs = hasattrs ? _fs->PopTarget():-1; - (void)attrs; // assert only + [[maybe_unused]] SQInteger attrs = hasattrs ? _fs->PopTarget():-1; assert((hasattrs && attrs == key-1) || !hasattrs); unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0); SQInteger table = _fs->TopTarget(); //<_can_suspend); @@ -1504,11 +1502,9 @@ bool SQVM::Call(SQObjectPtr &closure,SQInteger nparams,SQInteger stackbase,SQObj default: return false; } -#ifdef WITH_ASSERT if(!_suspended) { assert(_stackbase == prevstackbase); } -#endif return true; } diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index de5617a4d9..9e82944b64 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -596,8 +596,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON); /* Sell wagon */ - CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon)); - (void)ret; // assert only + [[maybe_unused]] CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon)); assert(ret.Succeeded()); new_vehs[i] = nullptr; @@ -650,8 +649,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon assert(Train::From(old_head)->GetNextUnit() == nullptr); for (int i = num_units - 1; i > 0; i--) { - CommandCost ret = CmdMoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false); - (void)ret; // assert only + [[maybe_unused]] CommandCost ret = CmdMoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false); assert(ret.Succeeded()); } } diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp index dea108f08d..3b2d24b145 100644 --- a/src/core/pool_func.hpp +++ b/src/core/pool_func.hpp @@ -33,9 +33,9 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) : first_free(0), first_unused(0), items(0), -#ifdef OTTD_ASSERT +#ifdef WITH_ASSERT checked(0), -#endif /* OTTD_ASSERT */ +#endif /* WITH_ASSERT */ cleaning(false), data(nullptr), alloc_cache(nullptr) @@ -133,10 +133,10 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size) { size_t index = this->FindFirstFree(); -#ifdef OTTD_ASSERT +#ifdef WITH_ASSERT assert(this->checked != 0); this->checked--; -#endif /* OTTD_ASSERT */ +#endif /* WITH_ASSERT */ if (index == NO_FREE_ITEM) { error("%s: no more free items", this->name); } diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index 33f952dde4..2201d5cb01 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -90,9 +90,9 @@ struct Pool : PoolBase { size_t first_free; ///< No item with index lower than this is free (doesn't say anything about this one!) size_t first_unused; ///< This and all higher indexes are free (doesn't say anything about first_unused-1 !) size_t items; ///< Number of used indexes (non-nullptr) -#ifdef OTTD_ASSERT +#ifdef WITH_ASSERT size_t checked; ///< Number of items we checked for -#endif /* OTTD_ASSERT */ +#endif /* WITH_ASSERT */ bool cleaning; ///< True if cleaning pool (deleting all items) Titem **data; ///< Pointer to array of pointers to Titem @@ -130,9 +130,9 @@ struct Pool : PoolBase { inline bool CanAllocate(size_t n = 1) { bool ret = this->items <= Tmax_size - n; -#ifdef OTTD_ASSERT +#ifdef WITH_ASSERT this->checked = ret ? n : 0; -#endif /* OTTD_ASSERT */ +#endif /* WITH_ASSERT */ return ret; } diff --git a/src/engine.cpp b/src/engine.cpp index 114cc1db6a..f330cf6048 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -537,8 +537,7 @@ void SetupEngines() /* Assert is safe; there won't be more than 256 original vehicles * in any case, and we just cleaned the pool. */ assert(Engine::CanAllocateItem()); - const Engine *e = new Engine(eid.type, eid.internal_id); - (void)e; // assert only + [[maybe_unused]] const Engine *e = new Engine(eid.type, eid.internal_id); assert(e->index == index); index++; } diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 713f6e5439..8816c8db37 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -101,8 +101,7 @@ static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl, uint end = *index_tbl++; do { - bool b = LoadNextSprite(start, file, sprite_id); - (void)b; // Unused without asserts + [[maybe_unused]] bool b = LoadNextSprite(start, file, sprite_id); assert(b); sprite_id++; } while (++start <= end); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index d88f7b692f..8be44488db 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2139,8 +2139,7 @@ static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAv uint32 seed2 = Random(); Industry *i = nullptr; size_t layout_index = RandomRange((uint32)indspec->layouts.size()); - CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, layout_index, seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i); - (void)ret; // assert only + [[maybe_unused]] CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, layout_index, seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i); assert(i != nullptr || ret.Failed()); return i; } diff --git a/src/misc/hashtable.hpp b/src/misc/hashtable.hpp index c7bb600dad..13dadc01ac 100644 --- a/src/misc/hashtable.hpp +++ b/src/misc/hashtable.hpp @@ -239,8 +239,7 @@ public: /** non-const item search & removal */ void Pop(Titem_ &item) { - bool ret = TryPop(item); - (void)ret; // assert only + [[maybe_unused]] bool ret = TryPop(item); assert(ret); } diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp index 0094521615..604ead52bb 100644 --- a/src/pathfinder/npf/npf.cpp +++ b/src/pathfinder/npf/npf.cpp @@ -1045,8 +1045,7 @@ static NPFFoundTargetData NPFRouteInternal(AyStarNode *start1, bool ignore_start _npf_aystar.user_data = user; /* GO! */ - int r = _npf_aystar.Main(); - (void)r; // assert only + [[maybe_unused]] int r = _npf_aystar.Main(); assert(r != AYSTAR_STILL_BUSY); if (result.best_bird_dist != 0) { diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index f056404b54..01f0e0fd3a 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -1474,14 +1474,12 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld) return 0; } -#ifdef OTTD_ASSERT - /** * Check whether the variable size of the variable in the saveload configuration * matches with the actual variable size. * @param sld The saveload configuration to test. */ -static bool IsVariableSizeRight(const SaveLoad &sld) +[[maybe_unused]] static bool IsVariableSizeRight(const SaveLoad &sld) { switch (sld.cmd) { case SL_VAR: @@ -1522,13 +1520,9 @@ static bool IsVariableSizeRight(const SaveLoad &sld) } } -#endif /* OTTD_ASSERT */ - bool SlObjectMember(void *ptr, const SaveLoad &sld) { -#ifdef OTTD_ASSERT assert(IsVariableSizeRight(sld)); -#endif VarType conv = GB(sld.conv, 0, 8); switch (sld.cmd) { diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index aa698af83a..41024b2f9a 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -651,8 +651,7 @@ SQRESULT Squirrel::LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printer } unsigned short bom = 0; if (size >= 2) { - size_t sr = fread(&bom, 1, sizeof(bom), file); - (void)sr; // Inside tar, no point checking return value of fread + [[maybe_unused]] size_t sr = fread(&bom, 1, sizeof(bom), file); } SQLEXREADFUNC func; diff --git a/src/spritecache.cpp b/src/spritecache.cpp index f2840c1b02..cf53aaf558 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -262,9 +262,7 @@ static void ResizeSpriteOut(SpriteLoader::Sprite *sprite, ZoomLevel zoom) SpriteLoader::CommonPixel *dst = sprite[zoom].data; const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data; -#ifdef WITH_ASSERT - const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width; -#endif + [[maybe_unused]] const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width; for (uint y = 0; y < sprite[zoom].height; y++) { const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width; diff --git a/src/stdafx.h b/src/stdafx.h index dc52fc0d53..e593344bf4 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -435,11 +435,6 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); # define assert(expression) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression); #endif -/* Asserts are enabled if NDEBUG isn't defined or WITH_ASSERT is defined. */ -#if !defined(NDEBUG) || defined(WITH_ASSERT) -# define OTTD_ASSERT -#endif - #if defined(OPENBSD) /* OpenBSD uses strcasecmp(3) */ # define _stricmp strcasecmp diff --git a/src/tgp.cpp b/src/tgp.cpp index e23b2643df..2c0c479100 100644 --- a/src/tgp.cpp +++ b/src/tgp.cpp @@ -658,9 +658,7 @@ static void HeightMapCurves(uint level) for (uint t = 0; t < lengthof(curve_maps); t++) { if (!HasBit(corner_bits, t)) continue; -#ifdef WITH_ASSERT - bool found = false; -#endif + [[maybe_unused]] bool found = false; const control_point_t *cm = curve_maps[t].list; for (uint i = 0; i < curve_maps[t].length - 1; i++) { const control_point_t &p1 = cm[i]; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index bc29d39177..d397825afc 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -106,13 +106,17 @@ Town::~Town() * and remove from list of sorted towns */ CloseWindowById(WC_TOWN_VIEW, this->index); - /* Check no industry is related to us. */ #ifdef WITH_ASSERT - for (const Industry *i : Industry::Iterate()) assert(i->town != this); + /* Check no industry is related to us. */ + for (const Industry *i : Industry::Iterate()) { + assert(i->town != this); + } /* ... and no object is related to us. */ - for (const Object *o : Object::Iterate()) assert(o->town != this); -#endif + for (const Object *o : Object::Iterate()) { + assert(o->town != this); + } +#endif /* WITH_ASSERT */ /* Check no tile is related to us. */ for (TileIndex tile = 0; tile < MapSize(); ++tile) { @@ -2177,8 +2181,7 @@ static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size if (t->cache.population > 0) return t; Backup cur_company(_current_company, OWNER_TOWN, FILE_LINE); - CommandCost rc = DoCommand(t->xy, t->index, 0, DC_EXEC, CMD_DELETE_TOWN); - (void)rc; // assert only + [[maybe_unused]] CommandCost rc = DoCommand(t->xy, t->index, 0, DC_EXEC, CMD_DELETE_TOWN); cur_company.Restore(); assert(rc.Succeeded()); @@ -2279,8 +2282,7 @@ HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile) */ static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits) { - CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); - (void)cc; // assert only + [[maybe_unused]] CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); assert(cc.Succeeded()); IncreaseBuildingCount(t, type); diff --git a/src/townname.cpp b/src/townname.cpp index 519391f14e..bce894bfcf 100644 --- a/src/townname.cpp +++ b/src/townname.cpp @@ -600,9 +600,7 @@ static char *MakeCzechTownName(char *buf, const char *last, uint32 seed) return strecpy(buf, _name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)], last); } -#ifdef WITH_ASSERT - const char *orig = buf; -#endif + [[maybe_unused]] const char *orig = buf; /* Probability of prefixes/suffixes * 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */ diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 05ad1025e8..5181dfdca2 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -1846,8 +1846,7 @@ static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner if (tt == TRANSPORT_RAIL) { /* Since all of our vehicles have been removed, it is safe to remove the rail * bridge / tunnel. */ - CommandCost ret = DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); - (void)ret; // assert only + [[maybe_unused]] CommandCost ret = DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); assert(ret.Succeeded()); } else { /* In any other case, we can safely reassign the ownership to OWNER_NONE. */ diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 3c3125606d..0f40e33dac 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -953,9 +953,7 @@ void CallVehicleTicks() PerformanceAccumulator::Reset(PFE_GL_AIRCRAFT); for (Vehicle *v : Vehicle::Iterate()) { -#ifdef WITH_ASSERT - size_t vehicle_index = v->index; -#endif + [[maybe_unused]] size_t vehicle_index = v->index; /* Vehicle could be deleted in this tick */ if (!v->Tick()) { diff --git a/src/vehiclelist.cpp b/src/vehiclelist.cpp index ea34c443cf..18dbef35f2 100644 --- a/src/vehiclelist.cpp +++ b/src/vehiclelist.cpp @@ -53,8 +53,7 @@ bool VehicleListIdentifier::UnpackIfValid(uint32 data) /* static */ VehicleListIdentifier VehicleListIdentifier::UnPack(uint32 data) { VehicleListIdentifier result; - bool ret = result.UnpackIfValid(data); - (void)ret; // assert only + [[maybe_unused]] bool ret = result.UnpackIfValid(data); assert(ret); return result; } diff --git a/src/widget.cpp b/src/widget.cpp index 638aa05fe7..abd413ca28 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -1272,9 +1272,7 @@ void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array) this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom); } /* 1b. Make the container higher if needed to accommodate all children nicely. */ -#ifdef WITH_ASSERT - uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height. -#endif + [[maybe_unused]] uint max_smallest = this->smallest_y + 3 * max_vert_fill; // Upper limit to computing smallest height. uint cur_height = this->smallest_y; for (;;) { for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { @@ -1439,9 +1437,7 @@ void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array) this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding_left + child_wid->padding_right); } /* 1b. Make the container wider if needed to accommodate all children nicely. */ -#ifdef WITH_ASSERT - uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height. -#endif + [[maybe_unused]] uint max_smallest = this->smallest_x + 3 * max_hor_fill; // Upper limit to computing smallest height. uint cur_width = this->smallest_x; for (;;) { for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) {