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) <noreply@anthropic.com>
This commit is contained in:
leftibot 2026-04-13 20:12:15 -06:00
parent d4c5bdb3e4
commit 7f63b5dbd1
7 changed files with 27 additions and 22 deletions

View File

@ -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)

View File

@ -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>(), "void");
m.add(user_type<bool>(), "bool");
m.add(user_type<Boxed_Value>(), "Object");

View File

@ -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

View File

@ -26,11 +26,6 @@
#include "chaiscript_optimizer.hpp"
#include "chaiscript_tracer.hpp"
#if defined(CHAISCRIPT_UTF16_UTF32)
#include <codecvt>
#include <locale>
#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<std::codecvt_utf8<target_char_type>, 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<target_char_type>(val));
}
};

View File

@ -22,12 +22,22 @@ namespace chaiscript {
struct Loadable_Module {
template<typename T>
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<wchar_t>(ch));
}
return result;
}
template<typename T>
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<char>(ch));
}
return result;
}
#if defined(_UNICODE) || defined(UNICODE)

View File

@ -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)

View File

@ -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<std::string>(R"("\u0041")") == "A");
CHECK(chai.eval<std::string>(R"("\u004F")") == "O");
CHECK(chai.eval<std::string>(R"("\u0030")") == "0");
}