xref: /llvm-project/libcxx/include/source_location (revision f69585235ec85d54e0f3fc41b2d5700430907f99)
173d94b19SJames Y Knight// -*- C++ -*-
273d94b19SJames Y Knight//===----------------------------------------------------------------------===//
373d94b19SJames Y Knight//
473d94b19SJames Y Knight// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
573d94b19SJames Y Knight// See https://llvm.org/LICENSE.txt for license information.
673d94b19SJames Y Knight// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
773d94b19SJames Y Knight//
873d94b19SJames Y Knight//===----------------------------------------------------------------------===//
973d94b19SJames Y Knight
1073d94b19SJames Y Knight#ifndef _LIBCPP_SOURCE_LOCATION
1173d94b19SJames Y Knight#define _LIBCPP_SOURCE_LOCATION
1273d94b19SJames Y Knight
1373d94b19SJames Y Knight/* source_location synopsis
1473d94b19SJames Y Knight
1573d94b19SJames Y Knightnamespace std {
1673d94b19SJames Y Knight  struct source_location {
1773d94b19SJames Y Knight    static consteval source_location current() noexcept;
1873d94b19SJames Y Knight    constexpr source_location() noexcept;
1973d94b19SJames Y Knight
2073d94b19SJames Y Knight    constexpr uint_least32_t line() const noexcept;
2173d94b19SJames Y Knight    constexpr uint_least32_t column() const noexcept;
2273d94b19SJames Y Knight    constexpr const char* file_name() const noexcept;
2373d94b19SJames Y Knight    constexpr const char* function_name() const noexcept;
2473d94b19SJames Y Knight  };
2573d94b19SJames Y Knight}
2673d94b19SJames Y Knight*/
2773d94b19SJames Y Knight
28b9a2658aSNikolas Klauser#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
29b9a2658aSNikolas Klauser#  include <__cxx03/source_location>
30b9a2658aSNikolas Klauser#else
3173d94b19SJames Y Knight#  include <__config>
3273d94b19SJames Y Knight#  include <cstdint>
3373d94b19SJames Y Knight#  include <version>
3473d94b19SJames Y Knight
3573d94b19SJames Y Knight#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
3673d94b19SJames Y Knight#    pragma GCC system_header
3773d94b19SJames Y Knight#  endif
3873d94b19SJames Y Knight
3973d94b19SJames Y Knight_LIBCPP_BEGIN_NAMESPACE_STD
4073d94b19SJames Y Knight
413aee4a96SStephan T. Lavavej#  if _LIBCPP_STD_VER >= 20
4273d94b19SJames Y Knight
4373d94b19SJames Y Knightclass source_location {
4473d94b19SJames Y Knight  // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
4573d94b19SJames Y Knight  // are hard-coded in the compiler and must not be changed here.
4673d94b19SJames Y Knight  struct __impl {
4773d94b19SJames Y Knight    const char* _M_file_name;
4873d94b19SJames Y Knight    const char* _M_function_name;
4973d94b19SJames Y Knight    unsigned _M_line;
5073d94b19SJames Y Knight    unsigned _M_column;
5173d94b19SJames Y Knight  };
5273d94b19SJames Y Knight  const __impl* __ptr_ = nullptr;
5373d94b19SJames Y Knight  // GCC returns the type 'const void*' from the builtin, while clang returns
5473d94b19SJames Y Knight  // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
5573d94b19SJames Y Knight  // in constant evaluation, so we don't want to use `void*` as the argument
5673d94b19SJames Y Knight  // type unless the builtin returned that, anyhow, and the invalid cast is
5773d94b19SJames Y Knight  // unavoidable.
58*f6958523SNikolas Klauser  using __bsl_ty _LIBCPP_NODEBUG = decltype(__builtin_source_location());
5973d94b19SJames Y Knight
6073d94b19SJames Y Knightpublic:
6173d94b19SJames Y Knight  // The defaulted __ptr argument is necessary so that the builtin is evaluated
6273d94b19SJames Y Knight  // in the context of the caller. An explicit value should never be provided.
6373d94b19SJames Y Knight  static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) noexcept {
6473d94b19SJames Y Knight    source_location __sl;
6573d94b19SJames Y Knight    __sl.__ptr_ = static_cast<const __impl*>(__ptr);
6673d94b19SJames Y Knight    return __sl;
6773d94b19SJames Y Knight  }
6873d94b19SJames Y Knight  _LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
6973d94b19SJames Y Knight
7073d94b19SJames Y Knight  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
7173d94b19SJames Y Knight    return __ptr_ != nullptr ? __ptr_->_M_line : 0;
7273d94b19SJames Y Knight  }
7373d94b19SJames Y Knight  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
7473d94b19SJames Y Knight    return __ptr_ != nullptr ? __ptr_->_M_column : 0;
7573d94b19SJames Y Knight  }
7673d94b19SJames Y Knight  _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
7773d94b19SJames Y Knight    return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
7873d94b19SJames Y Knight  }
7973d94b19SJames Y Knight  _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
8073d94b19SJames Y Knight    return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
8173d94b19SJames Y Knight  }
8273d94b19SJames Y Knight};
8373d94b19SJames Y Knight
843aee4a96SStephan T. Lavavej#  endif // _LIBCPP_STD_VER >= 20
8573d94b19SJames Y Knight
8673d94b19SJames Y Knight_LIBCPP_END_NAMESPACE_STD
8773d94b19SJames Y Knight
88b9a2658aSNikolas Klauser#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
89c166a9c7SNikolas Klauser
9073d94b19SJames Y Knight#endif // _LIBCPP_SOURCE_LOCATION
91