1 //===--------------------- catch_pointer_nullptr.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
10 #include <cassert>
11 #include <cstdlib>
12 #include <iostream>
13
14 // Roll our own assertion macro to get better error messages out of the tests.
15 // In particular on systems that don't use __PRETTY_FUNCTION__ in assertions.
16 #define my_assert(pred, msg) do_assert(pred, msg, __LINE__, __PRETTY_FUNCTION__)
17
do_assert(bool assert_passed,const char * msg,int line,const char * func)18 void do_assert(bool assert_passed, const char* msg, int line, const char* func) {
19 if (assert_passed) return;
20 std::cerr << __FILE__ << ":" << line << " " << func
21 << ": Assertion Failed `" << msg << "'\n\n";
22 std::abort();
23 }
24
25 struct A {};
26 struct Base {};
27 struct Derived : public Base {};
28
29 template <class To>
test_conversion(To)30 bool test_conversion(To) { return true; }
31
32 template <class To>
test_conversion(...)33 bool test_conversion(...) { return false; }
34
35 template <class Pointer>
36 struct CreatePointer {
operator ()CreatePointer37 Pointer operator()() const {
38 return (Pointer)0;
39 }
40 };
41
42 template <class Tp>
43 struct CreatePointer<Tp*> {
operator ()CreatePointer44 Tp* operator()() const {
45 return (Tp*)42;
46 }
47 };
48
49 template <class Throw, class Catch>
catch_pointer_test()50 void catch_pointer_test() {
51 Throw throw_ptr = CreatePointer<Throw>()();
52 // Use the compiler to determine if the exception of type Throw can be
53 // implicitly converted to type Catch.
54 const bool can_convert = test_conversion<Catch>(throw_ptr);
55 try {
56 throw throw_ptr;
57 assert(false);
58 } catch (Catch catch_ptr) {
59 Catch catch2 = CreatePointer<Catch>()();
60 my_assert(can_convert, "non-convertible type incorrectly caught");
61 my_assert(catch_ptr == catch2,
62 "Thrown pointer does not match caught ptr");
63 } catch (...) {
64 my_assert(!can_convert, "convertible type incorrectly not caught");
65 }
66 }
67
68 // Generate CV qualified pointer typedefs.
69 template <class Tp, bool First = false>
70 struct TestTypes {
71 typedef Tp* Type;
72 typedef Tp const* CType;
73 typedef Tp volatile* VType;
74 typedef Tp const volatile* CVType;
75 };
76
77 // Special case for cv-qualifying a pointer-to-member without adding an extra
78 // pointer to it.
79 template <class Member, class Class>
80 struct TestTypes<Member Class::*, true> {
81 typedef Member (Class::*Type);
82 typedef const Member (Class::*CType);
83 typedef volatile Member (Class::*VType);
84 typedef const volatile Member (Class::*CVType);
85 };
86
87 template <class Throw, class Catch, int level, bool first = false>
88 struct generate_tests_imp {
89 typedef TestTypes<Throw, first> ThrowTypes;
90 typedef TestTypes<Catch, first> CatchTypes;
operator ()generate_tests_imp91 void operator()() {
92 typedef typename ThrowTypes::Type Type;
93 typedef typename ThrowTypes::CType CType;
94 typedef typename ThrowTypes::VType VType;
95 typedef typename ThrowTypes::CVType CVType;
96
97 run_catch_tests<Type>();
98 run_catch_tests<CType>();
99 run_catch_tests<VType>();
100 run_catch_tests<CVType>();
101 }
102
103 template <class ThrowTp>
run_catch_testsgenerate_tests_imp104 void run_catch_tests() {
105 typedef typename CatchTypes::Type Type;
106 typedef typename CatchTypes::CType CType;
107 typedef typename CatchTypes::VType VType;
108 typedef typename CatchTypes::CVType CVType;
109
110 catch_pointer_test<ThrowTp, Type>();
111 catch_pointer_test<ThrowTp, CType>();
112 catch_pointer_test<ThrowTp, VType>();
113 catch_pointer_test<ThrowTp, CVType>();
114
115 generate_tests_imp<ThrowTp, Type, level-1>()();
116 generate_tests_imp<ThrowTp, CType, level-1>()();
117 generate_tests_imp<ThrowTp, VType, level-1>()();
118 generate_tests_imp<ThrowTp, CVType, level-1>()();
119 }
120 };
121
122 template <class Throw, class Catch, bool first>
123 struct generate_tests_imp<Throw, Catch, 0, first> {
operator ()generate_tests_imp124 void operator()() {
125 catch_pointer_test<Throw, Catch>();
126 }
127 };
128
129 template <class Throw, class Catch, int level>
130 struct generate_tests : generate_tests_imp<Throw, Catch, level, true> {};
131
main()132 int main()
133 {
134 generate_tests<int, int, 3>()();
135 generate_tests<Base, Derived, 2>()();
136 generate_tests<Derived, Base, 2>()();
137 generate_tests<int, void, 2>()();
138 generate_tests<void, int, 2>()();
139
140 generate_tests<int A::*, int A::*, 3>()();
141 generate_tests<int A::*, void, 2>()();
142 generate_tests<void, int A::*, 2>()();
143 }
144