[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
Wed Oct 29 18:27: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/5] [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/5] 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/5] 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_);
}
}
>From e0deb1e08e0d5b23bd68fb3fb4bd8195f76c7815 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 30 Oct 2025 09:18:17 +0800
Subject: [PATCH 4/5] Revert "Intro `TEST_MAYBE_UNUSED` macro for tests"
This reverts commit bc01ebc3efe3bc2ddfc2d0c98f338722cb5de819.
---
libcxx/test/support/test_macros.h | 10 ----------
1 file changed, 10 deletions(-)
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 021865c831c55..c4e1600572456 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -508,16 +508,6 @@ 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 d3b947ca817c4cadeb71f427bdff1d999f27079a Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 30 Oct 2025 09:26:42 +0800
Subject: [PATCH 5/5] Use `uint64_t` for `(de)allocated_`; use `unsigned int`
loop variable
---
.../string.capacity/deallocate_size.pass.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 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 ecc4172c98cf1..ecdc39701641d 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,8 +18,8 @@
#include "test_macros.h"
-static std::size_t allocated_;
-static std::size_t deallocated_;
+static std::uint64_t allocated_;
+static std::uint64_t deallocated_;
template <class T, class Sz>
struct test_alloc {
@@ -42,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::size_t>(n);
+ allocated_ += n;
return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
}
void deallocate(pointer p, size_type s) {
- deallocated_ += static_cast<std::size_t>(s);
+ deallocated_ += s;
std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
}
@@ -66,9 +66,10 @@ struct test_alloc {
template <class Sz>
void test() {
- for (int i = 1; i < 1000; ++i) {
+ for (unsigned int i = 1; i < 1000; ++i) {
{
- TEST_MAYBE_UNUSED std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> > s(i, 't');
+ std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> > s(i, 't');
+ (void)s;
}
assert(allocated_ == deallocated_);
}
More information about the libcxx-commits
mailing list