about summary refs log tree commit diff
path: root/absl/base/internal/exception_safety_testing.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-01-05T15·54-0800
committerDerek Mauro <dmauro@google.com>2018-01-08T15·38-0500
commit4132ce25956a91e1224e0f205b7f8c326304a995 (patch)
tree724485e8778f91d865db86af663af9e7be3293db /absl/base/internal/exception_safety_testing.h
parent0271cd35577599fa99b59202da17d3136956e4c0 (diff)
Changes imported from Abseil "staging" branch:
  - 0a519d9a4507158267cc515e0c7c83959d94fc78 Fix missing header include when compiling with _GLIBCXX_D... by Alex Strelnikov <strel@google.com>
  - d089af70781d92af9a5de2d84c417ddf2c87689a Internal change by Gennadiy Rozental <rogeeff@google.com>
  - 0d3afc89d3907923ede964d58c6bcca579e8ad65 Test absl::any for exception safety.  This test is tempor... by Jon Cohen <cohenjon@google.com>
  - 29af424b8a3174a7b3e657e478aa30a8a425aee2 Tweak the ABSL type trait library and expand its tests. by Abseil Team <absl-team@google.com>
  - 99ab42b2ebbe466cc3730fb6b16b5fad848f95af Rollback GLIBCXX_DEBUG fix due to internal breakage. by Alex Strelnikov <strel@google.com>
  - 1a5bcb93ee16d4dd2170254e54c4b62b38fbf17b Internal change. by Abseil Team <absl-team@google.com>
  - 46de7d09c7d4aef5b7b5389ce9b4f96b654aac02 absl::string_view::rfind: doc fix. by Abseil Team <absl-team@google.com>
  - edda4c7ddd2d76fbb5b3fd5226b95082083c57d9 Fix string_view_test with c++17/clang/libc++ to address by Xiaoyi Zhang <zhangxy@google.com>

GitOrigin-RevId: 0a519d9a4507158267cc515e0c7c83959d94fc78
Change-Id: Ie27de1be3e79bba011f05e924d34e8fcc62d8de5
Diffstat (limited to 'absl/base/internal/exception_safety_testing.h')
-rw-r--r--absl/base/internal/exception_safety_testing.h61
1 files changed, 42 insertions, 19 deletions
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<void*>(this)
                     << " re-constructed in ctor " << child_ctor;
     }
   }
 
-  TrackedObject(const TrackedObject&) = delete;
-  TrackedObject(TrackedObject&&) = delete;
-
   static std::unordered_map<TrackedObject*, absl::string_view>& GetAllocs() {
     static auto* m =
         new std::unordered_map<TrackedObject*, absl::string_view>();
@@ -120,10 +136,10 @@ using FactoryType = typename absl::result_of_t<Factory()>::element_type;
 template <typename Factory, typename Op, typename Checker>
 absl::optional<testing::AssertionResult> TestCheckerAtCountdown(
     Factory factory, const Op& op, int count, const Checker& check) {
-  exceptions_internal::countdown = count;
   auto t_ptr = factory();
   absl::optional<testing::AssertionResult> 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 <typename Factory, typename Op, typename... Checkers>
@@ -148,8 +168,9 @@ absl::optional<testing::AssertionResult> 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<Factory>* t_ptr) { return AbslCheckInvariants(t_ptr); });
+      factory, op, count, [](FactoryType<Factory>* 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>(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>(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<difference_type>::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 <typename TFactory, typename FunctionFromTPtrToVoid,
 testing::AssertionResult TestExceptionSafety(TFactory factory,
                                              FunctionFromTPtrToVoid&& op,
                                              const Checkers&... checkers) {
+  struct Cleanup {
+    ~Cleanup() { UnsetCountdown(); }
+  } c;
   for (int countdown = 0;; ++countdown) {
     auto out = exceptions_internal::TestAtCountdown(factory, op, countdown,
                                                     checkers...);
     if (!out.has_value()) {
-      UnsetCountdown();
       return testing::AssertionSuccess();
     }
     if (!*out) return *out;