1
0
Fork 0

Cleanup some workarounds and warnings (#4735)

* Cleanup thread_local usage in FastRandom

* Use constexpr to avoid clang warning

* Fix more Wglobal-constructor warnings

* Make MSVC happy?
This commit is contained in:
peterbell10 2020-05-10 17:16:49 +01:00 committed by GitHub
parent 154df6b09d
commit 84289a2ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 142 deletions

View File

@ -25,18 +25,9 @@ class cBlockIDMap
typedef std::map<AString, std::pair<short, short>, Comparator> ItemMap; typedef std::map<AString, std::pair<short, short>, Comparator> ItemMap;
public: public:
static bool m_bHasRunInit;
cBlockIDMap(void) cBlockIDMap(void)
{ {
// Dont load items.ini on construct, this will search the wrong path when running as a service.
}
void init()
{
m_bHasRunInit = true;
cIniFile Ini; cIniFile Ini;
if (!Ini.ReadFile("items.ini")) if (!Ini.ReadFile("items.ini"))
{ {
@ -189,8 +180,11 @@ protected:
bool cBlockIDMap::m_bHasRunInit = false; static cBlockIDMap & GetBlockIDMap()
static cBlockIDMap gsBlockIDMap; {
static cBlockIDMap IDMap;
return IDMap;
}
@ -224,11 +218,7 @@ int BlockStringToType(const AString & a_BlockTypeString)
return res; return res;
} }
if (!gsBlockIDMap.m_bHasRunInit) return GetBlockIDMap().Resolve(TrimString(a_BlockTypeString));
{
gsBlockIDMap.init();
}
return gsBlockIDMap.Resolve(TrimString(a_BlockTypeString));
} }
@ -243,11 +233,7 @@ bool StringToItem(const AString & a_ItemTypeString, cItem & a_Item)
ItemName = ItemName.substr(10); ItemName = ItemName.substr(10);
} }
if (!gsBlockIDMap.m_bHasRunInit) return GetBlockIDMap().ResolveItem(ItemName, a_Item);
{
gsBlockIDMap.init();
}
return gsBlockIDMap.ResolveItem(ItemName, a_Item);
} }
@ -256,11 +242,7 @@ bool StringToItem(const AString & a_ItemTypeString, cItem & a_Item)
AString ItemToString(const cItem & a_Item) AString ItemToString(const cItem & a_Item)
{ {
if (!gsBlockIDMap.m_bHasRunInit) return GetBlockIDMap().Desolve(a_Item.m_ItemType, a_Item.m_ItemDamage);
{
gsBlockIDMap.init();
}
return gsBlockIDMap.Desolve(a_Item.m_ItemType, a_Item.m_ItemDamage);
} }
@ -269,11 +251,7 @@ AString ItemToString(const cItem & a_Item)
AString ItemTypeToString(short a_ItemType) AString ItemTypeToString(short a_ItemType)
{ {
if (!gsBlockIDMap.m_bHasRunInit) return GetBlockIDMap().Desolve(a_ItemType, -1);
{
gsBlockIDMap.init();
}
return gsBlockIDMap.Desolve(a_ItemType, -1);
} }

View File

