From d5ff75ee31a84ebeab87ec9fca55319b8e4d2948 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Tue, 12 Aug 2025 18:41:47 +0100 Subject: [PATCH] Use stricter C++ compiler flags Enable more warnings, and treat all warnings as errors. This would have caught the incomplete switch fixed in f5f89be6591af13a51f0528d17157d6c41b96c25. --- cpp/CMakeLists.txt | 47 +++++++++++++++++++++++- cpp/src/api/api.cpp | 13 ++++--- cpp/src/api/convert.cpp | 2 + cpp/src/api/metadata/plugin_metadata.cpp | 2 +- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 7b008299..a3d74c19 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -180,12 +180,55 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows") endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(loot PRIVATE "-Wall" "-Wextra") + target_compile_options(loot PRIVATE + "-Werror" + "-Wall" + "-Wextra" + "-Wpedantic" + "-Wconversion" + "-Wdeprecated" + "-Wuninitialized" + "-Winit-self" + "-Wswitch-enum" + "-Wstrict-overflow=5" + "-Wstringop-overflow=4" + "-Wduplicated-branches" + "-Wduplicated-cond" + "-Wzero-as-null-pointer-constant" + "-Wfloat-equal" + "-Wshadow" + "-Wcast-qual" + "-Wcast-align" + "-Wsign-conversion" + "-Wlogical-op" + "-Wvla" + "-Wno-xor-used-as-pow") endif() if(MSVC) # Turn off permissive mode to be more standards-compliant and avoid compiler errors. - target_compile_options(loot PRIVATE "/permissive-" "/W4" "/Zc:__cplusplus" "/GL" "/MP") + target_compile_options(loot PRIVATE + "/permissive-" + "/W4" + "/Wall" + "/WX" + "/wd4514" + "/wd4623" + "/wd4625" + "/wd4626" + "/wd4710" + "/wd4738" + "/wd4820" + "/wd5026" + "/wd5027" + "/wd5045" + "/external:anglebrackets" + "/external:W0" + "/Zc:__cplusplus" + "/GL" + "/MP" + "/sdl" + "$<$:/RTC1>") target_link_options(loot PRIVATE "/LTCG") endif() diff --git a/cpp/src/api/api.cpp b/cpp/src/api/api.cpp index ac277e9f..76cd9575 100644 --- a/cpp/src/api/api.cpp +++ b/cpp/src/api/api.cpp @@ -43,8 +43,6 @@ LogLevel convert(uint8_t level) { return LogLevel::info; } else if (level == LIBLOOT_LOG_LEVEL_WARNING) { return LogLevel::warning; - } else if (level == LIBLOOT_LOG_LEVEL_ERROR) { - return LogLevel::error; } else { return LogLevel::error; } @@ -67,10 +65,13 @@ loot::rust::LogLevel convert(LogLevel level) { } } -void loggingCallback(uint8_t level, const char* message, void* context) { - auto& callback = *static_cast(context); - - callback(convert(level), message); +void loggingCallback(uint8_t level, const char* message, void* context) noexcept { + try { + auto& callback = *static_cast(context); + callback(convert(level), message); + } catch (...) { + // Can't do anything with the exception. + } } } diff --git a/cpp/src/api/convert.cpp b/cpp/src/api/convert.cpp index 1d40da1b..c5be6f3d 100644 --- a/cpp/src/api/convert.cpp +++ b/cpp/src/api/convert.cpp @@ -31,6 +31,8 @@ std::optional convert(loot::rust::EdgeType edgeType) { return loot::EdgeType::tieBreak; case loot::rust::EdgeType::BlueprintMaster: return loot::EdgeType::blueprintMaster; + case loot::rust::EdgeType::None: + return std::nullopt; default: return std::nullopt; } diff --git a/cpp/src/api/metadata/plugin_metadata.cpp b/cpp/src/api/metadata/plugin_metadata.cpp index 44980dbc..bfe021fd 100644 --- a/cpp/src/api/metadata/plugin_metadata.cpp +++ b/cpp/src/api/metadata/plugin_metadata.cpp @@ -39,7 +39,7 @@ namespace { template std::vector mergeVectors(std::vector first, const std::vector& second) { - const auto initialSizeOfFirst = first.size(); + const auto initialSizeOfFirst = first.end() - first.begin(); for (const auto& element : second) { const auto end = first.cbegin() + initialSizeOfFirst;