rethrow_if_nested.pass.cpp 2.6 KB

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