rethrow_exception.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // <exception>
  10. // void rethrow_exception [[noreturn]] (exception_ptr p);
  11. #include <exception>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. struct A
  15. {
  16. static int constructed;
  17. int data_;
  18. A(int data = 0) : data_(data) {++constructed;}
  19. ~A() {--constructed;}
  20. A(const A& a) : data_(a.data_) {++constructed;}
  21. };
  22. int A::constructed = 0;
  23. int main(int, char**)
  24. {
  25. {
  26. std::exception_ptr p;
  27. try
  28. {
  29. throw A(3);
  30. }
  31. catch (...)
  32. {
  33. p = std::current_exception();
  34. }
  35. try
  36. {
  37. std::rethrow_exception(p);
  38. assert(false);
  39. }
  40. catch (const A& a)
  41. {
  42. #ifndef _LIBCPP_ABI_MICROSOFT
  43. assert(A::constructed == 1);
  44. #else
  45. // On Windows the exception_ptr copies the exception
  46. assert(A::constructed == 2);
  47. #endif
  48. assert(p != nullptr);
  49. p = nullptr;
  50. assert(p == nullptr);
  51. assert(a.data_ == 3);
  52. assert(A::constructed == 1);
  53. }
  54. assert(A::constructed == 0);
  55. }
  56. assert(A::constructed == 0);
  57. return 0;
  58. }