blob: e7a2ea286b64bbe9d3e44485e2a4d8c8bef4ece3 [file] [log] [blame]
Howard Hinnantc649bde2012-02-01 20:53:21 +00001//===----------------- 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 Rathnayake4174e8b2016-05-31 12:01:32 +000010// UNSUPPORTED: libcxxabi-no-exceptions
11
Howard Hinnantc649bde2012-02-01 20:53:21 +000012#include <cassert>
13
14struct A
15{
Eric Fiselier5328c6b2016-06-15 19:59:16 +000016 A() : i(0), j(0) {} // explicitly initialize 'i' to prevent warnings
Howard Hinnantc649bde2012-02-01 20:53:21 +000017 const int i;
18 int j;
19};
20
21typedef const int A::*md1;
22typedef int A::*md2;
23
Eric Fiselier0cb62d12015-04-02 23:26:37 +000024struct B : public A
25{
Eric Fiselier5328c6b2016-06-15 19:59:16 +000026 B() : k(0), l(0) {} // explicitly initialize 'k' to prevent warnings.
Eric Fiselier0cb62d12015-04-02 23:26:37 +000027 const int k;
28 int l;
29};
30
31typedef const int B::*der1;
32typedef int B::*der2;
33
Howard Hinnantc649bde2012-02-01 20:53:21 +000034void 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 Fiselier0cb62d12015-04-02 23:26:37 +000050// Check that cv qualified conversions are allowed.
Howard Hinnantc649bde2012-02-01 20:53:21 +000051void test2()
52{
53 try
54 {
55 throw &A::j;
Eric Fiselier0cb62d12015-04-02 23:26:37 +000056 }
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 Fiselier554d59a2015-04-06 23:03:01 +000079// Check that Base -> Derived conversions are NOT allowed.
Eric Fiselier0cb62d12015-04-02 23:26:37 +000080void 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 Fiselier554d59a2015-04-06 23:03:01 +000097 assert(false);
Eric Fiselier0cb62d12015-04-02 23:26:37 +000098 }
99 catch (md1)
100 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000101 }
102}
103
Eric Fiselier554d59a2015-04-06 23:03:01 +0000104// Check that Base -> Derived conversions NOT are allowed with different cv
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000105// qualifiers.
106void test4()
107{
108 try
109 {
110 throw &A::j;
111 assert(false);
112 }
113 catch (der2)
114 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000115 assert(false);
116 }
117 catch (der1)
118 {
Eric Fiselier554d59a2015-04-06 23:03:01 +0000119 assert(false);
120 }
121 catch (md2)
122 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000123 }
124 catch (...)
125 {
126 assert(false);
127 }
128}
129
130// Check that no Derived -> Base conversions are allowed.
131void test5()
132{
133 try
134 {
135 throw &B::k;
Howard Hinnantc649bde2012-02-01 20:53:21 +0000136 assert(false);
137 }
138 catch (md1)
139 {
140 assert(false);
141 }
142 catch (md2)
143 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000144 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 Hinnantc649bde2012-02-01 20:53:21 +0000165 }
166}
167
168int main()
169{
170 test1();
171 test2();
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000172 test3();
173 test4();
174 test5();
Howard Hinnantc649bde2012-02-01 20:53:21 +0000175}