[libcxx-commits] [libcxx] [libc++][test] Fix simple warnings (PR #74186)

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 1 23:24:10 PST 2023


https://github.com/StephanTLavavej created https://github.com/llvm/llvm-project/pull/74186

Found while running libc++'s tests with MSVC's STL.

This contains 3 small commits to fix simple warnings; I hope that combining them into a single PR makes things easier.

* Avoid mixing preprocessor and runtime tests.
  + When MSVC sees `if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)` it emits `warning C4127: conditional expression is constant` because the first part is truly a compile-time constant. Transforming this logic avoids the warning. For safety, I'm adding braces to the controlled statement.
* Add void-casts to fix `-Wunused-variable` warnings.
  + Something about MSVC's STL or our test environment makes Clang/LLVM emit warnings here. As the variables are intentionally unused, void-casting is the simplest way to acknowledge this.
* Avoid sign/truncation warnings in `ConvertibleToIntegral.h`.
  + As these warnings are emitted purely within test support code, we can silence them with `static_cast` without worrying about disrupting any usage. Examples:
    ```
    libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(19): warning C4242: 'return': conversion from 'const int' to 'unsigned char', possible loss of data        
    libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(20): warning C4242: 'return': conversion from 'const int' to 'signed char', possible loss of data
    libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(32): warning C4242: 'return': conversion from 'int' to 'char', possible loss of data
    ```


>From b3c28e7797147c9ded10437b2f79a4eadd7ef64f Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Wed, 29 Nov 2023 21:59:33 -0800
Subject: [PATCH 1/3] Avoid mixing preprocessor and runtime tests.

move_backward.pass.cpp(95): warning C4127: conditional expression is constant
---
 .../alg.modifying.operations/alg.move/move.pass.cpp        | 7 ++++++-
 .../alg.move/move_backward.pass.cpp                        | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
index ce5cf0560fc81ae..e6019c477f93e27 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
@@ -94,8 +94,13 @@ struct Test1OutIters {
 
 TEST_CONSTEXPR_CXX20 bool test() {
   types::for_each(types::cpp17_input_iterator_list<int*>(), TestOutIters());
-  if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
+
+#if TEST_STD_VER < 23
+  if (!TEST_IS_CONSTANT_EVALUATED)
+#endif
+  {
     types::for_each(types::cpp17_input_iterator_list<std::unique_ptr<int>*>(), Test1OutIters());
+  }
 
   { // Make sure that padding bits aren't copied
     Derived src(1, 2, 3);
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
index 2ed4d37b9dbe645..dcee48244288bfe 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
@@ -92,8 +92,13 @@ struct Test1OutIters {
 
 TEST_CONSTEXPR_CXX20 bool test() {
   types::for_each(types::bidirectional_iterator_list<int*>(), TestOutIters());
-  if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
+
+#if TEST_STD_VER < 23
+  if (!TEST_IS_CONSTANT_EVALUATED)
+#endif
+  {
     types::for_each(types::bidirectional_iterator_list<std::unique_ptr<int>*>(), Test1OutIters());
+  }
 
   { // Make sure that padding bits aren't copied
     Derived src(1, 2, 3);

>From 390d7bcee42da10f8f90e2a3903bef52712927c6 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Thu, 30 Nov 2023 12:46:42 -0800
Subject: [PATCH 2/3] Add void-casts to fix -Wunused-variable warnings.

---
 libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp          | 1 +
 .../thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp   | 1 +
 .../thread.shared_mutex.class/default.pass.cpp                   | 1 +
 3 files changed, 3 insertions(+)

diff --git a/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp b/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
index a2d46ab1b94c7a5..796ab41716dd5f4 100644
--- a/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
@@ -61,6 +61,7 @@ void check_random_device_invalid(const std::string &token) {
 int main(int, char**) {
   {
     std::random_device r;
+    (void)r;
   }
   // Check the validity of various tokens
   {
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
index 48a96f90254e3e3..f953fa4f8d6df20 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
@@ -66,6 +66,7 @@ int main(int, char**)
     {
         using LG = std::scoped_lock<>;
         LG lg;
+        (void)lg;
     }
     {
         using LG = std::scoped_lock<TestMutex>;
diff --git a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/default.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/default.pass.cpp
index 91320a52b62aea6..6b4da89c4f2d491 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/default.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/default.pass.cpp
@@ -22,6 +22,7 @@
 int main(int, char**)
 {
     std::shared_mutex m;
+    (void)m;
 
   return 0;
 }

>From 77492aab43b20616fb2043fe544fc99af6bd99a1 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Thu, 30 Nov 2023 22:49:44 -0800
Subject: [PATCH 3/3] Avoid sign/truncation warnings in
 ConvertibleToIntegral.h.

Examples:

libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(19): warning C4242: 'return': conversion from 'const int' to 'unsigned char', possible loss of data
libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(20): warning C4242: 'return': conversion from 'const int' to 'signed char', possible loss of data
libcxx\test\std\containers\views\mdspan\layout_stride\../ConvertibleToIntegral.h(32): warning C4242: 'return': conversion from 'int' to 'char', possible loss of data
---
 .../std/containers/views/mdspan/ConvertibleToIntegral.h   | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h b/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
index 470f5d8e724648c..0ca5c330a390e4b 100644
--- a/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
+++ b/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
@@ -16,8 +16,8 @@ struct IntType {
 
   constexpr bool operator==(const IntType& rhs) const { return val == rhs.val; }
   constexpr operator int() const noexcept { return val; }
-  constexpr operator unsigned char() const { return val; }
-  constexpr operator signed char() const noexcept { return val; }
+  constexpr operator unsigned char() const { return static_cast<unsigned char>(val); }
+  constexpr operator signed char() const noexcept { return static_cast<signed char>(val); }
 };
 
 // only non-const convertible
@@ -28,8 +28,8 @@ struct IntTypeNC {
 
   constexpr bool operator==(const IntType& rhs) const { return val == rhs.val; }
   constexpr operator int() noexcept { return val; }
-  constexpr operator unsigned() { return val; }
-  constexpr operator char() noexcept { return val; }
+  constexpr operator unsigned() { return static_cast<unsigned>(val); }
+  constexpr operator char() noexcept { return static_cast<char>(val); }
 };
 
 // weird configurability of convertibility to int



More information about the libcxx-commits mailing list