1e434b34fSJonathan Roelofs //===--------------------- catch_pointer_nullptr.cpp ----------------------===//
2e434b34fSJonathan Roelofs //
3e434b34fSJonathan Roelofs //                     The LLVM Compiler Infrastructure
4e434b34fSJonathan Roelofs //
5e434b34fSJonathan Roelofs // This file is dual licensed under the MIT and the University of Illinois Open
6e434b34fSJonathan Roelofs // Source Licenses. See LICENSE.TXT for details.
7e434b34fSJonathan Roelofs //
8e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
9e434b34fSJonathan Roelofs 
1057e446daSAsiri Rathnayake // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions
11c79a8f77SEric Fiselier 
12e434b34fSJonathan Roelofs #include <cassert>
131b00fc5dSEric Fiselier #include <cstdlib>
141b00fc5dSEric Fiselier 
151b00fc5dSEric Fiselier struct A {};
16e434b34fSJonathan Roelofs 
17e434b34fSJonathan Roelofs void test1()
18e434b34fSJonathan Roelofs {
19e434b34fSJonathan Roelofs     try
20e434b34fSJonathan Roelofs     {
21e434b34fSJonathan Roelofs         throw nullptr;
22e434b34fSJonathan Roelofs         assert(false);
23e434b34fSJonathan Roelofs     }
24*2f984cabSRichard Smith     catch (int* p)
25e434b34fSJonathan Roelofs     {
26*2f984cabSRichard Smith         assert(!p);
27e434b34fSJonathan Roelofs     }
28e434b34fSJonathan Roelofs     catch (long*)
29e434b34fSJonathan Roelofs     {
30e434b34fSJonathan Roelofs         assert(false);
31e434b34fSJonathan Roelofs     }
32e434b34fSJonathan Roelofs }
33e434b34fSJonathan Roelofs 
34e434b34fSJonathan Roelofs void test2()
35e434b34fSJonathan Roelofs {
36e434b34fSJonathan Roelofs     try
37e434b34fSJonathan Roelofs     {
38e434b34fSJonathan Roelofs         throw nullptr;
39e434b34fSJonathan Roelofs         assert(false);
40e434b34fSJonathan Roelofs     }
41*2f984cabSRichard Smith     catch (A* p)
42e434b34fSJonathan Roelofs     {
43*2f984cabSRichard Smith         assert(!p);
44e434b34fSJonathan Roelofs     }
45e434b34fSJonathan Roelofs     catch (int*)
46e434b34fSJonathan Roelofs     {
47e434b34fSJonathan Roelofs         assert(false);
48e434b34fSJonathan Roelofs     }
49e434b34fSJonathan Roelofs }
50e434b34fSJonathan Roelofs 
511b00fc5dSEric Fiselier template <class Catch>
521b00fc5dSEric Fiselier void catch_nullptr_test() {
531b00fc5dSEric Fiselier   try {
541b00fc5dSEric Fiselier     throw nullptr;
551b00fc5dSEric Fiselier     assert(false);
56*2f984cabSRichard Smith   } catch (Catch c) {
57*2f984cabSRichard Smith     assert(!c);
581b00fc5dSEric Fiselier   } catch (...) {
591b00fc5dSEric Fiselier     assert(false);
601b00fc5dSEric Fiselier   }
611b00fc5dSEric Fiselier }
621b00fc5dSEric Fiselier 
63e434b34fSJonathan Roelofs 
64e434b34fSJonathan Roelofs int main()
65e434b34fSJonathan Roelofs {
661b00fc5dSEric Fiselier   // catch naked nullptrs
67e434b34fSJonathan Roelofs   test1();
68e434b34fSJonathan Roelofs   test2();
691b00fc5dSEric Fiselier 
701b00fc5dSEric Fiselier   catch_nullptr_test<int*>();
711b00fc5dSEric Fiselier   catch_nullptr_test<int**>();
721b00fc5dSEric Fiselier   catch_nullptr_test<int A::*>();
731b00fc5dSEric Fiselier   catch_nullptr_test<const int A::*>();
741b00fc5dSEric Fiselier   catch_nullptr_test<int A::**>();
75e434b34fSJonathan Roelofs }
76