blob: 6062dc6074959d6c72dc2b9d08653ff44018a464 [file] [log] [blame]
Eric Fiselier91fc04d2016-01-19 23:42:10 +00001//===------------------------- 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
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000017// UNSUPPORTED: libcxxabi-no-exceptions
18
Eric Fiselier065ab9e2017-07-06 00:29:09 +000019// NOTE: Pass -lc++abi explicitly and before -lc++ so that -lc++ doesn't drag
20// in the system libc++abi installation on OS X. (DYLD_LIBRARY_PATH is ignored
21// for shell tests because of Apple security features).
22
Eric Fiselier91fc04d2016-01-19 23:42:10 +000023// RUN: %cxx %flags %compile_flags -c %s -o %t.one.o
24// RUN: %cxx %flags %compile_flags -c %s -o %t.two.o -DTU_ONE
Eric Fiselier065ab9e2017-07-06 00:29:09 +000025// RUN: %cxx %flags %t.one.o %t.two.o -lc++abi %link_flags -o %t.exe
Eric Fiselier3a150432017-05-10 08:15:40 +000026// RUN: %t.exe
Eric Fiselier91fc04d2016-01-19 23:42:10 +000027
28#include <stdio.h>
29#include <cstring>
30#include <cassert>
31#include <typeinfo>
32
33// Check that the addresses of the typeinfo differ but still compare equal
34// via their NTBS.
35inline void
36AssertIncompleteTypeInfoEquals(std::type_info const& LHS, std::type_info const& RHS)
37{
38 assert(&LHS != &RHS);
39 assert(strcmp(LHS.name(), RHS.name()) == 0);
40}
41
42struct NeverDefined;
43void ThrowNeverDefinedMP();
44std::type_info const& ReturnTypeInfoNeverDefinedMP();
45
46struct IncompleteAtThrow;
47void ThrowIncompleteMP();
48void ThrowIncompletePP();
49void ThrowIncompletePMP();
50std::type_info const& ReturnTypeInfoIncompleteMP();
51std::type_info const& ReturnTypeInfoIncompletePP();
52
53struct CompleteAtThrow;
54void ThrowCompleteMP();
55void ThrowCompletePP();
56void ThrowCompletePMP();
57std::type_info const& ReturnTypeInfoCompleteMP();
58std::type_info const& ReturnTypeInfoCompletePP();
59
60void ThrowNullptr();
61
62#ifndef TU_ONE
63
64void ThrowNeverDefinedMP() { throw (int NeverDefined::*)nullptr; }
65std::type_info const& ReturnTypeInfoNeverDefinedMP() { return typeid(int NeverDefined::*); }
66
67void ThrowIncompleteMP() { throw (int IncompleteAtThrow::*)nullptr; }
68void ThrowIncompletePP() { throw (IncompleteAtThrow**)nullptr; }
69void ThrowIncompletePMP() { throw (int IncompleteAtThrow::**)nullptr; }
70std::type_info const& ReturnTypeInfoIncompleteMP() { return typeid(int IncompleteAtThrow::*); }
71std::type_info const& ReturnTypeInfoIncompletePP() { return typeid(IncompleteAtThrow**); }
72
73struct CompleteAtThrow {};
74void ThrowCompleteMP() { throw (int CompleteAtThrow::*)nullptr; }
75void ThrowCompletePP() { throw (CompleteAtThrow**)nullptr; }
76void ThrowCompletePMP() { throw (int CompleteAtThrow::**)nullptr; }
77std::type_info const& ReturnTypeInfoCompleteMP() { return typeid(int CompleteAtThrow::*); }
78std::type_info const& ReturnTypeInfoCompletePP() { return typeid(CompleteAtThrow**); }
79
80void ThrowNullptr() { throw nullptr; }
81
82#else
83
84struct IncompleteAtThrow {};
85
86int main() {
87 AssertIncompleteTypeInfoEquals(ReturnTypeInfoNeverDefinedMP(), typeid(int NeverDefined::*));
88 try {
89 ThrowNeverDefinedMP();
90 assert(false);
91 } catch (int IncompleteAtThrow::*) {
92 assert(false);
93 } catch (int CompleteAtThrow::*) {
94 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +000095 } catch (int NeverDefined::*p) {
96 assert(!p);
97 }
Marshall Clow3d356fd2017-06-22 00:49:03 +000098 catch(...) { assert(!"FAIL: Didn't catch NeverDefined::*" ); }
99
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000100 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompleteMP(), typeid(int IncompleteAtThrow::*));
101 try {
102 ThrowIncompleteMP();
103 assert(false);
104 } catch (CompleteAtThrow**) {
105 assert(false);
106 } catch (int CompleteAtThrow::*) {
107 assert(false);
108 } catch (IncompleteAtThrow**) {
109 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000110 } catch (int IncompleteAtThrow::*p) {
111 assert(!p);
112 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000113 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::*" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000114
115 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompletePP(), typeid(IncompleteAtThrow**));
116 try {
117 ThrowIncompletePP();
118 assert(false);
119 } catch (int IncompleteAtThrow::*) {
120 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000121 } catch (IncompleteAtThrow** p) {
122 assert(!p);
123 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000124 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow**" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000125
126 try {
127 ThrowIncompletePMP();
128 assert(false);
129 } catch (int IncompleteAtThrow::*) {
130 assert(false);
131 } catch (IncompleteAtThrow**) {
132 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000133 } catch (int IncompleteAtThrow::**p) {
134 assert(!p);
135 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000136 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::**" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000137
138 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompleteMP(), typeid(int CompleteAtThrow::*));
139 try {
140 ThrowCompleteMP();
141 assert(false);
142 } catch (IncompleteAtThrow**) {
143 assert(false);
144 } catch (int IncompleteAtThrow::*) {
145 assert(false);
146 } catch (CompleteAtThrow**) {
147 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000148 } catch (int CompleteAtThrow::*p) {
149 assert(!p);
150 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000151 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000152
153 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompletePP(), typeid(CompleteAtThrow**));
154 try {
155 ThrowCompletePP();
156 assert(false);
157 } catch (IncompleteAtThrow**) {
158 assert(false);
159 } catch (int IncompleteAtThrow::*) {
160 assert(false);
161 } catch (int CompleteAtThrow::*) {
162 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000163 } catch (CompleteAtThrow**p) {
164 assert(!p);
165 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000166 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow**" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000167
168 try {
169 ThrowCompletePMP();
170 assert(false);
171 } catch (IncompleteAtThrow**) {
172 assert(false);
173 } catch (int IncompleteAtThrow::*) {
174 assert(false);
175 } catch (int CompleteAtThrow::*) {
176 assert(false);
177 } catch (CompleteAtThrow**) {
178 assert(false);
Richard Smith081ea862016-07-19 20:19:37 +0000179 } catch (int CompleteAtThrow::**p) {
180 assert(!p);
181 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000182 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::**" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000183
184#if __cplusplus >= 201103L
185 // Catch nullptr as complete type
186 try {
187 ThrowNullptr();
Richard Smith081ea862016-07-19 20:19:37 +0000188 } catch (int IncompleteAtThrow::*p) {
189 assert(!p);
190 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000191 catch(...) { assert(!"FAIL: Didn't catch nullptr as IncompleteAtThrow::*" ); }
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000192
193 // Catch nullptr as an incomplete type
194 try {
195 ThrowNullptr();
Richard Smith081ea862016-07-19 20:19:37 +0000196 } catch (int CompleteAtThrow::*p) {
197 assert(!p);
198 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000199 catch(...) { assert(!"FAIL: Didn't catch nullptr as CompleteAtThrow::*" ); }
200
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000201 // Catch nullptr as a type that is never complete.
202 try {
203 ThrowNullptr();
Richard Smith081ea862016-07-19 20:19:37 +0000204 } catch (int NeverDefined::*p) {
205 assert(!p);
206 }
Marshall Clow3d356fd2017-06-22 00:49:03 +0000207 catch(...) { assert(!"FAIL: Didn't catch nullptr as NeverDefined::*" ); }
208
Eric Fiselier91fc04d2016-01-19 23:42:10 +0000209#endif
210}
211#endif