blob: 7caee7b3aa0e72174436d9beb24fe41ac6f49919 [file] [log] [blame]
Howard Hinnantc325fa72012-02-01 21:01:52 +00001//===--------------------- 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
Eric Fiselierc79a8f72015-08-20 01:22:17 +000010// UNSUPPORTED: c++98, c++03
11
Howard Hinnantc325fa72012-02-01 21:01:52 +000012#include <cassert>
Eric Fiselier1b00fc52015-04-02 23:26:37 +000013#include <cstdlib>
14
Eric Fiselier1b00fc52015-04-02 23:26:37 +000015struct A {};
Howard Hinnantc325fa72012-02-01 21:01:52 +000016
Howard Hinnantc325fa72012-02-01 21:01:52 +000017void test1()
18{
19 try
20 {
21 throw nullptr;
22 assert(false);
23 }
24 catch (int*)
25 {
26 }
27 catch (long*)
28 {
29 assert(false);
30 }
31}
32
Howard Hinnantc325fa72012-02-01 21:01:52 +000033void test2()
34{
35 try
36 {
37 throw nullptr;
38 assert(false);
39 }
40 catch (A*)
41 {
42 }
43 catch (int*)
44 {
45 assert(false);
46 }
47}
48
Eric Fiselier1b00fc52015-04-02 23:26:37 +000049template <class Catch>
50void catch_nullptr_test() {
51 try {
52 throw nullptr;
53 assert(false);
54 } catch (Catch) {
55 // nothing todo
56 } catch (...) {
57 assert(false);
58 }
59}
60
Howard Hinnantc325fa72012-02-01 21:01:52 +000061
62int main()
63{
Eric Fiselier1b00fc52015-04-02 23:26:37 +000064 // catch naked nullptrs
65 test1();
66 test2();
67
68 catch_nullptr_test<int*>();
69 catch_nullptr_test<int**>();
70 catch_nullptr_test<int A::*>();
71 catch_nullptr_test<const int A::*>();
72 catch_nullptr_test<int A::**>();
Howard Hinnantc325fa72012-02-01 21:01:52 +000073}