1e434b34fSJonathan Roelofs //===--------------------- catch_pointer_nullptr.cpp ----------------------===// 2e434b34fSJonathan Roelofs // 357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e434b34fSJonathan Roelofs // 7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===// 8e434b34fSJonathan Roelofs 960ba1fefSLouis Dionne // Catching an exception thrown as nullptr was not properly handled before 1060ba1fefSLouis Dionne // 2f984cab4fa7, which landed in macOS 10.13 11*c360553cSLouis Dionne // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} 1260ba1fefSLouis Dionne 1331cbe0f2SLouis Dionne // UNSUPPORTED: c++03 148c61114cSLouis Dionne // UNSUPPORTED: no-exceptions 15c79a8f77SEric Fiselier 16e434b34fSJonathan Roelofs #include <cassert> 171b00fc5dSEric Fiselier #include <cstdlib> 181b00fc5dSEric Fiselier 191b00fc5dSEric Fiselier struct A {}; 20e434b34fSJonathan Roelofs 21e434b34fSJonathan Roelofs void test1() 22e434b34fSJonathan Roelofs { 23e434b34fSJonathan Roelofs try 24e434b34fSJonathan Roelofs { 25e434b34fSJonathan Roelofs throw nullptr; 26e434b34fSJonathan Roelofs assert(false); 27e434b34fSJonathan Roelofs } 282f984cabSRichard Smith catch (int* p) 29e434b34fSJonathan Roelofs { 302f984cabSRichard Smith assert(!p); 31e434b34fSJonathan Roelofs } 32e434b34fSJonathan Roelofs catch (long*) 33e434b34fSJonathan Roelofs { 34e434b34fSJonathan Roelofs assert(false); 35e434b34fSJonathan Roelofs } 36e434b34fSJonathan Roelofs } 37e434b34fSJonathan Roelofs 38e434b34fSJonathan Roelofs void test2() 39e434b34fSJonathan Roelofs { 40e434b34fSJonathan Roelofs try 41e434b34fSJonathan Roelofs { 42e434b34fSJonathan Roelofs throw nullptr; 43e434b34fSJonathan Roelofs assert(false); 44e434b34fSJonathan Roelofs } 452f984cabSRichard Smith catch (A* p) 46e434b34fSJonathan Roelofs { 472f984cabSRichard Smith assert(!p); 48e434b34fSJonathan Roelofs } 49e434b34fSJonathan Roelofs catch (int*) 50e434b34fSJonathan Roelofs { 51e434b34fSJonathan Roelofs assert(false); 52e434b34fSJonathan Roelofs } 53e434b34fSJonathan Roelofs } 54e434b34fSJonathan Roelofs 551b00fc5dSEric Fiselier template <class Catch> 561b00fc5dSEric Fiselier void catch_nullptr_test() { 571b00fc5dSEric Fiselier try { 581b00fc5dSEric Fiselier throw nullptr; 591b00fc5dSEric Fiselier assert(false); 602f984cabSRichard Smith } catch (Catch c) { 612f984cabSRichard Smith assert(!c); 621b00fc5dSEric Fiselier } catch (...) { 631b00fc5dSEric Fiselier assert(false); 641b00fc5dSEric Fiselier } 651b00fc5dSEric Fiselier } 661b00fc5dSEric Fiselier 67e434b34fSJonathan Roelofs 68504bc07dSLouis Dionne int main(int, char**) 69e434b34fSJonathan Roelofs { 701b00fc5dSEric Fiselier // catch naked nullptrs 71e434b34fSJonathan Roelofs test1(); 72e434b34fSJonathan Roelofs test2(); 731b00fc5dSEric Fiselier 741b00fc5dSEric Fiselier catch_nullptr_test<int*>(); 751b00fc5dSEric Fiselier catch_nullptr_test<int**>(); 761b00fc5dSEric Fiselier catch_nullptr_test<int A::*>(); 771b00fc5dSEric Fiselier catch_nullptr_test<const int A::*>(); 781b00fc5dSEric Fiselier catch_nullptr_test<int A::**>(); 79504bc07dSLouis Dionne 80504bc07dSLouis Dionne return 0; 81e434b34fSJonathan Roelofs } 82