libstdc++
source_location
Go to the documentation of this file.
1// <source_location> -*- C++ -*-
2
3// Copyright (C) 2020-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/source_location
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_SRCLOC
30#define _GLIBCXX_SRCLOC 1
31
32#define __glibcxx_want_source_location
33#include <bits/version.h>
34
35#if __cpp_lib_source_location // C++ >= 20 && builtin_source_location
36#include <bits/c++config.h>
37
38namespace std
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41#ifdef __cpp_lib_contracts
42 namespace contracts
43 {
44 class contract_violation;
45 }
46#endif // __cpp_lib_contracts
47
48 /// A class that describes a location in source code.
49 struct source_location
50 {
51 private:
52 using uint_least32_t = __UINT_LEAST32_TYPE__;
53 struct __impl
54 {
55 const char* _M_file_name;
56 const char* _M_function_name;
57 unsigned _M_line;
58 unsigned _M_column;
59 };
60 using __builtin_ret_type = decltype(__builtin_source_location());
61
62 public:
63
64 // [support.srcloc.cons], creation
65 static consteval source_location
66 current(__builtin_ret_type __p = __builtin_source_location()) noexcept
67 {
68 source_location __ret;
69 __ret._M_impl = static_cast <const __impl*>(__p);
70 return __ret;
71 }
72
73 constexpr source_location() noexcept { }
74
75 // [support.srcloc.obs], observers
76 constexpr uint_least32_t
77 line() const noexcept
78 { return _M_impl ? _M_impl->_M_line : 0u; }
79
80 constexpr uint_least32_t
81 column() const noexcept
82 { return _M_impl ? _M_impl->_M_column : 0u; }
83
84 constexpr const char*
85 file_name() const noexcept
86 { return _M_impl ? _M_impl->_M_file_name : ""; }
87
88 constexpr const char*
89 function_name() const noexcept
90 { return _M_impl ? _M_impl->_M_function_name : ""; }
91
92 private:
93 const __impl* _M_impl = nullptr;
94
95 constexpr source_location (const void *__t)
96 : _M_impl (static_cast <const __impl*>(__t)) {}
97
98#ifdef __cpp_lib_contracts
99 /* To enable use of the source __impl*. */
100 friend class std::contracts::contract_violation;
101#endif // __cpp_lib_contracts
102 };
103
104_GLIBCXX_END_NAMESPACE_VERSION
105} // namespace std
106#endif // __cpp_lib_source_location
107#endif // _GLIBCXX_SRCLOC
ISO C++ entities toplevel namespace is std.