1e434b34fSJonathan Roelofs //===--------------------- catch_pointer_nullptr.cpp ----------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03
108c61114cSLouis Dionne // UNSUPPORTED: 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     }
242f984cabSRichard Smith     catch (int* p)
25e434b34fSJonathan Roelofs     {
262f984cabSRichard 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     }
412f984cabSRichard Smith     catch (A* p)
42e434b34fSJonathan Roelofs     {
432f984cabSRichard 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);
562f984cabSRichard Smith   } catch (Catch c) {
572f984cabSRichard Smith     assert(!c);
581b00fc5dSEric Fiselier   } catch (...) {
591b00fc5dSEric Fiselier     assert(false);
601b00fc5dSEric Fiselier   }
611b00fc5dSEric Fiselier }
621b00fc5dSEric Fiselier 
63e434b34fSJonathan Roelofs 
64*504bc07dSLouis Dionne int main(int, char**)
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::**>();
75*504bc07dSLouis Dionne 
76*504bc07dSLouis Dionne   return 0;
77e434b34fSJonathan Roelofs }
78