source_location 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_SOURCE_LOCATION
  10. #define _LIBCPP_SOURCE_LOCATION
  11. /* source_location synopsis
  12. namespace std {
  13. struct source_location {
  14. static consteval source_location current() noexcept;
  15. constexpr source_location() noexcept;
  16. constexpr uint_least32_t line() const noexcept;
  17. constexpr uint_least32_t column() const noexcept;
  18. constexpr const char* file_name() const noexcept;
  19. constexpr const char* function_name() const noexcept;
  20. };
  21. }
  22. */
  23. #include <__config>
  24. #include <cstdint>
  25. #include <version>
  26. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  27. # pragma GCC system_header
  28. #endif
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. #if _LIBCPP_STD_VER >= 20
  31. class source_location {
  32. // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
  33. // are hard-coded in the compiler and must not be changed here.
  34. struct __impl {
  35. const char* _M_file_name;
  36. const char* _M_function_name;
  37. unsigned _M_line;
  38. unsigned _M_column;
  39. };
  40. const __impl* __ptr_ = nullptr;
  41. // GCC returns the type 'const void*' from the builtin, while clang returns
  42. // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
  43. // in constant evaluation, so we don't want to use `void*` as the argument
  44. // type unless the builtin returned that, anyhow, and the invalid cast is
  45. // unavoidable.
  46. # if __has_builtin(__builtin_source_location)
  47. using __bsl_ty = decltype(__builtin_source_location());
  48. # else
  49. using __bsl_ty = __impl*;
  50. # endif
  51. public:
  52. // The defaulted __ptr argument is necessary so that the builtin is evaluated
  53. // in the context of the caller. An explicit value should never be provided.
  54. static consteval source_location
  55. current(__bsl_ty __ptr =
  56. # if __has_builtin(__builtin_source_location)
  57. __builtin_source_location()
  58. # else
  59. nullptr
  60. # endif
  61. ) noexcept {
  62. source_location __sl;
  63. __sl.__ptr_ = static_cast<const __impl*>(__ptr);
  64. return __sl;
  65. }
  66. _LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
  67. _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
  68. return __ptr_ != nullptr ? __ptr_->_M_line : 0;
  69. }
  70. _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
  71. return __ptr_ != nullptr ? __ptr_->_M_column : 0;
  72. }
  73. _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
  74. return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
  75. }
  76. _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
  77. return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
  78. }
  79. };
  80. #endif // _LIBCPP_STD_VER >= 20
  81. _LIBCPP_END_NAMESPACE_STD
  82. #endif // _LIBCPP_SOURCE_LOCATION