1*c05d8e5dSAndroid Build Coastguard Worker //===--------------------- catch_pointer_nullptr.cpp ----------------------===//
2*c05d8e5dSAndroid Build Coastguard Worker //
3*c05d8e5dSAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*c05d8e5dSAndroid Build Coastguard Worker //
5*c05d8e5dSAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*c05d8e5dSAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*c05d8e5dSAndroid Build Coastguard Worker //
8*c05d8e5dSAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*c05d8e5dSAndroid Build Coastguard Worker
10*c05d8e5dSAndroid Build Coastguard Worker // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions
11*c05d8e5dSAndroid Build Coastguard Worker
12*c05d8e5dSAndroid Build Coastguard Worker #include <cassert>
13*c05d8e5dSAndroid Build Coastguard Worker #include <cstdlib>
14*c05d8e5dSAndroid Build Coastguard Worker
15*c05d8e5dSAndroid Build Coastguard Worker struct A {};
16*c05d8e5dSAndroid Build Coastguard Worker
17*c05d8e5dSAndroid Build Coastguard Worker template<typename T, bool CanCatchNullptr>
catch_nullptr_test()18*c05d8e5dSAndroid Build Coastguard Worker static void catch_nullptr_test() {
19*c05d8e5dSAndroid Build Coastguard Worker try {
20*c05d8e5dSAndroid Build Coastguard Worker throw nullptr;
21*c05d8e5dSAndroid Build Coastguard Worker } catch (T &p) {
22*c05d8e5dSAndroid Build Coastguard Worker assert(CanCatchNullptr && !static_cast<bool>(p));
23*c05d8e5dSAndroid Build Coastguard Worker } catch (...) {
24*c05d8e5dSAndroid Build Coastguard Worker assert(!CanCatchNullptr);
25*c05d8e5dSAndroid Build Coastguard Worker }
26*c05d8e5dSAndroid Build Coastguard Worker }
27*c05d8e5dSAndroid Build Coastguard Worker
main()28*c05d8e5dSAndroid Build Coastguard Worker int main()
29*c05d8e5dSAndroid Build Coastguard Worker {
30*c05d8e5dSAndroid Build Coastguard Worker using nullptr_t = decltype(nullptr);
31*c05d8e5dSAndroid Build Coastguard Worker
32*c05d8e5dSAndroid Build Coastguard Worker // A reference to nullptr_t can catch nullptr.
33*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<nullptr_t, true>();
34*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<const nullptr_t, true>();
35*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<volatile nullptr_t, true>();
36*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<const volatile nullptr_t, true>();
37*c05d8e5dSAndroid Build Coastguard Worker
38*c05d8e5dSAndroid Build Coastguard Worker // No other reference type can.
39*c05d8e5dSAndroid Build Coastguard Worker #if 0
40*c05d8e5dSAndroid Build Coastguard Worker // FIXME: These tests fail, because the ABI provides no way for us to
41*c05d8e5dSAndroid Build Coastguard Worker // distinguish this from catching by value.
42*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<void *, false>();
43*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<void * const, false>();
44*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<int *, false>();
45*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<A *, false>();
46*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<int A::*, false>();
47*c05d8e5dSAndroid Build Coastguard Worker catch_nullptr_test<int (A::*)(), false>();
48*c05d8e5dSAndroid Build Coastguard Worker #endif
49*c05d8e5dSAndroid Build Coastguard Worker }
50