Richard Smith | 081ea86 | 2016-07-19 20:19:37 +0000 | [diff] [blame] | 1 | //===--------------------- catch_pointer_nullptr.cpp ----------------------===// |
| 2 | // |
| 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 | |
| 10 | // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions |
| 11 | |
| 12 | #include <cassert> |
| 13 | #include <cstdlib> |
| 14 | |
Richard Smith | 081ea86 | 2016-07-19 20:19:37 +0000 | [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 | dc0e3f7 | 2017-01-20 19:34:19 +0000 | [diff] [blame] | 22 | assert(CanCatchNullptr && !static_cast<bool>(p)); |
Richard Smith | 081ea86 | 2016-07-19 20:19:37 +0000 | [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 | } |