Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 1 | //===----------------- catch_member_data_pointer_01.cpp -------------------===// |
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 | |||||
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 10 | // UNSUPPORTED: libcxxabi-no-exceptions |
11 | |||||
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 12 | #include <cassert> |
13 | |||||
14 | struct A | ||||
15 | { | ||||
Eric Fiselier | 5328c6b | 2016-06-15 19:59:16 +0000 | [diff] [blame] | 16 | A() : i(0), j(0) {} // explicitly initialize 'i' to prevent warnings |
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 17 | const int i; |
18 | int j; | ||||
19 | }; | ||||
20 | |||||
21 | typedef const int A::*md1; | ||||
22 | typedef int A::*md2; | ||||
23 | |||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 24 | struct B : public A |
25 | { | ||||
Eric Fiselier | 5328c6b | 2016-06-15 19:59:16 +0000 | [diff] [blame] | 26 | B() : k(0), l(0) {} // explicitly initialize 'k' to prevent warnings. |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 27 | const int k; |
28 | int l; | ||||
29 | }; | ||||
30 | |||||
31 | typedef const int B::*der1; | ||||
32 | typedef int B::*der2; | ||||
33 | |||||
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 34 | void test1() |
35 | { | ||||
36 | try | ||||
37 | { | ||||
38 | throw &A::i; | ||||
39 | assert(false); | ||||
40 | } | ||||
41 | catch (md2) | ||||
42 | { | ||||
43 | assert(false); | ||||
44 | } | ||||
45 | catch (md1) | ||||
46 | { | ||||
47 | } | ||||
48 | } | ||||
49 | |||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 50 | // Check that cv qualified conversions are allowed. |
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 51 | void test2() |
52 | { | ||||
53 | try | ||||
54 | { | ||||
55 | throw &A::j; | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 56 | } |
57 | catch (md2) | ||||
58 | { | ||||
59 | } | ||||
60 | catch (...) | ||||
61 | { | ||||
62 | assert(false); | ||||
63 | } | ||||
64 | |||||
65 | try | ||||
66 | { | ||||
67 | throw &A::j; | ||||
68 | assert(false); | ||||
69 | } | ||||
70 | catch (md1) | ||||
71 | { | ||||
72 | } | ||||
73 | catch (...) | ||||
74 | { | ||||
75 | assert(false); | ||||
76 | } | ||||
77 | } | ||||
78 | |||||
Eric Fiselier | 554d59a | 2015-04-06 23:03:01 +0000 | [diff] [blame] | 79 | // Check that Base -> Derived conversions are NOT allowed. |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 80 | void test3() |
81 | { | ||||
82 | try | ||||
83 | { | ||||
84 | throw &A::i; | ||||
85 | assert(false); | ||||
86 | } | ||||
87 | catch (md2) | ||||
88 | { | ||||
89 | assert(false); | ||||
90 | } | ||||
91 | catch (der2) | ||||
92 | { | ||||
93 | assert(false); | ||||
94 | } | ||||
95 | catch (der1) | ||||
96 | { | ||||
Eric Fiselier | 554d59a | 2015-04-06 23:03:01 +0000 | [diff] [blame] | 97 | assert(false); |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 98 | } |
99 | catch (md1) | ||||
100 | { | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 101 | } |
102 | } | ||||
103 | |||||
Eric Fiselier | 554d59a | 2015-04-06 23:03:01 +0000 | [diff] [blame] | 104 | // Check that Base -> Derived conversions NOT are allowed with different cv |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 105 | // qualifiers. |
106 | void test4() | ||||
107 | { | ||||
108 | try | ||||
109 | { | ||||
110 | throw &A::j; | ||||
111 | assert(false); | ||||
112 | } | ||||
113 | catch (der2) | ||||
114 | { | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 115 | assert(false); |
116 | } | ||||
117 | catch (der1) | ||||
118 | { | ||||
Eric Fiselier | 554d59a | 2015-04-06 23:03:01 +0000 | [diff] [blame] | 119 | assert(false); |
120 | } | ||||
121 | catch (md2) | ||||
122 | { | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 123 | } |
124 | catch (...) | ||||
125 | { | ||||
126 | assert(false); | ||||
127 | } | ||||
128 | } | ||||
129 | |||||
130 | // Check that no Derived -> Base conversions are allowed. | ||||
131 | void test5() | ||||
132 | { | ||||
133 | try | ||||
134 | { | ||||
135 | throw &B::k; | ||||
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 136 | assert(false); |
137 | } | ||||
138 | catch (md1) | ||||
139 | { | ||||
140 | assert(false); | ||||
141 | } | ||||
142 | catch (md2) | ||||
143 | { | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 144 | assert(false); |
145 | } | ||||
146 | catch (der1) | ||||
147 | { | ||||
148 | } | ||||
149 | |||||
150 | try | ||||
151 | { | ||||
152 | throw &B::l; | ||||
153 | assert(false); | ||||
154 | } | ||||
155 | catch (md1) | ||||
156 | { | ||||
157 | assert(false); | ||||
158 | } | ||||
159 | catch (md2) | ||||
160 | { | ||||
161 | assert(false); | ||||
162 | } | ||||
163 | catch (der2) | ||||
164 | { | ||||
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 165 | } |
166 | } | ||||
167 | |||||
168 | int main() | ||||
169 | { | ||||
170 | test1(); | ||||
171 | test2(); | ||||
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 172 | test3(); |
173 | test4(); | ||||
174 | test5(); | ||||
Howard Hinnant | c649bde | 2012-02-01 20:53:21 +0000 | [diff] [blame] | 175 | } |