Nico Weber | 6ceed44 | 2013-12-05 21:54:49 +0000 | [diff] [blame] | 1 | //===--------------------- catch_const_pointer_nullptr.cpp ----------------===// |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 | ||||
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 6 | // |
7 | //===----------------------------------------------------------------------===// | ||||
8 | |||||
Louis Dionne | 8c61114 | 2020-04-17 10:29:15 -0400 | [diff] [blame] | 9 | // UNSUPPORTED: no-exceptions |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 10 | |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 11 | #include <cassert> |
12 | |||||
Eric Fiselier | f5ff11c | 2016-06-15 19:07:19 +0000 | [diff] [blame] | 13 | // 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 Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 20 | #if __has_feature(cxx_nullptr) |
21 | |||||
22 | struct A {}; | ||||
23 | |||||
24 | void test1() | ||||
25 | { | ||||
26 | try | ||||
27 | { | ||||
28 | throw nullptr; | ||||
29 | assert(false); | ||||
30 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 31 | catch (A* p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 32 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 33 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 34 | } |
35 | catch (const A*) | ||||
36 | { | ||||
37 | assert(false); | ||||
38 | } | ||||
39 | } | ||||
40 | |||||
41 | |||||
42 | void test2() | ||||
43 | { | ||||
44 | try | ||||
45 | { | ||||
46 | throw nullptr; | ||||
47 | assert(false); | ||||
48 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 49 | catch (const A* p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 50 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 51 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 52 | } |
53 | catch (A*) | ||||
54 | { | ||||
55 | assert(false); | ||||
56 | } | ||||
57 | } | ||||
58 | |||||
59 | void test3() | ||||
60 | { | ||||
61 | try | ||||
62 | { | ||||
63 | throw nullptr; | ||||
64 | assert(false); | ||||
65 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 66 | catch (const A* const p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 67 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 68 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 69 | } |
70 | catch (A*) | ||||
71 | { | ||||
72 | assert(false); | ||||
73 | } | ||||
74 | } | ||||
75 | |||||
76 | void test4() | ||||
77 | { | ||||
78 | try | ||||
79 | { | ||||
80 | throw nullptr; | ||||
81 | assert(false); | ||||
82 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 83 | catch (A* p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 84 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 85 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 86 | } |
Marshall Clow | 350bda6 | 2013-12-04 05:39:55 +0000 | [diff] [blame] | 87 | catch (const A* const) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 88 | { |
89 | assert(false); | ||||
90 | } | ||||
91 | } | ||||
92 | |||||
93 | void test5() | ||||
94 | { | ||||
95 | try | ||||
96 | { | ||||
97 | throw nullptr; | ||||
98 | assert(false); | ||||
99 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 100 | catch (A const* p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 101 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 102 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 103 | } |
104 | catch (A*) | ||||
105 | { | ||||
106 | assert(false); | ||||
107 | } | ||||
108 | } | ||||
109 | |||||
110 | void test6() | ||||
111 | { | ||||
112 | try | ||||
113 | { | ||||
114 | throw nullptr; | ||||
115 | assert(false); | ||||
116 | } | ||||
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 117 | catch (A* p) |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 118 | { |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 119 | assert(!p); |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 120 | } |
121 | catch (A const*) | ||||
122 | { | ||||
123 | assert(false); | ||||
124 | } | ||||
125 | } | ||||
126 | |||||
127 | |||||
128 | #else | ||||
129 | |||||
130 | void test1() {} | ||||
131 | void test2() {} | ||||
132 | void test3() {} | ||||
133 | void test4() {} | ||||
134 | void test5() {} | ||||
135 | void test6() {} | ||||
136 | |||||
137 | #endif | ||||
138 | |||||
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 139 | int main(int, char**) { |
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 140 | test1(); |
141 | test2(); | ||||
142 | test3(); | ||||
143 | test4(); | ||||
144 | test5(); | ||||
145 | test6(); | ||||
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 146 | |
147 | return 0; | ||||
Marshall Clow | a27b0cd | 2012-02-01 22:27:24 +0000 | [diff] [blame] | 148 | } |