From 4132ce25956a91e1224e0f205b7f8c326304a995 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 5 Jan 2018 07:54:33 -0800 Subject: Changes imported from Abseil "staging" branch: - 0a519d9a4507158267cc515e0c7c83959d94fc78 Fix missing header include when compiling with _GLIBCXX_D... by Alex Strelnikov - d089af70781d92af9a5de2d84c417ddf2c87689a Internal change by Gennadiy Rozental - 0d3afc89d3907923ede964d58c6bcca579e8ad65 Test absl::any for exception safety. This test is tempor... by Jon Cohen - 29af424b8a3174a7b3e657e478aa30a8a425aee2 Tweak the ABSL type trait library and expand its tests. by Abseil Team - 99ab42b2ebbe466cc3730fb6b16b5fad848f95af Rollback GLIBCXX_DEBUG fix due to internal breakage. by Alex Strelnikov - 1a5bcb93ee16d4dd2170254e54c4b62b38fbf17b Internal change. by Abseil Team - 46de7d09c7d4aef5b7b5389ce9b4f96b654aac02 absl::string_view::rfind: doc fix. by Abseil Team - edda4c7ddd2d76fbb5b3fd5226b95082083c57d9 Fix string_view_test with c++17/clang/libc++ to address by Xiaoyi Zhang GitOrigin-RevId: 0a519d9a4507158267cc515e0c7c83959d94fc78 Change-Id: Ie27de1be3e79bba011f05e924d34e8fcc62d8de5 --- absl/base/BUILD.bazel | 1 + absl/base/CMakeLists.txt | 19 +- absl/base/exception_safety_testing_test.cc | 25 +- absl/base/internal/exception_safety_testing.cc | 7 +- absl/base/internal/exception_safety_testing.h | 61 +++-- absl/meta/type_traits.h | 66 +++-- absl/meta/type_traits_test.cc | 337 ++++++++++++++++++------- absl/strings/internal/stl_type_traits.h | 121 +++++---- absl/strings/string_view.h | 2 +- absl/strings/string_view_test.cc | 25 +- absl/strings/strip_test.cc | 20 ++ absl/types/BUILD.bazel | 13 +- absl/types/CMakeLists.txt | 17 ++ absl/types/any_exception_safety_test.cc | 201 +++++++++++++++ 14 files changed, 724 insertions(+), 191 deletions(-) create mode 100644 absl/types/any_exception_safety_test.cc diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index f6d6c3d24caf..609d78ad6287 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -235,6 +235,7 @@ cc_library( hdrs = ["internal/exception_safety_testing.h"], copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG, deps = [ + ":base", ":config", ":pretty_function", "//absl/memory", diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index 1b24fd1bb60b..cb0daa69ffdc 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt @@ -33,6 +33,7 @@ list(APPEND BASE_INTERNAL_HEADERS "internal/cycleclock.h" "internal/endian.h" "internal/exception_testing.h" + "internal/exception_safety_testing.h" "internal/identity.h" "internal/invoke.h" "internal/log_severity.h" @@ -43,6 +44,7 @@ list(APPEND BASE_INTERNAL_HEADERS "internal/malloc_hook.h" "internal/malloc_hook_invoke.h" "internal/per_thread_tls.h" + "internal/pretty_function.h" "internal/raw_logging.h" "internal/scheduling_mode.h" "internal/spinlock.h" @@ -57,8 +59,9 @@ list(APPEND BASE_INTERNAL_HEADERS # absl_base main library -list(APPEND BASE_SRC +list(APPEND BASE_SRC "internal/cycleclock.cc" + "internal/exception_safety_testing.cc" "internal/raw_logging.cc" "internal/spinlock.cc" "internal/sysinfo.cc" @@ -318,6 +321,20 @@ absl_test( ${THREAD_IDENTITY_TEST_PUBLIC_LIBRARIES} ) +#test exceptions_safety_testing_test +set(EXCEPTION_SAFETY_TESTING_TEST_SRC "exception_safety_testing_test.cc") +set(EXCEPTION_SAFETY_TESTING_TEST_PUBLIC_LIBRARIES absl::base absl::memory absl::meta absl::strings absl::optional) + +absl_test( + TARGET + absl_exception_safety_testing_test + SOURCES + ${EXCEPTION_SAFETY_TESTING_TEST_SRC} + PUBLIC_LIBRARIES + ${EXCEPTION_SAFETY_TESTING_TEST_PUBLIC_LIBRARIES} + PRIVATE_COMPILE_FLAGS + ${ABSL_EXCEPTIONS_FLAG} +) # test absl_malloc_extension_system_malloc_test set(MALLOC_EXTENSION_SYSTEM_MALLOC_TEST_SRC "internal/malloc_extension_test.cc") diff --git a/absl/base/exception_safety_testing_test.cc b/absl/base/exception_safety_testing_test.cc index 1fc03861784e..5477b40a5bed 100644 --- a/absl/base/exception_safety_testing_test.cc +++ b/absl/base/exception_safety_testing_test.cc @@ -63,7 +63,7 @@ TEST_F(ThrowingValueTest, Throws) { // the countdown doesn't hit 0, and doesn't modify the state of the // ThrowingValue if it throws template -void TestOp(F&& f) { +void TestOp(const F& f) { UnsetCountdown(); ExpectNoThrow(f); @@ -153,11 +153,21 @@ TEST_F(ThrowingValueTest, ThrowingStreamOps) { TestOp([&]() { std::cout << bomb; }); } +template +void TestAllocatingOp(const F& f) { + UnsetCountdown(); + ExpectNoThrow(f); + + SetCountdown(); + EXPECT_THROW(f(), exceptions_internal::TestBadAllocException); + UnsetCountdown(); +} + TEST_F(ThrowingValueTest, ThrowingAllocatingOps) { // make_unique calls unqualified operator new, so these exercise the // ThrowingValue overloads. - TestOp([]() { return absl::make_unique>(1); }); - TestOp([]() { return absl::make_unique[]>(2); }); + TestAllocatingOp([]() { return absl::make_unique>(1); }); + TestAllocatingOp([]() { return absl::make_unique[]>(2); }); } TEST_F(ThrowingValueTest, NonThrowingMoveCtor) { @@ -399,7 +409,8 @@ struct CallOperator { }; struct NonNegative { - friend testing::AssertionResult AbslCheckInvariants(NonNegative* g) { + friend testing::AssertionResult AbslCheckInvariants( + NonNegative* g, absl::InternalAbslNamespaceFinder) { if (g->i >= 0) return testing::AssertionSuccess(); return testing::AssertionFailure() << "i should be non-negative but is " << g->i; @@ -503,7 +514,8 @@ struct HasReset : public NonNegative { void reset() { i = 0; } - friend bool AbslCheckInvariants(HasReset* h) { + friend bool AbslCheckInvariants(HasReset* h, + absl::InternalAbslNamespaceFinder) { h->reset(); return h->i == 0; } @@ -591,7 +603,8 @@ struct ExhaustivenessTester { return true; } - friend testing::AssertionResult AbslCheckInvariants(ExhaustivenessTester*) { + friend testing::AssertionResult AbslCheckInvariants( + ExhaustivenessTester*, absl::InternalAbslNamespaceFinder) { return testing::AssertionSuccess(); } diff --git a/absl/base/internal/exception_safety_testing.cc b/absl/base/internal/exception_safety_testing.cc index ab8d6c9fb4d0..821438ecc28c 100644 --- a/absl/base/internal/exception_safety_testing.cc +++ b/absl/base/internal/exception_safety_testing.cc @@ -23,8 +23,11 @@ namespace exceptions_internal { int countdown = -1; -void MaybeThrow(absl::string_view msg) { - if (countdown-- == 0) throw TestException(msg); +void MaybeThrow(absl::string_view msg, bool throw_bad_alloc) { + if (countdown-- == 0) { + if (throw_bad_alloc) throw TestBadAllocException(msg); + throw TestException(msg); + } } testing::AssertionResult FailureMessage(const TestException& e, diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h index a0127a8819fa..8eac2264884f 100644 --- a/absl/base/internal/exception_safety_testing.h +++ b/absl/base/internal/exception_safety_testing.h @@ -35,6 +35,8 @@ #include "absl/types/optional.h" namespace absl { +struct InternalAbslNamespaceFinder {}; + struct AllocInspector; // A configuration enum for Throwing*. Operations whose flags are set will @@ -71,31 +73,45 @@ constexpr bool ThrowingAllowed(NoThrow flags, NoThrow flag) { class TestException { public: explicit TestException(absl::string_view msg) : msg_(msg) {} - absl::string_view what() const { return msg_; } + virtual ~TestException() {} + virtual const char* what() const noexcept { return msg_.c_str(); } private: std::string msg_; }; +// TestBadAllocException exists because allocation functions must throw an +// exception which can be caught by a handler of std::bad_alloc. We use a child +// class of std::bad_alloc so we can customise the error message, and also +// derive from TestException so we don't accidentally end up catching an actual +// bad_alloc exception in TestExceptionSafety. +class TestBadAllocException : public std::bad_alloc, public TestException { + public: + explicit TestBadAllocException(absl::string_view msg) + : TestException(msg) {} + using TestException::what; +}; + extern int countdown; -void MaybeThrow(absl::string_view msg); +void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false); testing::AssertionResult FailureMessage(const TestException& e, int countdown) noexcept; class TrackedObject { + public: + TrackedObject(const TrackedObject&) = delete; + TrackedObject(TrackedObject&&) = delete; + protected: - explicit TrackedObject(absl::string_view child_ctor) { + explicit TrackedObject(const char* child_ctor) { if (!GetAllocs().emplace(this, child_ctor).second) { ADD_FAILURE() << "Object at address " << static_cast(this) << " re-constructed in ctor " << child_ctor; } } - TrackedObject(const TrackedObject&) = delete; - TrackedObject(TrackedObject&&) = delete; - static std::unordered_map& GetAllocs() { static auto* m = new std::unordered_map(); @@ -120,10 +136,10 @@ using FactoryType = typename absl::result_of_t::element_type; template absl::optional TestCheckerAtCountdown( Factory factory, const Op& op, int count, const Checker& check) { - exceptions_internal::countdown = count; auto t_ptr = factory(); absl::optional out; try { + exceptions_internal::countdown = count; op(t_ptr.get()); } catch (const exceptions_internal::TestException& e) { out.emplace(check(t_ptr.get())); @@ -141,6 +157,10 @@ int UpdateOut(Factory factory, const Op& op, int count, const Checker& checker, return 0; } +// Declare AbslCheckInvariants so that it can be found eventually via ADL. +// Taking `...` gives it the lowest possible precedence. +void AbslCheckInvariants(...); + // Returns an optional with the result of the check if op fails, or an empty // optional if op passes template @@ -148,8 +168,9 @@ absl::optional TestAtCountdown( Factory factory, const Op& op, int count, const Checkers&... checkers) { // Don't bother with the checkers if the class invariants are already broken. auto out = TestCheckerAtCountdown( - factory, op, count, - [](FactoryType* t_ptr) { return AbslCheckInvariants(t_ptr); }); + factory, op, count, [](FactoryType* t_ptr) { + return AbslCheckInvariants(t_ptr, InternalAbslNamespaceFinder()); + }); if (!out.has_value()) return out; // Run each checker, short circuiting after the first failure @@ -483,7 +504,7 @@ class ThrowingValue : private exceptions_internal::TrackedObject { static void* operator new(size_t s, Args&&... args) noexcept( !exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) { if (exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) { - exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION); + exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true); } return ::operator new(s, std::forward(args)...); } @@ -492,7 +513,7 @@ class ThrowingValue : private exceptions_internal::TrackedObject { static void* operator new[](size_t s, Args&&... args) noexcept( !exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) { if (exceptions_internal::ThrowingAllowed(Flags, NoThrow::kAllocation)) { - exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION); + exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true); } return ::operator new[](s, std::forward(args)...); } @@ -630,10 +651,7 @@ class ThrowingAllocator : private exceptions_internal::TrackedObject { p->~U(); } - size_type max_size() const - noexcept(!exceptions_internal::ThrowingAllowed(Flags, - NoThrow::kNoThrow)) { - ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION); + size_type max_size() const noexcept { return std::numeric_limits::max() / sizeof(value_type); } @@ -720,9 +738,12 @@ T TestThrowingCtor(Args&&... args) { // Tests that performing operation Op on a T follows exception safety // guarantees. By default only tests the basic guarantee. There must be a -// function, AbslCheckInvariants(T*) which returns -// anything convertible to bool and which makes sure the invariants of the type -// are upheld. This is called before any of the checkers. +// function, AbslCheckInvariants(T*, absl::InternalAbslNamespaceFinder) which +// returns anything convertible to bool and which makes sure the invariants of +// the type are upheld. This is called before any of the checkers. The +// InternalAbslNamespaceFinder is unused, and just helps find +// AbslCheckInvariants for absl types which become aliases to std::types in +// C++17. // // Parameters: // * TFactory: operator() returns a unique_ptr to the type under test (T). It @@ -740,11 +761,13 @@ template {}; template struct is_trivially_destructible : std::integral_constant::value> { + std::is_destructible::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE - static_assert(std::is_trivially_destructible::value == - is_trivially_destructible::value, - "Not compliant with std::is_trivially_destructible"); + static constexpr bool compliant = std::is_trivially_destructible::value == + is_trivially_destructible::value; + static_assert(compliant || std::is_trivially_destructible::value, + "Not compliant with std::is_trivially_destructible; " + "Standard: false, Implementation: true"); + static_assert(compliant || !std::is_trivially_destructible::value, + "Not compliant with std::is_trivially_destructible; " + "Standard: true, Implementation: false"); #endif // ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE }; @@ -186,18 +191,23 @@ struct is_trivially_destructible // GCC bug 51452: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 // LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116. // -// "T obj();" need to be well-formed and not call any non-trivial operation. +// "T obj();" need to be well-formed and not call any nontrivial operation. // Nontrivally destructible types will cause the expression to be nontrivial. template struct is_trivially_default_constructible - : std::integral_constant::value && - is_trivially_destructible::value> { + : std::integral_constant::value && + is_trivially_destructible::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE - static_assert(std::is_trivially_default_constructible::value == - is_trivially_default_constructible::value, - "Not compliant with std::is_trivially_default_constructible"); + static constexpr bool compliant = + std::is_trivially_default_constructible::value == + is_trivially_default_constructible::value; + static_assert(compliant || std::is_trivially_default_constructible::value, + "Not compliant with std::is_trivially_default_constructible; " + "Standard: false, Implementation: true"); + static_assert(compliant || !std::is_trivially_default_constructible::value, + "Not compliant with std::is_trivially_default_constructible; " + "Standard: true, Implementation: false"); #endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE }; @@ -217,12 +227,18 @@ struct is_trivially_default_constructible template struct is_trivially_copy_constructible : std::integral_constant::value && - is_trivially_destructible::value> { + std::is_copy_constructible::value && + is_trivially_destructible::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE - static_assert(std::is_trivially_copy_constructible::value == - is_trivially_copy_constructible::value, - "Not compliant with std::is_trivially_copy_constructible"); + static constexpr bool compliant = + std::is_trivially_copy_constructible::value == + is_trivially_copy_constructible::value; + static_assert(compliant || std::is_trivially_copy_constructible::value, + "Not compliant with std::is_trivially_copy_constructible; " + "Standard: false, Implementation: true"); + static_assert(compliant || !std::is_trivially_copy_constructible::value, + "Not compliant with std::is_trivially_copy_constructible; " + "Standard: true, Implementation: false"); #endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE }; @@ -240,15 +256,21 @@ struct is_trivially_copy_constructible // `declval() = declval()` is well-formed when treated as an unevaluated // operand. `is_trivially_assignable` requires the assignment to call no // operation that is not trivial. `is_trivially_copy_assignable` is simply -// `is_trivially_assignable`. +// `is_trivially_assignable`. template struct is_trivially_copy_assignable : std::integral_constant::value> { + std::is_copy_assignable::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE - static_assert(std::is_trivially_copy_assignable::value == - is_trivially_copy_assignable::value, - "Not compliant with std::is_trivially_copy_assignable"); + static constexpr bool compliant = + std::is_trivially_copy_assignable::value == + is_trivially_copy_assignable::value; + static_assert(compliant || std::is_trivially_copy_assignable::value, + "Not compliant with std::is_trivially_copy_assignable; " + "Standard: false, Implementation: true"); + static_assert(compliant || !std::is_trivially_copy_assignable::value, + "Not compliant with std::is_trivially_copy_assignable; " + "Standard: true, Implementation: false"); #endif // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE }; diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc index 67d1c4555c4a..c44d1c5ff850 100644 --- a/absl/meta/type_traits_test.cc +++ b/absl/meta/type_traits_test.cc @@ -99,6 +99,18 @@ class Trivial { int n_; }; +struct TrivialDestructor { + ~TrivialDestructor() = default; +}; + +struct NontrivialDestructor { + ~NontrivialDestructor() {} +}; + +struct DeletedDestructor { + ~DeletedDestructor() = delete; +}; + class TrivialDefaultCtor { public: TrivialDefaultCtor() = default; @@ -108,6 +120,23 @@ class TrivialDefaultCtor { int n_; }; +class NontrivialDefaultCtor { + public: + NontrivialDefaultCtor() : n_(1) {} + + private: + int n_; +}; + +class DeletedDefaultCtor { + public: + DeletedDefaultCtor() = delete; + explicit DeletedDefaultCtor(int n) : n_(n) {} + + private: + int n_; +}; + class TrivialCopyCtor { public: explicit TrivialCopyCtor(int n) : n_(n) {} @@ -121,22 +150,57 @@ class TrivialCopyCtor { int n_; }; +class NontrivialCopyCtor { + public: + explicit NontrivialCopyCtor(int n) : n_(n) {} + NontrivialCopyCtor(const NontrivialCopyCtor& t) : n_(t.n_) {} + NontrivialCopyCtor& operator=(const NontrivialCopyCtor&) = default; + + private: + int n_; +}; + +class DeletedCopyCtor { + public: + explicit DeletedCopyCtor(int n) : n_(n) {} + DeletedCopyCtor(const DeletedCopyCtor&) = delete; + DeletedCopyCtor& operator=(const DeletedCopyCtor&) = default; + + private: + int n_; +}; + class TrivialCopyAssign { public: explicit TrivialCopyAssign(int n) : n_(n) {} TrivialCopyAssign(const TrivialCopyAssign& t) : n_(t.n_) {} TrivialCopyAssign& operator=(const TrivialCopyAssign& t) = default; - ~TrivialCopyAssign() {} // can have non trivial destructor + ~TrivialCopyAssign() {} // can have nontrivial destructor private: int n_; }; -struct NonTrivialDestructor { - ~NonTrivialDestructor() {} +class NontrivialCopyAssign { + public: + explicit NontrivialCopyAssign(int n) : n_(n) {} + NontrivialCopyAssign(const NontrivialCopyAssign&) = default; + NontrivialCopyAssign& operator=(const NontrivialCopyAssign& t) { + n_ = t.n_; + return *this; + } + + private: + int n_; }; -struct TrivialDestructor { - ~TrivialDestructor() = default; +class DeletedCopyAssign { + public: + explicit DeletedCopyAssign(int n) : n_(n) {} + DeletedCopyAssign(const DeletedCopyAssign&) = default; + DeletedCopyAssign& operator=(const DeletedCopyAssign&) = delete; + + private: + int n_; }; struct NonCopyable { @@ -152,19 +216,105 @@ class Base { // In GCC/Clang, std::is_trivially_constructible requires that the destructor is // trivial. However, MSVC doesn't require that. This results in different -// behavior when checking is_trivially_constructible on any type with nontrivial -// destructor. Since absl::is_trivially_default_constructible and +// behavior when checking is_trivially_constructible on any type with +// nontrivial destructor. Since absl::is_trivially_default_constructible and // absl::is_trivially_copy_constructible both follows Clang/GCC's interpretation // and check is_trivially_destructible, it results in inconsistency with // std::is_trivially_xxx_constructible on MSVC. This macro is used to work // around this issue in test. In practice, a trivially constructible type // should also be trivially destructible. // GCC bug 51452: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 -// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116. -#ifdef _MSC_VER -#define ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE +// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116 +#ifndef _MSC_VER +#define ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE 1 #endif +// Old versions of libc++, around Clang 3.5 to 3.6, consider deleted destructors +// as also being trivial. With the resolution of CWG 1928 and CWG 1734, this +// is no longer considered true and has thus been amended. +// Compiler Explorer: https://godbolt.org/g/zT59ZL +// CWG issue 1734: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1734 +// CWG issue 1928: http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1928 +#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 3700 +#define ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL 1 +#endif + +// As of the moment, GCC versions >5.1 have a problem compiling for +// std::is_trivially_default_constructible, where +// NontrivialDestructor is a struct with a custom nontrivial destructor. Note +// that this problem only occurs for arrays of a known size, so something like +// std::is_trivially_default_constructible does not +// have any problems. +// Compiler Explorer: https://godbolt.org/g/dXRbdK +// GCC bug 83689: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83689 +#if defined(__clang__) || defined(_MSC_VER) || \ + (defined(__GNUC__) && __GNUC__ < 5) +#define ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL 1 +#endif + +TEST(TypeTraitsTest, TestTrivialDestructor) { + // Verify that arithmetic types and pointers have trivial destructors. + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + + // classes with destructors + EXPECT_TRUE(absl::is_trivially_destructible::value); + EXPECT_TRUE(absl::is_trivially_destructible::value); + + // Verify that types with a nontrivial or deleted destructor + // are marked as such. + EXPECT_FALSE(absl::is_trivially_destructible::value); +#ifdef ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL + EXPECT_FALSE(absl::is_trivially_destructible::value); +#endif + + // simple_pair of such types is trivial + EXPECT_TRUE((absl::is_trivially_destructible>::value)); + EXPECT_TRUE((absl::is_trivially_destructible< + simple_pair>::value)); + + // Verify that types without trivial destructors are correctly marked as such. + EXPECT_FALSE(absl::is_trivially_destructible::value); + EXPECT_FALSE(absl::is_trivially_destructible>::value); + + // Verify that simple_pairs of types without trivial destructors + // are not marked as trivial. + EXPECT_FALSE((absl::is_trivially_destructible< + simple_pair>::value)); + EXPECT_FALSE((absl::is_trivially_destructible< + simple_pair>::value)); + + // array of such types is trivial + using int10 = int[10]; + EXPECT_TRUE(absl::is_trivially_destructible::value); + using Trivial10 = Trivial[10]; + EXPECT_TRUE(absl::is_trivially_destructible::value); + using TrivialDestructor10 = TrivialDestructor[10]; + EXPECT_TRUE(absl::is_trivially_destructible::value); + + // Conversely, the opposite also holds. + using NontrivialDestructor10 = NontrivialDestructor[10]; + EXPECT_FALSE(absl::is_trivially_destructible::value); +} + TEST(TypeTraitsTest, TestTrivialDefaultCtor) { // arithmetic types and pointers have trivial default constructors. EXPECT_TRUE(absl::is_trivially_default_constructible::value); @@ -184,42 +334,67 @@ TEST(TypeTraitsTest, TestTrivialDefaultCtor) { EXPECT_TRUE(absl::is_trivially_default_constructible::value); EXPECT_TRUE(absl::is_trivially_default_constructible::value); EXPECT_TRUE( - absl::is_trivially_default_constructible::value); - EXPECT_TRUE( - absl::is_trivially_default_constructible::value); + absl::is_trivially_default_constructible::value); + EXPECT_TRUE(absl::is_trivially_default_constructible::value); + EXPECT_TRUE(absl::is_trivially_default_constructible::value); + EXPECT_TRUE(absl::is_trivially_default_constructible::value); // types with compiler generated default ctors EXPECT_TRUE(absl::is_trivially_default_constructible::value); EXPECT_TRUE( absl::is_trivially_default_constructible::value); -#ifndef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE - // types with non trivial destructor are non trivial + // Verify that types without them are not. EXPECT_FALSE( - absl::is_trivially_default_constructible::value); + absl::is_trivially_default_constructible::value); + EXPECT_FALSE( + absl::is_trivially_default_constructible::value); + +#ifdef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE + // types with nontrivial destructor are nontrivial + EXPECT_FALSE( + absl::is_trivially_default_constructible::value); #endif // types with vtables EXPECT_FALSE(absl::is_trivially_default_constructible::value); - // Verify that arrays of such types are trivially default constructible - typedef int int10[10]; - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - typedef Trivial Trivial10[10]; - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - typedef Trivial TrivialDefaultCtor10[10]; - EXPECT_TRUE( - absl::is_trivially_default_constructible::value); - // Verify that simple_pair has trivial constructors where applicable. EXPECT_TRUE((absl::is_trivially_default_constructible< simple_pair>::value)); + EXPECT_TRUE((absl::is_trivially_default_constructible< + simple_pair>::value)); + EXPECT_TRUE((absl::is_trivially_default_constructible< + simple_pair>::value)); // Verify that types without trivial constructors are // correctly marked as such. EXPECT_FALSE(absl::is_trivially_default_constructible::value); EXPECT_FALSE( absl::is_trivially_default_constructible>::value); + + // Verify that simple_pairs of types without trivial constructors + // are not marked as trivial. + EXPECT_FALSE((absl::is_trivially_default_constructible< + simple_pair>::value)); + EXPECT_FALSE((absl::is_trivially_default_constructible< + simple_pair>::value)); + + // Verify that arrays of such types are trivially default constructible + using int10 = int[10]; + EXPECT_TRUE(absl::is_trivially_default_constructible::value); + using Trivial10 = Trivial[10]; + EXPECT_TRUE(absl::is_trivially_default_constructible::value); + using TrivialDefaultCtor10 = TrivialDefaultCtor[10]; + EXPECT_TRUE( + absl::is_trivially_default_constructible::value); + + // Conversely, the opposite also holds. +#ifdef ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL + using NontrivialDefaultCtor10 = NontrivialDefaultCtor[10]; + EXPECT_FALSE( + absl::is_trivially_default_constructible::value); +#endif } TEST(TypeTraitsTest, TestTrivialCopyCtor) { @@ -241,18 +416,26 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) { EXPECT_TRUE(absl::is_trivially_copy_constructible::value); EXPECT_TRUE(absl::is_trivially_copy_constructible::value); EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE( - absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); + EXPECT_TRUE(absl::is_trivially_copy_constructible::value); + EXPECT_TRUE(absl::is_trivially_copy_constructible::value); + EXPECT_TRUE(absl::is_trivially_copy_constructible::value); + EXPECT_TRUE(absl::is_trivially_copy_constructible::value); // types with compiler generated copy ctors EXPECT_TRUE(absl::is_trivially_copy_constructible::value); EXPECT_TRUE(absl::is_trivially_copy_constructible::value); -#ifndef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE - // type with non-trivial destructor are non-trivial copy construbtible + // Verify that types without them (i.e. nontrivial or deleted) are not. + EXPECT_FALSE( + absl::is_trivially_copy_constructible::value); + EXPECT_FALSE(absl::is_trivially_copy_constructible::value); + EXPECT_FALSE( + absl::is_trivially_copy_constructible::value); + +#ifdef ABSL_TRIVIALLY_CONSTRUCTIBLE_VERIFY_TRIVIALLY_DESTRUCTIBLE + // type with nontrivial destructor are nontrivial copy construbtible EXPECT_FALSE( - absl::is_trivially_copy_constructible::value); + absl::is_trivially_copy_constructible::value); #endif // types with vtables @@ -266,9 +449,10 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) { EXPECT_TRUE((absl::is_trivially_copy_constructible< simple_pair>::value)); - // Verify that arrays are not - typedef int int10[10]; - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); + // Verify that types without trivial copy constructors are + // correctly marked as such. + EXPECT_FALSE(absl::is_trivially_copy_constructible::value); + EXPECT_FALSE(absl::is_trivially_copy_constructible>::value); // Verify that simple_pairs of types without trivial copy constructors // are not marked as trivial. @@ -277,18 +461,14 @@ TEST(TypeTraitsTest, TestTrivialCopyCtor) { EXPECT_FALSE((absl::is_trivially_copy_constructible< simple_pair>::value)); - // Verify that types without trivial copy constructors are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); - EXPECT_FALSE(absl::is_trivially_copy_constructible>::value); - - // types with deleted copy constructors are not copy constructible - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); + // Verify that arrays are not + using int10 = int[10]; + EXPECT_FALSE(absl::is_trivially_copy_constructible::value); } TEST(TypeTraitsTest, TestTrivialCopyAssign) { // Verify that arithmetic types and pointers have trivial copy - // constructors. + // assignment operators. EXPECT_TRUE(absl::is_trivially_copy_assignable::value); EXPECT_TRUE(absl::is_trivially_copy_assignable::value); EXPECT_TRUE(absl::is_trivially_copy_assignable::value); @@ -305,9 +485,10 @@ TEST(TypeTraitsTest, TestTrivialCopyAssign) { EXPECT_TRUE(absl::is_trivially_copy_assignable::value); EXPECT_TRUE(absl::is_trivially_copy_assignable::value); EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE( - absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable::value); // const qualified types are not assignable EXPECT_FALSE(absl::is_trivially_copy_assignable::value); @@ -316,65 +497,37 @@ TEST(TypeTraitsTest, TestTrivialCopyAssign) { EXPECT_TRUE(absl::is_trivially_copy_assignable::value); EXPECT_TRUE(absl::is_trivially_copy_assignable::value); + // Verify that types without them (i.e. nontrivial or deleted) are not. + EXPECT_FALSE(absl::is_trivially_copy_assignable::value); + EXPECT_FALSE(absl::is_trivially_copy_assignable::value); + EXPECT_FALSE(absl::is_trivially_copy_assignable::value); + // types with vtables EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - // Verify that arrays are not trivially copy assignable - typedef int int10[10]; - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - // Verify that simple_pair is trivially assignable EXPECT_TRUE( (absl::is_trivially_copy_assignable>::value)); + EXPECT_TRUE( + (absl::is_trivially_copy_assignable>::value)); + EXPECT_TRUE((absl::is_trivially_copy_assignable< + simple_pair>::value)); - // Verify that types without trivial copy constructors are + // Verify that types not trivially copy assignable are // correctly marked as such. EXPECT_FALSE(absl::is_trivially_copy_assignable::value); EXPECT_FALSE(absl::is_trivially_copy_assignable>::value); - // types with deleted copy assignment are not copy assignable - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); -} - -TEST(TypeTraitsTest, TestTrivialDestructor) { - // Verify that arithmetic types and pointers have trivial copy - // constructors. - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - - // classes with destructors - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_FALSE(absl::is_trivially_destructible::value); - - // simple_pair of such types is trivial - EXPECT_TRUE((absl::is_trivially_destructible>::value)); - EXPECT_TRUE((absl::is_trivially_destructible< - simple_pair>::value)); + // Verify that simple_pairs of types not trivially copy assignable + // are not marked as trivial. + EXPECT_FALSE((absl::is_trivially_copy_assignable< + simple_pair>::value)); + EXPECT_FALSE((absl::is_trivially_copy_assignable< + simple_pair>::value)); - // array of such types is trivial - typedef int int10[10]; - EXPECT_TRUE(absl::is_trivially_destructible::value); - typedef TrivialDestructor TrivialDestructor10[10]; - EXPECT_TRUE(absl::is_trivially_destructible::value); - typedef NonTrivialDestructor NonTrivialDestructor10[10]; - EXPECT_FALSE(absl::is_trivially_destructible::value); + // Verify that arrays are not trivially copy assignable + using int10 = int[10]; + EXPECT_FALSE(absl::is_trivially_copy_assignable::value); } #define ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(trait_name, ...) \ diff --git a/absl/strings/internal/stl_type_traits.h b/absl/strings/internal/stl_type_traits.h index 8c3d877e6356..04c4a5323b86 100644 --- a/absl/strings/internal/stl_type_traits.h +++ b/absl/strings/internal/stl_type_traits.h @@ -79,31 +79,48 @@ struct IsSTLContainer template class T, typename = void> struct IsBaseOfSpecializationImpl : std::false_type {}; -// IsBaseOfSpecializationImpl must have three partial specializations, -// because we must only compare templates that take the same number of -// arguments. Otherwise, for example, std::vector can be compared with std::map, -// and fail to compile because of too few or too many template arguments. -// -// We must also SFINAE on the existence of an allocator_type. Otherwise, we may -// try to compare, for example, a std::pair with a -// std::vector. This would fail to compile, because -// of expected properties of the type passed in as the allocator. -template