current_exception.pass.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // This test needs to be rewritten for the Windows exception_ptr semantics
  9. // which copy the exception each time the exception_ptr is copied.
  10. // XFAIL: LIBCXX-WINDOWS-FIXME
  11. // UNSUPPORTED: libcpp-no-exceptions
  12. // <exception>
  13. // exception_ptr current_exception();
  14. #include <exception>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. struct A
  18. {
  19. static int constructed;
  20. A() {++constructed;}
  21. ~A() {--constructed;}
  22. A(const A&) {++constructed;}
  23. };
  24. int A::constructed = 0;
  25. int main(int, char**)
  26. {
  27. {
  28. std::exception_ptr p = std::current_exception();
  29. assert(p == nullptr);
  30. }
  31. {
  32. try
  33. {
  34. assert(A::constructed == 0);
  35. throw A();
  36. assert(false);
  37. }
  38. catch (...)
  39. {
  40. assert(A::constructed == 1);
  41. }
  42. assert(A::constructed == 0);
  43. }
  44. assert(A::constructed == 0);
  45. {
  46. std::exception_ptr p2;
  47. try
  48. {
  49. assert(A::constructed == 0);
  50. throw A();
  51. assert(false);
  52. }
  53. catch (...)
  54. {
  55. std::exception_ptr p = std::current_exception();
  56. assert(A::constructed == 1);
  57. assert(p != nullptr);
  58. p2 = std::current_exception();
  59. assert(A::constructed == 1);
  60. assert(p == p2);
  61. }
  62. assert(A::constructed == 1);
  63. }
  64. assert(A::constructed == 0);
  65. {
  66. std::exception_ptr p2;
  67. try
  68. {
  69. assert(A::constructed == 0);
  70. throw A();
  71. assert(false);
  72. }
  73. catch (A&)
  74. {
  75. std::exception_ptr p = std::current_exception();
  76. assert(A::constructed == 1);
  77. assert(p != nullptr);
  78. p2 = std::current_exception();
  79. assert(A::constructed == 1);
  80. assert(p == p2);
  81. }
  82. assert(A::constructed == 1);
  83. }
  84. assert(A::constructed == 0);
  85. {
  86. std::exception_ptr p2;
  87. try
  88. {
  89. assert(A::constructed == 0);
  90. throw A();
  91. assert(false);
  92. }
  93. catch (A)
  94. {
  95. std::exception_ptr p = std::current_exception();
  96. assert(A::constructed == 2);
  97. assert(p != nullptr);
  98. p2 = std::current_exception();
  99. assert(A::constructed == 2);
  100. assert(p == p2);
  101. }
  102. assert(A::constructed == 1);
  103. }
  104. assert(A::constructed == 0);
  105. {
  106. try
  107. {
  108. assert(A::constructed == 0);
  109. throw A();
  110. assert(false);
  111. }
  112. catch (...)
  113. {
  114. assert(A::constructed == 1);
  115. try
  116. {
  117. assert(A::constructed == 1);
  118. throw;
  119. assert(false);
  120. }
  121. catch (...)
  122. {
  123. assert(A::constructed == 1);
  124. }
  125. assert(A::constructed == 1);
  126. }
  127. assert(A::constructed == 0);
  128. }
  129. assert(A::constructed == 0);
  130. {
  131. try
  132. {
  133. assert(A::constructed == 0);
  134. throw A();
  135. assert(false);
  136. }
  137. catch (...)
  138. {
  139. assert(A::constructed == 1);
  140. try
  141. {
  142. std::exception_ptr p = std::current_exception();
  143. assert(A::constructed == 1);
  144. assert(p != nullptr);
  145. throw;
  146. assert(false);
  147. }
  148. catch (...)
  149. {
  150. assert(A::constructed == 1);
  151. }
  152. assert(A::constructed == 1);
  153. }
  154. assert(A::constructed == 0);
  155. }
  156. assert(A::constructed == 0);
  157. {
  158. try
  159. {
  160. assert(A::constructed == 0);
  161. throw A();
  162. assert(false);
  163. }
  164. catch (...)
  165. {
  166. assert(A::constructed == 1);
  167. try
  168. {
  169. assert(A::constructed == 1);
  170. throw;
  171. assert(false);
  172. }
  173. catch (...)
  174. {
  175. std::exception_ptr p = std::current_exception();
  176. assert(A::constructed == 1);
  177. assert(p != nullptr);
  178. }
  179. assert(A::constructed == 1);
  180. }
  181. assert(A::constructed == 0);
  182. }
  183. assert(A::constructed == 0);
  184. {
  185. try
  186. {
  187. assert(A::constructed == 0);
  188. throw A();
  189. assert(false);
  190. }
  191. catch (...)
  192. {
  193. assert(A::constructed == 1);
  194. try
  195. {
  196. assert(A::constructed == 1);
  197. throw;
  198. assert(false);
  199. }
  200. catch (...)
  201. {
  202. assert(A::constructed == 1);
  203. }
  204. std::exception_ptr p = std::current_exception();
  205. assert(A::constructed == 1);
  206. assert(p != nullptr);
  207. }
  208. assert(A::constructed == 0);
  209. }
  210. assert(A::constructed == 0);
  211. {
  212. try
  213. {
  214. assert(A::constructed == 0);
  215. throw A();
  216. assert(false);
  217. }
  218. catch (...)
  219. {
  220. assert(A::constructed == 1);
  221. try
  222. {
  223. assert(A::constructed == 1);
  224. throw;
  225. assert(false);
  226. }
  227. catch (...)
  228. {
  229. assert(A::constructed == 1);
  230. }
  231. assert(A::constructed == 1);
  232. }
  233. std::exception_ptr p = std::current_exception();
  234. assert(A::constructed == 0);
  235. assert(p == nullptr);
  236. }
  237. assert(A::constructed == 0);
  238. {
  239. std::exception_ptr p;
  240. try
  241. {
  242. assert(A::constructed == 0);
  243. throw A();
  244. assert(false);
  245. }
  246. catch (...)
  247. {
  248. assert(A::constructed == 1);
  249. try
  250. {
  251. assert(A::constructed == 1);
  252. throw;
  253. assert(false);
  254. }
  255. catch (...)
  256. {
  257. p = std::current_exception();
  258. assert(A::constructed == 1);
  259. }
  260. assert(A::constructed == 1);
  261. }
  262. assert(A::constructed == 1);
  263. assert(p != nullptr);
  264. }
  265. assert(A::constructed == 0);
  266. return 0;
  267. }