xref: /freebsd/contrib/llvm-project/libcxx/include/source_location (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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
10#ifndef _LIBCPP_SOURCE_LOCATION
11#define _LIBCPP_SOURCE_LOCATION
12
13/* source_location synopsis
14
15namespace std {
16  struct source_location {
17    static consteval source_location current() noexcept;
18    constexpr source_location() noexcept;
19
20    constexpr uint_least32_t line() const noexcept;
21    constexpr uint_least32_t column() const noexcept;
22    constexpr const char* file_name() const noexcept;
23    constexpr const char* function_name() const noexcept;
24  };
25}
26*/
27
28#include <__config>
29#include <cstdint>
30#include <version>
31
32#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33#  pragma GCC system_header
34#endif
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) &&                                               \
39    !(defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER <= 1403)
40
41class source_location {
42  // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
43  // are hard-coded in the compiler and must not be changed here.
44  struct __impl {
45    const char* _M_file_name;
46    const char* _M_function_name;
47    unsigned _M_line;
48    unsigned _M_column;
49  };
50  const __impl* __ptr_ = nullptr;
51  // GCC returns the type 'const void*' from the builtin, while clang returns
52  // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
53  // in constant evaluation, so we don't want to use `void*` as the argument
54  // type unless the builtin returned that, anyhow, and the invalid cast is
55  // unavoidable.
56  using __bsl_ty = decltype(__builtin_source_location());
57
58public:
59  // The defaulted __ptr argument is necessary so that the builtin is evaluated
60  // in the context of the caller. An explicit value should never be provided.
61  static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) 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
68  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
69    return __ptr_ != nullptr ? __ptr_->_M_line : 0;
70  }
71  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
72    return __ptr_ != nullptr ? __ptr_->_M_column : 0;
73  }
74  _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
75    return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
76  }
77  _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
78    return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
79  }
80};
81
82#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) && !(defined(_LIBCPP_APPLE_CLANG_VER) &&
83       // _LIBCPP_APPLE_CLANG_VER <= 1403)
84
85_LIBCPP_END_NAMESPACE_STD
86
87#endif // _LIBCPP_SOURCE_LOCATION
88