1 //===------------------------- incomplete_type.cpp --------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#rtti-layout 9 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 14 // addresses. 15 16 // UNSUPPORTED: no-exceptions 17 // UNSUPPORTED: no-rtti 18 19 // The fix for PR25898 landed in the system dylibs in macOS 10.13 20 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.12 21 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.11 22 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.10 23 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.9 24 25 // RUN: %{cxx} %{flags} %{compile_flags} -Wno-unreachable-code -c %s -o %t.one.o 26 // RUN: %{cxx} %{flags} %{compile_flags} -Wno-unreachable-code -c %s -o %t.two.o -DTU_ONE 27 // RUN: %{cxx} %{flags} %t.one.o %t.two.o %{link_flags} -o %t.exe 28 // RUN: %{exec} %t.exe 29 30 #include <stdio.h> 31 #include <cstring> 32 #include <cassert> 33 #include <typeinfo> 34 35 // Check that the addresses of the typeinfo differ but still compare equal 36 // via their NTBS. 37 inline void 38 AssertIncompleteTypeInfoEquals(std::type_info const& LHS, std::type_info const& RHS) 39 { 40 assert(&LHS != &RHS); 41 assert(strcmp(LHS.name(), RHS.name()) == 0); 42 } 43 44 struct NeverDefined; 45 void ThrowNeverDefinedMP(); 46 std::type_info const& ReturnTypeInfoNeverDefinedMP(); 47 48 struct IncompleteAtThrow; 49 void ThrowIncompleteMP(); 50 void ThrowIncompletePP(); 51 void ThrowIncompletePMP(); 52 std::type_info const& ReturnTypeInfoIncompleteMP(); 53 std::type_info const& ReturnTypeInfoIncompletePP(); 54 55 struct CompleteAtThrow; 56 void ThrowCompleteMP(); 57 void ThrowCompletePP(); 58 void ThrowCompletePMP(); 59 std::type_info const& ReturnTypeInfoCompleteMP(); 60 std::type_info const& ReturnTypeInfoCompletePP(); 61 62 void ThrowNullptr(); 63 64 #ifndef TU_ONE 65 66 void ThrowNeverDefinedMP() { throw (int NeverDefined::*)nullptr; } 67 std::type_info const& ReturnTypeInfoNeverDefinedMP() { return typeid(int NeverDefined::*); } 68 69 void ThrowIncompleteMP() { throw (int IncompleteAtThrow::*)nullptr; } 70 void ThrowIncompletePP() { throw (IncompleteAtThrow**)nullptr; } 71 void ThrowIncompletePMP() { throw (int IncompleteAtThrow::**)nullptr; } 72 std::type_info const& ReturnTypeInfoIncompleteMP() { return typeid(int IncompleteAtThrow::*); } 73 std::type_info const& ReturnTypeInfoIncompletePP() { return typeid(IncompleteAtThrow**); } 74 75 struct CompleteAtThrow {}; 76 void ThrowCompleteMP() { throw (int CompleteAtThrow::*)nullptr; } 77 void ThrowCompletePP() { throw (CompleteAtThrow**)nullptr; } 78 void ThrowCompletePMP() { throw (int CompleteAtThrow::**)nullptr; } 79 std::type_info const& ReturnTypeInfoCompleteMP() { return typeid(int CompleteAtThrow::*); } 80 std::type_info const& ReturnTypeInfoCompletePP() { return typeid(CompleteAtThrow**); } 81 82 void ThrowNullptr() { throw nullptr; } 83 84 #else 85 86 struct IncompleteAtThrow {}; 87 88 int main(int, char**) { 89 AssertIncompleteTypeInfoEquals(ReturnTypeInfoNeverDefinedMP(), typeid(int NeverDefined::*)); 90 try { 91 ThrowNeverDefinedMP(); 92 assert(false); 93 } catch (int IncompleteAtThrow::*) { 94 assert(false); 95 } catch (int CompleteAtThrow::*) { 96 assert(false); 97 } catch (int NeverDefined::*p) { 98 assert(!p); 99 } 100 catch(...) { assert(!"FAIL: Didn't catch NeverDefined::*" ); } 101 102 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompleteMP(), typeid(int IncompleteAtThrow::*)); 103 try { 104 ThrowIncompleteMP(); 105 assert(false); 106 } catch (CompleteAtThrow**) { 107 assert(false); 108 } catch (int CompleteAtThrow::*) { 109 assert(false); 110 } catch (IncompleteAtThrow**) { 111 assert(false); 112 } catch (int IncompleteAtThrow::*p) { 113 assert(!p); 114 } 115 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::*" ); } 116 117 AssertIncompleteTypeInfoEquals(ReturnTypeInfoIncompletePP(), typeid(IncompleteAtThrow**)); 118 try { 119 ThrowIncompletePP(); 120 assert(false); 121 } catch (int IncompleteAtThrow::*) { 122 assert(false); 123 } catch (IncompleteAtThrow** p) { 124 assert(!p); 125 } 126 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow**" ); } 127 128 try { 129 ThrowIncompletePMP(); 130 assert(false); 131 } catch (int IncompleteAtThrow::*) { 132 assert(false); 133 } catch (IncompleteAtThrow**) { 134 assert(false); 135 } catch (int IncompleteAtThrow::**p) { 136 assert(!p); 137 } 138 catch(...) { assert(!"FAIL: Didn't catch IncompleteAtThrow::**" ); } 139 140 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompleteMP(), typeid(int CompleteAtThrow::*)); 141 try { 142 ThrowCompleteMP(); 143 assert(false); 144 } catch (IncompleteAtThrow**) { 145 assert(false); 146 } catch (int IncompleteAtThrow::*) { 147 assert(false); 148 } catch (CompleteAtThrow**) { 149 assert(false); 150 } catch (int CompleteAtThrow::*p) { 151 assert(!p); 152 } 153 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::" ); } 154 155 AssertIncompleteTypeInfoEquals(ReturnTypeInfoCompletePP(), typeid(CompleteAtThrow**)); 156 try { 157 ThrowCompletePP(); 158 assert(false); 159 } catch (IncompleteAtThrow**) { 160 assert(false); 161 } catch (int IncompleteAtThrow::*) { 162 assert(false); 163 } catch (int CompleteAtThrow::*) { 164 assert(false); 165 } catch (CompleteAtThrow**p) { 166 assert(!p); 167 } 168 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow**" ); } 169 170 try { 171 ThrowCompletePMP(); 172 assert(false); 173 } catch (IncompleteAtThrow**) { 174 assert(false); 175 } catch (int IncompleteAtThrow::*) { 176 assert(false); 177 } catch (int CompleteAtThrow::*) { 178 assert(false); 179 } catch (CompleteAtThrow**) { 180 assert(false); 181 } catch (int CompleteAtThrow::**p) { 182 assert(!p); 183 } 184 catch(...) { assert(!"FAIL: Didn't catch CompleteAtThrow::**" ); } 185 186 #if __cplusplus >= 201103L 187 // Catch nullptr as complete type 188 try { 189 ThrowNullptr(); 190 } catch (int IncompleteAtThrow::*p) { 191 assert(!p); 192 } 193 catch(...) { assert(!"FAIL: Didn't catch nullptr as IncompleteAtThrow::*" ); } 194 195 // Catch nullptr as an incomplete type 196 try { 197 ThrowNullptr(); 198 } catch (int CompleteAtThrow::*p) { 199 assert(!p); 200 } 201 catch(...) { assert(!"FAIL: Didn't catch nullptr as CompleteAtThrow::*" ); } 202 203 // Catch nullptr as a type that is never complete. 204 try { 205 ThrowNullptr(); 206 } catch (int NeverDefined::*p) { 207 assert(!p); 208 } 209 catch(...) { assert(!"FAIL: Didn't catch nullptr as NeverDefined::*" ); } 210 #endif 211 212 return 0; 213 } 214 #endif 215