Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / language.support / support.exception / except.nested / rethrow_if_nested.pass.cpp
blob39bf62b8193bb45e2e198368f96fd0f0f6fc3ec1
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-exceptions
11 // FIXME: This test fails in MSVC mode due to a stack overflow
12 // XFAIL: msvc
14 // <exception>
16 // class nested_exception;
18 // template <class E> void rethrow_if_nested(const E& e);
20 #include <exception>
21 #include <cstdlib>
22 #include <cassert>
24 #include "test_macros.h"
26 class A
28 int data_;
29 public:
30 explicit A(int data) : data_(data) {}
31 A(const A&) = default;
32 A& operator=(const A&) = default;
33 virtual ~A() TEST_NOEXCEPT {}
35 friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
38 class B
39 : public std::nested_exception,
40 public A
42 public:
43 explicit B(int data) : A(data) {}
44 B(const B& b) : A(b) {}
47 class C
49 public:
50 virtual ~C() {}
51 C * operator&() const { assert(false); return nullptr; } // should not be called
54 class D : private std::nested_exception {};
57 class E1 : public std::nested_exception {};
58 class E2 : public std::nested_exception {};
59 class E : public E1, public E2 {};
61 int main(int, char**)
64 try
66 A a(3); // not a polymorphic type --> no effect
67 std::rethrow_if_nested(a);
68 assert(true);
70 catch (...)
72 assert(false);
76 try
78 D s; // inaccessible base class --> no effect
79 std::rethrow_if_nested(s);
80 assert(true);
82 catch (...)
84 assert(false);
88 try
90 E s; // ambiguous base class --> no effect
91 std::rethrow_if_nested(s);
92 assert(true);
94 catch (...)
96 assert(false);
102 throw B(5);
104 catch (const B& b)
108 throw b;
110 catch (const A& a)
114 std::rethrow_if_nested(a);
115 assert(false);
117 catch (const B& b2)
119 assert(b2 == B(5));
127 std::rethrow_if_nested(C());
128 assert(true);
130 catch (...)
132 assert(false);
137 return 0;