1e434b34fSJonathan Roelofs //===--------------------- catch_const_pointer_nullptr.cpp ----------------===//
2e434b34fSJonathan Roelofs //
3*57b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*57b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*57b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs 
957e446daSAsiri Rathnayake // UNSUPPORTED: libcxxabi-no-exceptions
1057e446daSAsiri Rathnayake 
11e434b34fSJonathan Roelofs #include <cassert>
12e434b34fSJonathan Roelofs 
13f5ff11c4SEric Fiselier // Clang emits  warnings about exceptions of type 'Child' being caught by
14f5ff11c4SEric Fiselier // an earlier handler of type 'Base'. Congrats clang, you've just
15f5ff11c4SEric Fiselier // diagnosed the behavior under test.
16f5ff11c4SEric Fiselier #if defined(__clang__)
17f5ff11c4SEric Fiselier #pragma clang diagnostic ignored "-Wexceptions"
18f5ff11c4SEric Fiselier #endif
19f5ff11c4SEric Fiselier 
20e434b34fSJonathan Roelofs #if __has_feature(cxx_nullptr)
21e434b34fSJonathan Roelofs 
22e434b34fSJonathan Roelofs struct A {};
23e434b34fSJonathan Roelofs 
24e434b34fSJonathan Roelofs void test1()
25e434b34fSJonathan Roelofs {
26e434b34fSJonathan Roelofs     try
27e434b34fSJonathan Roelofs     {
28e434b34fSJonathan Roelofs         throw nullptr;
29e434b34fSJonathan Roelofs         assert(false);
30e434b34fSJonathan Roelofs     }
312f984cabSRichard Smith     catch (A* p)
32e434b34fSJonathan Roelofs     {
332f984cabSRichard Smith         assert(!p);
34e434b34fSJonathan Roelofs     }
35e434b34fSJonathan Roelofs     catch (const A*)
36e434b34fSJonathan Roelofs     {
37e434b34fSJonathan Roelofs         assert(false);
38e434b34fSJonathan Roelofs     }
39e434b34fSJonathan Roelofs }
40e434b34fSJonathan Roelofs 
41e434b34fSJonathan Roelofs 
42e434b34fSJonathan Roelofs void test2()
43e434b34fSJonathan Roelofs {
44e434b34fSJonathan Roelofs     try
45e434b34fSJonathan Roelofs     {
46e434b34fSJonathan Roelofs         throw nullptr;
47e434b34fSJonathan Roelofs         assert(false);
48e434b34fSJonathan Roelofs     }
492f984cabSRichard Smith     catch (const A* p)
50e434b34fSJonathan Roelofs     {
512f984cabSRichard Smith         assert(!p);
52e434b34fSJonathan Roelofs     }
53e434b34fSJonathan Roelofs     catch (A*)
54e434b34fSJonathan Roelofs     {
55e434b34fSJonathan Roelofs         assert(false);
56e434b34fSJonathan Roelofs     }
57e434b34fSJonathan Roelofs }
58e434b34fSJonathan Roelofs 
59e434b34fSJonathan Roelofs void test3()
60e434b34fSJonathan Roelofs {
61e434b34fSJonathan Roelofs     try
62e434b34fSJonathan Roelofs     {
63e434b34fSJonathan Roelofs         throw nullptr;
64e434b34fSJonathan Roelofs         assert(false);
65e434b34fSJonathan Roelofs     }
662f984cabSRichard Smith     catch (const A* const p)
67e434b34fSJonathan Roelofs     {
682f984cabSRichard Smith         assert(!p);
69e434b34fSJonathan Roelofs     }
70e434b34fSJonathan Roelofs     catch (A*)
71e434b34fSJonathan Roelofs     {
72e434b34fSJonathan Roelofs         assert(false);
73e434b34fSJonathan Roelofs     }
74e434b34fSJonathan Roelofs }
75e434b34fSJonathan Roelofs 
76e434b34fSJonathan Roelofs void test4()
77e434b34fSJonathan Roelofs {
78e434b34fSJonathan Roelofs     try
79e434b34fSJonathan Roelofs     {
80e434b34fSJonathan Roelofs         throw nullptr;
81e434b34fSJonathan Roelofs         assert(false);
82e434b34fSJonathan Roelofs     }
832f984cabSRichard Smith     catch (A* p)
84e434b34fSJonathan Roelofs     {
852f984cabSRichard Smith         assert(!p);
86e434b34fSJonathan Roelofs     }
87e434b34fSJonathan Roelofs     catch (const A* const)
88e434b34fSJonathan Roelofs     {
89e434b34fSJonathan Roelofs         assert(false);
90e434b34fSJonathan Roelofs     }
91e434b34fSJonathan Roelofs }
92e434b34fSJonathan Roelofs 
93e434b34fSJonathan Roelofs void test5()
94e434b34fSJonathan Roelofs {
95e434b34fSJonathan Roelofs     try
96e434b34fSJonathan Roelofs     {
97e434b34fSJonathan Roelofs         throw nullptr;
98e434b34fSJonathan Roelofs         assert(false);
99e434b34fSJonathan Roelofs     }
1002f984cabSRichard Smith     catch (A const* p)
101e434b34fSJonathan Roelofs     {
1022f984cabSRichard Smith         assert(!p);
103e434b34fSJonathan Roelofs     }
104e434b34fSJonathan Roelofs     catch (A*)
105e434b34fSJonathan Roelofs     {
106e434b34fSJonathan Roelofs         assert(false);
107e434b34fSJonathan Roelofs     }
108e434b34fSJonathan Roelofs }
109e434b34fSJonathan Roelofs 
110e434b34fSJonathan Roelofs void test6()
111e434b34fSJonathan Roelofs {
112e434b34fSJonathan Roelofs     try
113e434b34fSJonathan Roelofs     {
114e434b34fSJonathan Roelofs         throw nullptr;
115e434b34fSJonathan Roelofs         assert(false);
116e434b34fSJonathan Roelofs     }
1172f984cabSRichard Smith     catch (A* p)
118e434b34fSJonathan Roelofs     {
1192f984cabSRichard Smith         assert(!p);
120e434b34fSJonathan Roelofs     }
121e434b34fSJonathan Roelofs     catch (A const*)
122e434b34fSJonathan Roelofs     {
123e434b34fSJonathan Roelofs         assert(false);
124e434b34fSJonathan Roelofs     }
125e434b34fSJonathan Roelofs }
126e434b34fSJonathan Roelofs 
127e434b34fSJonathan Roelofs 
128e434b34fSJonathan Roelofs #else
129e434b34fSJonathan Roelofs 
130e434b34fSJonathan Roelofs void test1() {}
131e434b34fSJonathan Roelofs void test2() {}
132e434b34fSJonathan Roelofs void test3() {}
133e434b34fSJonathan Roelofs void test4() {}
134e434b34fSJonathan Roelofs void test5() {}
135e434b34fSJonathan Roelofs void test6() {}
136e434b34fSJonathan Roelofs 
137e434b34fSJonathan Roelofs #endif
138e434b34fSJonathan Roelofs 
139e434b34fSJonathan Roelofs int main()
140e434b34fSJonathan Roelofs {
141e434b34fSJonathan Roelofs     test1();
142e434b34fSJonathan Roelofs     test2();
143e434b34fSJonathan Roelofs     test3();
144e434b34fSJonathan Roelofs     test4();
145e434b34fSJonathan Roelofs     test5();
146e434b34fSJonathan Roelofs     test6();
147e434b34fSJonathan Roelofs }
148