[libcxx-commits] [libcxx] [libc++][test] Make `deallocate_size.pass.cpp` MSVC-friendly (PR #165162)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 28 18:44:01 PDT 2025


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/165162

>From 44e2430afb2320296df3fb9af0367135a10ab80d Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Mon, 27 Oct 2025 01:14:34 +0800
Subject: [PATCH 1/3] [libc++][test] Make `deallocate_size.pass.cpp`
 MSVC-friendly

This patch contains several changes to `deallocate_size.pass.cpp`:
1. `static_cast`-ing parameters to suitable types to avoid narrowing.
2. Changeing the type of `allocated_` to possibly larger and seemingly
more appropriate `ptrdiff_t`.
3. Avoiding `assert`-ing count of allocations when a `basic_string` is
allocated, just `assert`-ing after destruction instead.
---
 .../string.capacity/deallocate_size.pass.cpp    | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 00f9e2b846783..08735f84bb0aa 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -12,12 +12,13 @@
 
 #include <string>
 #include <cassert>
+#include <cstddef>
 #include <cstdint>
 #include <type_traits>
 
 #include "test_macros.h"
 
-static int allocated_;
+static std::ptrdiff_t allocated_;
 
 template <class T, class Sz>
 struct test_alloc {
@@ -40,13 +41,13 @@ struct test_alloc {
   TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}
 
   pointer allocate(size_type n, const void* = nullptr) {
-    allocated_ += n;
-    return std::allocator<value_type>().allocate(n);
+    allocated_ += static_cast<std::ptrdiff_t>(n);
+    return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
   }
 
   void deallocate(pointer p, size_type s) {
-    allocated_ -= s;
-    std::allocator<value_type>().deallocate(p, s);
+    allocated_ -= static_cast<std::ptrdiff_t>(s);
+    std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
   }
 
   template <class U>
@@ -64,14 +65,14 @@ struct test_alloc {
 
 template <class Sz>
 void test() {
+  using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
   for (int i = 1; i < 1000; ++i) {
-    using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
     {
       Str s(i, 't');
-      assert(allocated_ == 0 || allocated_ >= i);
+      (void)s;
     }
+    assert(allocated_ == 0);
   }
-  assert(allocated_ == 0);
 }
 
 int main(int, char**) {

>From bc01ebc3efe3bc2ddfc2d0c98f338722cb5de819 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Wed, 29 Oct 2025 09:42:58 +0800
Subject: [PATCH 2/3] Intro `TEST_MAYBE_UNUSED` macro for tests

---
 libcxx/test/support/test_macros.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index c4e1600572456..021865c831c55 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -508,6 +508,16 @@ inline Tp const& DoNotOptimize(Tp const& value) {
 #define TEST_NO_UNIQUE_ADDRESS
 #endif
 
+#if TEST_STD_VER >= 17 && __has_cpp_attribute(maybe_unused)
+#  define TEST_MAYBE_UNUSED [[maybe_unused]]
+#elif __has_cpp_attribute(__maybe_unused__)
+#  define TEST_MAYBE_UNUSED [[__maybe_unused__]]
+#elif __has_cpp_attribute(gnu::unused)
+#  define TEST_MAYBE_UNUSED [[gnu::unused]]
+#else
+#  define TEST_MAYBE_UNUSED
+#endif
+
 #ifdef _LIBCPP_SHORT_WCHAR
 #  define TEST_SHORT_WCHAR
 #endif

>From f1d66e638ef872657c795f94874eb863496c333e Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Wed, 29 Oct 2025 09:43:47 +0800
Subject: [PATCH 3/3] Use `TEST_MAYBE_UNUSED`, separately count allocations and
 deallocations

---
 .../string.capacity/deallocate_size.pass.cpp        | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 08735f84bb0aa..ecc4172c98cf1 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -18,7 +18,8 @@
 
 #include "test_macros.h"
 
-static std::ptrdiff_t allocated_;
+static std::size_t allocated_;
+static std::size_t deallocated_;
 
 template <class T, class Sz>
 struct test_alloc {
@@ -41,12 +42,12 @@ struct test_alloc {
   TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}
 
   pointer allocate(size_type n, const void* = nullptr) {
-    allocated_ += static_cast<std::ptrdiff_t>(n);
+    allocated_ += static_cast<std::size_t>(n);
     return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
   }
 
   void deallocate(pointer p, size_type s) {
-    allocated_ -= static_cast<std::ptrdiff_t>(s);
+    deallocated_ += static_cast<std::size_t>(s);
     std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
   }
 
@@ -65,13 +66,11 @@ struct test_alloc {
 
 template <class Sz>
 void test() {
-  using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
   for (int i = 1; i < 1000; ++i) {
     {
-      Str s(i, 't');
-      (void)s;
+      TEST_MAYBE_UNUSED std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> > s(i, 't');
     }
-    assert(allocated_ == 0);
+    assert(allocated_ == deallocated_);
   }
 }
 



More information about the libcxx-commits mailing list