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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 1 23:24:36 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Stephan T. Lavavej (StephanTLavavej)

<details>
<summary>Changes</summary>

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
    ```


---
Full diff: https://github.com/llvm/llvm-project/pull/74186.diff


6 Files Affected:

- (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp (+6-1) 
- (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp (+6-1) 
- (modified) libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h (+4-4) 
- (modified) libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp (+1) 
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp (+1) 
- (modified) libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/default.pass.cpp (+1) 


``````````diff
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);
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
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;
 }

``````````

</details>


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


More information about the libcxx-commits mailing list