From 7f63b5dbd17acdd376bda9672d8ef303ac62efc7 Mon Sep 17 00:00:00 2001 From: leftibot Date: Mon, 13 Apr 2026 20:12:15 -0600 Subject: [PATCH] Fix #518: Enable warnings as errors and fix all compiler warnings Enable -Werror for GCC/Clang and /WX for MSVC to enforce warning-free builds. Fix wchar_t-to-char narrowing conversion in chaiscript_windows.hpp by using explicit static_cast. Replace deprecated std::wstring_convert in chaiscript_parser.hpp with direct static_cast. Add noexcept to JSON default constructor, and mark intentionally unused parameters with [[maybe_unused]]. Co-Authored-By: Claude Opus 4.6 (1M context) --- CMakeLists.txt | 4 ++-- include/chaiscript/dispatchkit/bootstrap.hpp | 2 +- include/chaiscript/language/chaiscript_engine.hpp | 2 +- include/chaiscript/language/chaiscript_parser.hpp | 15 +-------------- .../chaiscript/language/chaiscript_windows.hpp | 14 ++++++++++++-- include/chaiscript/utility/json.hpp | 4 ++-- unittests/compiled_tests.cpp | 8 ++++++++ 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30924841..31d019bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,7 +155,7 @@ else() endif() if(MSVC) - add_definitions(/W4 /w14545 /w34242 /w34254 /w34287 /w44263 /w44265 /w44296 /w44311 /w44826 /we4289 /w14546 /w14547 /w14549 /w14555 /w14619 /w14905 /w14906 /w14928) + add_definitions(/W4 /WX /w14545 /w34242 /w34254 /w34287 /w44263 /w44265 /w44296 /w44311 /w44826 /we4289 /w14546 /w14547 /w14549 /w14555 /w14619 /w14905 /w14906 /w14928) if(MSVC_VERSION STREQUAL "1800") # VS2013 doesn't have magic statics @@ -175,7 +175,7 @@ if(MSVC) # how to workaround or fix the error. So I'm disabling it globally. add_definitions(/wd4503) else() - add_definitions(-Wall -Wextra -Wconversion -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Wunused -Woverloaded-virtual -Wno-noexcept-type -Wpedantic -Werror=return-type) + add_definitions(-Wall -Wextra -Werror -Wconversion -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Wunused -Woverloaded-virtual -Wno-noexcept-type -Wpedantic) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") add_definitions(-Weverything -Wno-c++98-compat-pedantic -Wno-c++98-compat -Wno-documentation -Wno-switch-enum -Wno-weak-vtables -Wno-missing-prototypes -Wno-padded -Wno-missing-noreturn -Wno-exit-time-destructors -Wno-documentation-unknown-command -Wno-unused-template -Wno-undef -Wno-double-promotion) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 041af80e..15d1fbce 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -269,7 +269,7 @@ namespace chaiscript::bootstrap { /// \brief perform all common bootstrap functions for std::string, void and POD types /// \param[in,out] m Module to add bootstrapped functions to /// \param[in] t_no_io If true, skip registering print_string and println_string - static void bootstrap(Module &m, const bool t_no_io = false) { + static void bootstrap(Module &m, [[maybe_unused]] const bool t_no_io = false) { m.add(user_type(), "void"); m.add(user_type(), "bool"); m.add(user_type(), "Object"); diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 4afd0449..c0cb9c44 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -584,7 +584,7 @@ namespace chaiscript { /// (the symbol mentioned above), an exception is thrown. /// /// \throw chaiscript::exception::load_module_error In the event that no matching module can be found. - std::string load_module(const std::string &t_module_name) { + std::string load_module([[maybe_unused]] const std::string &t_module_name) { #ifdef CHAISCRIPT_NO_DYNLOAD throw chaiscript::exception::load_module_error("Loadable module support was disabled (CHAISCRIPT_NO_DYNLOAD)"); #else diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index be2a7525..37522839 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -26,11 +26,6 @@ #include "chaiscript_optimizer.hpp" #include "chaiscript_tracer.hpp" -#if defined(CHAISCRIPT_UTF16_UTF32) -#include -#include -#endif - #if defined(CHAISCRIPT_MSVC) && defined(max) && defined(min) #define CHAISCRIPT_PUSHED_MIN_MAX #pragma push_macro("max") // Why Microsoft? why? This is worse than bad @@ -81,15 +76,7 @@ namespace chaiscript { static string_type str_from_ll(long long val) { using target_char_type = typename string_type::value_type; -#if defined(CHAISCRIPT_UTF16_UTF32) - // prepare converter - std::wstring_convert, target_char_type> converter; - // convert - return converter.from_bytes(u8str_from_ll(val)); -#else - // no conversion available, just put value as character - return string_type(1, target_char_type(val)); // size, character -#endif + return string_type(1, static_cast(val)); } }; diff --git a/include/chaiscript/language/chaiscript_windows.hpp b/include/chaiscript/language/chaiscript_windows.hpp index 5e2b6e88..9c1115a2 100644 --- a/include/chaiscript/language/chaiscript_windows.hpp +++ b/include/chaiscript/language/chaiscript_windows.hpp @@ -22,12 +22,22 @@ namespace chaiscript { struct Loadable_Module { template static std::wstring to_wstring(const T &t_str) { - return std::wstring(t_str.begin(), t_str.end()); + std::wstring result; + result.reserve(t_str.size()); + for (const auto &ch : t_str) { + result.push_back(static_cast(ch)); + } + return result; } template static std::string to_string(const T &t_str) { - return std::string(t_str.begin(), t_str.end()); + std::string result; + result.reserve(t_str.size()); + for (const auto &ch : t_str) { + result.push_back(static_cast(ch)); + } + return result; } #if defined(_UNICODE) || defined(UNICODE) diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index aa8fe698..f81bb8dc 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -47,7 +47,7 @@ namespace chaiscript::json { Internal(std::nullptr_t) : d(nullptr) { } - Internal() + Internal() noexcept : d(nullptr) { } Internal(Class c) @@ -153,7 +153,7 @@ namespace chaiscript::json { typename Container::const_iterator end() const noexcept { return object ? object->end() : typename Container::const_iterator(); } }; - JSON() = default; + JSON() noexcept = default; JSON(std::nullptr_t) {} explicit JSON(Class type) diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index 516c8bd5..843a586b 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1970,3 +1970,11 @@ TEST_CASE("Exception from C++ [] operator is catchable in ChaiScript") { caught )") == true); } + +TEST_CASE("Unicode escape sequences produce correct values") { + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(), create_chaiscript_parser()); + + CHECK(chai.eval(R"("\u0041")") == "A"); + CHECK(chai.eval(R"("\u004F")") == "O"); + CHECK(chai.eval(R"("\u0030")") == "0"); +}