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/internal/exception_safety_testing.h | 61 ++++++++++++++++++--------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'absl/base/internal/exception_safety_testing.h') diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h index a0127a88..8eac2264 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