1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#rtti-layout
10 // Two abi::__pbase_type_info objects can always be compared for equality
11 // (i.e. of the types represented) or ordering by comparison of their name
12 // NTBS addresses. In addition, unless either or both have either of the
13 // incomplete flags set, equality can be tested by comparing the type_info
16 // UNSUPPORTED: no-exceptions
17 // UNSUPPORTED: no-rtti
19 // The fix for PR25898 landed in the system dylibs in macOS 10.13
20 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12}}
22 // RUN: %{cxx} %{flags} %{compile_flags} -Wno-unreachable-code -c %s -o %t.one.o
23 // RUN: %{cxx} %{flags} %{compile_flags} -Wno-unreachable-code -c %s -o %t.two.o -DTU_ONE
24 // RUN: %{cxx} %{flags} %t.one.o %t.two.o %{link_flags} -o %t.exe
25 // RUN: %{exec} %t.exe
32 // Check that the addresses of the typeinfo differ but still compare equal
35 AssertIncompleteTypeInfoEquals(std::type_info
const& LHS
, std::type_info
const& RHS
)
38 assert(strcmp(LHS
.name(), RHS
.name()) == 0);
42 void ThrowNeverDefinedMP();
43 std::type_info
const& ReturnTypeInfoNeverDefinedMP();
45 struct IncompleteAtThrow
;
46 void ThrowIncompleteMP();
47 void ThrowIncompletePP();
48 void ThrowIncompletePMP();
49 std::type_info
const& ReturnTypeInfoIncompleteMP();
50 std::type_info
const& ReturnTypeInfoIncompletePP();
52 struct CompleteAtThrow
;
53 void ThrowCompleteMP();
54 void ThrowCompletePP();
55 void ThrowCompletePMP();
56 std::type_info
const& ReturnTypeInfoCompleteMP();
57 std::type_info
const& ReturnTypeInfoCompletePP();
63 void ThrowNeverDefinedMP() { throw (int NeverDefined::*)nullptr; }
64 std::type_info
const& ReturnTypeInfoNeverDefinedMP() { return typeid(int NeverDefined::*); }
66 void ThrowIncompleteMP() { throw (int IncompleteAtThrow::*)nullptr; }
67 void ThrowIncompletePP() { throw (IncompleteAtThrow
**)nullptr; }
68 void ThrowIncompletePMP() { throw (int IncompleteAtThrow::**)nullptr; }
69 std::type_info
const& ReturnTypeInfoIncompleteMP() { return typeid(int IncompleteAtThrow::*); }
70 std::type_info
const& ReturnTypeInfoIncompletePP() { return typeid(IncompleteAtThrow
**); }
72 struct CompleteAtThrow
{};
73 void ThrowCompleteMP() { throw (int CompleteAtThrow::*)nullptr; }
74 void ThrowCompletePP() { throw (CompleteAtThrow
**)nullptr; }
75 void ThrowCompletePMP() { throw (int CompleteAtThrow::**)nullptr; }
76 std::type_info
const& ReturnTypeInfoCompleteMP() { return typeid(int CompleteAtThrow::*); }
77 std::type_info
const& ReturnTypeInfoCompletePP() { return typeid(CompleteAtThrow
**); }
79 void ThrowNullptr() { throw nullptr; }
83 struct IncompleteAtThrow
{};
85 int main(int, char**) {
86 AssertIncompleteTypeInfoEquals(ReturnTypeInfoNeverDefinedMP(), typeid(int NeverDefined::*));
88 ThrowNeverDefinedMP();
90 } catch (int IncompleteAtThrow::*) {
92 } catch (int CompleteAtThrow::*) {
94 } catch (int NeverDefined::*p
) {
97 catch(...) { assert(!"FAIL: Didn't catch NeverDefined::*" ); }
99 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompleteMP(), typeid(int IncompleteAtThrow::*));
103 } catch (CompleteAtThrow
**) {
105 } catch (int CompleteAtThrow::*) {
107 } catch (IncompleteAtThrow
**) {
109 } catch (int IncompleteAtThrow::*p
) {
112 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::*" ); }
114 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompletePP(), typeid(IncompleteAtThrow
**));
118 } catch (int IncompleteAtThrow::*) {
120 } catch (IncompleteAtThrow
** p
) {
123 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow**" ); }
126 ThrowIncompletePMP();
128 } catch (int IncompleteAtThrow::*) {
130 } catch (IncompleteAtThrow
**) {
132 } catch (int IncompleteAtThrow::**p
) {
135 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::**" ); }
137 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompleteMP(), typeid(int CompleteAtThrow::*));
141 } catch (IncompleteAtThrow
**) {
143 } catch (int IncompleteAtThrow::*) {
145 } catch (CompleteAtThrow
**) {
147 } catch (int CompleteAtThrow::*p
) {
150 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::" ); }
152 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompletePP(), typeid(CompleteAtThrow
**));
156 } catch (IncompleteAtThrow
**) {
158 } catch (int IncompleteAtThrow::*) {
160 } catch (int CompleteAtThrow::*) {
162 } catch (CompleteAtThrow
**p
) {
165 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow**" ); }
170 } catch (IncompleteAtThrow
**) {
172 } catch (int IncompleteAtThrow::*) {
174 } catch (int CompleteAtThrow::*) {
176 } catch (CompleteAtThrow
**) {
178 } catch (int CompleteAtThrow::**p
) {
181 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::**" ); }
183 #if __cplusplus >= 201103L
184 // Catch nullptr as complete type
187 } catch (int IncompleteAtThrow::*p
) {
190 catch(...) { assert(!"FAIL: Didn't catch nullptr as IncompleteAtThrow::*" ); }
192 // Catch nullptr as an incomplete type
195 } catch (int CompleteAtThrow::*p
) {
198 catch(...) { assert(!"FAIL: Didn't catch nullptr as CompleteAtThrow::*" ); }
200 // Catch nullptr as a type that is never complete.
203 } catch (int NeverDefined::*p
) {
206 catch(...) { assert(!"FAIL: Didn't catch nullptr as NeverDefined::*" ); }