Howard Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 1 | //===--------------------- 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 Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame^] | 11 | #include <cstdlib> |
| 12 | |
| 13 | #ifndef __has_feature |
| 14 | #define __has_feature(x) 0 |
| 15 | #endif |
| 16 | |
| 17 | struct A {}; |
Howard Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 18 | |
| 19 | #if __has_feature(cxx_nullptr) |
| 20 | |
| 21 | void 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 Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 37 | void 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 Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame^] | 53 | template <class Catch> |
| 54 | void 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 Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 65 | #else |
| 66 | |
| 67 | void test1() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void test2() |
| 72 | { |
| 73 | } |
| 74 | |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame^] | 75 | template <class Catch> |
| 76 | void catch_nullptr_test() |
| 77 | { |
| 78 | } |
| 79 | |
Howard Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | int main() |
| 83 | { |
Eric Fiselier | 0cb62d1 | 2015-04-02 23:26:37 +0000 | [diff] [blame^] | 84 | // 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 Hinnant | ebe4505 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 93 | } |