Skip to content

Commit

Permalink
[libc++] [test] Fix MSVC warnings (llvm#93257)
Browse files Browse the repository at this point in the history
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 llvm#73440.
  + See llvm#73598.
  + See llvm#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 llvm#79791.
* MSVC no longer emits warning C4521: multiple copy constructors
specified.
+ This warning was removed from the compiler, since at least 2021-12-09.
  • Loading branch information
StephanTLavavej authored and vg0204 committed May 29, 2024
1 parent 6678b40 commit f125415
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// XFAIL: !has-64-bit-atomics
// XFAIL: !has-1024-bit-atomics

// MSVC warning C4310: cast truncates constant value
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310

// bool compare_exchange_strong(T&, T, memory_order, memory_order) const noexcept;
// bool compare_exchange_strong(T&, T, memory_order = memory_order::seq_cst) const noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// XFAIL: !has-64-bit-atomics
// XFAIL: !has-1024-bit-atomics

// MSVC warning C4310: cast truncates constant value
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310

// bool compare_exchange_weak(T&, T, memory_order, memory_order) const noexcept;
// bool compare_exchange_weak(T&, T, memory_order = memory_order::seq_cst) const noexcept;

Expand Down
3 changes: 3 additions & 0 deletions libcxx/test/std/atomics/atomics.ref/wait.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// XFAIL: !has-64-bit-atomics
// XFAIL: !has-1024-bit-atomics

// MSVC warning C4310: cast truncates constant value
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4310

// void wait(T, memory_order = memory_order::seq_cst) const noexcept;

#include <atomic>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ constexpr bool test() {

// Test P2447R4 "Annex C examples"

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

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

bool test_P2447R4_annex_c_examples() {
// 1. Overload resolution is affected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ int main(int, char**)
// responds with an empty message, which we probably want to
// treat as a failure code otherwise, but we can detect that
// with the preprocessor.
#if defined(_NEWLIB_VERSION)
const bool is_newlib = true;
#else
const bool is_newlib = false;
#endif
(void)is_newlib;
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
|| msg.rfind("No error information", 0) == 0 // Musl
|| msg.rfind("Unknown error", 0) == 0 // Glibc
#if defined(_NEWLIB_VERSION)
|| msg.empty()
#endif
);
|| (is_newlib && msg.empty()));
assert(errno == E2BIG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ int main(int, char**) {
// responds with an empty message, which we probably want to
// treat as a failure code otherwise, but we can detect that
// with the preprocessor.
#if defined(_NEWLIB_VERSION)
const bool is_newlib = true;
#else
const bool is_newlib = false;
#endif
(void)is_newlib;
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
|| msg.rfind("No error information", 0) == 0 // Musl
|| msg.rfind("Unknown error", 0) == 0 // Glibc
#if defined(_NEWLIB_VERSION)
|| msg.empty()
#endif
);
|| (is_newlib && msg.empty()));
assert(errno == E2BIG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ T basic_gcd_(T m, T n) {
template <typename T>
T basic_gcd(T m, T n) {
using Tp = std::make_unsigned_t<T>;
if (m < 0 && m != std::numeric_limits<T>::min())
m = -m;
if (n < 0 && n != std::numeric_limits<T>::min())
n = -n;
if constexpr (std::is_signed_v<T>) {
if (m < 0 && m != std::numeric_limits<T>::min())
m = -m;
if (n < 0 && n != std::numeric_limits<T>::min())
n = -n;
}
return basic_gcd_(static_cast<Tp>(m), static_cast<Tp>(n));
}

Expand Down
1 change: 0 additions & 1 deletion libcxx/test/support/msvc_stdlib_force_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const AssertionDialogAvoider assertion_dialog_avoider{};
// Silence compiler warnings.
# pragma warning(disable : 4180) // qualifier applied to function type has no meaning; ignored
# pragma warning(disable : 4324) // structure was padded due to alignment specifier
# pragma warning(disable : 4521) // multiple copy constructors specified
# pragma warning(disable : 4702) // unreachable code
# pragma warning(disable : 28251) // Inconsistent annotation for 'new': this instance has no annotations.
#endif // !defined(__clang__)
Expand Down

0 comments on commit f125415

Please sign in to comment.