diff --git a/include/xtensor/core/xmath.hpp b/include/xtensor/core/xmath.hpp index 77f864929..5f3a8aef8 100644 --- a/include/xtensor/core/xmath.hpp +++ b/include/xtensor/core/xmath.hpp @@ -38,18 +38,18 @@ namespace xt template struct numeric_constants { - static constexpr T PI = 3.141592653589793238463; - static constexpr T PI_2 = 1.57079632679489661923; - static constexpr T PI_4 = 0.785398163397448309616; - static constexpr T D_1_PI = 0.318309886183790671538; - static constexpr T D_2_PI = 0.636619772367581343076; - static constexpr T D_2_SQRTPI = 1.12837916709551257390; - static constexpr T SQRT2 = 1.41421356237309504880; - static constexpr T SQRT1_2 = 0.707106781186547524401; - static constexpr T E = 2.71828182845904523536; - static constexpr T LOG2E = 1.44269504088896340736; - static constexpr T LOG10E = 0.434294481903251827651; - static constexpr T LN2 = 0.693147180559945309417; + static constexpr T PI = static_cast(3.141592653589793238463); + static constexpr T PI_2 = static_cast(1.57079632679489661923); + static constexpr T PI_4 = static_cast(0.785398163397448309616); + static constexpr T D_1_PI = static_cast(0.318309886183790671538); + static constexpr T D_2_PI = static_cast(0.636619772367581343076); + static constexpr T D_2_SQRTPI = static_cast(1.12837916709551257390); + static constexpr T SQRT2 = static_cast(1.41421356237309504880); + static constexpr T SQRT1_2 = static_cast(0.707106781186547524401); + static constexpr T E = static_cast(2.71828182845904523536); + static constexpr T LOG2E = static_cast(1.44269504088896340736); + static constexpr T LOG10E = static_cast(0.434294481903251827651); + static constexpr T LN2 = static_cast(0.693147180559945309417); }; /*********** @@ -569,6 +569,10 @@ namespace xt namespace math { +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" +#endif template struct minimum { @@ -2403,6 +2407,9 @@ namespace xt return !math::isnan(rhs) ? lhs + rhs : lhs; } }; +#if defined(__clang__) +#pragma clang diagnostic pop +#endif struct nan_multiplies { diff --git a/include/xtensor/core/xoperation.hpp b/include/xtensor/core/xoperation.hpp index 533ad6513..02b880a0f 100644 --- a/include/xtensor/core/xoperation.hpp +++ b/include/xtensor/core/xoperation.hpp @@ -83,6 +83,10 @@ namespace xt namespace detail { +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" +#endif DEFINE_COMPLEX_OVERLOAD(+); DEFINE_COMPLEX_OVERLOAD(-); DEFINE_COMPLEX_OVERLOAD(*); @@ -101,13 +105,23 @@ namespace xt DEFINE_COMPLEX_OVERLOAD(>=); DEFINE_COMPLEX_OVERLOAD(==); DEFINE_COMPLEX_OVERLOAD(!=); +#if defined(__clang__) +#pragma clang diagnostic pop +#endif UNARY_OPERATOR_FUNCTOR(identity, +); UNARY_OPERATOR_FUNCTOR(negate, -); +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" +#endif BINARY_OPERATOR_FUNCTOR(plus, +); BINARY_OPERATOR_FUNCTOR(minus, -); BINARY_OPERATOR_FUNCTOR(multiplies, *); BINARY_OPERATOR_FUNCTOR(divides, /); +#if defined(__clang__) +#pragma clang diagnostic pop +#endif BINARY_OPERATOR_FUNCTOR(modulus, %); BINARY_OPERATOR_FUNCTOR(logical_or, ||); BINARY_OPERATOR_FUNCTOR(logical_and, &&); diff --git a/include/xtensor/generators/xbuilder.hpp b/include/xtensor/generators/xbuilder.hpp index 8a2f61147..9efda6e21 100644 --- a/include/xtensor/generators/xbuilder.hpp +++ b/include/xtensor/generators/xbuilder.hpp @@ -498,8 +498,8 @@ namespace xt inline value_type access(const tuple_type& t, size_type axis, It first, It last) const { // trim off extra indices if provided to match behavior of containers - auto dim_offset = std::distance(first, last) - std::get<0>(t).dimension(); - size_t axis_dim = *(first + axis + dim_offset); + auto dim_offset = static_cast(std::distance(first, last)) - std::get<0>(t).dimension(); + size_t axis_dim = static_cast(*(first + static_cast(axis + dim_offset))); auto match = [&](auto& arr) { if (axis_dim >= arr.shape()[axis]) @@ -520,7 +520,7 @@ namespace xt const size_t stride = std::accumulate( shape.begin() + i + 1, shape.end(), - 1, + size_t(1), std::multiplies() ); if (i == axis) @@ -529,11 +529,13 @@ namespace xt } else { - const auto len = (*(first + i + dim_offset)); + const auto len = static_cast( + *(first + static_cast(i + dim_offset)) + ); offset += len * stride; } } - const auto element = arr.begin() + offset; + const auto element = arr.begin() + static_cast(offset); return *element; }; @@ -576,16 +578,18 @@ namespace xt const size_t stride = std::accumulate( shape.begin() + i + 1, shape.end(), - 1, + size_t(1), std::multiplies() ); - const auto len = (*(first + i + after_axis)); + const auto len = static_cast( + *(first + static_cast(i + after_axis)) + ); offset += len * stride; } - const auto element = arr.begin() + offset; + const auto element = arr.begin() + static_cast(offset); return *element; }; - size_type i = *(first + axis); + size_type i = static_cast(*(first + static_cast(axis))); return apply(i, get_item, t); } }; diff --git a/include/xtensor/misc/xfft.hpp b/include/xtensor/misc/xfft.hpp index 954b55a9b..a74288e02 100644 --- a/include/xtensor/misc/xfft.hpp +++ b/include/xtensor/misc/xfft.hpp @@ -61,8 +61,8 @@ namespace xt auto odd = radix2(xt::view(ev, xt::range(1, _, 2))); #endif - auto range = xt::arange(N / 2); - auto exp = xt::exp(static_cast(-2i) * pi * range / N); + auto range = xt::arange(static_cast(N / 2)); + auto exp = xt::exp(static_cast(-2i) * pi * range / static_cast(N)); auto t = exp * odd; auto first_half = even + t; auto second_half = even - t; @@ -82,15 +82,17 @@ namespace xt // Find a power-of-2 convolution length m such that m >= n * 2 + 1 const std::size_t n = data.size(); - size_t m = std::ceil(std::log2(n * 2 + 1)); - m = std::pow(2, m); + size_t m = static_cast(std::ceil(std::log2(n * 2 + 1))); + m = static_cast(std::pow(2, m)); // Trignometric table auto exp_table = xt::xtensor, 1>::from_shape({n}); xt::xtensor i = xt::pow(xt::linspace(0, n - 1, n), 2); i %= (n * 2); - auto angles = xt::eval(precision{3.141592653589793238463} * i / n); + auto angles = xt::eval( + static_cast(3.141592653589793238463) * i / static_cast(n) + ); auto j = std::complex(0, 1); exp_table = xt::exp(-angles * j); @@ -162,7 +164,8 @@ namespace xt if constexpr (xtl::is_complex::type::value_type>::value) { // check the length of the data on that axis - const std::size_t n = e.shape(axis); + const std::size_t saxis = xt::normalize_axis(e.dimension(), axis); + const std::size_t n = e.shape(saxis); if (n == 0) { XTENSOR_THROW(std::runtime_error, "Cannot take the iFFT along an empty dimention"); diff --git a/include/xtensor/misc/xmanipulation.hpp b/include/xtensor/misc/xmanipulation.hpp index f100744ac..f3e7e9f0a 100644 --- a/include/xtensor/misc/xmanipulation.hpp +++ b/include/xtensor/misc/xmanipulation.hpp @@ -293,11 +293,11 @@ namespace xt { const std::size_t ax1 = normalize_axis(dim, axis1); const std::size_t ax2 = normalize_axis(dim, axis2); - auto perm = xtl::make_sequence(dim, 0); using id_t = typename S::value_type; + auto perm = xtl::make_sequence(dim, 0); std::iota(perm.begin(), perm.end(), id_t(0)); - perm[ax1] = ax2; - perm[ax2] = ax1; + perm[ax1] = static_cast(ax2); + perm[ax2] = static_cast(ax1); return perm; } } @@ -339,18 +339,18 @@ namespace xt // Initializing to src_norm handles case where `dest == -1` and the loop // does not go check `perm_idx == dest_norm` a `dim+1`th time. - auto perm = xtl::make_sequence(dim, src_norm); + auto perm = xtl::make_sequence(dim, static_cast(src_norm)); id_t perm_idx = 0; for (id_t i = 0; xtl::cmp_less(i, dim); ++i) { if (xtl::cmp_equal(perm_idx, dest_norm)) { - perm[perm_idx] = src_norm; + perm[static_cast(perm_idx)] = static_cast(src_norm); ++perm_idx; } if (xtl::cmp_not_equal(i, src_norm)) { - perm[perm_idx] = i; + perm[static_cast(perm_idx)] = i; ++perm_idx; } } diff --git a/include/xtensor/misc/xsort.hpp b/include/xtensor/misc/xsort.hpp index aa9ea4cfa..86366a69d 100644 --- a/include/xtensor/misc/xsort.hpp +++ b/include/xtensor/misc/xsort.hpp @@ -81,7 +81,7 @@ namespace xt const std::ptrdiff_t secondary_stride = get_secondary_stride(ev); const auto begin = ev.data(); - const auto end = begin + n_iters * secondary_stride; + const auto end = begin + static_cast(n_iters) * secondary_stride; for (auto iter = begin; iter != end; iter += secondary_stride) { fct(iter, iter + secondary_stride); @@ -100,9 +100,9 @@ namespace xt XTENSOR_ASSERT(secondary_stride1 == secondary_stride2); const auto begin1 = e1.data(); - const auto end1 = begin1 + n_iters * secondary_stride1; + const auto end1 = begin1 + static_cast(n_iters) * secondary_stride1; const auto begin2 = e2.data(); - const auto end2 = begin2 + n_iters * secondary_stride2; + const auto end2 = begin2 + static_cast(n_iters) * secondary_stride2; auto iter1 = begin1; auto iter2 = begin2; for (; (iter1 != end1) && (iter2 != end2); iter1 += secondary_stride1, iter2 += secondary_stride2) @@ -304,6 +304,7 @@ namespace xt XTENSOR_ASSERT(std::distance(data_begin, data_end) >= 0); XTENSOR_ASSERT(std::distance(idx_begin, idx_end) == std::distance(data_begin, data_end)); (void) idx_end; // TODO(C++17) [[maybe_unused]] only used in assertion. + (void) data_end; std::iota(idx_begin, idx_end, 0); switch (method) @@ -315,7 +316,10 @@ namespace xt idx_end, [&](const auto i, const auto j) { - return comp(*(data_begin + i), *(data_begin + j)); + return comp( + *(data_begin + static_cast(i)), + *(data_begin + static_cast(j)) + ); } ); } @@ -326,7 +330,10 @@ namespace xt idx_end, [&](const auto i, const auto j) { - return comp(*(data_begin + i), *(data_begin + j)); + return comp( + *(data_begin + static_cast(i)), + *(data_begin + static_cast(j)) + ); } ); } @@ -722,7 +729,7 @@ namespace xt res_end, kth_container.rbegin(), kth_container.rend(), - [&ev_begin](auto const& i, auto const& j) + [&ev_begin](const auto& i, const auto& j) { return *(ev_begin + i) < *(ev_begin + j); } @@ -784,7 +791,7 @@ namespace xt for (auto i : indices) { auto idx = current_index; - idx[current_dim] = i; + idx[current_dim] = static_cast(i); select_indices_impl(shape, indices, axis, current_dim + 1, idx, out); } } @@ -793,7 +800,7 @@ namespace xt for (id_t i = 0; xtl::cmp_less(i, shape[current_dim]); ++i) { auto idx = current_index; - idx[current_dim] = i; + idx[current_dim] = static_cast(i); select_indices_impl(shape, indices, axis, current_dim + 1, idx, out); } } @@ -802,7 +809,7 @@ namespace xt for (auto i : indices) { auto idx = current_index; - idx[current_dim] = i; + idx[current_dim] = static_cast(i); out.push_back(std::move(idx)); } } @@ -811,7 +818,7 @@ namespace xt for (id_t i = 0; xtl::cmp_less(i, shape[current_dim]); ++i) { auto idx = current_index; - idx[current_dim] = i; + idx[current_dim] = static_cast(i); out.push_back(std::move(idx)); } } @@ -834,7 +841,7 @@ namespace xt const std::size_t ax = normalize_axis(e.dimension(), axis); using shape_t = get_strides_t::shape_type>; auto shape = xtl::forward_sequence(e.shape()); - shape[ax] = indices.size(); + shape[ax] = static_cast(indices.size()); return reshape_view( index_view(std::forward(e), select_indices(e.shape(), indices, ax)), std::move(shape) @@ -915,12 +922,16 @@ namespace xt auto kth_gamma = detail::quantile_kth_gamma(n, probas, alpha, beta); // Select relevant values for computing interpolating quantiles - auto e_partition = xt::partition(std::forward(e), kth_gamma.first, ax); - auto e_kth = detail::fancy_indexing(std::move(e_partition), std::move(kth_gamma.first), ax); + auto e_partition = xt::partition(std::forward(e), kth_gamma.first, static_cast(ax)); + auto e_kth = detail::fancy_indexing( + std::move(e_partition), + std::move(kth_gamma.first), + static_cast(ax) + ); // Reshape interpolation coefficients auto gm1_g_shape = xtl::make_sequence(e.dimension(), 1); - gm1_g_shape[ax] = kth_gamma.second.size(); + gm1_g_shape[ax] = static_cast(kth_gamma.second.size()); auto gm1_g_reshaped = reshape_view(std::move(kth_gamma.second), std::move(gm1_g_shape)); // Compute interpolation @@ -932,7 +943,7 @@ namespace xt e_kth_g_shape[ax + 1] /= 2; auto quantiles = xt::sum(reshape_view(std::move(e_kth_g), std::move(e_kth_g_shape)), ax); // Cannot do a transpose on a non-strided expression so we have to eval - return moveaxis(eval(std::move(quantiles)), ax, 0); + return moveaxis(eval(std::move(quantiles)), static_cast(ax), 0); } // Static proba array overload @@ -1237,7 +1248,6 @@ namespace xt template inline auto argmin(const xexpression& e) { - using value_type = typename E::value_type; auto&& ed = eval(e.derived_cast()); auto begin = ed.template begin(); auto end = ed.template end(); @@ -1267,7 +1277,6 @@ namespace xt template inline auto argmax(const xexpression& e) { - using value_type = typename E::value_type; auto&& ed = eval(e.derived_cast()); auto begin = ed.template begin(); auto end = ed.template end(); diff --git a/include/xtensor/optional/xoptional.hpp b/include/xtensor/optional/xoptional.hpp index 6c690b0d6..66c8b52d5 100644 --- a/include/xtensor/optional/xoptional.hpp +++ b/include/xtensor/optional/xoptional.hpp @@ -199,7 +199,7 @@ namespace xt template constexpr result_type apply_impl(const U& t, const Args&... args) const { - return t & apply_impl(args...); + return t && apply_impl(args...); } template @@ -211,7 +211,7 @@ namespace xt template constexpr B1 simd_apply_impl(const B1& b1, const B2& b2, const Args&... args) const { - return b1 & simd_apply_impl(b2, args...); + return b1 && simd_apply_impl(b2, args...); } }; @@ -242,7 +242,6 @@ namespace xt struct xreducer_temporary_type> { using type = xtl::xoptional, bool>; - ; }; } diff --git a/include/xtensor/utils/xutils.hpp b/include/xtensor/utils/xutils.hpp index 2e97f67db..8da835fab 100644 --- a/include/xtensor/utils/xutils.hpp +++ b/include/xtensor/utils/xutils.hpp @@ -33,8 +33,12 @@ #if (defined(_MSC_VER) && _MSC_VER >= 1910) #define NOEXCEPT(T) #else +#ifdef XTENSOR_ENABLE_ASSERT +#define NOEXCEPT(T) +#else #define NOEXCEPT(T) noexcept(T) #endif +#endif namespace xt { diff --git a/include/xtensor/views/index_mapper.hpp b/include/xtensor/views/index_mapper.hpp index 672cb2207..672cc7e05 100644 --- a/include/xtensor/views/index_mapper.hpp +++ b/include/xtensor/views/index_mapper.hpp @@ -16,7 +16,7 @@ namespace xt { template - struct index_mapper; + class index_mapper; /** * @enum access_t @@ -400,8 +400,9 @@ namespace xt { constexpr size_t n_indices_full = n_indices_full_v; - constexpr size_t underlying_n_dimensions = xt::static_dimension< - typename std::decay_t::shape_type>::value; + constexpr size_t underlying_n_dimensions = static_cast( + xt::static_dimension::shape_type>::value + ); // If there is too many indices, we need to drop the first ones. // If the number of dimensions of the underlying container is known at compile time we can drop them @@ -515,8 +516,9 @@ namespace xt } else { + using size_type = typename std::decay_t::size_type; assert(i < slice.size()); - return size_t(slice(i)); + return static_cast(slice(static_cast(i))); } } else diff --git a/include/xtensor/views/xindex_view.hpp b/include/xtensor/views/xindex_view.hpp index 8b911afb8..c316c8879 100644 --- a/include/xtensor/views/xindex_view.hpp +++ b/include/xtensor/views/xindex_view.hpp @@ -511,7 +511,7 @@ namespace xt template inline auto xindex_view::element(It first, It /*last*/) -> reference { - return m_e[m_indices[(*first)]]; + return m_e[m_indices[static_cast(*first)]]; } /** @@ -523,7 +523,7 @@ namespace xt template inline auto xindex_view::element(It first, It /*last*/) const -> const_reference { - return m_e[m_indices[(*first)]]; + return m_e[m_indices[static_cast(*first)]]; } /** diff --git a/include/xtensor/views/xslice.hpp b/include/xtensor/views/xslice.hpp index a7a4dae6e..3565288c8 100644 --- a/include/xtensor/views/xslice.hpp +++ b/include/xtensor/views/xslice.hpp @@ -718,7 +718,14 @@ namespace xt type operator()(T t) { - return (xtl::is_integral::value) ? static_cast(t) : t; + if constexpr (xtl::is_integral::value) + { + return static_cast(t); + } + else + { + return t; + } } }; @@ -780,7 +787,7 @@ namespace xt { if constexpr (is_xslice::value) { - return slice.size(); + return static_cast(slice.size()); } else { @@ -797,7 +804,7 @@ namespace xt { if constexpr (is_xslice::value) { - return slice.step_size(idx); + return static_cast(slice.step_size(idx)); } else { @@ -810,7 +817,7 @@ namespace xt { if constexpr (is_xslice::value) { - return slice.step_size(idx, n); + return static_cast(slice.step_size(idx, n)); } else { @@ -828,7 +835,7 @@ namespace xt if constexpr (is_xslice::value) { using ST = typename S::size_type; - return slice(static_cast(i)); + return static_cast(slice(static_cast(i))); } else { diff --git a/include/xtensor/views/xstrided_view.hpp b/include/xtensor/views/xstrided_view.hpp index f0fda4214..8845ce1fd 100644 --- a/include/xtensor/views/xstrided_view.hpp +++ b/include/xtensor/views/xstrided_view.hpp @@ -834,7 +834,7 @@ namespace xt if (iter != std::end(shape)) { const auto total = std::accumulate(shape.cbegin(), shape.cend(), -1, std::multiplies{}); - const auto missing_dimension = size / total; + const auto missing_dimension = size / static_cast(total); (*iter) = static_cast(missing_dimension); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8341230ee..ae6012e77 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,59 +57,73 @@ if(NOT _cxx_std_flag) message(FATAL_ERROR "xtensor needs a C++20-compliant compiler.") endif() -OPTION(XTENSOR_ENABLE_WERROR "Turn on -Werror" OFF) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DXSIMD_ENABLE_XTL_COMPLEX=1") -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT WIN32)) - CHECK_CXX_COMPILER_FLAG(-march=native arch_native_supported) - if(arch_native_supported AND NOT CMAKE_CXX_FLAGS MATCHES "-march") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") - endif() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} -Wunused-parameter -Wextra -Wreorder -Wconversion -Wno-sign-conversion ") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wold-style-cast -Wunused-variable -ftemplate-backtrace-limit=0") - if (XTENSOR_DISABLE_EXCEPTIONS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") - endif() - if (XTENSOR_ENABLE_WERROR) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -DSKIP_ON_WERROR") - endif() -elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} /Zc:__cplusplus /MP /bigobj") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO") - add_definitions(-D_CRT_SECURE_NO_WARNINGS) - add_definitions(-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING) - if (XTENSOR_DISABLE_EXCEPTIONS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-") - else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") - endif() -elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") -if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU") - CHECK_CXX_COMPILER_FLAG(-march=native arch_native_supported) - if(arch_native_supported AND NOT CMAKE_CXX_FLAGS MATCHES "-march") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") - endif() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} -Wunused-parameter -Wextra -Wreorder -Wconversion -Wsign-conversion") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wold-style-cast -Wunused-variable") - if (XTENSOR_DISABLE_EXCEPTIONS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") - endif() - if (XTENSOR_ENABLE_WERROR) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -DSKIP_ON_WERROR") - endif() - elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} /bigobj") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO") + +# Common warning flags for all compilers +if(MSVC) + set(CMAKE_CXX_FLAGS_WARNINGS "/W4 /WX") +else() + set(CMAKE_CXX_FLAGS_WARNINGS "-Werror -Wextra -Wall") +endif() + +# Helper macro to reduce MSVC-specific boilerplate +macro(add_msvc_flags) add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO") +endmacro() + +# Helper macro for exception handling +macro(add_exception_flags) if (XTENSOR_DISABLE_EXCEPTIONS) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-") + if(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") + endif() else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") + if(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") + endif() endif() - endif() +endmacro() + +# Compiler-specific configuration +if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT WIN32)) + # GCC / Intel (non-Windows) + CHECK_CXX_COMPILER_FLAG(-march=native arch_native_supported) + if(arch_native_supported AND NOT CMAKE_CXX_FLAGS MATCHES "-march") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} ${CMAKE_CXX_FLAGS_WARNINGS}") + add_exception_flags() + +elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + # MSVC + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} ${CMAKE_CXX_FLAGS_WARNINGS} /Zc:__cplusplus /MP /bigobj") + add_msvc_flags() + add_exception_flags() + +elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # Clang (check frontend variant) + if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU") + # Clang with GNU frontend + CHECK_CXX_COMPILER_FLAG(-march=native arch_native_supported) + if(arch_native_supported AND NOT CMAKE_CXX_FLAGS MATCHES "-march") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} ${CMAKE_CXX_FLAGS_WARNINGS}") + add_exception_flags() + + elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") + # Clang with MSVC frontend + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_cxx_std_flag} ${CMAKE_CXX_FLAGS_WARNINGS} /bigobj") + add_msvc_flags() + add_exception_flags() + endif() + else() - message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}") + message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}") endif() diff --git a/test/test_common.hpp b/test/test_common.hpp index 3134c0bfe..033d8854e 100644 --- a/test/test_common.hpp +++ b/test/test_common.hpp @@ -459,7 +459,7 @@ namespace xt } template - void test_bound_check(V& vec) + void test_bound_check([[maybe_unused]] V& vec) { #if XTENSOR_ENABLE_ASSERT XT_EXPECT_ANY_THROW(vec(10, 10, 10)); @@ -469,7 +469,7 @@ namespace xt } template - void test_access_check(V& vec) + void test_access_check([[maybe_unused]] V& vec) { XT_EXPECT_ANY_THROW(vec.at(10, 10, 10)); XT_EXPECT_ANY_THROW(vec.at(0, 0, 0, 0, 0, 0)); @@ -769,7 +769,7 @@ namespace xt { INFO("incompatible shapes"); - shape_type s4 = {2, 1, 3, 2}; + [[maybe_unused]] shape_type s4 = {2, 1, 3, 2}; XT_EXPECT_THROW(vec.broadcast_shape(s4), broadcast_error); } } diff --git a/test/test_common_macros.hpp b/test/test_common_macros.hpp index 5d359a62d..a804828d1 100644 --- a/test/test_common_macros.hpp +++ b/test/test_common_macros.hpp @@ -11,7 +11,7 @@ #include "test_utils.hpp" #if defined(XTENSOR_DISABLE_EXCEPTIONS) -#warning "XT_EXPECT_THROW, XT_ASSERT_THROW, XT_EXPECT_ANY_THROW and XT_ASSERT_ANY_THROW are disabled" +// #warning "XT_EXPECT_THROW, XT_ASSERT_THROW, XT_EXPECT_ANY_THROW and XT_ASSERT_ANY_THROW are disabled" #define XT_EXPECT_THROW(x, y) #define XT_ASSERT_THROW(x, y) #define XT_EXPECT_ANY_THROW(x) diff --git a/test/test_xbuffer_adaptor.cpp b/test/test_xbuffer_adaptor.cpp index d6ad32b21..b2ea1c605 100644 --- a/test/test_xbuffer_adaptor.cpp +++ b/test/test_xbuffer_adaptor.cpp @@ -198,7 +198,7 @@ namespace xt double* data1 = new double[size1]; buffer_adaptor adapt(data1, size1); - size_t size2 = 50; + [[maybe_unused]] size_t size2 = 50; XT_EXPECT_THROW(adapt.resize(size2), std::runtime_error); EXPECT_EQ(adapt.size(), size1); } diff --git a/test/test_xcomplex.cpp b/test/test_xcomplex.cpp index 920d21b8e..8f52d8855 100644 --- a/test/test_xcomplex.cpp +++ b/test/test_xcomplex.cpp @@ -133,9 +133,9 @@ namespace xt }; EXPECT_TRUE(allclose(cmplexpected_angle, cmplres_angle)); - using assign_t_angle = xassign_traits, decltype(cmplres_angle)>; #if XTENSOR_USE_XSIMD + using assign_t_angle = xassign_traits, decltype(cmplres_angle)>; EXPECT_TRUE(assign_t_angle::simd_linear_assign()); #endif @@ -147,9 +147,9 @@ namespace xt }; EXPECT_TRUE(allclose(cmplexpected_conj, cmplres_conj)); - using assign_t_conj = xassign_traits>, decltype(cmplres_conj)>; #if XTENSOR_USE_XSIMD + using assign_t_conj = xassign_traits>, decltype(cmplres_conj)>; auto b1 = cmplres_angle.template load_simd(0); auto b2 = cmplres_conj.template load_simd(0); static_cast(b1); @@ -164,9 +164,9 @@ namespace xt {0.57322529, 0.62248637, 0.14673763} }; - using assign_t_norm = xassign_traits, decltype(cmplres_norm)>; #if XTENSOR_USE_XSIMD + using assign_t_norm = xassign_traits, decltype(cmplres_norm)>; EXPECT_TRUE(assign_t_norm::simd_linear_assign()); #endif @@ -192,9 +192,9 @@ namespace xt ++it; } - using assign_t_arg = xassign_traits, decltype(cmplres)>; #if XTENSOR_USE_XSIMD + using assign_t_arg = xassign_traits, decltype(cmplres)>; EXPECT_TRUE(assign_t_arg::simd_linear_assign()); #endif } diff --git a/test/test_xexpression.cpp b/test/test_xexpression.cpp index c1ad4d14f..371ec75bb 100644 --- a/test/test_xexpression.cpp +++ b/test/test_xexpression.cpp @@ -106,7 +106,13 @@ namespace xt using strides_type = typename expr_type::strides_type; using inner_strides_type = typename expr_type::inner_strides_type; using backstrides_type = typename expr_type::backstrides_type; - using inner_strides_tybackstrides_typepe = typename expr_type::inner_backstrides_type; + using inner_backstrides_type = typename expr_type::inner_backstrides_type; + // Suppress unused typedef warnings + (void) sizeof(expr_type); + (void) sizeof(strides_type); + (void) sizeof(inner_strides_type); + (void) sizeof(backstrides_type); + (void) sizeof(inner_backstrides_type); } TEST(xexpression, shared_expr_return) diff --git a/test/test_xexpression_holder.cpp b/test/test_xexpression_holder.cpp index cff5d6fb6..f773099bc 100644 --- a/test/test_xexpression_holder.cpp +++ b/test/test_xexpression_holder.cpp @@ -24,7 +24,8 @@ namespace xt xexpression_holder holder_a = xexpression_holder(a); xexpression_holder holder_b(b); - xexpression_holder holder_c(std::move(xexpression_holder(c))); + xexpression_holder temp(c); + xexpression_holder holder_c(std::move(temp)); } TEST(xexpression_holder, assign) diff --git a/test/test_xfft.cpp b/test/test_xfft.cpp index 7665cb592..f71180065 100644 --- a/test/test_xfft.cpp +++ b/test/test_xfft.cpp @@ -47,10 +47,10 @@ namespace xt size_t n = 10; size_t A = 1; size_t dim = 10; - auto x = xt::linspace(0, n - 1, n) * xt::ones({dim, n}); - xt::xarray y = A * xt::sin(2 * xt::numeric_constants::PI * x * k / n); + auto x = xt::linspace(0.0f, static_cast(n - 1), n) * xt::ones({dim, n}); + xt::xarray y = A * xt::sin(2 * xt::numeric_constants::PI * x * k / static_cast(n)); y = xt::transpose(y); - auto res = xt::fft::fft(y, 0) / (n / 2.0); + auto res = xt::fft::fft(y, 0) / (static_cast(n) / 2.0); REQUIRE(A == doctest::Approx(std::abs(res(k, 0))).epsilon(.0001)); REQUIRE(A == doctest::Approx(std::abs(res(k, 1))).epsilon(.0001)); } @@ -61,9 +61,9 @@ namespace xt size_t n = 15; size_t A = 1; size_t dim = 2; - auto x = xt::linspace(0, n - 1, n) * xt::ones({dim, n}); - xt::xarray y = A * xt::sin(2 * xt::numeric_constants::PI * x * k / n); - auto res = xt::fft::fft(y) / (n / 2.0); + auto x = xt::linspace(0.0f, static_cast(n - 1), n) * xt::ones({dim, n}); + xt::xarray y = A * xt::sin(2 * xt::numeric_constants::PI * x * k / static_cast(n)); + auto res = xt::fft::fft(y) / (static_cast(n) / 2.0); REQUIRE(A == doctest::Approx(std::abs(res(0, k))).epsilon(.0001)); REQUIRE(A == doctest::Approx(std::abs(res(1, k))).epsilon(.0001)); } diff --git a/test/test_xindex_view.cpp b/test/test_xindex_view.cpp index cdf22c1f6..448eda85d 100644 --- a/test/test_xindex_view.cpp +++ b/test/test_xindex_view.cpp @@ -42,8 +42,8 @@ namespace xt EXPECT_EQ(expected, e(1, 1)); auto t = v + 3; - EXPECT_DOUBLE_EQ((e_copy(1, 1) + 6), t(0)); - EXPECT_EQ((e(1, 1) + 3), t(0)); + EXPECT_DOUBLE_EQ((e_copy(1, 1) + 6), t(0ul)); + EXPECT_EQ((e(1, 1) + 3), t(0ul)); v = broadcast(123, v.shape()); EXPECT_EQ(123, e(1, 1)); diff --git a/test/test_xmath.cpp b/test/test_xmath.cpp index 17bc57895..77c3ba85e 100644 --- a/test/test_xmath.cpp +++ b/test/test_xmath.cpp @@ -43,9 +43,9 @@ namespace xt xarray res2 = xt::abs(b); auto f = abs(b); - using assign_traits = xassign_traits, decltype(f)>; #if XTENSOR_USE_XSIMD + using assign_traits = xassign_traits, decltype(f)>; EXPECT_TRUE(assign_traits::simd_linear_assign()); #endif } diff --git a/test/test_xreducer.cpp b/test/test_xreducer.cpp index aa0a824b5..848e68262 100644 --- a/test/test_xreducer.cpp +++ b/test/test_xreducer.cpp @@ -34,22 +34,22 @@ namespace xt { -#define CHECK_RESULT_TYPE(EXPRESSION, EXPECTED_TYPE) \ - { \ - using result_type = typename std::decay_t::value_type; \ - EXPECT_TRUE((std::is_same::value)); \ +#define CHECK_RESULT_TYPE(EXPRESSION, EXPECTED_TYPE) \ + { \ + using result_type_ = typename std::decay_t::value_type; \ + EXPECT_TRUE((std::is_same::value)); \ } -#define CHECK_TAG_TYPE(EXPRESSION, EXPECTED_TYPE) \ - { \ - using result_type = typename std::decay_t::expression_tag; \ - EXPECT_TRUE((std::is_same::value)); \ +#define CHECK_TAG_TYPE(EXPRESSION, EXPECTED_TYPE) \ + { \ + using result_type_ = typename std::decay_t::expression_tag; \ + EXPECT_TRUE((std::is_same::value)); \ } -#define CHECK_TYPE(VALUE, EXPECTED_TYPE) \ - { \ - using result_type = typename std::decay_t; \ - EXPECT_TRUE((std::is_same::value)); \ +#define CHECK_TYPE(VALUE, EXPECTED_TYPE) \ + { \ + using result_type_ = typename std::decay_t; \ + EXPECT_TRUE((std::is_same::value)); \ } struct xreducer_features @@ -157,7 +157,6 @@ namespace xt #define TEST_VALUE_HAS_VALUE(INPUT, V_TYPE, OPTIONAL) \ using result_type = std::conditional_t, double>; \ - \ auto res = xt::sum(INPUT, feats.m_axes); \ CHECK_RESULT_TYPE(res, result_type); \ CHECK_TYPE(xt::value(res)(1, 1, 1), V_TYPE); \ diff --git a/test/test_xsort.cpp b/test/test_xsort.cpp index bbe928d4d..c8f9dc143 100644 --- a/test/test_xsort.cpp +++ b/test/test_xsort.cpp @@ -173,7 +173,7 @@ namespace xt 47, 41, 40, 34, 33, 43, 64 }; - EXPECT_EQ(ex, xt::argsort(a, {0}, xt::sorting_method::stable)); + EXPECT_EQ(ex, xt::argsort(a, 0, xt::sorting_method::stable)); } TEST(xsort, sort_easy) diff --git a/test/test_xstrided_view.cpp b/test/test_xstrided_view.cpp index 5b8f8c01d..22eca5412 100644 --- a/test/test_xstrided_view.cpp +++ b/test/test_xstrided_view.cpp @@ -687,9 +687,9 @@ namespace xt xtensor par = xt::reshape_view(xt::arange(9), {3, 3}); EXPECT_EQ(a, par); } - using assign_traits = xassign_traits, decltype(av)>; #if XTENSOR_USE_XSIMD + using assign_traits = xassign_traits, decltype(av)>; EXPECT_TRUE(assign_traits::simd_linear_assign()); #endif diff --git a/test/test_xtensor.cpp b/test/test_xtensor.cpp index f7f81c75d..a615ce639 100644 --- a/test/test_xtensor.cpp +++ b/test/test_xtensor.cpp @@ -217,7 +217,7 @@ namespace xt #ifdef XTENSOR_ENABLE_ASSERT xtensor_dynamic b; std::vector s = {2u, 2u}; - xtensor_dynamic::strides_type strides = {2u, 1u}; + [[maybe_unused]] xtensor_dynamic::strides_type strides = {2u, 1u}; EXPECT_THROW(b.resize(s), std::runtime_error); EXPECT_THROW(b.resize(s, layout_type::dynamic), std::runtime_error); EXPECT_THROW(b.resize(s, strides), std::runtime_error); diff --git a/test/test_xutils.cpp b/test/test_xutils.cpp index 62df3144e..a6586aadb 100644 --- a/test/test_xutils.cpp +++ b/test/test_xutils.cpp @@ -108,6 +108,7 @@ namespace xt { ASSERT_TRUE(foo(std::make_tuple(1, 2, 3)) == 2); static_assert(!noexcept(foo(std::make_tuple(1, 2, 3)))); +#ifndef XTENSOR_ENABLE_ASSERT auto func_ne = [](int i) noexcept { return i; @@ -117,6 +118,7 @@ namespace xt static_assert(!noexcept(apply(1, func_ne, t))); #else static_assert(noexcept(apply(1, func_ne, t))); +#endif #endif } diff --git a/test/test_xview.cpp b/test/test_xview.cpp index 5f81047b2..00a82639d 100644 --- a/test/test_xview.cpp +++ b/test/test_xview.cpp @@ -1293,7 +1293,6 @@ namespace xt using xfix = xt::xtensor_fixed, layout_type::row_major>; using ctes = xt::xtensor; - using carr = xt::xarray; using cfix = xt::xtensor_fixed, layout_type::column_major>; EXPECT_TRUE((detail::is_contiguous_view, xall, xall>())); @@ -1430,12 +1429,14 @@ namespace xt auto vxa = view(xt::arange(100), range(0, 10)); using assign_traits = xassign_traits; + (void) sizeof(assign_traits); // suppress unused typedef warning #if XTENSOR_USE_XSIMD EXPECT_TRUE(assign_traits::simd_linear_assign()); #endif using assign_traits2 = xassign_traits; + (void) sizeof(assign_traits2); // suppress unused typedef warning #if XTENSOR_USE_XSIMD EXPECT_FALSE(assign_traits2::simd_linear_assign());