blob: 2389ba920f8939e6422cedb03bdf9bea80f1375a [file] [log] [blame]
Nico Weber6ceed442013-12-05 21:54:49 +00001//===--------------------- catch_const_pointer_nullptr.cpp ----------------===//
Marshall Clowa27b0cd2012-02-01 22:27:24 +00002//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clowa27b0cd2012-02-01 22:27:24 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne8c611142020-04-17 10:29:15 -04009// UNSUPPORTED: no-exceptions
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000010
Marshall Clowa27b0cd2012-02-01 22:27:24 +000011#include <cassert>
12
Eric Fiselierf5ff11c2016-06-15 19:07:19 +000013// Clang emits warnings about exceptions of type 'Child' being caught by
14// an earlier handler of type 'Base'. Congrats clang, you've just
15// diagnosed the behavior under test.
16#if defined(__clang__)
17#pragma clang diagnostic ignored "-Wexceptions"
18#endif
19
Marshall Clowa27b0cd2012-02-01 22:27:24 +000020#if __has_feature(cxx_nullptr)
21
22struct A {};
23
24void test1()
25{
26 try
27 {
28 throw nullptr;
29 assert(false);
30 }
Richard Smith2f984ca2016-07-19 20:19:37 +000031 catch (A* p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +000032 {
Richard Smith2f984ca2016-07-19 20:19:37 +000033 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +000034 }
35 catch (const A*)
36 {
37 assert(false);
38 }
39}
40
41
42void test2()
43{
44 try
45 {
46 throw nullptr;
47 assert(false);
48 }
Richard Smith2f984ca2016-07-19 20:19:37 +000049 catch (const A* p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +000050 {
Richard Smith2f984ca2016-07-19 20:19:37 +000051 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +000052 }
53 catch (A*)
54 {
55 assert(false);
56 }
57}
58
59void test3()
60{
61 try
62 {
63 throw nullptr;
64 assert(false);
65 }
Richard Smith2f984ca2016-07-19 20:19:37 +000066 catch (const A* const p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +000067 {
Richard Smith2f984ca2016-07-19 20:19:37 +000068 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +000069 }
70 catch (A*)
71 {
72 assert(false);
73 }
74}
75
76void test4()
77{
78 try
79 {
80 throw nullptr;
81 assert(false);
82 }
Richard Smith2f984ca2016-07-19 20:19:37 +000083 catch (A* p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +000084 {
Richard Smith2f984ca2016-07-19 20:19:37 +000085 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +000086 }
Marshall Clow350bda62013-12-04 05:39:55 +000087 catch (const A* const)
Marshall Clowa27b0cd2012-02-01 22:27:24 +000088 {
89 assert(false);
90 }
91}
92
93void test5()
94{
95 try
96 {
97 throw nullptr;
98 assert(false);
99 }
Richard Smith2f984ca2016-07-19 20:19:37 +0000100 catch (A const* p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000101 {
Richard Smith2f984ca2016-07-19 20:19:37 +0000102 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000103 }
104 catch (A*)
105 {
106 assert(false);
107 }
108}
109
110void test6()
111{
112 try
113 {
114 throw nullptr;
115 assert(false);
116 }
Richard Smith2f984ca2016-07-19 20:19:37 +0000117 catch (A* p)
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000118 {
Richard Smith2f984ca2016-07-19 20:19:37 +0000119 assert(!p);
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000120 }
121 catch (A const*)
122 {
123 assert(false);
124 }
125}
126
127
128#else
129
130void test1() {}
131void test2() {}
132void test3() {}
133void test4() {}
134void test5() {}
135void test6() {}
136
137#endif
138
Louis Dionne504bc072020-10-08 13:36:33 -0400139int main(int, char**) {
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000140 test1();
141 test2();
142 test3();
143 test4();
144 test5();
145 test6();
Louis Dionne504bc072020-10-08 13:36:33 -0400146
147 return 0;
Marshall Clowa27b0cd2012-02-01 22:27:24 +0000148}