Richard Smith | 2f984ca | 2016-07-19 20:19:37 | [diff] [blame] | 1 | //===--------------------- catch_pointer_nullptr.cpp ----------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [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 |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Louis Dionne | 31cbe0f | 2020-06-01 14:38:23 | [diff] [blame] | 9 | // UNSUPPORTED: c++03, |
Louis Dionne | 8c61114 | 2020-04-17 14:29:15 | [diff] [blame] | 10 | // UNSUPPORTED: no-exceptions |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 | [diff] [blame] | 11 | |
| 12 | #include <cassert> |
| 13 | #include <cstdlib> |
| 14 | |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 | [diff] [blame] | 15 | struct A {}; |
| 16 | |
| 17 | template<typename T, bool CanCatchNullptr> |
| 18 | static void catch_nullptr_test() { |
| 19 | try { |
| 20 | throw nullptr; |
| 21 | } catch (T &p) { |
Eric Fiselier | 35c8983 | 2017-01-20 19:34:19 | [diff] [blame] | 22 | assert(CanCatchNullptr && !static_cast<bool>(p)); |
Richard Smith | 2f984ca | 2016-07-19 20:19:37 | [diff] [blame] | 23 | } catch (...) { |
| 24 | assert(!CanCatchNullptr); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | using nullptr_t = decltype(nullptr); |
| 31 | |
| 32 | // A reference to nullptr_t can catch nullptr. |
| 33 | catch_nullptr_test<nullptr_t, true>(); |
| 34 | catch_nullptr_test<const nullptr_t, true>(); |
| 35 | catch_nullptr_test<volatile nullptr_t, true>(); |
| 36 | catch_nullptr_test<const volatile nullptr_t, true>(); |
| 37 | |
| 38 | // No other reference type can. |
| 39 | #if 0 |
| 40 | // FIXME: These tests fail, because the ABI provides no way for us to |
| 41 | // distinguish this from catching by value. |
| 42 | catch_nullptr_test<void *, false>(); |
| 43 | catch_nullptr_test<void * const, false>(); |
| 44 | catch_nullptr_test<int *, false>(); |
| 45 | catch_nullptr_test<A *, false>(); |
| 46 | catch_nullptr_test<int A::*, false>(); |
| 47 | catch_nullptr_test<int (A::*)(), false>(); |
| 48 | #endif |
| 49 | } |