1e434b34fSJonathan Roelofs //===--------------------- catch_pointer_nullptr.cpp ----------------------===// 2e434b34fSJonathan Roelofs // 3e434b34fSJonathan Roelofs // The LLVM Compiler Infrastructure 4e434b34fSJonathan Roelofs // 5e434b34fSJonathan Roelofs // This file is dual licensed under the MIT and the University of Illinois Open 6e434b34fSJonathan Roelofs // Source Licenses. See LICENSE.TXT for details. 7e434b34fSJonathan Roelofs // 8e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===// 9e434b34fSJonathan Roelofs 10*c79a8f77SEric Fiselier // UNSUPPORTED: c++98, c++03 11*c79a8f77SEric 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 } 24e434b34fSJonathan Roelofs catch (int*) 25e434b34fSJonathan Roelofs { 26e434b34fSJonathan Roelofs } 27e434b34fSJonathan Roelofs catch (long*) 28e434b34fSJonathan Roelofs { 29e434b34fSJonathan Roelofs assert(false); 30e434b34fSJonathan Roelofs } 31e434b34fSJonathan Roelofs } 32e434b34fSJonathan Roelofs 33e434b34fSJonathan Roelofs void test2() 34e434b34fSJonathan Roelofs { 35e434b34fSJonathan Roelofs try 36e434b34fSJonathan Roelofs { 37e434b34fSJonathan Roelofs throw nullptr; 38e434b34fSJonathan Roelofs assert(false); 39e434b34fSJonathan Roelofs } 40e434b34fSJonathan Roelofs catch (A*) 41e434b34fSJonathan Roelofs { 42e434b34fSJonathan Roelofs } 43e434b34fSJonathan Roelofs catch (int*) 44e434b34fSJonathan Roelofs { 45e434b34fSJonathan Roelofs assert(false); 46e434b34fSJonathan Roelofs } 47e434b34fSJonathan Roelofs } 48e434b34fSJonathan Roelofs 491b00fc5dSEric Fiselier template <class Catch> 501b00fc5dSEric Fiselier void catch_nullptr_test() { 511b00fc5dSEric Fiselier try { 521b00fc5dSEric Fiselier throw nullptr; 531b00fc5dSEric Fiselier assert(false); 541b00fc5dSEric Fiselier } catch (Catch) { 551b00fc5dSEric Fiselier // nothing todo 561b00fc5dSEric Fiselier } catch (...) { 571b00fc5dSEric Fiselier assert(false); 581b00fc5dSEric Fiselier } 591b00fc5dSEric Fiselier } 601b00fc5dSEric Fiselier 61e434b34fSJonathan Roelofs 62e434b34fSJonathan Roelofs int main() 63e434b34fSJonathan Roelofs { 641b00fc5dSEric Fiselier // catch naked nullptrs 65e434b34fSJonathan Roelofs test1(); 66e434b34fSJonathan Roelofs test2(); 671b00fc5dSEric Fiselier 681b00fc5dSEric Fiselier catch_nullptr_test<int*>(); 691b00fc5dSEric Fiselier catch_nullptr_test<int**>(); 701b00fc5dSEric Fiselier catch_nullptr_test<int A::*>(); 711b00fc5dSEric Fiselier catch_nullptr_test<const int A::*>(); 721b00fc5dSEric Fiselier catch_nullptr_test<int A::**>(); 73e434b34fSJonathan Roelofs } 74