brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · e9c3ba3 Raw
54 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310// UNSUPPORTED: no-exceptions11 12#include <cassert>13#include <cstddef>14#include <cstdlib>15#include <type_traits>16 17struct A {};18 19template<typename T, bool CanCatchNullptr>20static void catch_nullptr_test() {21  try {22    throw nullptr;23  } catch (T &p) {24    assert(CanCatchNullptr && !static_cast<bool>(p));25  } catch (...) {26    assert(!CanCatchNullptr);27  }28}29 30int main(int, char**)31{32  static_assert(std::is_same<std::nullptr_t, decltype(nullptr)>::value, "");33 34  // A reference to nullptr_t can catch nullptr.35  catch_nullptr_test<std::nullptr_t, true>();36  catch_nullptr_test<const std::nullptr_t, true>();37  catch_nullptr_test<volatile std::nullptr_t, true>();38  catch_nullptr_test<const volatile std::nullptr_t, true>();39 40  // No other reference type can.41#if 042  // FIXME: These tests fail, because the ABI provides no way for us to43  // distinguish this from catching by value.44  catch_nullptr_test<void *, false>();45  catch_nullptr_test<void * const, false>();46  catch_nullptr_test<int *, false>();47  catch_nullptr_test<A *, false>();48  catch_nullptr_test<int A::*, false>();49  catch_nullptr_test<int (A::*)(), false>();50#endif51 52  return 0;53}54