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___FORMAT_FORMATTER_POINTER_H
11 #define _LIBCPP___FORMAT_FORMATTER_POINTER_H
12 
13 #include <__algorithm/copy.h>
14 #include <__availability>
15 #include <__config>
16 #include <__debug>
17 #include <__format/format_error.h>
18 #include <__format/format_fwd.h>
19 #include <__format/formatter.h>
20 #include <__format/formatter_integral.h>
21 #include <__format/parser_std_format_spec.h>
22 #include <__iterator/access.h>
23 #include <__nullptr>
24 #include <cstdint>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_BEGIN_NAMESPACE_STD
31 
32 #if _LIBCPP_STD_VER > 17
33 
34 // TODO FMT Remove this once we require compilers with proper C++20 support.
35 // If the compiler has no concepts support, the format header will be disabled.
36 // Without concepts support enable_if needs to be used and that too much effort
37 // to support compilers with partial C++20 support.
38 #  if !defined(_LIBCPP_HAS_NO_CONCEPTS)
39 
40 namespace __format_spec {
41 
42 template <__formatter::__char_type _CharT>
43 class _LIBCPP_TEMPLATE_VIS __formatter_pointer : public __parser_pointer<_CharT> {
44 public:
45   _LIBCPP_HIDE_FROM_ABI auto format(const void* __ptr, auto& __ctx) -> decltype(__ctx.out()) {
46     _LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default,
47                    "The call to parse should have updated the alignment");
48     if (this->__width_needs_substitution())
49       this->__substitute_width_arg_id(__ctx.arg(this->__width));
50 
51     // This code looks a lot like the code to format a hexadecimal integral,
52     // but that code isn't public. Making that code public requires some
53     // refactoring.
54     // TODO FMT Remove code duplication.
55     char __buffer[2 + 2 * sizeof(uintptr_t)];
56     __buffer[0] = '0';
57     __buffer[1] = 'x';
58     char* __last = __to_buffer(__buffer + 2, _VSTD::end(__buffer), reinterpret_cast<uintptr_t>(__ptr), 16);
59 
60     unsigned __size = __last - __buffer;
61     if (__size >= this->__width)
62       return _VSTD::copy(__buffer, __last, __ctx.out());
63 
64     return __formatter::__write(__ctx.out(), __buffer, __last, __size, this->__width, this->__fill, this->__alignment);
65   }
66 };
67 
68 } // namespace __format_spec
69 
70 // [format.formatter.spec]/2.4
71 // For each charT, the pointer type specializations template<>
72 // - struct formatter<nullptr_t, charT>;
73 // - template<> struct formatter<void*, charT>;
74 // - template<> struct formatter<const void*, charT>;
75 template <__formatter::__char_type _CharT>
76 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<nullptr_t, _CharT>
77     : public __format_spec::__formatter_pointer<_CharT> {};
78 template <__formatter::__char_type _CharT>
79 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<void*, _CharT>
80     : public __format_spec::__formatter_pointer<_CharT> {};
81 template <__formatter::__char_type _CharT>
82 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<const void*, _CharT>
83     : public __format_spec::__formatter_pointer<_CharT> {};
84 
85 #  endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
86 
87 #endif //_LIBCPP_STD_VER > 17
88 
89 _LIBCPP_END_NAMESPACE_STD
90 
91 #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H
92