rethrow_if_nested.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: libcpp-no-exceptions
  9. // This test fails due to a stack overflow
  10. // XFAIL: LIBCXX-WINDOWS-FIXME
  11. // <exception>
  12. // class nested_exception;
  13. // template <class E> void rethrow_if_nested(const E& e);
  14. #include <exception>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. class A
  19. {
  20. int data_;
  21. public:
  22. explicit A(int data) : data_(data) {}
  23. virtual ~A() TEST_NOEXCEPT {}
  24. friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
  25. };
  26. class B
  27. : public std::nested_exception,
  28. public A
  29. {
  30. public:
  31. explicit B(int data) : A(data) {}
  32. B(const B& b) : A(b) {}
  33. };
  34. class C
  35. {
  36. public:
  37. virtual ~C() {}
  38. C * operator&() const { assert(false); return nullptr; } // should not be called
  39. };
  40. class D : private std::nested_exception {};
  41. class E1 : public std::nested_exception {};
  42. class E2 : public std::nested_exception {};
  43. class E : public E1, public E2 {};
  44. int main(int, char**)
  45. {
  46. {
  47. try
  48. {
  49. A a(3); // not a polymorphic type --> no effect
  50. std::rethrow_if_nested(a);
  51. assert(true);
  52. }
  53. catch (...)
  54. {
  55. assert(false);
  56. }
  57. }
  58. {
  59. try
  60. {
  61. D s; // inaccessible base class --> no effect
  62. std::rethrow_if_nested(s);
  63. assert(true);
  64. }
  65. catch (...)
  66. {
  67. assert(false);
  68. }
  69. }
  70. {
  71. try
  72. {
  73. E s; // ambiguous base class --> no effect
  74. std::rethrow_if_nested(s);
  75. assert(true);
  76. }
  77. catch (...)
  78. {
  79. assert(false);
  80. }
  81. }
  82. {
  83. try
  84. {
  85. throw B(5);
  86. }
  87. catch (const B& b)
  88. {
  89. try
  90. {
  91. throw b;
  92. }
  93. catch (const A& a)
  94. {
  95. try
  96. {
  97. std::rethrow_if_nested(a);
  98. assert(false);
  99. }
  100. catch (const B& b2)
  101. {
  102. assert(b2 == B(5));
  103. }
  104. }
  105. }
  106. }
  107. {
  108. try
  109. {
  110. std::rethrow_if_nested(C());
  111. assert(true);
  112. }
  113. catch (...)
  114. {
  115. assert(false);
  116. }
  117. }
  118. return 0;
  119. }