1 //===------------------------- incomplete_type.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 // http://mentorembedded.github.io/cxx-abi/abi.html#rtti-layout
10
11 // Two abi::__pbase_type_info objects can always be compared for equality
12 // (i.e. of the types represented) or ordering by comparison of their name
13 // NTBS addresses. In addition, unless either or both have either of the
14 // incomplete flags set, equality can be tested by comparing the type_info
15 // addresses.
16
17 // UNSUPPORTED: libcxxabi-no-exceptions
18
19 // RUN: %cxx %flags %compile_flags -c %s -o %t.one.o
20 // RUN: %cxx %flags %compile_flags -c %s -o %t.two.o -DTU_ONE
21 // RUN: %cxx %flags %t.one.o %t.two.o %link_flags -o %t.exe
22 // RUN: %exec %t.exe
23
24 #include <stdio.h>
25 #include <cstring>
26 #include <cassert>
27 #include <typeinfo>
28
29 // Check that the addresses of the typeinfo differ but still compare equal
30 // via their NTBS.
31 inline void
AssertIncompleteTypeInfoEquals(std::type_info const & LHS,std::type_info const & RHS)32 AssertIncompleteTypeInfoEquals(std::type_info const& LHS, std::type_info const& RHS)
33 {
34 assert(&LHS != &RHS);
35 assert(strcmp(LHS.name(), RHS.name()) == 0);
36 }
37
38 struct NeverDefined;
39 void ThrowNeverDefinedMP();
40 std::type_info const& ReturnTypeInfoNeverDefinedMP();
41
42 struct IncompleteAtThrow;
43 void ThrowIncompleteMP();
44 void ThrowIncompletePP();
45 void ThrowIncompletePMP();
46 std::type_info const& ReturnTypeInfoIncompleteMP();
47 std::type_info const& ReturnTypeInfoIncompletePP();
48
49 struct CompleteAtThrow;
50 void ThrowCompleteMP();
51 void ThrowCompletePP();
52 void ThrowCompletePMP();
53 std::type_info const& ReturnTypeInfoCompleteMP();
54 std::type_info const& ReturnTypeInfoCompletePP();
55
56 void ThrowNullptr();
57
58 #ifndef TU_ONE
59
ThrowNeverDefinedMP()60 void ThrowNeverDefinedMP() { throw (int NeverDefined::*)nullptr; }
ReturnTypeInfoNeverDefinedMP()61 std::type_info const& ReturnTypeInfoNeverDefinedMP() { return typeid(int NeverDefined::*); }
62
ThrowIncompleteMP()63 void ThrowIncompleteMP() { throw (int IncompleteAtThrow::*)nullptr; }
ThrowIncompletePP()64 void ThrowIncompletePP() { throw (IncompleteAtThrow**)nullptr; }
ThrowIncompletePMP()65 void ThrowIncompletePMP() { throw (int IncompleteAtThrow::**)nullptr; }
ReturnTypeInfoIncompleteMP()66 std::type_info const& ReturnTypeInfoIncompleteMP() { return typeid(int IncompleteAtThrow::*); }
ReturnTypeInfoIncompletePP()67 std::type_info const& ReturnTypeInfoIncompletePP() { return typeid(IncompleteAtThrow**); }
68
69 struct CompleteAtThrow {};
ThrowCompleteMP()70 void ThrowCompleteMP() { throw (int CompleteAtThrow::*)nullptr; }
ThrowCompletePP()71 void ThrowCompletePP() { throw (CompleteAtThrow**)nullptr; }
ThrowCompletePMP()72 void ThrowCompletePMP() { throw (int CompleteAtThrow::**)nullptr; }
ReturnTypeInfoCompleteMP()73 std::type_info const& ReturnTypeInfoCompleteMP() { return typeid(int CompleteAtThrow::*); }
ReturnTypeInfoCompletePP()74 std::type_info const& ReturnTypeInfoCompletePP() { return typeid(CompleteAtThrow**); }
75
ThrowNullptr()76 void ThrowNullptr() { throw nullptr; }
77
78 #else
79
80 struct IncompleteAtThrow {};
81
main()82 int main() {
83 AssertIncompleteTypeInfoEquals(ReturnTypeInfoNeverDefinedMP(), typeid(int NeverDefined::*));
84 try {
85 ThrowNeverDefinedMP();
86 assert(false);
87 } catch (int IncompleteAtThrow::*) {
88 assert(false);
89 } catch (int CompleteAtThrow::*) {
90 assert(false);
91 } catch (int NeverDefined::*p) {
92 assert(!p);
93 }
94 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompleteMP(), typeid(int IncompleteAtThrow::*));
95 try {
96 ThrowIncompleteMP();
97 assert(false);
98 } catch (CompleteAtThrow**) {
99 assert(false);
100 } catch (int CompleteAtThrow::*) {
101 assert(false);
102 } catch (IncompleteAtThrow**) {
103 assert(false);
104 } catch (int IncompleteAtThrow::*p) {
105 assert(!p);
106 }
107
108 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompletePP(), typeid(IncompleteAtThrow**));
109 try {
110 ThrowIncompletePP();
111 assert(false);
112 } catch (int IncompleteAtThrow::*) {
113 assert(false);
114 } catch (IncompleteAtThrow** p) {
115 assert(!p);
116 }
117
118 try {
119 ThrowIncompletePMP();
120 assert(false);
121 } catch (int IncompleteAtThrow::*) {
122 assert(false);
123 } catch (IncompleteAtThrow**) {
124 assert(false);
125 } catch (int IncompleteAtThrow::**p) {
126 assert(!p);
127 }
128
129 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompleteMP(), typeid(int CompleteAtThrow::*));
130 try {
131 ThrowCompleteMP();
132 assert(false);
133 } catch (IncompleteAtThrow**) {
134 assert(false);
135 } catch (int IncompleteAtThrow::*) {
136 assert(false);
137 } catch (CompleteAtThrow**) {
138 assert(false);
139 } catch (int CompleteAtThrow::*p) {
140 assert(!p);
141 }
142
143 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompletePP(), typeid(CompleteAtThrow**));
144 try {
145 ThrowCompletePP();
146 assert(false);
147 } catch (IncompleteAtThrow**) {
148 assert(false);
149 } catch (int IncompleteAtThrow::*) {
150 assert(false);
151 } catch (int CompleteAtThrow::*) {
152 assert(false);
153 } catch (CompleteAtThrow**p) {
154 assert(!p);
155 }
156
157 try {
158 ThrowCompletePMP();
159 assert(false);
160 } catch (IncompleteAtThrow**) {
161 assert(false);
162 } catch (int IncompleteAtThrow::*) {
163 assert(false);
164 } catch (int CompleteAtThrow::*) {
165 assert(false);
166 } catch (CompleteAtThrow**) {
167 assert(false);
168 } catch (int CompleteAtThrow::**p) {
169 assert(!p);
170 }
171
172 #if __cplusplus >= 201103L
173 // Catch nullptr as complete type
174 try {
175 ThrowNullptr();
176 } catch (int IncompleteAtThrow::*p) {
177 assert(!p);
178 }
179
180 // Catch nullptr as an incomplete type
181 try {
182 ThrowNullptr();
183 } catch (int CompleteAtThrow::*p) {
184 assert(!p);
185 }
186 // Catch nullptr as a type that is never complete.
187 try {
188 ThrowNullptr();
189 } catch (int NeverDefined::*p) {
190 assert(!p);
191 }
192 #endif
193 }
194 #endif
195