// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_DEBUGGING
#define _LIBCPP_DEBUGGING

/*
// all freestanding
namespace std {
  // [debugging.utility], utility
  void breakpoint() noexcept;
  void breakpoint_if_debugging() noexcept;
  bool is_debugger_present() noexcept;
}
*/

#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
#  include <__cxx03/__config>
#else

#  include <__config>
#  include <version>

#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#    pragma GCC system_header
#  endif

#  if _LIBCPP_STD_VER >= 26

_LIBCPP_BEGIN_NAMESPACE_STD

_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS

[[nodiscard]] _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_OVERRIDABLE_FUNC_VIS bool is_debugger_present() noexcept;

_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS

// Functions are marked nodebug and always_inline to force the debugger land in the calling frame upon hitting a
// breakpoint, rather than in the standard library.

_LIBCPP_NODEBUG _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_ALWAYS_INLINE inline void breakpoint() noexcept {
#    if __has_builtin(__builtin_debugtrap)
  __builtin_debugtrap();
#    else
  __builtin_trap();
#    endif
}

_LIBCPP_NODEBUG _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_ALWAYS_INLINE inline void breakpoint_if_debugging() noexcept {
  if (is_debugger_present())
    breakpoint();
}

_LIBCPP_END_NAMESPACE_STD

#  endif // _LIBCPP_STD_VER >= 26

#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)

#endif // _LIBCPP_DEBUGGING
