source_location 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 && __has_builtin(__builtin_source_location) && \
  31. !(defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER <= 1403)
  32. class source_location {
  33. // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
  34. // are hard-coded in the compiler and must not be changed here.
  35. struct __impl {
  36. const char* _M_file_name;
  37. const char* _M_function_name;
  38. unsigned _M_line;
  39. unsigned _M_column;
  40. };
  41. const __impl* __ptr_ = nullptr;
  42. // GCC returns the type 'const void*' from the builtin, while clang returns
  43. // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
  44. // in constant evaluation, so we don't want to use `void*` as the argument
  45. // type unless the builtin returned that, anyhow, and the invalid cast is
  46. // unavoidable.
  47. using __bsl_ty = decltype(__builtin_source_location());
  48. public:
  49. // The defaulted __ptr argument is necessary so that the builtin is evaluated
  50. // in the context of the caller. An explicit value should never be provided.
  51. static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) noexcept {
  52. source_location __sl;
  53. __sl.__ptr_ = static_cast<const __impl*>(__ptr);
  54. return __sl;
  55. }
  56. _LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
  57. _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
  58. return __ptr_ != nullptr ? __ptr_->_M_line : 0;
  59. }
  60. _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
  61. return __ptr_ != nullptr ? __ptr_->_M_column : 0;
  62. }
  63. _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
  64. return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
  65. }
  66. _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
  67. return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
  68. }
  69. };
  70. #endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) && !(defined(_LIBCPP_APPLE_CLANG_VER) &&
  71. // _LIBCPP_APPLE_CLANG_VER <= 1403)
  72. _LIBCPP_END_NAMESPACE_STD
  73. #endif // _LIBCPP_SOURCE_LOCATION