From 886e92c1abf17a98abb9fb7f58bc02028a3f9a7e Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Mon, 9 Jan 2023 14:54:44 -0800 Subject: [PATCH] [libc++][test] Silence allocator conversion warnings ... by accepting `std::size_t` instead of `int` in `allocate` and `deallocate` functions. Drive-by: To conform to the allocator requirements, the `Allocator` types in these tests need to have (1) converting constructors and (2) cross-specialization `==` that returns `true` at least for copies of the same allocator. Differential Revision: https://reviews.llvm.org/D141334 --- .../containers/sequences/vector/robust_against_adl.pass.cpp | 5 +++-- .../containers/sequences/vector.bool/ctor_exceptions.pass.cpp | 8 +++++--- .../sequences/vector/vector.cons/exceptions.pass.cpp | 11 ++++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp b/libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp index f8a3e44..ed991fe 100644 --- a/libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp +++ b/libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp @@ -10,6 +10,7 @@ // +#include #include #include "test_macros.h" @@ -22,8 +23,8 @@ struct MyAlloc { using value_type = T; MyAlloc() = default; template MyAlloc(const MyAlloc&) {} - T *allocate(int n) { return std::allocator().allocate(n); } - void deallocate(T *p, int n) { return std::allocator().deallocate(p, n); } + T *allocate(std::size_t n) { return std::allocator().allocate(n); } + void deallocate(T *p, std::size_t n) { return std::allocator().deallocate(p, n); } }; int main(int, char**) diff --git a/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp index 592d733..df3fc14 100644 --- a/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp @@ -11,6 +11,7 @@ // (bug report: https://llvm.org/PR58392) // Check that vector constructors don't leak memory when an operation inside the constructor throws an exception +#include #include #include @@ -30,10 +31,11 @@ struct Allocator { throw 0; } - T* allocate(int n) { return std::allocator().allocate(n); } - void deallocate(T* ptr, int n) { std::allocator().deallocate(ptr, n); } + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + void deallocate(T* ptr, std::size_t n) { std::allocator().deallocate(ptr, n); } - friend bool operator==(const Allocator&, const Allocator&) { return false; } + template + friend bool operator==(const Allocator&, const Allocator&) { return true; } }; template diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp index 26ad7b4..1b7d62e 100644 --- a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp @@ -11,6 +11,7 @@ // (bug report: https://llvm.org/PR58392) // Check that vector constructors don't leak memory when an operation inside the constructor throws an exception +#include #include #include @@ -22,15 +23,19 @@ struct Allocator { using value_type = T; using is_always_equal = std::false_type; + template + Allocator(const Allocator&) {} + Allocator(bool should_throw = true) { if (should_throw) throw 0; } - T* allocate(int n) { return std::allocator().allocate(n); } - void deallocate(T* ptr, int n) { std::allocator().deallocate(ptr, n); } + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + void deallocate(T* ptr, std::size_t n) { std::allocator().deallocate(ptr, n); } - friend bool operator==(const Allocator&, const Allocator&) { return false; } + template + friend bool operator==(const Allocator&, const Allocator&) { return true; } }; struct ThrowingT { -- 2.7.4