[libcxx-commits] [libcxx] [libc++] [test] Fix MSVC warnings (PR #93257)

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 23 17:52:17 PDT 2024


https://github.com/StephanTLavavej updated https://github.com/llvm/llvm-project/pull/93257

>From 05ef2eaef0caa46310e31e24cdf4e435df241213 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 20 May 2024 13:35:21 -0700
Subject: [PATCH 1/7] Avoid MSVC warning C5101: use of preprocessor directive
 in function-like macro argument list is undefined behavior.

Followup to LLVM-73440. See LLVM-73598 and LLVM-73836.
---
 .../syserr.errcat.objects/generic_category.pass.cpp      | 9 ++++++---
 .../syserr.errcat.objects/system_category.pass.cpp       | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
index d4bbde75ae882..f9e49d02b8860 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
@@ -50,12 +50,15 @@ 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)
+        [[maybe_unused]] constexpr bool is_newlib = true;
+#else
+        [[maybe_unused]] constexpr bool is_newlib = false;
+#endif
         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);
     }
diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
index eefbddd27a7f5..a14d6cd6cb4fb 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
@@ -56,12 +56,15 @@ 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)
+    [[maybe_unused]] constexpr bool is_newlib = true;
+#else
+    [[maybe_unused]] constexpr bool is_newlib = false;
+#endif
     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);
   }

>From 63140492222ac7022adf534b63c8cf2e42ce90cd Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 20 May 2024 13:46:00 -0700
Subject: [PATCH 2/7] Avoid MSVC warning C4267: 'return': conversion from
 'size_t' to 'int', possible loss of data.

---
 .../views/views.span/span.cons/initializer_list.pass.cpp      | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/std/containers/views/views.span/span.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/views/views.span/span.cons/initializer_list.pass.cpp
index 74a5094f61261..bc76e23fea3c0 100644
--- a/libcxx/test/std/containers/views/views.span/span.cons/initializer_list.pass.cpp
+++ b/libcxx/test/std/containers/views/views.span/span.cons/initializer_list.pass.cpp
@@ -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

>From db9ebe052b20e859cbe82e3ce5ecd27b86a6a6e1 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 20 May 2024 14:40:41 -0700
Subject: [PATCH 3/7] Avoid MSVC warning C4146: unary minus operator applied to
 unsigned type, result still unsigned.

---
 .../numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp  | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
index 212804356a056..bf40b174b209c 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
@@ -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));
 }
 

>From baa373d4f3a52b85a3bdd676d2cba738bb62155d Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Wed, 22 May 2024 23:09:58 -0700
Subject: [PATCH 4/7] Silence MSVC warning C4310: cast truncates constant
 value.

These warnings are being emitted by `T(255)`.

Followup to LLVM-79791.
---
 .../std/atomics/atomics.ref/compare_exchange_strong.pass.cpp   | 3 +++
 .../std/atomics/atomics.ref/compare_exchange_weak.pass.cpp     | 3 +++
 libcxx/test/std/atomics/atomics.ref/wait.pass.cpp              | 3 +++
 3 files changed, 9 insertions(+)

diff --git a/libcxx/test/std/atomics/atomics.ref/compare_exchange_strong.pass.cpp b/libcxx/test/std/atomics/atomics.ref/compare_exchange_strong.pass.cpp
index 72b2f444c476c..90aa5ea5b6df4 100644
--- a/libcxx/test/std/atomics/atomics.ref/compare_exchange_strong.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/compare_exchange_strong.pass.cpp
@@ -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;
 
diff --git a/libcxx/test/std/atomics/atomics.ref/compare_exchange_weak.pass.cpp b/libcxx/test/std/atomics/atomics.ref/compare_exchange_weak.pass.cpp
index 5219a8e3714f9..99c1385a2fe0b 100644
--- a/libcxx/test/std/atomics/atomics.ref/compare_exchange_weak.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/compare_exchange_weak.pass.cpp
@@ -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;
 
diff --git a/libcxx/test/std/atomics/atomics.ref/wait.pass.cpp b/libcxx/test/std/atomics/atomics.ref/wait.pass.cpp
index e5310febf5c5e..f246803ba2592 100644
--- a/libcxx/test/std/atomics/atomics.ref/wait.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/wait.pass.cpp
@@ -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>

>From 190a6343ab73aacf6402b6ccd47d17135c2d98e4 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sun, 19 May 2024 16:26:19 -0700
Subject: [PATCH 5/7] MSVC no longer emits warning C4521 'multiple copy
 constructors specified'.

Since at least MSVC-PR-368907 on 2021-12-09.
---
 libcxx/test/support/msvc_stdlib_force_include.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/test/support/msvc_stdlib_force_include.h b/libcxx/test/support/msvc_stdlib_force_include.h
index 6c26085e72c45..35783c1607b0e 100644
--- a/libcxx/test/support/msvc_stdlib_force_include.h
+++ b/libcxx/test/support/msvc_stdlib_force_include.h
@@ -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__)

>From b1eaed2bf5f35a3dfedb395e87529445e3021eae Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Thu, 23 May 2024 17:13:37 -0700
Subject: [PATCH 6/7] clang-format.

---
 .../syserr.errcat.objects/generic_category.pass.cpp            | 3 +--
 .../syserr.errcat.objects/system_category.pass.cpp             | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
index f9e49d02b8860..ecc544dd94c12 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
@@ -58,8 +58,7 @@ int main(int, char**)
         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
-                      || (is_newlib && msg.empty())
-        );
+                      || (is_newlib && msg.empty()));
         assert(errno == E2BIG);
     }
 
diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
index a14d6cd6cb4fb..913df951cae54 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
@@ -64,8 +64,7 @@ int main(int, char**) {
     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
-                  || (is_newlib && msg.empty())
-    );
+                  || (is_newlib && msg.empty()));
     assert(errno == E2BIG);
   }
 

>From fae04b68a090230cbe3e33109b685cb452922b48 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Thu, 23 May 2024 17:48:57 -0700
Subject: [PATCH 7/7] You enter what seems to be an older, more primitive
 world.

---
 .../syserr.errcat.objects/generic_category.pass.cpp          | 5 +++--
 .../syserr.errcat.objects/system_category.pass.cpp           | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
index ecc544dd94c12..7283fdc769d86 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp
@@ -51,10 +51,11 @@ int main(int, char**)
         // treat as a failure code otherwise, but we can detect that
         // with the preprocessor.
 #if defined(_NEWLIB_VERSION)
-        [[maybe_unused]] constexpr bool is_newlib = true;
+        const bool is_newlib = true;
 #else
-        [[maybe_unused]] constexpr bool is_newlib = false;
+        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
diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
index 913df951cae54..02a1baf599983 100644
--- a/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
+++ b/libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp
@@ -57,10 +57,11 @@ int main(int, char**) {
     // treat as a failure code otherwise, but we can detect that
     // with the preprocessor.
 #if defined(_NEWLIB_VERSION)
-    [[maybe_unused]] constexpr bool is_newlib = true;
+    const bool is_newlib = true;
 #else
-    [[maybe_unused]] constexpr bool is_newlib = false;
+    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



More information about the libcxx-commits mailing list