Skip to content

Commit 73d94b1

Browse files
committed
[Libcxx] Add <source_location> header.
This requires the __builtin_source_location() builtin, as implemented by GCC and Clang. Fixes #56363 Differential Revision: https://reviews.llvm.org/D120634
1 parent 0d91b05 commit 73d94b1

27 files changed

+586
-226
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Status
274274
------------------------------------------------- -----------------
275275
``__cpp_lib_smart_ptr_for_overwrite`` *unimplemented*
276276
------------------------------------------------- -----------------
277-
``__cpp_lib_source_location`` *unimplemented*
277+
``__cpp_lib_source_location`` ``201907L``
278278
------------------------------------------------- -----------------
279279
``__cpp_lib_span`` ``202002L``
280280
------------------------------------------------- -----------------

libcxx/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Implemented Papers
5151
- P0339R6 - ``polymorphic_allocator<>`` as a vocabulary type
5252
- P1169R4 - ``static operator()``
5353
- P0415R1 - ``constexpr`` for ``std::complex``
54+
- P1208R6 - ``std::source_location``
5455

5556
Improvements and New Features
5657
-----------------------------

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
"`3390 <https://wg21.link/LWG3390>`__","``make_move_iterator()``\ cannot be used to construct a ``move_iterator``\ for a move-only iterator","Prague","|Complete|","14.0","|ranges|"
296296
"`3393 <https://wg21.link/LWG3393>`__","Missing/incorrect feature test macro for coroutines","Prague","|Complete|","14.0"
297297
"`3395 <https://wg21.link/LWG3395>`__","Definition for three-way comparison needs to be updated (US 152)","Prague","","","|spaceship|"
298-
"`3396 <https://wg21.link/LWG3396>`__","Clarify point of reference for ``source_location::current()``\ (DE 169)","Prague","",""
298+
"`3396 <https://wg21.link/LWG3396>`__","Clarify point of reference for ``source_location::current()``\ (DE 169)","Prague","|Nothing To Do|","16.0"
299299
"`3397 <https://wg21.link/LWG3397>`__","``ranges::basic_istream_view::iterator``\ should not provide ``iterator_category``\ ","Prague","|Complete|","16.0","|ranges|"
300300
"`3398 <https://wg21.link/LWG3398>`__","``tuple_element_t``\ is also wrong for ``const subrange``\ ","Prague","|Complete|","14.0","|ranges|"
301301
"`3446 <https://wg21.link/LWG3446>`__","``indirectly_readable_traits``\ ambiguity for types with both ``value_type``\ and ``element_type``\ ","November virtual meeting","|Complete|","13.0","|ranges|"

libcxx/docs/Status/Cxx20Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"`P1065R2 <https://wg21.link/P1065R2>`__","LWG","Constexpr INVOKE","Cologne","|Complete|","12.0"
113113
"`P1135R6 <https://wg21.link/P1135R6>`__","LWG","The C++20 Synchronization Library","Cologne","|Complete|","11.0"
114114
"`P1207R4 <https://wg21.link/P1207R4>`__","LWG","Movability of Single-pass Iterators","Cologne","|Complete|","15.0","|ranges|"
115-
"`P1208R6 <https://wg21.link/P1208R6>`__","LWG","Adopt source_location for C++20","Cologne","",""
115+
"`P1208R6 <https://wg21.link/P1208R6>`__","LWG","Adopt source_location for C++20","Cologne","|Complete|","16.0"
116116
"`P1355R2 <https://wg21.link/P1355R2>`__","LWG","Exposing a narrow contract for ceil2","Cologne","|Complete|","9.0"
117117
"`P1361R2 <https://wg21.link/P1361R2>`__","LWG","Integration of chrono with text formatting","Cologne","|In Progress|",""
118118
"`P1423R3 <https://wg21.link/P1423R3>`__","LWG","char8_t backward compatibility remediation","Cologne","|Complete|","15.0"

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ set(files
824824
set
825825
setjmp.h
826826
shared_mutex
827+
source_location
827828
span
828829
sstream
829830
stack

libcxx/include/module.modulemap.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,10 @@ module std [system] {
12911291
header "shared_mutex"
12921292
export version
12931293
}
1294+
module source_location {
1295+
header "source_location"
1296+
export *
1297+
}
12941298
module span {
12951299
header "span"
12961300
export ranges.__ranges.enable_borrowed_range

libcxx/include/source_location

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
15+
namespace 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+
40+
class source_location {
41+
// The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
42+
// are hard-coded in the compiler and must not be changed here.
43+
struct __impl {
44+
const char* _M_file_name;
45+
const char* _M_function_name;
46+
unsigned _M_line;
47+
unsigned _M_column;
48+
};
49+
const __impl* __ptr_ = nullptr;
50+
// GCC returns the type 'const void*' from the builtin, while clang returns
51+
// `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
52+
// in constant evaluation, so we don't want to use `void*` as the argument
53+
// type unless the builtin returned that, anyhow, and the invalid cast is
54+
// unavoidable.
55+
using __bsl_ty = decltype(__builtin_source_location());
56+
57+
public:
58+
// The defaulted __ptr argument is necessary so that the builtin is evaluated
59+
// in the context of the caller. An explicit value should never be provided.
60+
static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) noexcept {
61+
source_location __sl;
62+
__sl.__ptr_ = static_cast<const __impl*>(__ptr);
63+
return __sl;
64+
}
65+
_LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
66+
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+
81+
#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location)
82+
83+
_LIBCPP_END_NAMESPACE_STD
84+
85+
#endif // _LIBCPP_SOURCE_LOCATION

libcxx/include/version

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ __cpp_lib_void_t 201411L <type_traits>
365365
# define __cpp_lib_shared_ptr_arrays 201707L
366366
# define __cpp_lib_shift 201806L
367367
// # define __cpp_lib_smart_ptr_for_overwrite 202002L
368-
// # define __cpp_lib_source_location 201907L
368+
# if __has_builtin(__builtin_source_location)
369+
# define __cpp_lib_source_location 201907L
370+
# endif
369371
# define __cpp_lib_span 202002L
370372
# define __cpp_lib_ssize 201902L
371373
# define __cpp_lib_starts_ends_with 201711L

0 commit comments

Comments
 (0)