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 10e434b34fSJonathan Roelofs #include <cassert> 11*1b00fc5dSEric Fiselier #include <cstdlib> 12*1b00fc5dSEric Fiselier 13*1b00fc5dSEric Fiselier #ifndef __has_feature 14*1b00fc5dSEric Fiselier #define __has_feature(x) 0 15*1b00fc5dSEric Fiselier #endif 16*1b00fc5dSEric Fiselier 17*1b00fc5dSEric Fiselier struct A {}; 18e434b34fSJonathan Roelofs 19e434b34fSJonathan Roelofs #if __has_feature(cxx_nullptr) 20e434b34fSJonathan Roelofs 21e434b34fSJonathan Roelofs void test1() 22e434b34fSJonathan Roelofs { 23e434b34fSJonathan Roelofs try 24e434b34fSJonathan Roelofs { 25e434b34fSJonathan Roelofs throw nullptr; 26e434b34fSJonathan Roelofs assert(false); 27e434b34fSJonathan Roelofs } 28e434b34fSJonathan Roelofs catch (int*) 29e434b34fSJonathan Roelofs { 30e434b34fSJonathan Roelofs } 31e434b34fSJonathan Roelofs catch (long*) 32e434b34fSJonathan Roelofs { 33e434b34fSJonathan Roelofs assert(false); 34e434b34fSJonathan Roelofs } 35e434b34fSJonathan Roelofs } 36e434b34fSJonathan Roelofs 37e434b34fSJonathan Roelofs void test2() 38e434b34fSJonathan Roelofs { 39e434b34fSJonathan Roelofs try 40e434b34fSJonathan Roelofs { 41e434b34fSJonathan Roelofs throw nullptr; 42e434b34fSJonathan Roelofs assert(false); 43e434b34fSJonathan Roelofs } 44e434b34fSJonathan Roelofs catch (A*) 45e434b34fSJonathan Roelofs { 46e434b34fSJonathan Roelofs } 47e434b34fSJonathan Roelofs catch (int*) 48e434b34fSJonathan Roelofs { 49e434b34fSJonathan Roelofs assert(false); 50e434b34fSJonathan Roelofs } 51e434b34fSJonathan Roelofs } 52e434b34fSJonathan Roelofs 53*1b00fc5dSEric Fiselier template <class Catch> 54*1b00fc5dSEric Fiselier void catch_nullptr_test() { 55*1b00fc5dSEric Fiselier try { 56*1b00fc5dSEric Fiselier throw nullptr; 57*1b00fc5dSEric Fiselier assert(false); 58*1b00fc5dSEric Fiselier } catch (Catch) { 59*1b00fc5dSEric Fiselier // nothing todo 60*1b00fc5dSEric Fiselier } catch (...) { 61*1b00fc5dSEric Fiselier assert(false); 62*1b00fc5dSEric Fiselier } 63*1b00fc5dSEric Fiselier } 64*1b00fc5dSEric Fiselier 65e434b34fSJonathan Roelofs #else 66e434b34fSJonathan Roelofs 67e434b34fSJonathan Roelofs void test1() 68e434b34fSJonathan Roelofs { 69e434b34fSJonathan Roelofs } 70e434b34fSJonathan Roelofs 71e434b34fSJonathan Roelofs void test2() 72e434b34fSJonathan Roelofs { 73e434b34fSJonathan Roelofs } 74e434b34fSJonathan Roelofs 75*1b00fc5dSEric Fiselier template <class Catch> 76*1b00fc5dSEric Fiselier void catch_nullptr_test() 77*1b00fc5dSEric Fiselier { 78*1b00fc5dSEric Fiselier } 79*1b00fc5dSEric Fiselier 80e434b34fSJonathan Roelofs #endif 81e434b34fSJonathan Roelofs 82e434b34fSJonathan Roelofs int main() 83e434b34fSJonathan Roelofs { 84*1b00fc5dSEric Fiselier // catch naked nullptrs 85e434b34fSJonathan Roelofs test1(); 86e434b34fSJonathan Roelofs test2(); 87*1b00fc5dSEric Fiselier 88*1b00fc5dSEric Fiselier catch_nullptr_test<int*>(); 89*1b00fc5dSEric Fiselier catch_nullptr_test<int**>(); 90*1b00fc5dSEric Fiselier catch_nullptr_test<int A::*>(); 91*1b00fc5dSEric Fiselier catch_nullptr_test<const int A::*>(); 92*1b00fc5dSEric Fiselier catch_nullptr_test<int A::**>(); 93e434b34fSJonathan Roelofs } 94