[Libcxx] Add <source_location> header.

This requires the __builtin_source_location() builtin, as implemented
by GCC and Clang.

Fixes https://github.com/llvm/llvm-project/issues/56363

Differential Revision: https://reviews.llvm.org/D120634

NOKEYCHECK=True
GitOrigin-RevId: 73d94b19161355d06f20678d628555719eebbdcc
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 0a06f81..7320bdf 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -824,6 +824,7 @@
   set
   setjmp.h
   shared_mutex
+  source_location
   span
   sstream
   stack
diff --git a/include/module.modulemap.in b/include/module.modulemap.in
index 8706a46..73bd8a2 100644
--- a/include/module.modulemap.in
+++ b/include/module.modulemap.in
@@ -1291,6 +1291,10 @@
     header "shared_mutex"
     export version
   }
+  module source_location {
+    header "source_location"
+    export *
+  }
   module span {
     header "span"
     export ranges.__ranges.enable_borrowed_range
diff --git a/include/source_location b/include/source_location
new file mode 100644
index 0000000..4c4a096
--- /dev/null
+++ b/include/source_location
@@ -0,0 +1,85 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP_SOURCE_LOCATION
+#define _LIBCPP_SOURCE_LOCATION
+
+/* source_location synopsis
+
+namespace std {
+  struct source_location {
+    static consteval source_location current() noexcept;
+    constexpr source_location() noexcept;
+
+    constexpr uint_least32_t line() const noexcept;
+    constexpr uint_least32_t column() const noexcept;
+    constexpr const char* file_name() const noexcept;
+    constexpr const char* function_name() const noexcept;
+  };
+}
+*/
+
+#include <__config>
+#include <cstdint>
+#include <version>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location)
+
+class source_location {
+  // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
+  // are hard-coded in the compiler and must not be changed here.
+  struct __impl {
+    const char* _M_file_name;
+    const char* _M_function_name;
+    unsigned _M_line;
+    unsigned _M_column;
+  };
+  const __impl* __ptr_ = nullptr;
+  // GCC returns the type 'const void*' from the builtin, while clang returns
+  // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
+  // in constant evaluation, so we don't want to use `void*` as the argument
+  // type unless the builtin returned that, anyhow, and the invalid cast is
+  // unavoidable.
+  using __bsl_ty = decltype(__builtin_source_location());
+
+public:
+  // The defaulted __ptr argument is necessary so that the builtin is evaluated
+  // in the context of the caller. An explicit value should never be provided.
+  static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) noexcept {
+    source_location __sl;
+    __sl.__ptr_ = static_cast<const __impl*>(__ptr);
+    return __sl;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
+    return __ptr_ != nullptr ? __ptr_->_M_line : 0;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
+    return __ptr_ != nullptr ? __ptr_->_M_column : 0;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
+    return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
+    return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
+  }
+};
+
+#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location)
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_SOURCE_LOCATION
diff --git a/include/version b/include/version
index d348325..2b81418 100644
--- a/include/version
+++ b/include/version
@@ -365,7 +365,9 @@
 # define __cpp_lib_shared_ptr_arrays                    201707L
 # define __cpp_lib_shift                                201806L
 // # define __cpp_lib_smart_ptr_for_overwrite              202002L
-// # define __cpp_lib_source_location                      201907L
+# if __has_builtin(__builtin_source_location)
+#   define __cpp_lib_source_location                    201907L
+# endif
 # define __cpp_lib_span                                 202002L
 # define __cpp_lib_ssize                                201902L
 # define __cpp_lib_starts_ends_with                     201711L