From 239b3ab10b460da34c490a7e06a21c984e21ffb6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 26 Nov 2021 20:21:12 +0000 Subject: Enable all Jason Turner recommended warnings --- Jamroot.jam | 15 +++++++++++++++ application/main.cpp | 2 +- game/activity.h | 5 ++++- game/network/network.cpp | 4 ++-- game/network/rail.cpp | 8 ++++---- game/terrain.cpp | 16 ++++++++-------- game/vehicles/railVehicleClass.cpp | 2 +- gfx/gl/shader.cpp | 2 +- gfx/image.cpp | 6 +++++- gfx/image.h | 2 +- gfx/manualCameraController.cpp | 6 +++--- gfx/models/mesh.cpp | 11 ++++++----- gfx/models/obj.h | 2 +- gfx/models/obj.impl.cpp | 6 +++--- gfx/models/texture.cpp | 3 ++- gfx/window.cpp | 5 +++-- lib/cache.h | 3 +++ lib/jsonParse-persistence.cpp | 9 +++++---- lib/jsonParse.impl.cpp | 20 ++++++++++---------- lib/maths.cpp | 4 ++-- lib/maths.h | 6 +++--- lib/persistence.h | 13 ++++++++++++- lib/ptr.hpp | 4 ++-- test/test-maths.cpp | 4 ++-- 24 files changed, 99 insertions(+), 59 deletions(-) diff --git a/Jamroot.jam b/Jamroot.jam index 9ba8966..f3fd9b6 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -17,6 +17,21 @@ project : requirements 20 debug:pedantic debug:on + debug:-Wnon-virtual-dtor + debug:-Wold-style-cast + debug:-Wcast-align + debug:-Wunused + debug:-Woverloaded-virtual + debug:-Wpedantic + debug:-Wconversion + debug:-Wsign-conversion + debug:-Wnull-dereference + debug:-Wdouble-promotion + debug:-Wformat=2 + gcc,debug:-Wduplicated-cond + gcc,debug:-Wduplicated-branches + gcc,debug:-Wlogical-op + gcc,debug:-Wuseless-cast release:on coverage:on tidy:gfx/models/obj.cpp diff --git a/application/main.cpp b/application/main.cpp index 88cec82..07a593b 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -120,7 +120,7 @@ public: } Shader shader; - Camera camera({-1250.0F, -1250.0F, 35.0F}, 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 10000.0F); + Camera camera({-1250.0F, -1250.0F, 35.0F}, 70.0F, rdiv(DISPLAY_WIDTH, DISPLAY_HEIGHT), 0.1F, 10000.0F); shader.setView(camera.GetViewProjection()); shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); shader.setUniform("lightColor", {.6, .6, .6}); diff --git a/game/activity.h b/game/activity.h index f6ec609..b5fc168 100644 --- a/game/activity.h +++ b/game/activity.h @@ -19,9 +19,12 @@ public: }; using ActivityPtr = std::unique_ptr; -template concept ActivityConcept = std::is_base_of_v; +template +concept ActivityConcept = std::is_base_of_v; template class Can { public: + virtual ~Can() = default; + virtual void doActivity(AC *, TickDuration) = 0; }; diff --git a/game/network/network.cpp b/game/network/network.cpp index 6eaccef..47e51e2 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -36,8 +36,8 @@ void Network::joinLinks(const LinkPtr & l, const LinkPtr & ol) { if (l != ol) { - for (const auto oe : {0, 1}) { - for (const auto te : {0, 1}) { + for (const auto oe : {0U, 1U}) { + for (const auto te : {0U, 1U}) { if (l->ends[te].node == ol->ends[oe].node) { l->ends[te].nexts.emplace_back(ol, oe); ol->ends[oe].nexts.emplace_back(l, te); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 5127e34..46f11d6 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -135,7 +135,7 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) vertices.reserve(2 * railCrossSection.size()); const auto len = round_sleepers(length / 2.F); const auto e {flat_orientation(diff)}; - for (int ei : {1, 0}) { + for (auto ei : {1U, 0U}) { const auto trans {glm::translate(ends[ei].node->pos) * e}; for (const auto & rcs : railCrossSection) { const glm::vec3 m {(trans * glm::vec4 {rcs.first, 1})}; @@ -164,10 +164,10 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, const auto step {glm::vec3 {arc_length(arc), e1p.z - e0p.z, slength} / segs}; const auto trans {glm::translate(centreBase)}; - auto segCount = std::lround(segs); + auto segCount = static_cast(std::lround(segs)) + 1; std::vector vertices; - vertices.reserve((segCount + 1) * railCrossSection.size()); - for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount >= 0; swing += step, --segCount) { + vertices.reserve(segCount * railCrossSection.size()); + for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { const auto t { trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, 0.F, swing.y})}; for (const auto & rcs : railCrossSection) { diff --git a/game/terrain.cpp b/game/terrain.cpp index a7dd334..79b845d 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -25,7 +25,7 @@ Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Tex // Initial coordinates for (auto y = 0; y < size; y += 1) { for (auto x = 0; x < size; x += 1) { - auto & vertex = vertices[x + (y * size)]; + auto & vertex = vertices[static_cast(x + (y * size))]; vertex.pos = {resolution * (x - offset), resolution * (y - offset), -1.5}; vertex.normal = up; vertex.texCoord = {x, y}; @@ -35,21 +35,21 @@ Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Tex std::mt19937 gen(std::random_device {}()); std::uniform_int_distribution<> rpos(2, size - 2); std::uniform_int_distribution<> rsize(10, 30); - std::uniform_int_distribution<> rheight(1, 3); + std::uniform_real_distribution rheight(1, 3); for (int h = 0; h < 500;) { const glm::ivec2 hpos {rpos(gen), rpos(gen)}; const glm::ivec2 hsize {rsize(gen), rsize(gen)}; if (const auto lim1 = hpos - hsize; lim1.x > 0 && lim1.y > 0) { if (const auto lim2 = hpos + hsize; lim2.x < size && lim2.y < size) { - const auto height = (float)rheight(gen); + const auto height = rheight(gen); const glm::ivec2 hsizesqrd {hsize.x * hsize.x, hsize.y * hsize.y}; for (auto y = lim1.y; y < lim2.y; y += 1) { for (auto x = lim1.x; x < lim2.x; x += 1) { const auto dist {hpos - glm::ivec2 {x, y}}; const glm::ivec2 distsqrd {dist.x * dist.x, dist.y * dist.y}; const auto out {rdiv(sq(x - hpos.x), sq(hsize.x)) + rdiv(sq(y - hpos.y), sq(hsize.y))}; - if (out <= 1.0) { - auto & vertex = vertices[x + (y * size)]; + if (out <= 1.0F) { + auto & vertex = vertices[static_cast(x + (y * size))]; const auto m {1.F / (7.F * out - 8.F) + 1.F}; vertex.pos.z += height * m; } @@ -72,10 +72,10 @@ Terrain::Terrain(const std::string & fileName) : std::vector vertices; vertices.reserve((map.width * map.height) + 4); - for (auto y = 0; y < map.height; y += 1) { - for (auto x = 0; x < map.width; x += 1) { + for (auto y = 0U; y < map.height; y += 1) { + for (auto x = 0U; x < map.width; x += 1) { vertices.emplace_back(glm::vec3 {resolution * (x - (map.width / 2)), resolution * (y - (map.height / 2)), - ((float)map.data[x + (y * map.width)] * 0.1F) - 1.5F}, + (map.data[x + (y * map.width)] * 0.1F) - 1.5F}, glm::vec2 {(x % 2) / 2.01, (y % 2) / 2.01}, up); } } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 52b7dbe..406f2d8 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -57,7 +57,7 @@ RailVehicleClass::bogieOffset(ObjParser & o) if (!object.first.starts_with("Bogie")) { continue; } - std::set> vertexIds; + std::set> vertexIds; for (const auto & face : object.second) { for (const auto & faceElement : face) { vertexIds.emplace(o.vertices[faceElement.x - 1].y, faceElement.x - 1); diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index b97d6fc..14cf404 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -76,7 +76,7 @@ Shader::setUniform(const GLchar * uniform, glm::vec3 v) const void Shader::setModel(const Location & loc, Program pid) const { - auto & prog = programs[(int)pid]; + auto & prog = programs[static_cast(pid)]; glUseProgram(prog.m_program); if (prog.model_uniform >= 0) { const auto model {glm::translate(loc.pos) * rotate_ypr(loc.rot)}; diff --git a/gfx/image.cpp b/gfx/image.cpp index 41c59a2..e3e3b01 100644 --- a/gfx/image.cpp +++ b/gfx/image.cpp @@ -6,7 +6,11 @@ Image::Image(const char * fileName, int flags) : width {}, height {}, numComponents {} { stbi_set_flip_vertically_on_load(1); - unsigned char * bytes = stbi_load(fileName, &width, &height, &numComponents, flags); + int w, h, nc; + unsigned char * bytes = stbi_load(fileName, &w, &h, &nc, flags); + width = static_cast(w); + height = static_cast(h); + numComponents = static_cast(nc); if (!bytes) { throw std::runtime_error {std::string {"Unable to load image: "} + fileName}; diff --git a/gfx/image.h b/gfx/image.h index d43737f..23e9a9b 100644 --- a/gfx/image.h +++ b/gfx/image.h @@ -14,7 +14,7 @@ public: NO_COPY(Image); NO_MOVE(Image); - int width, height, numComponents; + unsigned int width, height, numComponents; std::span data; }; diff --git a/gfx/manualCameraController.cpp b/gfx/manualCameraController.cpp index 022fb72..cacf6ac 100644 --- a/gfx/manualCameraController.cpp +++ b/gfx/manualCameraController.cpp @@ -43,8 +43,8 @@ ManualCameraController::handleInput(SDL_Event & e) case SDL_MOUSEMOTION: if (mrb) { if (ctrl) { - direction -= 0.01F * (float)e.motion.xrel; - pitch = std::clamp(pitch - 0.01F * (float)e.motion.yrel, 0.1F, half_pi); + direction -= 0.01F * static_cast(e.motion.xrel); + pitch = std::clamp(pitch - 0.01F * static_cast(e.motion.yrel), 0.1F, half_pi); } else { focus += rotate_flat(-direction) * glm::vec2 {-e.motion.xrel, e.motion.yrel}; @@ -52,7 +52,7 @@ ManualCameraController::handleInput(SDL_Event & e) } return true; case SDL_MOUSEWHEEL: - dist = std::clamp(dist - (float)e.wheel.y * 4.F, 5.F, 200.F); + dist = std::clamp(dist - static_cast(e.wheel.y) * 4.F, 5.F, 200.F); break; } return false; diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 52ce6bb..82eafbc 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -2,10 +2,10 @@ #include "vertex.hpp" #include -#define offset_ptr(T, m) (((char *)1) + offsetof(T, m) - 1) +#define offset_ptr(T, m) ((reinterpret_cast(1)) + offsetof(T, m) - 1) Mesh::Mesh(const std::span vertices, const std::span indices, GLenum m) : - m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {(GLsizei)indices.size()}, mode {m} + m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {static_cast(indices.size())}, mode {m} { glGenVertexArrays(1, &m_vertexArrayObject); glBindVertexArray(m_vertexArrayObject); @@ -13,7 +13,8 @@ Mesh::Mesh(const std::span vertices, const std::span(sizeof(Vertex) * vertices.size()), vertices.data(), + GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offset_ptr(Vertex, pos)); @@ -25,8 +26,8 @@ Mesh::Mesh(const std::span vertices, const std::span(sizeof(indices[0]) * indices.size()), indices.data(), + GL_STATIC_DRAW); glBindVertexArray(0); } diff --git a/gfx/models/obj.h b/gfx/models/obj.h index 9a2a30e..6db16c0 100644 --- a/gfx/models/obj.h +++ b/gfx/models/obj.h @@ -25,7 +25,7 @@ public: std::vector vertices; std::vector texCoords; std::vector normals; - using FaceElement = glm::vec<3, int>; + using FaceElement = glm::vec<3, unsigned int>; using Face = std::vector; using Faces = std::vector; using Object = std::pair; diff --git a/gfx/models/obj.impl.cpp b/gfx/models/obj.impl.cpp index 02996da..745689e 100644 --- a/gfx/models/obj.impl.cpp +++ b/gfx/models/obj.impl.cpp @@ -52,15 +52,15 @@ ObjParser::createMeshData() const const auto & fe {face[idx]}; if (const auto existing = std::find(vertexOrder.begin(), vertexOrder.end(), fe); existing != vertexOrder.end()) { - indices.push_back(std::distance(vertexOrder.begin(), existing)); + indices.push_back(static_cast(std::distance(vertexOrder.begin(), existing))); } else { - indices.push_back(overtices.size()); + indices.push_back(static_cast(overtices.size())); overtices.emplace_back(vertices[fe.x - 1], texCoords[fe.y - 1], -normals[fe.z - 1]); vertexOrder.emplace_back(fe); } }; - f(0); + f(0U); f(idx); f(idx - 1); } diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 898f495..cd275e8 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -18,7 +18,8 @@ Texture::Texture(const std::filesystem::path & fileName) : m_texture {} glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast(tex.width), static_cast(tex.height), 0, + GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } Texture::~Texture() diff --git a/gfx/window.cpp b/gfx/window.cpp index f7bb125..2e3c843 100644 --- a/gfx/window.cpp +++ b/gfx/window.cpp @@ -3,8 +3,9 @@ #include Window::Window(int width, int height, const std::string & title) : - m_window {m_window.create(SDL_CreateWindow, SDL_DestroyWindow, title.c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL)}, + m_window {m_window.create(SDL_CreateWindow, SDL_DestroyWindow, title.c_str(), + static_cast(SDL_WINDOWPOS_CENTERED), static_cast(SDL_WINDOWPOS_CENTERED), width, height, + static_cast(SDL_WINDOW_OPENGL))}, m_glContext {m_glContext.create(SDL_GL_CreateContext, SDL_GL_DeleteContext, m_window)} { if (glewInit() != GLEW_OK) { diff --git a/lib/cache.h b/lib/cache.h index 48b9c55..2a6e3e5 100644 --- a/lib/cache.h +++ b/lib/cache.h @@ -7,6 +7,9 @@ template class Cache { public: using Ptr = std::shared_ptr; + + virtual ~Cache() = default; + [[nodiscard]] Ptr get(const std::string & key) { diff --git a/lib/jsonParse-persistence.cpp b/lib/jsonParse-persistence.cpp index 5062796..a9b9a83 100644 --- a/lib/jsonParse-persistence.cpp +++ b/lib/jsonParse-persistence.cpp @@ -97,7 +97,7 @@ namespace Persistence { wrh(std::ostream & strm, char ch) { using namespace std::literals; - strm << R"(\u)"sv << std::setw(4) << std::hex << (int)ch << std::setw(1); + strm << R"(\u)"sv << std::setw(4) << std::hex << static_cast(ch) << std::setw(1); } static inline void wre(std::ostream & strm, char e) @@ -116,7 +116,7 @@ namespace Persistence { static constexpr OutFuncs outFuncs {[]() { OutFuncs outFuncs; outFuncs.fill(&wrv); - for (int x = 0; x < 0x20; x += 1) { + for (auto x = 0U; x < 0x20U; x += 1) { outFuncs[x] = &wrh; } outFuncs['\"'] = &wre<'"'>; @@ -160,7 +160,8 @@ namespace Persistence { strm << value; } - void JsonWritePersistence::pushValue(std::nullptr_t) const + void + JsonWritePersistence::pushValue(std::nullptr_t) const { strm << "null"; } @@ -170,7 +171,7 @@ namespace Persistence { { strm << '"'; std::for_each(value.begin(), value.end(), [this](char ch) { - outFuncs[(unsigned char)ch](strm, ch); + outFuncs[static_cast(ch)](strm, ch); }); strm << '"'; } diff --git a/lib/jsonParse.impl.cpp b/lib/jsonParse.impl.cpp index 0913847..ce1020f 100644 --- a/lib/jsonParse.impl.cpp +++ b/lib/jsonParse.impl.cpp @@ -19,24 +19,24 @@ void json::jsonParser::appendEscape(unsigned long cp, std::string & str) { if (cp <= 0x7F) { - str += (char)cp; + str += static_cast(cp); } else if (cp <= 0x7FF) { - str += char((cp >> 6) + 192); - str += char((cp & 63) + 128); + str += static_cast((cp >> 6) + 192); + str += static_cast((cp & 63) + 128); } else if ((0xd800 <= cp && cp <= 0xdfff) || cp > 0x10FFFF) { throw std::range_error("Invalid UTF-8 sequence"); } else if (cp <= 0xFFFF) { - str += char((cp >> 12) + 224); - str += char(((cp >> 6) & 63) + 128); - str += char((cp & 63) + 128); + str += static_cast((cp >> 12) + 224); + str += static_cast(((cp >> 6) & 63) + 128); + str += static_cast((cp & 63) + 128); } else { - str += char((cp >> 18) + 240); - str += char(((cp >> 12) & 63) + 128); - str += char(((cp >> 6) & 63) + 128); - str += char((cp & 63) + 128); + str += static_cast((cp >> 18) + 240); + str += static_cast(((cp >> 12) & 63) + 128); + str += static_cast(((cp >> 6) & 63) + 128); + str += static_cast((cp & 63) + 128); } } diff --git a/lib/maths.cpp b/lib/maths.cpp index e894d02..4d9f8d4 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -173,10 +173,10 @@ find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd) float operator"" _mph(const long double v) { - return mph_to_ms(v); + return static_cast(mph_to_ms(v)); } float operator"" _kph(const long double v) { - return kph_to_ms(v); + return static_cast(kph_to_ms(v)); } diff --git a/lib/maths.h b/lib/maths.h index 285c69a..18332b7 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -69,7 +69,7 @@ template inline constexpr auto rdiv(Ta a, Tb b) { - return ((R)a / (R)b); + return (static_cast(a) / static_cast(b)); } constexpr inline glm::vec2 @@ -105,14 +105,14 @@ float find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 b // Conversions template -inline constexpr float +inline constexpr auto mph_to_ms(T v) { return v / 2.237; } template -inline constexpr float +inline constexpr auto kph_to_ms(T v) { return v / 3.6; diff --git a/lib/persistence.h b/lib/persistence.h index 252afde..0faa24d 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -26,6 +26,10 @@ namespace Persistence { using Stack = std::stack; struct Writer { + Writer() = default; + virtual ~Writer() = default; + DEFAULT_MOVE_COPY(Writer); + virtual void beginObject() const = 0; virtual void beginArray() const = 0; virtual void pushValue(bool value) const = 0; @@ -85,6 +89,7 @@ namespace Persistence { template struct SelectionT : public SelectionV { using SelectionV::SelectionV; + using Selection::setValue; using P = std::conditional_t, T, T &&>; void @@ -102,6 +107,8 @@ namespace Persistence { struct Persistable; struct PersistenceStore { + virtual ~PersistenceStore() = default; + template [[nodiscard]] inline bool persistType(const T * const, const std::type_info & ti); enum class NameAction { Push, HandleAndContinue, Ignore }; @@ -292,6 +299,7 @@ namespace Persistence { struct SelectionObj : public SelectionV { struct MakeObjectByTypeName : public SelectionV { using SelectionV::SelectionV; + using Selection::setValue; void setValue(std::string && type) override @@ -316,6 +324,7 @@ namespace Persistence { struct RememberObjectById : public SelectionV { using SelectionV::SelectionV; + using Selection::setValue; void setValue(std::string && id) override @@ -383,8 +392,10 @@ namespace Persistence { } using SelectionV::SelectionV; + using Selection::setValue; - void setValue(std::nullptr_t) override + void + setValue(std::nullptr_t) override { this->v.reset(); } diff --git a/lib/ptr.hpp b/lib/ptr.hpp index b92b63e..0b00285 100644 --- a/lib/ptr.hpp +++ b/lib/ptr.hpp @@ -16,9 +16,9 @@ public: template static auto - create(Obj * (*factory)(Args...), void (*deleter)(Obj *), Params &&... params) + create(Obj * (*factory)(Params...), void (*deleter)(Obj *), Args &&... args) { - return wrapped_ptr {factory(std::forward(params)...), deleter}; + return wrapped_ptr {factory(std::forward(args)...), deleter}; } }; diff --git a/test/test-maths.cpp b/test/test-maths.cpp index 02d3708..decebcc 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -152,8 +152,8 @@ BOOST_DATA_TEST_CASE(test_find_arc_centre, {{2, 2}, pi, {3, 3}, half_pi, {3, 2}, true}, {{2, 2}, pi, {1, 3}, -half_pi, {1, 2}, false}, {{-1100, -1000}, pi, {-900, -800}, half_pi, {-900, -1000}, true}, - {{1100, 1000}, 0, {1050, 900}, pi + 0.92, {973, 1000}, true}, - {{1050, 900}, 0.92, {1000, 800}, pi, {1127, 800}, false}, + {{1100, 1000}, 0, {1050, 900}, pi + 0.92F, {973, 1000}, true}, + {{1050, 900}, 0.92F, {1000, 800}, pi, {1127, 800}, false}, }), s, es, e, ee, exp, lr) { -- cgit v1.2.3