@ -177,13 +177,6 @@ include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/TCLAP/include")
configure_file("BuildInfo.h.cmake" "${CMAKE_BINARY_DIR}/include/BuildInfo.h") configure_file("BuildInfo.h.cmake" "${CMAKE_BINARY_DIR}/include/BuildInfo.h")
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set_source_files_properties(BlockType.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=global-constructors")
set_source_files_properties(ByteBuffer.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=global-constructors")
set_source_files_properties(ClientHandle.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=global-constructors ")
set_source_files_properties(Statistics.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=global-constructors")
endif()
if (NOT MSVC) if (NOT MSVC)
# Bindings need to reference other folders, so they are done here instead # Bindings need to reference other folders, so they are done here instead
# lib dependencies are not included # lib dependencies are not included

View File

@ -5,38 +5,18 @@
#include "Globals.h" #include "Globals.h"
#include "FastRandom.h" #include "FastRandom.h"
#if defined (__GNUC__)
#define ATTRIBUTE_TLS static __thread
#elif defined (_MSC_VER)
#define ATTRIBUTE_TLS static __declspec(thread)
#else
#define ATTRIBUTE_TLS thread_local
#endif
MTRand & GetRandomProvider() MTRand & GetRandomProvider()
{ {
// Some compilers don't support thread_local for non-POD types, this is purely a work around for that restriction. thread_local MTRand Random = []
// There should be minimal overhead for the non-initializing case and all thread's instances are deleted properly. {
ATTRIBUTE_TLS MTRand * LocalPtr = nullptr; cRandomDeviceSeeder Seeder;
if (LocalPtr == nullptr) return MTRand(Seeder);
{ }();
// This list allows deletion of elements as if they had static storage duration return Random;
static std::mutex CSDeleteList;
static std::list<std::unique_ptr<MTRand>> DeleteList;
cRandomDeviceSeeder seeder;
auto NewInstance = cpp14::make_unique<MTRand>(seeder);
auto TempPtr = NewInstance.get();
std::lock_guard<std::mutex> Lock(CSDeleteList);
DeleteList.push_front(std::move(NewInstance));
LocalPtr = TempPtr; // Set after push_back so LocalPtr won't dangle if it throws
}
return *LocalPtr;
} }
@ -45,15 +25,11 @@ MTRand & GetRandomProvider()
UInt32 Detail::GetRandomSeed() UInt32 Detail::GetRandomSeed()
{ {
ATTRIBUTE_TLS bool SeedCounterInitialized = false; thread_local UInt32 SeedCounter = []
ATTRIBUTE_TLS UInt32 SeedCounter = 0; {
std::random_device rd;
if (!SeedCounterInitialized) std::uniform_int_distribution<UInt32> dist;
{ return dist(rd);
std::random_device rd; }();
std::uniform_int_distribution<UInt32> dist;
SeedCounter = dist(rd);
SeedCounterInitialized = true;
}
return ++SeedCounter; return ++SeedCounter;
} }

View File

@ -67,10 +67,6 @@ SET (HDRS
VillageGen.h VillageGen.h
) )
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set_source_files_properties(CompoGenBiomal.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=global-constructors")
endif()
if(NOT MSVC) if(NOT MSVC)
add_library(Generating ${SRCS} ${HDRS}) add_library(Generating ${SRCS} ${HDRS})
target_link_libraries(Generating fmt::fmt OSSupport Blocks Bindings) target_link_libraries(Generating fmt::fmt OSSupport Blocks Bindings)

View File

@ -27,30 +27,28 @@ class cPattern
public: public:
struct BlockInfo struct BlockInfo
{ {
BLOCKTYPE m_BlockType; BLOCKTYPE m_BlockType = E_BLOCK_STONE;
NIBBLETYPE m_BlockMeta; NIBBLETYPE m_BlockMeta = 0;
}; };
cPattern(BlockInfo * a_TopBlocks, size_t a_Count) constexpr cPattern(std::initializer_list<BlockInfo> a_TopBlocks)
{ {
ASSERT(a_TopBlocks.size() <= cChunkDef::Height);
// Copy the pattern into the top: // Copy the pattern into the top:
for (size_t i = 0; i < a_Count; i++) size_t i = 0;
for (const auto & Block : a_TopBlocks)
{ {
m_Pattern[i] = a_TopBlocks[i]; m_Pattern[i] = Block;
++i;
} }
// Fill the rest with stone: // The remaining blocks default to stone
static BlockInfo Stone = {E_BLOCK_STONE, 0};
for (int i = static_cast<int>(a_Count); i < cChunkDef::Height; i++)
{
m_Pattern[i] = Stone;
}
} }
const BlockInfo * Get(void) const { return m_Pattern; } const BlockInfo * Get(void) const { return m_Pattern; }
protected: protected:
BlockInfo m_Pattern[cChunkDef::Height]; BlockInfo m_Pattern[cChunkDef::Height] = {};
} ; } ;
@ -58,9 +56,9 @@ protected:
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// The arrays to use for the top block pattern definitions: // Land top block patterns:
static cPattern::BlockInfo tbGrass[] = static constexpr cPattern patGrass =
{ {
{E_BLOCK_GRASS, 0}, {E_BLOCK_GRASS, 0},
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
@ -68,7 +66,7 @@ static cPattern::BlockInfo tbGrass[] =
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
} ; } ;
static cPattern::BlockInfo tbSand[] = static constexpr cPattern patSand =
{ {
{ E_BLOCK_SAND, 0}, { E_BLOCK_SAND, 0},
{ E_BLOCK_SAND, 0}, { E_BLOCK_SAND, 0},
@ -76,7 +74,7 @@ static cPattern::BlockInfo tbSand[] =
{ E_BLOCK_SANDSTONE, 0}, { E_BLOCK_SANDSTONE, 0},
} ; } ;
static cPattern::BlockInfo tbDirt[] = static constexpr cPattern patDirt =
{ {
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
@ -84,7 +82,7 @@ static cPattern::BlockInfo tbDirt[] =
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
} ; } ;
static cPattern::BlockInfo tbPodzol[] = static constexpr cPattern patPodzol =
{ {
{E_BLOCK_DIRT, E_META_DIRT_PODZOL}, {E_BLOCK_DIRT, E_META_DIRT_PODZOL},
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
@ -92,7 +90,7 @@ static cPattern::BlockInfo tbPodzol[] =
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
} ; } ;
static cPattern::BlockInfo tbGrassLess[] = static constexpr cPattern patGrassLess =
{ {
{E_BLOCK_DIRT, E_META_DIRT_GRASSLESS}, {E_BLOCK_DIRT, E_META_DIRT_GRASSLESS},
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
@ -100,7 +98,7 @@ static cPattern::BlockInfo tbGrassLess[] =
{E_BLOCK_DIRT, E_META_DIRT_NORMAL}, {E_BLOCK_DIRT, E_META_DIRT_NORMAL},
} ; } ;
static cPattern::BlockInfo tbMycelium[] = static constexpr cPattern patMycelium =
{ {
{E_BLOCK_MYCELIUM, 0}, {E_BLOCK_MYCELIUM, 0},
{E_BLOCK_DIRT, 0}, {E_BLOCK_DIRT, 0},
@ -108,7 +106,7 @@ static cPattern::BlockInfo tbMycelium[] =
{E_BLOCK_DIRT, 0}, {E_BLOCK_DIRT, 0},
} ; } ;
static cPattern::BlockInfo tbGravel[] = static constexpr cPattern patGravel =
{ {
{E_BLOCK_GRAVEL, 0}, {E_BLOCK_GRAVEL, 0},
{E_BLOCK_GRAVEL, 0}, {E_BLOCK_GRAVEL, 0},
@ -116,7 +114,7 @@ static cPattern::BlockInfo tbGravel[] =
{E_BLOCK_STONE, 0}, {E_BLOCK_STONE, 0},
} ; } ;
static cPattern::BlockInfo tbStone[] = static constexpr cPattern patStone =
{ {
{E_BLOCK_STONE, 0}, {E_BLOCK_STONE, 0},
{E_BLOCK_STONE, 0}, {E_BLOCK_STONE, 0},
@ -127,9 +125,9 @@ static cPattern::BlockInfo tbStone[] =
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Ocean floor pattern top-block definitions: // Ocean floor patterns:
static cPattern::BlockInfo tbOFSand[] = static constexpr cPattern patOFSand =
{ {
{E_BLOCK_SAND, 0}, {E_BLOCK_SAND, 0},
{E_BLOCK_SAND, 0}, {E_BLOCK_SAND, 0},
@ -137,7 +135,7 @@ static cPattern::BlockInfo tbOFSand[] =
{E_BLOCK_SANDSTONE, 0} {E_BLOCK_SANDSTONE, 0}
} ; } ;
static cPattern::BlockInfo tbOFClay[] = static constexpr cPattern patOFClay =
{ {
{ E_BLOCK_CLAY, 0}, { E_BLOCK_CLAY, 0},
{ E_BLOCK_CLAY, 0}, { E_BLOCK_CLAY, 0},
@ -145,7 +143,7 @@ static cPattern::BlockInfo tbOFClay[] =
{ E_BLOCK_SAND, 0}, { E_BLOCK_SAND, 0},
} ; } ;
static cPattern::BlockInfo tbOFOrangeClay[] = static constexpr cPattern patOFOrangeClay =
{ {
{ E_BLOCK_STAINED_CLAY, E_META_STAINED_GLASS_ORANGE}, { E_BLOCK_STAINED_CLAY, E_META_STAINED_GLASS_ORANGE},
{ E_BLOCK_STAINED_CLAY, E_META_STAINED_GLASS_ORANGE}, { E_BLOCK_STAINED_CLAY, E_META_STAINED_GLASS_ORANGE},
@ -156,26 +154,6 @@ static cPattern::BlockInfo tbOFOrangeClay[] =
////////////////////////////////////////////////////////////////////////////////
// Individual patterns to use:
static cPattern patGrass (tbGrass, ARRAYCOUNT(tbGrass));
static cPattern patSand (tbSand, ARRAYCOUNT(tbSand));
static cPattern patDirt (tbDirt, ARRAYCOUNT(tbDirt));
static cPattern patPodzol (tbPodzol, ARRAYCOUNT(tbPodzol));
static cPattern patGrassLess(tbGrassLess, ARRAYCOUNT(tbGrassLess));
static cPattern patMycelium (tbMycelium, ARRAYCOUNT(tbMycelium));
static cPattern patGravel (tbGravel, ARRAYCOUNT(tbGravel));
static cPattern patStone (tbStone, ARRAYCOUNT(tbStone));
static cPattern patOFSand (tbOFSand, ARRAYCOUNT(tbOFSand));
static cPattern patOFClay (tbOFClay, ARRAYCOUNT(tbOFClay));
static cPattern patOFOrangeClay(tbOFOrangeClay, ARRAYCOUNT(tbOFOrangeClay));
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// cCompoGenBiomal: // cCompoGenBiomal:

View File

@ -40,10 +40,6 @@ SET (HDRS
WinStackWalker.h WinStackWalker.h
) )
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_flags_cxx("-Wno-error=global-constructors ")
endif()
if(NOT MSVC) if(NOT MSVC)
add_library(OSSupport ${SRCS} ${HDRS}) add_library(OSSupport ${SRCS} ${HDRS})
target_link_libraries(OSSupport fmt::fmt) target_link_libraries(OSSupport fmt::fmt)

View File

@ -36,12 +36,7 @@
#ifdef __clang__ static constexpr Vector3i gCrossCoords[] =
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
static const Vector3i gCrossCoords[] =
{ {
{ 1, 0, 0}, { 1, 0, 0},
{-1, 0, 0}, {-1, 0, 0},
@ -53,7 +48,7 @@ static const Vector3i gCrossCoords[] =
static const Vector3i gNeighborCoords[] = static constexpr Vector3i gNeighborCoords[] =
{ {
{ 1, 0, 0}, { 1, 0, 0},
{-1, 0, 0}, {-1, 0, 0},
@ -61,11 +56,7 @@ static const Vector3i gNeighborCoords[] =
{ 0, -1, 0}, { 0, -1, 0},
{ 0, 0, 1}, { 0, 0, 1},
{ 0, 0, -1}, { 0, 0, -1},
} ; };
#ifdef __clang__
#pragma clang diagnostic pop
#endif

View File

@ -6,6 +6,10 @@
#include "Statistics.h" #include "Statistics.h"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
cStatInfo cStatInfo::ms_Info[statCount] = cStatInfo cStatInfo::ms_Info[statCount] =
{ {
@ -76,6 +80,10 @@ cStatInfo cStatInfo::ms_Info[statCount] =
cStatInfo(statTreasureFished, "stat.treasureFished") cStatInfo(statTreasureFished, "stat.treasureFished")
}; };
#ifdef __clang__
#pragma clang diagnostic pop
#endif

View File

@ -17,8 +17,8 @@ public:
T x, y, z; T x, y, z;
inline Vector3(void) : x(0), y(0), z(0) {} constexpr Vector3(void) : x(0), y(0), z(0) {}
inline Vector3(T a_x, T a_y, T a_z) : x(a_x), y(a_y), z(a_z) {} constexpr Vector3(T a_x, T a_y, T a_z) : x(a_x), y(a_y), z(a_z) {}
#ifdef TOLUA_EXPOSITION // Hardcoded copy constructors (tolua++ does not support function templates .. yet) #ifdef TOLUA_EXPOSITION // Hardcoded copy constructors (tolua++ does not support function templates .. yet)
@ -31,7 +31,12 @@ public:
// tolua_end // tolua_end
// Conversion constructors where U is not the same as T leaving the copy-constructor implicitly generated // Conversion constructors where U is not the same as T leaving the copy-constructor implicitly generated
template <typename U, typename = typename std::enable_if<!std::is_same<U, T>::value>::type> template <typename U, typename = typename std::enable_if<!std::is_same<U, T>::value>::type>
Vector3(const Vector3<U> & a_Rhs): x(static_cast<T>(a_Rhs.x)), y(static_cast<T>(a_Rhs.y)), z(static_cast<T>(a_Rhs.z)) {} constexpr Vector3(const Vector3<U> & a_Rhs):
x(static_cast<T>(a_Rhs.x)),
y(static_cast<T>(a_Rhs.y)),
z(static_cast<T>(a_Rhs.z))
{
}
// tolua_begin // tolua_begin
inline void Set(T a_x, T a_y, T a_z) inline void Set(T a_x, T a_y, T a_z)