Skip to content

Commit 266fac8

Browse files
[libc++] [test] Fix MSVC warnings (#93257)
Found while running libc++'s tests with MSVC's STL. * Avoid MSVC warning C5101: use of preprocessor directive in function-like macro argument list is undefined behavior. + We can easily make this portable by extracting `const bool is_newlib`. + Followup to #73440. + See #73598. + See #73836. * Avoid MSVC warning C4267: 'return': conversion from 'size_t' to 'int', possible loss of data. + This warning is valid, but harmless for the test, so `static_cast<int>` will avoid it. * Avoid MSVC warning C4146: unary minus operator applied to unsigned type, result still unsigned. + This warning is also valid (the scenario is sometimes intentional, but surprising enough that it's worth warning about). This is a C++17 test, so we can easily avoid it by testing `is_signed_v` at compile-time before testing `m < 0` and `n < 0` at run-time. * Silence MSVC warning C4310: cast truncates constant value. + These warnings are being emitted by `T(255)`. Disabling the warning is simpler than attempting to restructure the code. + Followup to #79791. * MSVC no longer emits warning C4521: multiple copy constructors specified. + This warning was removed from the compiler, since at least 2021-12-09.
1 parent 51752ed commit 266fac8

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

libcxx/test/std/atomics/atomics.ref/compare_exchange_strong.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// XFAIL: !has-64-bit-atomics
1010
// XFAIL: !has-1024-bit-atomics
1111

12+
// MSVC warning C4310: cast truncates constant value
13+
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310
14+
1215
// bool compare_exchange_strong(T&, T, memory_order, memory_order) const noexcept;
1316
// bool compare_exchange_strong(T&, T, memory_order = memory_order::seq_cst) const noexcept;
1417

libcxx/test/std/atomics/atomics.ref/compare_exchange_weak.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// XFAIL: !has-64-bit-atomics
1010
// XFAIL: !has-1024-bit-atomics
1111

12+
// MSVC warning C4310: cast truncates constant value
13+
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310
14+
1215
// bool compare_exchange_weak(T&, T, memory_order, memory_order) const noexcept;
1316
// bool compare_exchange_weak(T&, T, memory_order = memory_order::seq_cst) const noexcept;
1417

libcxx/test/std/atomics/atomics.ref/wait.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// XFAIL: !has-64-bit-atomics
1212
// XFAIL: !has-1024-bit-atomics
1313

14+
// MSVC warning C4310: cast truncates constant value
15+
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310
16+
1417
// void wait(T, memory_order = memory_order::seq_cst) const noexcept;
1518

1619
#include <atomic>

libcxx/test/std/containers/views/views.span/span.cons/initializer_list.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ constexpr bool test() {
9393

9494
// Test P2447R4 "Annex C examples"
9595

96-
constexpr int three(std::span<void* const> sp) { return sp.size(); }
96+
constexpr int three(std::span<void* const> sp) { return static_cast<int>(sp.size()); }
9797

98-
constexpr int four(std::span<const std::any> sp) { return sp.size(); }
98+
constexpr int four(std::span<const std::any> sp) { return static_cast<int>(sp.size()); }
9999

100100
bool test_P2447R4_annex_c_examples() {
101101
// 1. Overload resolution is affected

libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ int main(int, char**)
5050
// responds with an empty message, which we probably want to
5151
// treat as a failure code otherwise, but we can detect that
5252
// with the preprocessor.
53+
#if defined(_NEWLIB_VERSION)
54+
const bool is_newlib = true;
55+
#else
56+
const bool is_newlib = false;
57+
#endif
58+
(void)is_newlib;
5359
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
5460
|| msg.rfind("No error information", 0) == 0 // Musl
5561
|| msg.rfind("Unknown error", 0) == 0 // Glibc
56-
#if defined(_NEWLIB_VERSION)
57-
|| msg.empty()
58-
#endif
59-
);
62+
|| (is_newlib && msg.empty()));
6063
assert(errno == E2BIG);
6164
}
6265

libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ int main(int, char**) {
5656
// responds with an empty message, which we probably want to
5757
// treat as a failure code otherwise, but we can detect that
5858
// with the preprocessor.
59+
#if defined(_NEWLIB_VERSION)
60+
const bool is_newlib = true;
61+
#else
62+
const bool is_newlib = false;
63+
#endif
64+
(void)is_newlib;
5965
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
6066
|| msg.rfind("No error information", 0) == 0 // Musl
6167
|| msg.rfind("Unknown error", 0) == 0 // Glibc
62-
#if defined(_NEWLIB_VERSION)
63-
|| msg.empty()
64-
#endif
65-
);
68+
|| (is_newlib && msg.empty()));
6669
assert(errno == E2BIG);
6770
}
6871

libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ T basic_gcd_(T m, T n) {
5757
template <typename T>
5858
T basic_gcd(T m, T n) {
5959
using Tp = std::make_unsigned_t<T>;
60-
if (m < 0 && m != std::numeric_limits<T>::min())
61-
m = -m;
62-
if (n < 0 && n != std::numeric_limits<T>::min())
63-
n = -n;
60+
if constexpr (std::is_signed_v<T>) {
61+
if (m < 0 && m != std::numeric_limits<T>::min())
62+
m = -m;
63+
if (n < 0 && n != std::numeric_limits<T>::min())
64+
n = -n;
65+
}
6466
return basic_gcd_(static_cast<Tp>(m), static_cast<Tp>(n));
6567
}
6668

libcxx/test/support/msvc_stdlib_force_include.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const AssertionDialogAvoider assertion_dialog_avoider{};
6767
// Silence compiler warnings.
6868
# pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored
6969
# pragma warning(disable : 4324) // structure was padded due to alignment specifier
70-
# pragma warning(disable : 4521) // multiple copy constructors specified
7170
# pragma warning(disable : 4702) // unreachable code
7271
# pragma warning(disable : 28251) // Inconsistent annotation for 'new': this instance has no annotations.
7372
#endif // !defined(__clang__)

0 commit comments

Comments
 (0)