| 1 |
// Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-options "-D_GLIBCXX_USE_CXX11_ABI=0" } |
| 19 |
// { dg-do run { target c++11 } } |
| 20 |
// { dg-require-effective-target rtti } |
| 21 |
|
| 22 |
#include <fstream> |
| 23 |
#include <system_error> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
void |
| 27 |
test01() |
| 28 |
{ |
| 29 |
using std::ios; |
| 30 |
bool caught_ios_failure = false; |
| 31 |
bool rethrown = false; |
| 32 |
bool caught_system_error = false; |
| 33 |
try { |
| 34 |
std::ifstream f; |
| 35 |
f.exceptions(ios::failbit | ios::badbit | ios::eofbit); |
| 36 |
try { |
| 37 |
f.get(); |
| 38 |
} |
| 39 |
catch (const ios::failure&) // catch as old ABI type |
| 40 |
{ |
| 41 |
caught_ios_failure = true; |
| 42 |
#if _GLIBCXX_USE_DUAL_ABI || _GLIBCXX_USE_CXX11_ABI == 1 |
| 43 |
rethrown = true; |
| 44 |
throw; // re-throw, to catch as new ABI type |
| 45 |
#endif |
| 46 |
} |
| 47 |
} |
| 48 |
catch (const std::system_error& e) |
| 49 |
{ |
| 50 |
caught_system_error = true; |
| 51 |
} |
| 52 |
|
| 53 |
VERIFY( caught_ios_failure ); |
| 54 |
if (rethrown) |
| 55 |
VERIFY( caught_system_error ); |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
test02() |
| 60 |
{ |
| 61 |
using std::ios; |
| 62 |
const std::exception* p = nullptr; |
| 63 |
bool caught_ios_failure = false; |
| 64 |
bool caught_exception = false; |
| 65 |
try { |
| 66 |
std::ifstream f; |
| 67 |
f.exceptions(ios::failbit | ios::badbit | ios::eofbit); |
| 68 |
try { |
| 69 |
f.get(); |
| 70 |
} |
| 71 |
catch (const std::exception& e1) |
| 72 |
{ |
| 73 |
caught_exception = true; |
| 74 |
p = &e1; |
| 75 |
throw; |
| 76 |
} |
| 77 |
} |
| 78 |
catch (const ios::failure& e2) |
| 79 |
{ |
| 80 |
caught_ios_failure = true; |
| 81 |
#if _GLIBCXX_USE_DUAL_ABI |
| 82 |
// If the Dual ABI is active the library throws the new type, |
| 83 |
// so e1 was an object of that new type and so &e1 != &e2. |
| 84 |
VERIFY( p != &e2 ); |
| 85 |
#else |
| 86 |
// Otherwise there's only one type of ios::failure, so &e1 == &e2. |
| 87 |
VERIFY( p == &e2 ); |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
VERIFY( caught_exception ); |
| 92 |
VERIFY( caught_ios_failure ); |
| 93 |
} |
| 94 |
|
| 95 |
int |
| 96 |
main() |
| 97 |
{ |
| 98 |
test01(); |
| 99 |
test02(); |
| 100 |
} |