Howard Hinnant | c325fa7 | 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 | |
Eric Fiselier | c79a8f7 | 2015-08-20 01:22:17 +0000 | [diff] [blame] | 10 | // UNSUPPORTED: c++98, c++03 |
| 11 | |
Howard Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 12 | #include <cassert> |
Eric Fiselier | 1b00fc5 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 13 | #include <cstdlib> |
| 14 | |
Eric Fiselier | 1b00fc5 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 15 | struct A {}; |
Howard Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 16 | |
Howard Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 17 | void 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 Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 33 | void 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 Fiselier | 1b00fc5 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 49 | template <class Catch> |
| 50 | void 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 Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 61 | |
| 62 | int main() |
| 63 | { |
Eric Fiselier | 1b00fc5 | 2015-04-02 23:26:37 +0000 | [diff] [blame] | 64 | // 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 Hinnant | c325fa7 | 2012-02-01 21:01:52 +0000 | [diff] [blame] | 73 | } |