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

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 4 20:33:22 PST 2023


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

>From a7954b7ce39f5c8f8885efd2feb851b4d273339e 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 1/4] Add void-casts to fix -Wunused-variable warnings.

Also apply clang-format from CI.
---
 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                 | 3 ++-
 3 files changed, 4 insertions(+), 1 deletion(-)

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 a2d46ab1b94c7..796ab41716dd5 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 48a96f90254e3..f953fa4f8d6df 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 91320a52b62ae..5504645bb31f9 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;
+    return 0;
 }

>From 75e0cd8b849545b4adeb2a6ae50561962187641b 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 2/4] 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 470f5d8e72464..0ca5c330a390e 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

>From 3fc15e77330e0978b43d782c2438005a62b9eb1d Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 4 Dec 2023 17:01:41 -0800
Subject: [PATCH 3/4] Fix: Add `TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED`.

---
 .../alg.modifying.operations/alg.move/move.pass.cpp         | 2 +-
 .../alg.move/move_backward.pass.cpp                         | 2 +-
 libcxx/test/support/test_macros.h                           | 6 ++++++
 3 files changed, 8 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 ce5cf0560fc81..b1ad6873bc5e5 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,7 +94,7 @@ 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_AT_LEAST_23_OR_RUNTIME_EVALUATED)
     types::for_each(types::cpp17_input_iterator_list<std::unique_ptr<int>*>(), Test1OutIters());
 
   { // Make sure that padding bits aren't copied
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 2ed4d37b9dbe6..61dea47b51071 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,7 +92,7 @@ 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_AT_LEAST_23_OR_RUNTIME_EVALUATED)
     types::for_each(types::bidirectional_iterator_list<std::unique_ptr<int>*>(), Test1OutIters());
 
   { // Make sure that padding bits aren't copied
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index f3c6d8080ff6d..dbba1b4804fca 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -151,6 +151,12 @@
 # define TEST_IS_CONSTANT_EVALUATED false
 #endif
 
+#if TEST_STD_VER >= 23
+#  define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED true
+#else
+#  define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)
+#endif
+
 #if TEST_STD_VER >= 14
 # define TEST_CONSTEXPR_CXX14 constexpr
 #else

>From 9fa9e1b65922575fe634f07be66b573c1e49444c Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 4 Dec 2023 20:19:58 -0800
Subject: [PATCH 4/4] Cleanup: Add `TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED`
 for consistency.

---
 .../algorithms/alg.nonmodifying/alg.count/count.pass.cpp    | 2 +-
 .../string.view/string.view.comparison/equal.pass.cpp       | 4 ++--
 .../string.view/string.view.comparison/greater.pass.cpp     | 4 ++--
 .../string.view.comparison/greater_equal.pass.cpp           | 4 ++--
 .../string.view/string.view.comparison/less.pass.cpp        | 4 ++--
 .../string.view/string.view.comparison/less_equal.pass.cpp  | 4 ++--
 .../string.view/string.view.comparison/not_equal.pass.cpp   | 4 ++--
 libcxx/test/support/test_macros.h                           | 6 ++++++
 8 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count.pass.cpp
index cc832febdba75..904100c1cf0bb 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count.pass.cpp
@@ -38,7 +38,7 @@ struct Test {
 TEST_CONSTEXPR_CXX20 bool test() {
   types::for_each(types::cpp17_input_iterator_list<const int*>(), Test());
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     std::vector<bool> vec(256 + 64);
     for (ptrdiff_t i = 0; i != 256; ++i) {
       for (size_t offset = 0; offset != 64; ++offset) {
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/equal.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/equal.pass.cpp
index 4f00bd7f6edf1..fc99df4072131 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/equal.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/equal.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) == v[j]) == expected);
       assert((v[i] == ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) == v[j]) == expected);
         assert((v[i] == std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() == abc0def) == false);
   assert((abc0def == abc.data()) == false);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) == abc0def) == false);
     assert((abc0def == std::basic_string<CharT, Traits>(abc)) == false);
   }
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/greater.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/greater.pass.cpp
index b965c155ec51b..fd3a438167994 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/greater.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/greater.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) > v[j]) == expected);
       assert((v[i] > ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) > v[j]) == expected);
         assert((v[i] > std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() > abc0def) == false);
   assert((abc0def > abc.data()) == true);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) > abc0def) == false);
     assert((abc0def > std::basic_string<CharT, Traits>(abc)) == true);
   }
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/greater_equal.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/greater_equal.pass.cpp
index 435e7db773199..0d9081e1c01d8 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/greater_equal.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/greater_equal.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) >= v[j]) == expected);
       assert((v[i] >= ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) >= v[j]) == expected);
         assert((v[i] >= std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() >= abc0def) == false);
   assert((abc0def >= abc.data()) == true);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) >= abc0def) == false);
     assert((abc0def >= std::basic_string<CharT, Traits>(abc)) == true);
   }
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/less.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/less.pass.cpp
index 7461b05ce3085..bb61b1df193e6 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/less.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/less.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) < v[j]) == expected);
       assert((v[i] < ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) < v[j]) == expected);
         assert((v[i] < std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() < abc0def) == true);
   assert((abc0def < abc.data()) == false);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) < abc0def) == true);
     assert((abc0def < std::basic_string<CharT, Traits>(abc)) == false);
   }
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/less_equal.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/less_equal.pass.cpp
index 3192db0dc7d7d..d91e112d6dabf 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/less_equal.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/less_equal.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) <= v[j]) == expected);
       assert((v[i] <= ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) <= v[j]) == expected);
         assert((v[i] <= std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() <= abc0def) == true);
   assert((abc0def <= abc.data()) == false);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) <= abc0def) == true);
     assert((abc0def <= std::basic_string<CharT, Traits>(abc)) == false);
   }
diff --git a/libcxx/test/std/strings/string.view/string.view.comparison/not_equal.pass.cpp b/libcxx/test/std/strings/string.view/string.view.comparison/not_equal.pass.cpp
index 0082a56839331..a4c8be9f878f0 100644
--- a/libcxx/test/std/strings/string.view/string.view.comparison/not_equal.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.comparison/not_equal.pass.cpp
@@ -51,7 +51,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
       assert((ConvertibleTo<SV>(v[i]) != v[j]) == expected);
       assert((v[i] != ConvertibleTo<SV>(v[j])) == expected);
 
-      if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+      if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
         assert((std::basic_string<CharT, Traits>(v[i]) != v[j]) == expected);
         assert((v[i] != std::basic_string<CharT, Traits>(v[j])) == expected);
       }
@@ -72,7 +72,7 @@ TEST_CONSTEXPR_CXX14 bool test() {
   assert((abc.data() != abc0def) == true);
   assert((abc0def != abc.data()) == true);
 
-  if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
+  if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
     assert((std::basic_string<CharT, Traits>(abc) != abc0def) == true);
     assert((abc0def != std::basic_string<CharT, Traits>(abc)) == true);
   }
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index dbba1b4804fca..02678d987d265 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -157,6 +157,12 @@
 #  define TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)
 #endif
 
+#if TEST_STD_VER >= 20
+#  define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED true
+#else
+#  define TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED (!TEST_IS_CONSTANT_EVALUATED)
+#endif
+
 #if TEST_STD_VER >= 14
 # define TEST_CONSTEXPR_CXX14 constexpr
 #else



More information about the libcxx-commits mailing list