From bf7dc572f199007cbe042d5ea41bcf873dcedd8f Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 12 Dec 2019 17:16:00 -0800 Subject: [libcxx] [test] Fix valarray UB and MSVC warnings. [libcxx] [test] Calling min and max on an empty valarray is UB. libcxx/test/std/numerics/numarray/template.valarray/valarray.members/min.pass.cpp libcxx/test/std/numerics/numarray/template.valarray/valarray.members/max.pass.cpp The calls `v1.min();` and `v1.max();` were emitting nodiscard warnings with MSVC's STL. Upon closer inspection, these calls were triggering undefined behavior. N4842 [valarray.members] says: "T min() const; 8 Preconditions: size() > 0 is true. T max() const; 10 Preconditions: size() > 0 is true." As these tests already provide coverage for non-empty valarrays (immediately above), I've simply deleted the code for empty valarrays. [libcxx] [test] Add macros to msvc_stdlib_force_include.h (NFC). libcxx/test/support/msvc_stdlib_force_include.h These macros are being used by: libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/result_of11.pass.cpp Defining them to nothing allows that test to pass. [libcxx] [test] Silence MSVC warning C5063 for is_constant_evaluated (NFC). libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.pass.cpp This test is intentionally writing code that MSVC intentionally warns about, so the warning should be silenced. Additionally, comment an endif for clarity. [libcxx] [test] Silence MSVC warning C4127 (NFC). libcxx/test/support/charconv_test_helpers.h MSVC avoids emitting this warning when it sees a single constexpr value being tested, but this condition is a mix of compile-time and run-time. Using push-disable-pop is the least intrusive way to silence this. [libcxx] [test] Silence MSVC truncation warning (NFC). libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp This test is intentionally truncating float to int, which MSVC intentionally warns about, so push-disable-pop is necessary. [libcxx] [test] Avoid truncation warnings in erase_if tests (NFC). libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp libcxx/test/std/containers/associative/multimap/multimap.erasure/erase_if.pass.cpp libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp These tests use maps with `short` keys and values, emitting MSVC truncation warnings from `int`. Adding `static_cast` to `key_type` and `mapped_type` avoids these warnings. As these tests require C++20 mode (or newer), for brevity I've changed the multimap tests to use emplace to initialize the test data. This has no effect on the erase_if testing. --- .../std/containers/associative/map/map.erasure/erase_if.pass.cpp | 2 +- .../associative/multimap/multimap.erasure/erase_if.pass.cpp | 2 +- .../sequences/vector/vector.cons/construct_iter_iter.pass.cpp | 7 +++++++ libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp | 2 +- libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) (limited to 'libcxx/test/std/containers') diff --git a/libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp b/libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp index f7442f4cfb9..af2d35c66ec 100644 --- a/libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp +++ b/libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp @@ -24,7 +24,7 @@ M make (Init vals) { M ret; for (int v : vals) - ret[v] = v + 10; + ret[static_cast(v)] = static_cast(v + 10); return ret; } diff --git a/libcxx/test/std/containers/associative/multimap/multimap.erasure/erase_if.pass.cpp b/libcxx/test/std/containers/associative/multimap/multimap.erasure/erase_if.pass.cpp index 4655da4b2dc..6f2d56a9730 100644 --- a/libcxx/test/std/containers/associative/multimap/multimap.erasure/erase_if.pass.cpp +++ b/libcxx/test/std/containers/associative/multimap/multimap.erasure/erase_if.pass.cpp @@ -24,7 +24,7 @@ M make (Init vals) { M ret; for (int v : vals) - ret.insert(typename M::value_type(v, v + 10)); + ret.emplace(static_cast(v), static_cast(v + 10)); return ret; } diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp index 28c51eb0348..2b3b212aecd 100644 --- a/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp @@ -156,7 +156,14 @@ void test_ctor_with_different_value_type() { // Make sure initialization is performed with each element value, not with // a memory blob. float array[3] = {0.0f, 1.0f, 2.0f}; +#ifdef TEST_COMPILER_C1XX + #pragma warning(push) + #pragma warning(disable: 4244) // conversion from 'float' to 'int', possible loss of data +#endif // TEST_COMPILER_C1XX std::vector v(array, array + 3); +#ifdef TEST_COMPILER_C1XX + #pragma warning(pop) +#endif // TEST_COMPILER_C1XX assert(v[0] == 0); assert(v[1] == 1); assert(v[2] == 2); diff --git a/libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp b/libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp index 652e6f6b49f..2f188655ca8 100644 --- a/libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.map/erase_if.pass.cpp @@ -24,7 +24,7 @@ M make (Init vals) { M ret; for (int v : vals) - ret[v] = v + 10; + ret[static_cast(v)] = static_cast(v + 10); return ret; } diff --git a/libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp b/libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp index 6da279e915d..23d18872d14 100644 --- a/libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.multimap/erase_if.pass.cpp @@ -24,7 +24,7 @@ M make (Init vals) { M ret; for (int v : vals) - ret.insert(typename M::value_type(v, v + 10)); + ret.emplace(static_cast(v), static_cast(v + 10)); return ret; } -- cgit v1.2.3