blob: ccf4405a37043e0598e505351aabd8b9f6ed6396 [file] [log] [blame]
Nico Webercfbfdd92013-12-05 21:54:49 +00001//===--------------------- catch_const_pointer_nullptr.cpp ----------------===//
Marshall Clowcf95bab2012-02-01 22:27:24 +00002//
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
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000010// UNSUPPORTED: libcxxabi-no-exceptions
11
Marshall Clowcf95bab2012-02-01 22:27:24 +000012#include <cassert>
13
Eric Fiselierdf448d82016-06-15 19:07:19 +000014// Clang emits warnings about exceptions of type 'Child' being caught by
15// an earlier handler of type 'Base'. Congrats clang, you've just
16// diagnosed the behavior under test.
17#if defined(__clang__)
18#pragma clang diagnostic ignored "-Wexceptions"
19#endif
20
Marshall Clowcf95bab2012-02-01 22:27:24 +000021#if __has_feature(cxx_nullptr)
22
23struct A {};
24
25void test1()
26{
27 try
28 {
29 throw nullptr;
30 assert(false);
31 }
Richard Smith081ea862016-07-19 20:19:37 +000032 catch (A* p)
Marshall Clowcf95bab2012-02-01 22:27:24 +000033 {
Richard Smith081ea862016-07-19 20:19:37 +000034 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +000035 }
36 catch (const A*)
37 {
38 assert(false);
39 }
40}
41
42
43void test2()
44{
45 try
46 {
47 throw nullptr;
48 assert(false);
49 }
Richard Smith081ea862016-07-19 20:19:37 +000050 catch (const A* p)
Marshall Clowcf95bab2012-02-01 22:27:24 +000051 {
Richard Smith081ea862016-07-19 20:19:37 +000052 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +000053 }
54 catch (A*)
55 {
56 assert(false);
57 }
58}
59
60void test3()
61{
62 try
63 {
64 throw nullptr;
65 assert(false);
66 }
Richard Smith081ea862016-07-19 20:19:37 +000067 catch (const A* const p)
Marshall Clowcf95bab2012-02-01 22:27:24 +000068 {
Richard Smith081ea862016-07-19 20:19:37 +000069 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +000070 }
71 catch (A*)
72 {
73 assert(false);
74 }
75}
76
77void test4()
78{
79 try
80 {
81 throw nullptr;
82 assert(false);
83 }
Richard Smith081ea862016-07-19 20:19:37 +000084 catch (A* p)
Marshall Clowcf95bab2012-02-01 22:27:24 +000085 {
Richard Smith081ea862016-07-19 20:19:37 +000086 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +000087 }
Marshall Clowc22004f2013-12-04 05:39:55 +000088 catch (const A* const)
Marshall Clowcf95bab2012-02-01 22:27:24 +000089 {
90 assert(false);
91 }
92}
93
94void test5()
95{
96 try
97 {
98 throw nullptr;
99 assert(false);
100 }
Richard Smith081ea862016-07-19 20:19:37 +0000101 catch (A const* p)
Marshall Clowcf95bab2012-02-01 22:27:24 +0000102 {
Richard Smith081ea862016-07-19 20:19:37 +0000103 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +0000104 }
105 catch (A*)
106 {
107 assert(false);
108 }
109}
110
111void test6()
112{
113 try
114 {
115 throw nullptr;
116 assert(false);
117 }
Richard Smith081ea862016-07-19 20:19:37 +0000118 catch (A* p)
Marshall Clowcf95bab2012-02-01 22:27:24 +0000119 {
Richard Smith081ea862016-07-19 20:19:37 +0000120 assert(!p);
Marshall Clowcf95bab2012-02-01 22:27:24 +0000121 }
122 catch (A const*)
123 {
124 assert(false);
125 }
126}
127
128
129#else
130
131void test1() {}
132void test2() {}
133void test3() {}
134void test4() {}
135void test5() {}
136void test6() {}
137
138#endif
139
140int main()
141{
142 test1();
143 test2();
144 test3();
145 test4();
146 test5();
147 test6();
148}