xref: /llvm-project/libc/src/stdio/printf_core/ptr_converter.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Pointer Converter for printf ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
11 
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/int_converter.h"
15 #include "src/stdio/printf_core/string_converter.h"
16 #include "src/stdio/printf_core/writer.h"
17 
18 namespace LIBC_NAMESPACE_DECL {
19 namespace printf_core {
20 
21 LIBC_INLINE int convert_pointer(Writer *writer, const FormatSection &to_conv) {
22   FormatSection new_conv = to_conv;
23 
24   if (to_conv.conv_val_ptr == nullptr) {
25     constexpr char NULLPTR_STR[] = "(nullptr)";
26     new_conv.conv_name = 's';
27     new_conv.conv_val_ptr = const_cast<char *>(NULLPTR_STR);
28     return convert_string(writer, new_conv);
29   }
30   new_conv.conv_name = 'x';
31   new_conv.flags =
32       static_cast<FormatFlags>(to_conv.flags | FormatFlags::ALTERNATE_FORM);
33   new_conv.length_modifier = LengthModifier::t;
34   new_conv.conv_val_raw = reinterpret_cast<uintptr_t>(to_conv.conv_val_ptr);
35   return convert_int(writer, new_conv);
36 }
37 
38 } // namespace printf_core
39 } // namespace LIBC_NAMESPACE_DECL
40 
41 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H
42