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