1e434b34fSJonathan Roelofs //===--------------------- catch_pointer_nullptr.cpp ----------------------===// 2e434b34fSJonathan Roelofs // 3*57b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*57b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*57b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e434b34fSJonathan Roelofs // 7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===// 8e434b34fSJonathan Roelofs 957e446daSAsiri Rathnayake // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions 10c79a8f77SEric Fiselier 11e434b34fSJonathan Roelofs #include <cassert> 121b00fc5dSEric Fiselier #include <cstdlib> 131b00fc5dSEric Fiselier 141b00fc5dSEric Fiselier struct A {}; 15e434b34fSJonathan Roelofs 16e434b34fSJonathan Roelofs void test1() 17e434b34fSJonathan Roelofs { 18e434b34fSJonathan Roelofs try 19e434b34fSJonathan Roelofs { 20e434b34fSJonathan Roelofs throw nullptr; 21e434b34fSJonathan Roelofs assert(false); 22e434b34fSJonathan Roelofs } 232f984cabSRichard Smith catch (int* p) 24e434b34fSJonathan Roelofs { 252f984cabSRichard Smith assert(!p); 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 } 402f984cabSRichard Smith catch (A* p) 41e434b34fSJonathan Roelofs { 422f984cabSRichard Smith assert(!p); 43e434b34fSJonathan Roelofs } 44e434b34fSJonathan Roelofs catch (int*) 45e434b34fSJonathan Roelofs { 46e434b34fSJonathan Roelofs assert(false); 47e434b34fSJonathan Roelofs } 48e434b34fSJonathan Roelofs } 49e434b34fSJonathan Roelofs 501b00fc5dSEric Fiselier template <class Catch> 511b00fc5dSEric Fiselier void catch_nullptr_test() { 521b00fc5dSEric Fiselier try { 531b00fc5dSEric Fiselier throw nullptr; 541b00fc5dSEric Fiselier assert(false); 552f984cabSRichard Smith } catch (Catch c) { 562f984cabSRichard Smith assert(!c); 571b00fc5dSEric Fiselier } catch (...) { 581b00fc5dSEric Fiselier assert(false); 591b00fc5dSEric Fiselier } 601b00fc5dSEric Fiselier } 611b00fc5dSEric Fiselier 62e434b34fSJonathan Roelofs 63e434b34fSJonathan Roelofs int main() 64e434b34fSJonathan Roelofs { 651b00fc5dSEric Fiselier // catch naked nullptrs 66e434b34fSJonathan Roelofs test1(); 67e434b34fSJonathan Roelofs test2(); 681b00fc5dSEric Fiselier 691b00fc5dSEric Fiselier catch_nullptr_test<int*>(); 701b00fc5dSEric Fiselier catch_nullptr_test<int**>(); 711b00fc5dSEric Fiselier catch_nullptr_test<int A::*>(); 721b00fc5dSEric Fiselier catch_nullptr_test<const int A::*>(); 731b00fc5dSEric Fiselier catch_nullptr_test<int A::**>(); 74e434b34fSJonathan Roelofs } 75