From cb778b6bb60d609a18a0ebc48c79e8ee6d4bd92d Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Mon, 17 May 2021 13:57:29 +1000 Subject: [PATCH] Minor changes to address msvc warnings and make cmake work on Windows --- CMakeLists.txt | 31 ++++++----- .../valijson/adapters/picojson_adapter.hpp | 6 +++ include/valijson/utils/picojson_utils.hpp | 6 +++ include/valijson/validation_visitor.hpp | 54 +++++++++++-------- tests/test_adapter_comparison.cpp | 6 +++ tests/test_poly_constraint.cpp | 2 +- tests/test_validator.cpp | 6 +++ 7 files changed, 75 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index efd5288..c3423fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,20 @@ option(valijson_BUILD_TESTS "Build valijson test suite." TRUE) option(valijson_EXCLUDE_BOOST "Exclude Boost when building test suite." FALSE) option(valijson_USE_EXCEPTIONS "Use exceptions in valijson and included libs." TRUE) -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") +if(MSVC) + add_definitions(-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING) + + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") +else() + include(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) + if(!COMPILER_SUPPORTS_CXX11) + message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") + endif() + + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") + SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") +endif() add_library(valijson INTERFACE) # create alias, so that user could always write target_link_libraries(... ValiJSON::valijson) @@ -29,16 +42,6 @@ if(NOT valijson_BUILD_TESTS AND NOT valijson_BUILD_EXAMPLES) return() endif() -SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") - -include(CheckCXXCompilerFlag) -CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) -if(COMPILER_SUPPORTS_CXX11) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -else() - message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") -endif() - #TODO() if(valijson_USE_EXCEPTIONS) add_definitions(-DVALIJSON_USE_EXCEPTIONS=1) @@ -124,7 +127,11 @@ if(valijson_BUILD_TESTS) # Unit tests executable add_executable(test_suite ${TEST_SOURCES}) - set_target_properties(test_suite PROPERTIES COMPILE_FLAGS " -pedantic -Werror -Wshadow -Wunused") + if(MSVC) + + else() + set_target_properties(test_suite PROPERTIES COMPILE_FLAGS " -pedantic -Werror -Wshadow -Wunused") + endif() # Definition for using picojson set_target_properties(test_suite PROPERTIES COMPILE_DEFINITIONS "PICOJSON_USE_INT64") diff --git a/include/valijson/adapters/picojson_adapter.hpp b/include/valijson/adapters/picojson_adapter.hpp index 49e7af4..045ff68 100644 --- a/include/valijson/adapters/picojson_adapter.hpp +++ b/include/valijson/adapters/picojson_adapter.hpp @@ -27,7 +27,13 @@ #include +#ifdef _MSC_VER +#pragma warning(disable: 4706) #include +#pragma warning(default: 4706) +#else +#include +#endif #include #include diff --git a/include/valijson/utils/picojson_utils.hpp b/include/valijson/utils/picojson_utils.hpp index fa2cc3e..36420d2 100644 --- a/include/valijson/utils/picojson_utils.hpp +++ b/include/valijson/utils/picojson_utils.hpp @@ -2,7 +2,13 @@ #include +#ifdef _MSC_VER +#pragma warning(disable: 4706) #include +#pragma warning(default: 4706) +#else +#include +#endif #include diff --git a/include/valijson/validation_visitor.hpp b/include/valijson/validation_visitor.hpp index a29f0f7..227ff3e 100644 --- a/include/valijson/validation_visitor.hpp +++ b/include/valijson/validation_visitor.hpp @@ -1150,31 +1150,39 @@ public: return true; } - const typename AdapterType::Array targetArray = m_target.asArray(); - const typename AdapterType::Array::const_iterator end = targetArray.end(); - bool validated = true; - const typename AdapterType::Array::const_iterator secondLast = --targetArray.end(); - unsigned int outerIndex = 0; - typename AdapterType::Array::const_iterator outerItr = targetArray.begin(); - for (; outerItr != secondLast; ++outerItr) { - unsigned int innerIndex = outerIndex + 1; - typename AdapterType::Array::const_iterator innerItr(outerItr); - for (++innerItr; innerItr != end; ++innerItr) { - if (outerItr->equalTo(*innerItr, true)) { - if (m_results) { - m_results->pushError(m_context, "Elements at indexes #" + std::to_string(outerIndex) - + " and #" + std::to_string(innerIndex) + " violate uniqueness constraint."); - validated = false; - } else { - return false; - } - } - ++innerIndex; - } - ++outerIndex; - } +#ifdef VALIJSON_USE_EXCEPTIONS + try +#endif + { + const typename AdapterType::Array targetArray = m_target.asArray(); + const typename AdapterType::Array::const_iterator end = targetArray.end(); + const typename AdapterType::Array::const_iterator secondLast = --targetArray.end(); + unsigned int outerIndex = 0; + typename AdapterType::Array::const_iterator outerItr = targetArray.begin(); + for (; outerItr != secondLast; ++outerItr) { + unsigned int innerIndex = outerIndex + 1; + typename AdapterType::Array::const_iterator innerItr(outerItr); + for (++innerItr; innerItr != end; ++innerItr) { + if (outerItr->equalTo(*innerItr, true)) { + if (!m_results) { + return false; + } + m_results->pushError(m_context, "Elements at indexes #" + std::to_string(outerIndex) + + " and #" + std::to_string(innerIndex) + " violate uniqueness constraint."); + validated = false; + } + ++innerIndex; + } + ++outerIndex; + } + } +#ifdef VALIJSON_USE_EXCEPTIONS + catch (...) { + throw; + } +#endif return validated; } diff --git a/tests/test_adapter_comparison.cpp b/tests/test_adapter_comparison.cpp index 59945cb..b269626 100644 --- a/tests/test_adapter_comparison.cpp +++ b/tests/test_adapter_comparison.cpp @@ -1,4 +1,10 @@ +#ifdef _MSC_VER +#pragma warning(disable: 4706) #include +#pragma warning(default: 4706) +#else +#include +#endif #include diff --git a/tests/test_poly_constraint.cpp b/tests/test_poly_constraint.cpp index fa69aa7..639fa59 100644 --- a/tests/test_poly_constraint.cpp +++ b/tests/test_poly_constraint.cpp @@ -34,7 +34,7 @@ public: } bool validate( - const Adapter &target, + const Adapter &, const std::vector &context, ValidationResults *results) const override { diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index e9a7eea..d8ea284 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -1,4 +1,10 @@ +#ifdef _MSC_VER +#pragma warning(disable: 4706) #include +#pragma warning(default: 4706) +#else +#include +#endif #include