current_exception.pass.cpp 6.4 KB

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