blob: 5718bff8f7fb643e29efe580d4d5d65f760c1448 [file] [log] [blame]
Richard Smith2f984ca2016-07-19 20:19:371//===--------------------- catch_pointer_nullptr.cpp ----------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:403// 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 Smith2f984ca2016-07-19 20:19:376//
7//===----------------------------------------------------------------------===//
8
Louis Dionne31cbe0f2020-06-01 14:38:239// UNSUPPORTED: c++03,
Louis Dionne8c611142020-04-17 14:29:1510// UNSUPPORTED: no-exceptions
Richard Smith2f984ca2016-07-19 20:19:3711
12#include <cassert>
13#include <cstdlib>
14
Richard Smith2f984ca2016-07-19 20:19:3715struct A {};
16
17template<typename T, bool CanCatchNullptr>
18static void catch_nullptr_test() {
19 try {
20 throw nullptr;
21 } catch (T &p) {
Eric Fiselier35c89832017-01-20 19:34:1922 assert(CanCatchNullptr && !static_cast<bool>(p));
Richard Smith2f984ca2016-07-19 20:19:3723 } catch (...) {
24 assert(!CanCatchNullptr);
25 }
26}
27
28int 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}