blob: b969119b0d88fa8f03d986a1f4a379528a663973 [file] [log] [blame]
Howard Hinnantebe45052012-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
10#include <cassert>
Eric Fiselier0cb62d12015-04-02 23:26:37 +000011#include <cstdlib>
12
13#ifndef __has_feature
14#define __has_feature(x) 0
15#endif
16
17struct A {};
Howard Hinnantebe45052012-02-01 21:01:52 +000018
19#if __has_feature(cxx_nullptr)
20
21void test1()
22{
23 try
24 {
25 throw nullptr;
26 assert(false);
27 }
28 catch (int*)
29 {
30 }
31 catch (long*)
32 {
33 assert(false);
34 }
35}
36
Howard Hinnantebe45052012-02-01 21:01:52 +000037void test2()
38{
39 try
40 {
41 throw nullptr;
42 assert(false);
43 }
44 catch (A*)
45 {
46 }
47 catch (int*)
48 {
49 assert(false);
50 }
51}
52
Eric Fiselier0cb62d12015-04-02 23:26:37 +000053template <class Catch>
54void catch_nullptr_test() {
55 try {
56 throw nullptr;
57 assert(false);
58 } catch (Catch) {
59 // nothing todo
60 } catch (...) {
61 assert(false);
62 }
63}
64
Howard Hinnantebe45052012-02-01 21:01:52 +000065#else
66
67void test1()
68{
69}
70
71void test2()
72{
73}
74
Eric Fiselier0cb62d12015-04-02 23:26:37 +000075template <class Catch>
76void catch_nullptr_test()
77{
78}
79
Howard Hinnantebe45052012-02-01 21:01:52 +000080#endif
81
82int main()
83{
Eric Fiselier0cb62d12015-04-02 23:26:37 +000084 // catch naked nullptrs
85 test1();
86 test2();
87
88 catch_nullptr_test<int*>();
89 catch_nullptr_test<int**>();
90 catch_nullptr_test<int A::*>();
91 catch_nullptr_test<const int A::*>();
92 catch_nullptr_test<int A::**>();
Howard Hinnantebe45052012-02-01 21:01:52 +000093}