Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: use "[[maybe_unused]]" instead of a wide variety of other ways we had #9331

Merged
merged 2 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/3rdparty/squirrel/squirrel/sqcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,7 @@ class SQCompiler
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(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
Expand Down
6 changes: 1 addition & 5 deletions src/3rdparty/squirrel/squirrel/sqvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,9 +1471,7 @@ bool SQVM::DeleteSlot(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr

bool SQVM::Call(SQObjectPtr &closure,SQInteger nparams,SQInteger stackbase,SQObjectPtr &outres,SQBool raiseerror,SQBool can_suspend)
{
#ifdef WITH_ASSERT
SQInteger prevstackbase = _stackbase;
#endif
[[maybe_unused]] SQInteger prevstackbase = _stackbase;
switch(type(closure)) {
case OT_CLOSURE: {
assert(!can_suspend || this->_can_suspend);
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/autoreplace_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/pool_func.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/pool_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
3 changes: 1 addition & 2 deletions src/gfxinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/industry_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/misc/hashtable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ class CHashTableT {
/** 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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/pathfinder/npf/npf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 1 addition & 7 deletions src/saveload/saveload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/script/squirrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/spritecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +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
# define assert_msg(expression, msg, ...) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s\n\t" msg, __LINE__, __FILE__, #expression, __VA_ARGS__);
#else
# define assert_msg(expression, msg, ...)
#endif

#if defined(OPENBSD)
/* OpenBSD uses strcasecmp(3) */
# define _stricmp strcasecmp
Expand Down
4 changes: 1 addition & 3 deletions src/tgp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
18 changes: 10 additions & 8 deletions src/town_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -2177,8 +2181,7 @@ static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size
if (t->cache.population > 0) return t;

Backup<CompanyID> 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());

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/townname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 1 addition & 2 deletions src/tunnelbridge_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 1 addition & 3 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
3 changes: 1 addition & 2 deletions src/vehiclelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 2 additions & 6 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down